Building a Fake Server for Testing Projects

la_verdad_de_la_milanesa

NDERAKORE5000

Posted on September 18, 2024

Building a Fake Server for Testing Projects

As a developers, one of the challenges we often face is working with realistic data during the development phase. When learning new technologies or frameworks, having access to reliable and varied data is crucial for testing and experimentation. In this post, I want to share my experience building a fake server to generate test data for a React project. I’ll walk you through the problem I was trying to solve, the considerations I made, the idea behind the project, the tech stack used, and the incremental development process. I’ll also provide some use cases and conclude with the entire code of the server.


Problem

When starting a new project, especially one involving front-end frameworks like React, having realistic test data is essential. Without it, it’s challenging to visualize how the application will behave with actual data. This is especially true when you're working with APIs or integrating third-party services that provide dynamic data.

For instance, in my case, I wanted to test a React application that needed to handle various types of data, such as tasks, weather reports, and health reports. However, fetching this data from real APIs during the development phase wasn't always practical or feasible. I needed a way to generate this data on the fly to simulate different scenarios and test how my application would handle them.


Considerations

  1. Data Variety: To simulate realistic scenarios, the data needed to be diverse. For instance, weather data should include temperature, wind speed, and humidity, while health data might include heart rate and blood pressure.

  2. Dynamic Data: The data should be generated dynamically to allow for a range of different responses based on user requests.

  3. Incremental Development: Building the fake server incrementally allowed me to start with basic functionality and gradually add more features. This approach helps in managing complexity and ensures that each component is working correctly before adding new features.

  4. Integration with React: The fake server should be compatible with React to facilitate seamless integration and testing.


Idea

The idea was to create a simple HTTP server that generates fake data based on various routes. The server would respond to different endpoints with JSON data, which would be used to simulate real-world scenarios. This approach allows developers to test their applications with realistic, but artificially generated, data.

The server was designed to handle several types of data, including tasks, weather reports, and health reports. Each type of data could be requested individually or in bulk, and the server would generate and return the appropriate JSON response.


Stack

  • Python: The server was built using Python, leveraging its simplicity and flexibility for rapid development.
  • http.server: A built-in Python module to create a basic HTTP server.
  • Faker: A library used to generate fake data, such as names, dates, and addresses.
  • UUID: A module to generate unique identifiers for each data entry.
  • Regex: Used to match different URL patterns and handle various routes.

Starting Points: Categories of Data

The server was designed to handle the following categories of data:

  1. Tasks: Single or multiple tasks with attributes like task description, status, and priority.
  2. Weather Reports: Single or multiple weather reports with attributes like temperature, wind speed, and humidity.
  3. Health Reports: Single or multiple health reports with attributes like heart rate, blood pressure, and temperature.

Incremental Development

Step 1: Basic Task Endpoint

Initially, I created a simple endpoint to serve a single task:



# Single task
if re.match(r'/task$', self.path) is not None:
    data_req = {
        'id': str(uuid.uuid4()),
        'task': Faker().sentence(),
        'status': choice(['completed', 'pending'])
    }
    message = json.dumps(data_req)
    self.send_response(200)
    self.send_header('Content-Type', 'application/json; charset=utf-8')
    self.end_headers()
    self.wfile.write(bytes(message, 'utf-8'))


Enter fullscreen mode Exit fullscreen mode

This endpoint returns a single task with a unique ID, a random description, and a status.

Step 2: Multiple Tasks Endpoint

Next, I added the capability to return multiple tasks:



# Several tasks
if re.match(r'/task/\d+$', self.path) is not None:
    quant = int(self.path.split('/')[-1])
    data_req = {
        'id': str(uuid.uuid4()),
        'req_url': self.path,
        'tasks': [{
            'id': str(uuid.uuid4()),
            'task': Faker().sentence(),
            'status': choice(['completed', 'pending']),
        } for _ in range(quant)]
    }
    message = json.dumps(data_req)
    self.send_response(200)
    self.send_header('Content-Type', 'application/json; charset=utf-8')
    self.end_headers()
    self.wfile.write(bytes(message, 'utf-8'))


Enter fullscreen mode Exit fullscreen mode

