Пример #1
0
    def request_and_response(self, env, result):
        """
        Client makes a request, server received and responds back.
        """
        # The HTTPClient sends a GET request to some section of the API. The
        # HTTPServer will respond with the next message in it's response queue
        # no matter the HTTP method (GET, POST etc.) or the section of the API
        # it has been sent.
        result.log('Client sends GET request')
        env.http_client.get(api='/random/text')

        # Create some JSON.
        json_content = {'this': ['is', 'a', 'json', 'object']}

        # We then prepare an HTTPResponse. Headers are added as a dictionary and
        # content as a list. For this example we just indicate that the content
        # type is JSON and dump the JSON as a string so it can be sent.
        prepared_response = HTTPResponse(
            headers={'Content-type': 'application/json'},
            content=[json.dumps(json_content)])

        # The HTTPServer then responds. Under the hood this adds the response to
        # the HTTPServer's response queue which will be immediately sent as the
        # HTTPClient has already sent a request.
        result.log('Server receives request and sends response')
        env.http_server.respond(prepared_response)

        # The HTTPClient then receives the HTTPServer's response.
        response = env.http_client.receive()

        # We are verifying the JSON sent back is the same as the one sent by the
        # HTTPServer.
        result.equal(response.json(), json_content,
                     'JSON response from server')
Пример #2
0
 def do_POST(self):
     """
     Override individual request methods (e.g. do_POST) to determine how the
     server will respond to individual requests. You will have to create the
     response, then call _send_header and _send_content.
     """
     response = HTTPResponse(content=["POST response."])
     self._send_header(response.status_code, response.headers)
     self._send_content(response.content)
Пример #3
0
    def test_server_method_request(self, method):
        text = str(uuid.uuid4())
        res = HTTPResponse(content=[text])
        self.server.queue_response(res)

        method_request = getattr(requests, method)
        url = 'http://{}:{}/'.format(self.server.host, self.server.port)
        r = method_request(url)
        assert requests.codes.ok == r.status_code
        assert 'text/plain' == r.headers['content-type']
        assert text == r.text.strip('\n')
Пример #4
0
    def test_client_method_request(self, method):
        method_request = getattr(self.client, method)
        method_request('random/text')

        text = str(uuid.uuid4())
        res = HTTPResponse(content=[text])
        self.server.queue_response(res)

        r = self.client.receive()

        assert requests.codes.ok == r.status_code
        assert 'text/plain' == r.headers['content-type']
        assert text == r.text.strip('\n')
Пример #5
0
 def get_response(self, request):
     """
     Override the get_response method to determine how the server will
     respond to all requests. You must return an HTTPResponse object as the
     _parse_request method expects this.
     """
     text_file = self.server.handler_attributes["text_file"]
     with open(text_file) as input:
         text = input.read()
     response = HTTPResponse(content=[text])
     self.server.log_callback(
         "Creating custom response from {}".format(text_file)
     )
     return response
Пример #6
0
def test_server_method_request(method):
    server = create_server('http_server', 'localhost', 0)

    text = str(uuid.uuid4())
    res = HTTPResponse(content=[text])
    server.queue_response(res)

    method_request = getattr(requests, method)
    r = method_request('http://{}:{}/'.format(server.host, server.port))

    assert requests.codes.ok == r.status_code
    assert 'text/plain' == r.headers['content-type']
    assert text == r.text.strip('\n')

    stop_devices([server])
Пример #7
0
    def test_wait_for_response(self):
        # Send HTTP request
        self.client.get('random/text')

        # Send response
        wait = 0.2
        time.sleep(wait)
        text = str(uuid.uuid4())
        res = HTTPResponse(content=[text])
        self.server.respond(res)

        # Receive response
        r = self.client.receive()

        # Verify response
        assert requests.codes.ok == r.status_code
        assert 'text/plain' == r.headers['Content-type']
        assert text == r.text
Пример #8
0
def test_client_method_request(method):
    server = create_server('http_server', 'localhost', 0)
    client = create_client('http_client', server.host, server.port, 10)

    method_request = getattr(client, method)
    method_request('random/text')

    text = str(uuid.uuid4())
    res = HTTPResponse(content=[text])
    server.queue_response(res)

    r = client.receive()

    assert requests.codes.ok == r.status_code
    assert 'text/plain' == r.headers['content-type']
    assert text == r.text.strip('\n')

    stop_devices([server, client])