def test_json(): callback_manager.load("tests/serve/mock/callbacks") storage = MockData() request = RequestBuilder.from_dict( dict( method="post", host="api.com", pathname="/counter", query={}, body="", protocol="http", bodyAsJson={}, headers={}, )) response = ResponseBuilder.from_dict( dict(statusCode=200, body="", bodyAsJson={"field": "value"}, headers={})) new_response = callback_manager(request, response, storage) assert 1 == new_response.bodyAsJson["count"] assert "value" == new_response.bodyAsJson["field"] assert "count" in new_response.body request_set = RequestBuilder.from_dict( dict( method="post", host="api.com", pathname="/counter", query={}, body="", protocol="http", bodyAsJson={"set": 10}, headers={}, )) new_response = callback_manager(request_set, response, storage) assert 10 == new_response.bodyAsJson["count"] assert "value" == new_response.bodyAsJson["field"] assert "count" in new_response.body new_response = callback_manager(request, response, storage) assert 11 == new_response.bodyAsJson["count"] assert "value" == new_response.bodyAsJson["field"] assert "count" in new_response.body
def test_example_from_readme(): request = RequestBuilder.from_dict({ "host": "api.github.com", "protocol": "https", "method": "get", "pathname": "/v1/users", "query": { "a": "b", "q": ["1", "2"] }, }) response = ResponseBuilder.from_dict({ "statusCode": 200, "headers": { "content-type": "text/plain" }, "body": "(response body string)", }) exchange = HttpExchange(request=request, response=response) output_file = StringIO() writer = HttpExchangeWriter(output_file) writer.write(exchange) input_file = output_file input_file.seek(0) for exchange in HttpExchangeReader.from_jsonl(input_file): assert exchange.request.method == HttpMethod.GET assert exchange.request.protocol == Protocol.HTTPS assert exchange.response.statusCode == 200
def test_text_full(mock_data_store): callback_manager.load("tests/serve/mock/callbacks") mock_data_store.clear() request = RequestBuilder.from_dict( dict( method="post", host="another.api.com", pathname="/echo", query={}, protocol="http", body="Hello", bodyAsJson={}, headers={}, )) response = ResponseBuilder.from_dict( dict(statusCode=200, body="", bodyAsJson={"field": "value"}, headers={})) new_response = callback_manager(request, response, MockData()) assert "Hello" == new_response.body assert "value" == new_response.headers["X-Echo-Header"]
def test_from_url(): test_url = "https://api.github.com/v1/repos?id=1&q=v1&q=v2" req = RequestBuilder.from_url(test_url) assert req.method == HttpMethod.GET assert req.host == "api.github.com" assert req.protocol == Protocol.HTTPS assert req.path == "/v1/repos?id=1&q=v1&q=v2" assert req.pathname == "/v1/repos" assert req.query == {"id": "1", "q": ["v1", "v2"]}
def test_request_with_bodyAsJson(): dict_req = { "host": "example.com", "protocol": "http", "method": "post", "path": "/a/path", "bodyAsJson": {"key": "value"}, } req = RequestBuilder.from_dict(dict_req) assert req.body == '{"key": "value"}'
def test_httpbin(): httpretty.register_uri(httpretty.GET, "https://httpbin.org/ip", body='{"origin": "127.0.0.1"}') rq = request.Request("https://httpbin.org/ip") rs = request.urlopen(rq) req = RequestBuilder.from_urllib_request(rq) res = ResponseBuilder.from_http_client_response(rs) assert req.protocol == Protocol.HTTPS assert req.method == HttpMethod.GET assert req.path == "/ip" assert res.statusCode == 200 assert res.bodyAsJson == {"origin": "127.0.0.1"} assert isinstance(res.body, str)
def test_request_from_dict(): dict_req = { "host": "api.github.com", "protocol": "https", "method": "get", "pathname": "/v1/users", "query": {"a": "b", "q": ["1", "2"]}, } req = RequestBuilder.from_dict(dict_req) assert req.method == HttpMethod.GET assert req.host == "api.github.com" assert req.protocol == Protocol.HTTPS assert req.body == "" assert req.headers == {} assert req.pathname == "/v1/users" assert req.path == "/v1/users?a=b&q=1&q=2"
def test_no_callback(): callback_manager.load("tests/serve/mock/callbacks") request = RequestBuilder.from_dict( dict( method="get", host="api.com", pathname="/nothing", query={}, body="", protocol="http", bodyAsJson={}, headers={}, )) response = ResponseBuilder.from_dict( dict(statusCode=200, body="", bodyAsJson={}, headers={})) new_response = callback_manager(request, response, MockData()) assert response == new_response
def test_text(mock_data_store): callback_manager.load("tests/serve/mock/callbacks") mock_data_store.clear() request = RequestBuilder.from_dict( dict( method="get", host="api.com", pathname="/text_counter", query={"set": 10}, body="", protocol="http", bodyAsJson={}, headers={}, )) response = ResponseBuilder.from_dict( dict(statusCode=200, body="Called", bodyAsJson={}, headers={})) new_response = callback_manager(request, response, MockData()) assert 10 == new_response.headers["x-hmt-counter"] assert "Called 10 times" == new_response.body
def test_from_url_with_root_path(): test_url = "https://api.github.com/" req = RequestBuilder.from_url(test_url) assert req.path == "/" assert req.pathname == "/"
def test_validate_protocol(): assert RequestBuilder.validate_protocol("http") == Protocol.HTTP assert RequestBuilder.validate_protocol("https") == Protocol.HTTPS
def on_request(self, data: bytes) -> RequestInfo: req_lines = data.decode("utf-8").split("\r\n") method, fullpath, protocol = req_lines[0].split(" ") parsed_fullpath = parse.urlparse(fullpath) query = parse.parse_qs(parsed_fullpath.query) headers = {} host_line: int = 0 body_start: int = 0 last_line: int = 0 for line in req_lines[1:]: last_line += 1 if not line: break else: header, value = line.split(": ") if header.lower() == "host": host_line = last_line headers["Host"] = value else: headers[header] = value body_start = last_line + 1 body = [] # TODO: @Nikolay, is cur_id correct? for body_line in range(body_start, len(req_lines)): if req_lines[body_line]: body.append(req_lines[body_line]) body = "\r\n".join(body) route_info = self._router.route(parsed_fullpath.path, headers) fullpath = ( "{}?{}".format(route_info.path, parsed_fullpath.query) if query else route_info.path ) req_lines[0] = " ".join((method, fullpath, protocol)) req_lines[host_line] = "Host: {}".format(route_info.host) headers["Host"] = route_info.host data = "\r\n".join(req_lines).encode("utf-8") # ignoring type due to this error """ 46:34 - error: Argument of type 'str' cannot be assigned to parameter 'method' of type 'Literal['connect', 'head', 'trace', 'options', 'delete', 'patch', 'post', 'put', 'get']' 'str' cannot be assigned to 'Literal['connect']' 'str' cannot be assigned to 'Literal['head']' 'str' cannot be assigned to 'Literal['trace']' 'str' cannot be assigned to 'Literal['options']' 'str' cannot be assigned to 'Literal['delete']' """ self._request = RequestBuilder.from_dict( dict( method=method.lower(), host=route_info.host, path=fullpath, pathname=route_info.path, protocol=route_info.scheme, query=query, body=body, bodyAsJson=json.loads(body) if body else {}, headers=headers, ) ) return RequestInfo( data=data, scheme=route_info.scheme, target_host=route_info.hostname, target_port=route_info.port, )