This endpoint allows specifying the number of tasks to generate, providing more flexibility for testing.

Step 3: Weather and Health Reports

Similarly, I created endpoints for weather and health reports:



# Single weather report
if re.match(r'/weather$', self.path) is not None:
    data_req = {
        'id': str(uuid.uuid4()),
        'weather_report': {
            'temperature': choice([i for i in range(30, 45)]),
            'wind_speed': str(choice([i for i in range(50, 130)])) + 'km/h',
            'humidity': choice([i for i in range(5, 100)]),
            'hours_of_sun': choice([i for i in range(15, 20)]),
            'sunrise_time': Faker().time(),
            'sunset': Faker().time(),
            'report_date': Faker().date()
        }
    }
    message = json.dumps(data_req)
    self.send_response(200)
    self.send_header('Content-Type', 'application/json; charset=utf-8')
    self.end_headers()
    self.wfile.write(bytes(message, 'utf-8'))


Enter fullscreen mode Exit fullscreen mode


# Single health report
if re.match(r'/health$', self.path) is not None:
    data_req = {
        'id': str(uuid.uuid4()),
        'health_report': {
            'temperature': choice([i for i in range(30, 45)]),
            'heart_rate': str(choice([i for i in range(50, 130)])),
            'blood_pressure': choice([i for i in range(15, 20)]),
            'report_time': Faker().time(),
            'report_date': Faker().date(),
        }
    }
    message = json.dumps(data_req)
    self.send_response(200)
    self.send_header('Content-Type', 'application/json; charset=utf-8')
    self.end_headers()
    self.wfile.write(bytes(message, 'utf-8'))


Enter fullscreen mode Exit fullscreen mode

Step 4: Adding More Features

Finally, I enhanced the server to support local and secure locations:



# Secure lat & long
if re.match(r'/lat_long/secure$', self.path) is not None:
    f = Faker()
    f.add_provider(geo)
    data_req = {
        'id': str(uuid.uuid4()),
        'lat_long': f.location_on_land()
    }
    message = json.dumps(data_req)
    self.send_response(200)
    self.send_header('Content-Type', 'application/json; charset=utf-8')
    self.end_headers()
    self.wfile.write(bytes(message, 'utf-8'))


Enter fullscreen mode Exit fullscreen mode

Use Cases / How to Use with React

To integrate this server with a React application:

  1. Fetching Data: Use fetch or libraries like axios to request data from the server.


   useEffect(() => {
       fetch('http://localhost:8080/weather')
           .then(response => response.json())
           .then(data => setWeather(data.weather_report))
           .catch(error => console.error('Error:', error));
   }, []);


Enter fullscreen mode Exit fullscreen mode
  1. Displaying Data: Create React components to display the fetched data, such as weather widgets or task lists.

  2. Dynamic Requests: Use query parameters or URL segments to request specific data, such as multiple weather reports or tasks.



   useEffect(() => {
       fetch('http://localhost:8080/weather/5')
           .then(response => response.json())
           .then(data => setWeatherReports(data.weather_reports))
           .catch(error => console.error('Error:', error));
   }, []);


Enter fullscreen mode Exit fullscreen mode

Conclusion

Building this fake server was a rewarding experience. It allowed me to create a robust testing environment for my React applications, making it easier to simulate various scenarios and handle different types of data. The incremental development approach was particularly useful in managing complexity and ensuring each part of the server functioned correctly before adding new features.

This project is a great example of how incremental development can lead to a well-structured and functional application. There’s always room for more features and improvements, such as adding more data types or refining the data generation process. Overall, it’s a basic yet valuable exercise for any developer looking to enhance their skills and build practical tools for testing and development.


Full Code of the Server



import json
from http.server import BaseHTTPRequestHandler
from urllib import parse
from faker import Faker
from random import choice
import uuid
import re

# PROVIDERS
from faker.providers import profile
from faker.providers import geo

class Fake_Server(BaseHTTPRequestHandler):

    def do_GET(self):
        # $$$$$$$\            $$\       $$\ $$\
        # $$  __$$\           $$ |      $$ |\__|
        # $$ |  $$ |$$\   $$\ $$$$$$$\  $$ |$$\  $$$$$$$\
        # $$$$$$$  |$$ |  $$ |$$  __$$\ $$ |$$ |$$  _____|
        # $$  ____/ $$ |  $$ |$$ |  $$ |$$ |$$ |$$ /
        # $$ |      $$ |  $$ |$$ |  $$ |$$ |$$ |$$ |
        # $$ |      \$$$$$$  |$$$$$$$  |$$ |$$ |\$$$$$$$\
        # \__|       \______/ \_______/ \__|\__| \_______|


        if self.path == '/':
            # Returns the main page of the server. Contains documentation related to available end-points.
            self.path = '/public_doc/index.html'
            try:
                file_to_open = open(self.path[1:]).read()
                self.send_response(200)
            except:
                file_to_open = 'File not found!'
                self.send_response(404)
            self.end_headers()
            self.wfile.write(bytes(file_to_open, 'utf-8'))

        # $$$$$$$\                       $$$$$$\  $$\ $$\
        # $$  __$$\                     $$  __$$\ \__|$$ |
        # $$ |  $$ | $$$$$$\   $$$$$$\  $$ /  \__|$$\ $$ | $$$$$$\
        # $$$$$$$  |$$  __$$\ $$  __$$\ $$$$\     $$ |$$ |$$  __$$\
        # $$  ____/ $$ |  \__|$$ /  $$ |$$  _|    $$ |$$ |$$$$$$$$ |
        # $$ |      $$ |      $$ |  $$ |$$ |      $$ |$$ |$$   ____|
        # $$ |      $$ |      \$$$$$$  |$$ |      $$ |$$ |\$$$$$$$\
        # \__|      \__|       \______/ \__|      \__|\__| \_______|




        # Basic Profile
        if re.match(r'/basic_profile$', self.path) is not None:
            """
            This end-point returns a basic user profile.
            It contains the following elements:
            {'id': <str: It corresponds to a unique basic id for the request made, it can be used for profile-id.>, 
            'req_url': <str: It contains the url of the request completely parceled. Can be used as debug mode>, 
            'basic_profile':
                 {'username': <str: A username for the user.>, 
                 'name': <str:Full name of the user.>, 
                 'sex': <str: A single letter to define the gender of the user.>, 
                 'address': <str: An address for the user.>,
                  'mail':<str: A valid e-mail address.>,
                   'birthdate':< date: Retorna un objeto python tipo 'date'-> ej: date(1954, 9, 4)>
                   }
                   }
            """

            parsed_path = parse.urlparse(self.path)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'basic_profile': Faker().simple_profile()
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Basic Profile various
        if re.match(r'/basic_profile/various/\d$', self.path) is not None:
            """
            It is the same end-point as '/basic_profile' but allows requesting <n> number of profiles simultaneously.
            Retorna un objeto como este:
            "{
                'id': <str: It corresponds to a unique basic id for the request made, it can be used for profile-id.>, 
                'req_url': <str: It contains the url of the request completely parceled. Can be used as debug mode>, 
                'required_quantity': <int: It corresponds to the required quantity. It is for debugging purposes.>, 
                'profiles':<array: Corresponds to an array of objects that contains the required number of basic profiles.> 
                }"
            """

            parsed_path = parse.urlparse(self.path)
            quant = int(self.path.split('/')[-1])
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'required_quantity': quant,
                'profiles': [Faker().simple_profile() for i in range(0, quant)]
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Basic Profile by Sex only one
        if re.match(r'/basic_profile/sex/[M|F]$', self.path) is not None:
            # End-point that returns a basic male or female profile as requested.
            parsed_path = parse.urlparse(self.path)
            sex = self.path.split('/')[-1]
            F = Faker()
            F.add_provider(profile)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'sex_req': sex,
                'basic_profile': Faker().simple_profile(sex=sex)
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Basic Profile by Sex various
        if re.match(r'/basic_profile/sex/[M|F]/\d+$', self.path) is not None:
            """
            Returns a group of basic profiles as requested, male or female and determined amount.
            Sample answer:
            "{
                'id': <str:Corresponds to a unique ID for the server response ... It is used for the calls of react 'unique-key'.>,
                'req_url': <str: The fully parceled url. To debug>,
                'sex_req': <str: One digit corresponding to the sex of the required profiles.>,
                'quantity_required': <int: The number of profiles required corresponds.>,
                'profiles': <array: It is an array containing each of the individual basic profiles.>
                }"
            """
            parsed_path = parse.urlparse(self.path)
            sex = self.path.split('/')[-2]
            quant = int(self.path.split('/')[-1])
            F = Faker()
            F.add_provider(profile)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'sex_req': sex,
                'quantity_required': quant,
                'profiles': [Faker().simple_profile(sex=sex) for i in range(0, quant)]
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Full Profile
        if re.match(r'/full_profile$', self.path) is not None:
            """
            Returns a complete personal profile. (User profile.)
            Sample answer: 
            "{
                'id': <str: It corresponds to a unique basic id for the request made, it can be used for profile-id.>, 
                'req_url': <str: It contains the url of the request completely parceled. Can be used as debug mode>, 
                'full_profile': {
                    'job': <str: Describes the profession of the generated user.>,
                    'company': <str: Corresponds to the name of the company of the generated user.>,
                    'ssn': <str: Corresponds to the social security number of the created user.>,
                    'residence': <str: Corresponds to a user's residence address.>,
                    'current_location': <touple: A python-tuple of GPS positions corresponds to the current position of the user.>,
                    'blood_group': <str: Corresponds to the user's blood group.>, 
                    'website': <array: Corresponds to an array of urls.>, 
                    'username': <str:Corresponds to a username for the user.>,
                    'name': <str: Corresponds to the full name of the user.>,
                    'sex': <str: A digit corresponding to the gender of the user generated.>,
                    'address': <str: A second address, can be used as a work address.>,
                    'mail': <str: An email address for the user.>,
                    'birthdate': <date: Date object, corresponding to the user's birthday.>
                       }
                       }"
            """
            parsed_path = parse.urlparse(self.path)
            F = Faker()
            F.add_provider(profile)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'full_profile': F.profile()
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Full Profile by Sex only one
        if re.match(r'/full_profile/sex/[M|F]$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            F = Faker()
            F.add_provider(profile)
            sex = self.path.split('/')[-1]
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'sex_required': sex,
                'full_profile': F.profile(sex=sex)
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        if re.match(r'/basic_profile/\d+', self.path) is not None:

            total_req = int(self.path.split('/')[-1])
            req = {
            'req_url': self.path,
            'quantity_req': self.path.split('/')[-1],
            'profiles': [Faker().simple_profile() for i in range(0, total_req)]
            }
            message = json.dumps(str(req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Full Profile by Sex various
        if re.match(r'/full_profile/sex/[M|F]/\d+$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            F = Faker()
            F.add_provider(profile)
            sex = self.path.split('/')[-2]
            quant = int(self.path.split('/')[-1])
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'sex_required': sex,
                'required_quantity': quant,
                'profiles': [F.profile(sex=sex) for i in range(0, quant)]
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Full name only one
        if re.match(r'/full_name$', self.path) is not None:
            # Returns an object containing a full name, last name, and first names.
            parsed_path = parse.urlparse(self.path)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'full_name': Faker().name()
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Full name various
        if re.match(r'/full_name/various/\d+$', self.path) is not None:
            # It allows to receive multiple elements within an array of full names.
            parsed_path = parse.urlparse(self.path)
            quant = int(self.path.split('/')[-1])
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'required_quantity': quant,
                'names': [Faker().name() for i in range(0, quant)]
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Surname only
        if re.match(r'/last_name$', self.path) is not None:
            # Returns a unique surname.
            parsed_path = parse.urlparse(self.path)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'last_name': Faker().last_name()
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Surname various
        if re.match(r'/last_name/\d+$', self.path) is not None:
            # It allows to obtain a list of 'n' surnames, as requested.
            parsed_path = parse.urlparse(self.path)
            quant = int(self.path.split('/')[-1])
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'quantity_required': quant,
                'last_names': [Faker().last_name() for i in range(0, quant)]
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Name only one
        if re.match(r'/first_name', self.path) is not None:

            parsed_path = parse.urlparse(self.path)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'first_name': Faker().first_name()
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Name varius
        if re.match(r'/first_name/\d+$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            quant = int(self.path.split('/')[-1])
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'required_quantity': quant,
                'first_name': [Faker().first_name() for i in range(0, quant)]
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Address only one
        if re.match(r'/address$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'address': Faker().address()
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Address only various
        if re.match(r'/address/\d+$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            quant = int(self.path.split('/')[-1])
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'required_quantity': quant,
                'addresses': [Faker().address() for i in range(0, quant)]
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # $$$$$$$$\                  $$\
        # \__$$  __|                 $$ |
        #    $$ | $$$$$$\   $$$$$$$\ $$ |  $$\  $$$$$$$\
        #    $$ | \____$$\ $$  _____|$$ | $$  |$$  _____|
        #    $$ | $$$$$$$ |\$$$$$$\  $$$$$$  / \$$$$$$\
        #    $$ |$$  __$$ | \____$$\ $$  _$$<   \____$$\
        #    $$ |\$$$$$$$ |$$$$$$$  |$$ | \$$\ $$$$$$$  |
        #    \__| \_______|\_______/ \__|  \__|\_______/




        # Single task
        if re.match(r'/task', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'task': Faker().sentence(),
                'responsible': str(Faker().name()),
                'status': choice(['completed', 'pending', 'finished', 'lacks_approval']),
                'created': str(Faker().date()),
                'priority_level' : choice(['high ',' medium ',' low ',' moderate ',' undefined']),
                'term_in_days': choice([i for i in range(0,30)])
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Several tasks
        if re.match(r'/task/\d+$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            total_req = int(self.path.split('/')[-1])
            req = {
                'req_url': self.path,
                'quantity': self.path.split('/')[-1],
                'tasks': [{
                    'id': str(uuid.uuid4()),
                    'task': Faker().sentence(),
                    'responsable': str(Faker().name()),
                    'status': choice(['completed', 'pending', 'finished', 'leacks_approval']),
                    'created': str(Faker().date()),
                    'priority_level': choice(['high', 'medium', 'low', 'moderate', 'undefined']),
                    'term_in_days': choice([i for i in range(0, 30)])
                } for i in range(0, total_req)],
            }

            message = json.dumps(str(req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        #  $$$$$$\
        # $$  __$$\
        # $$ /  \__| $$$$$$\   $$$$$$\
        # $$ |$$$$\ $$  __$$\ $$  __$$\
        # $$ |\_$$ |$$$$$$$$ |$$ /  $$ |
        # $$ |  $$ |$$   ____|$$ |  $$ |
        # \$$$$$$  |\$$$$$$$\ \$$$$$$  |
        #  \______/  \_______| \______/



        # Single lat & long
        if re.match(r'/lat_long$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            f = Faker()
            f.add_provider(geo)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'lat_long': [str(i) for i in f.latlng()]
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Local lat & long only one
        if re.match(r'/lat_long/local/\w+$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            loc = self.path.split('/')[-1].split('_')[-1]
            f = Faker()
            f.add_provider(geo)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'lat_long': f.local_latlng(country_code=str(loc))
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Local lat & long various
        if re.match(r'/lat_long/local/\w+/\d+$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            loc = self.path.split('/')[-2].split('_')[-1]
            quant = int(self.path.split('/')[-1])
            f = Faker()
            f.add_provider(geo)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'required_area': loc,
                'required_quantity': quant,
                'lat_long':  [[i for i in f.local_latlng(country_code=str(loc))] for i in range(0, quant)]
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Secure lat & long
        if re.match(r'/lat_long/secure$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)

            f = Faker()
            f.add_provider(geo)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'lat_long':   f.location_on_land()
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Secure lat & long various
        if re.match(r'/lat_long/secure/\d+$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            quant = int(self.path.split('/')[-1])
            f = Faker()
            f.add_provider(geo)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'required_quantity': quant,
                'lat_long': [[i for i in f.location_on_land()] for i in range(0, quant)]
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # $$\      $$\                      $$\     $$\
        # $$ | $\  $$ |                     $$ |    $$ |
        # $$ |$$$\ $$ | $$$$$$\   $$$$$$\ $$$$$$\   $$$$$$$\   $$$$$$\   $$$$$$\
        # $$ $$ $$\$$ |$$  __$$\  \____$$\\_$$  _|  $$  __$$\ $$  __$$\ $$  __$$\
        # $$$$  _$$$$ |$$$$$$$$ | $$$$$$$ | $$ |    $$ |  $$ |$$$$$$$$ |$$ |  \__|
        # $$$  / \$$$ |$$   ____|$$  __$$ | $$ |$$\ $$ |  $$ |$$   ____|$$ |
        # $$  /   \$$ |\$$$$$$$\ \$$$$$$$ | \$$$$  |$$ |  $$ |\$$$$$$$\ $$ |
        # \__/     \__| \_______| \_______|  \____/ \__|  \__| \_______|\__|



        # Single weather report
        if re.match(r'/weather$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'weather_report': {
                    'temperature': choice([i for i in range(30, 45)]),
                    'wind_speed': str(choice([i for i in range(50, 130)])) + 'km/h',
                    'humidity': choice([i for i in range(5, 100)]),
                    'hours_of_sun': choice([i for i in range(15, 20)]),
                    'sunrise_time': Faker().time(),
                    'sunset': Faker().time(),
                    'report_date': Faker().date()
                }
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Various weather reports
        if re.match(r'/weather/\d+$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            quant = int(self.path.split('/')[-1])
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'required_quantity': quant,
                'weather_reports': [{
                    'temperature': choice([i for i in range(30, 45)]),
                    'wind_speed': str(choice([i for i in range(50, 130)])) + 'km/h',
                    'humidity': choice([i for i in range(5, 100)]),
                    'hours_of_sun': choice([i for i in range(15, 20)]),
                    'sunrise_time': Faker().time(),
                    'sunset': Faker().time(),
                    'report_date': Faker().date()
                } for i in range(0, quant)]
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))



        # $$\   $$\                     $$\   $$\     $$\
        # $$ |  $$ |                    $$ |  $$ |    $$ |
        # $$ |  $$ | $$$$$$\   $$$$$$\  $$ |$$$$$$\   $$$$$$$\
        # $$$$$$$$ |$$  __$$\  \____$$\ $$ |\_$$  _|  $$  __$$\
        # $$  __$$ |$$$$$$$$ | $$$$$$$ |$$ |  $$ |    $$ |  $$ |
        # $$ |  $$ |$$   ____|$$  __$$ |$$ |  $$ |$$\ $$ |  $$ |
        # $$ |  $$ |\$$$$$$$\ \$$$$$$$ |$$ |  \$$$$  |$$ |  $$ |
        # \__|  \__| \_______| \_______|\__|   \____/ \__|  \__|

        # Single health report
        if re.match(r'/health$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'health_report': {
                    'temperature': choice([i for i in range(30, 45)]),
                    'heart_rate': str(choice([i for i in range(50, 130)])),
                    'blood_pressure': choice([i for i in range(15, 20)]),
                    'report_time': Faker().time(),
                    'report_date': Faker().date(),
                }
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))

        # Various health reports
        if re.match(r'/health/various/\d+$', self.path) is not None:
            parsed_path = parse.urlparse(self.path)
            quant = int(self.path.split('/')[-1])
            data_req = {
                'id': str(uuid.uuid4()),
                'req_url': self.path,
                'health_reports': [{
                                    'temperature': choice([i for i in range(30, 45)]),
                                    'heart_rate': str(choice([i for i in range(50, 130)])),
                                    'blood_pressure': choice([i for i in range(15, 20)]),
                                    'report_time': Faker().time(),
                                    'report_date': Faker().date(),
                                } for i in range(0,quant)]
            }
            message = json.dumps(str(data_req))
            self.send_response(200)
            self.send_header('Content-Type', 'application/json; charset=utf-8')
            self.end_headers()
            self.wfile.write(bytes(message, 'utf-8'))





if __name__ == '__main__':
    from http.server import HTTPServer
    server = HTTPServer(('localhost', 8080), Fake_Server)
    print('Server startet at: [localhost:8080]')
    print('Use <ctrl-c> to stop.')
    server.serve_forever()






Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
la_verdad_de_la_milanesa
NDERAKORE5000

Posted on September 18, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related