def test_router_method(router_decorator: Callable, method: HttpMethod) -> None: ok_response = HttpResponse("OK", HttpStatus.OK) request = HttpRequest(method, "/pet") def noop(): pass @router_decorator("/pet") def get_pet(req: HttpRequest) -> HttpResponse: return ok_response assert get_pet(HttpRequest(HttpMethod.GET)) == ok_response assert http(request) == ok_response
def test_router_not_found() -> None: app = Application() request = HttpRequest(HttpMethod.GET, "/petxxx") response = app(request) assert response.status_code == HttpStatus.NOT_FOUND
def test_can_define_sub_groups() -> None: app = Application() # /group_a with app.group("/group_a") as group_a: # /group_a/one @group_a.get("/one") def one(request: HttpRequest) -> HttpResponse: return HttpResponse("group a.1", status=200) # /group_a/two @group_a.get("/two") def two(request: HttpRequest) -> HttpResponse: return HttpResponse("group a.2", status=200) # /group_a/three with group_a.group("/three") as group_a_subgroup: # /group_a/three/one @group_a_subgroup.get("/one") def three(request: HttpRequest) -> HttpResponse: return HttpResponse("group a.3.1", status=200) # /group_b with app.group("/group_b") as group_b: # /group_b/one @group_b.get("/one") def one(request: HttpRequest) -> HttpResponse: return HttpResponse("group b.1", status=200) request = HttpRequest(HttpMethod.GET, "/group_a/one") response = app(request) assert str(response) == "group a.1" response = group_a(request) assert str(response) == "group a.1" response = group_b(request) assert response.status_code == HttpStatus.NOT_FOUND request = HttpRequest(HttpMethod.GET, "/group_b/one") response = app(request) assert str(response) == "group b.1"
def test_json_body(): request = HttpRequest.from_wsgi(test_wsgi_body) body = request.parsed_body assert isinstance(body, JsonBody) assert "test_1" in body assert body["test_1"] == "1" assert body.get("test2", "default") == "default"
def test_successing_pipeline(): pipeline = MiddlewarePipeline() pipeline.append(ErrorCatchingMiddleware(), ProxingMiddleware(), RespondingMiddleware()) response = pipeline(HttpRequest("get")) response.body.seek(0) assert int(response.status_code) == 201 assert response.body.read() == b"Proxed Response"
def test_erroring_middleware_pipeline(): pipeline = MiddlewarePipeline() pipeline.append(ErrorCatchingMiddleware(), ProxingMiddleware(), ErroringMiddleware()) response = pipeline(HttpRequest("get")) assert response.status_code == 500 assert isinstance(response.body, RuntimeError)
def test_erroring_middleware_pipeline(): pipeline = MiddlewarePipeline() pipeline.append(ErrorCatchingMiddleware(), ProxingMiddleware(), ErroringMiddleware()) response = pipeline(HttpRequest("get")) assert 500 == int(response.status_code) assert response.as_str(), "Error"
def test_parse_files() -> None: request = HttpRequest( cat_file_body["REQUEST_METHOD"], body=cat_file_body["wsgi.input"], headers={"content-type": cat_file_body["CONTENT_TYPE"]}, ) body = request.parsed_body body["image"].seek(0) cat_file.seek(0) assert body["image"].read() == cat_file.read()
def test_parse_form_body() -> None: request = HttpRequest( form_body["REQUEST_METHOD"], body=form_body["wsgi.input"], headers={"content-type": form_body["CONTENT_TYPE"]}, ) body = request.parsed_body assert isinstance(body, FormHttpMessage) assert str(body["test_1"]) == "1" assert body.get("test2", "default") == "default"
def test_create_json_request() -> None: json_data = json.dumps({"test": "OK"}).encode("utf8") instance = HttpRequest( HttpMethod.GET, "/json", BytesIO(json_data), headers=HttpHeaders({"Content-Type": "application/json"}), ) assert dict(instance.parsed_body) == {"test": "OK"}
def test_can_convert_uploaded_file_to_bytes() -> None: request = HttpRequest( multipart_body["REQUEST_METHOD"], body=multipart_body["wsgi.input"], headers={"content-type": multipart_body["CONTENT_TYPE"]}, ) body = request.parsed_body file_bytes = bytes(body["file_a"]) assert file_bytes == b"GIF87a\x02\x00\x02\x00\x91\x00\x00\x00\x00\x00\xff\x8c\x00\xff\xff\xff\x00\x00\x00!\xf9\x04\t\x00\x00\x03\x00,\x00\x00\x00\x00\x02\x00\x02\x00\x00\x02\x02\x8cS\x00;"
def test_get_cookies() -> None: instance = HttpRequest( HttpMethod.GET, headers=HttpHeaders({"cookie": "key=value; anotherkey=anothervalue"}), ) cookies = instance.cookies assert len(cookies) == 2 assert str(cookies["key"]) == "value" assert str(cookies["anotherkey"]) == "anothervalue"
def test_parse_json_body() -> None: request = HttpRequest( json_body["REQUEST_METHOD"], body=json_body["wsgi.input"], headers={"content-type": json_body["CONTENT_TYPE"]}, ) body = request.parsed_body assert isinstance(body, JsonHttpMessage) assert "test_1" in body assert body["test_1"] == "1" assert body.get("test2", "default") == "default"
def test_multipart_body(): request = HttpRequest.from_wsgi(test_wsgi_body) body = request.parsed_body assert isinstance(body, MultipartBody) assert str(body["id"]) == "51b8a72aaaf909e303000034" assert str(body["test_1"]) == "only string value" assert str(body["test_2"]) == "1232" assert isinstance(body["file_a"], UploadedFile) assert isinstance(body["file_b"], UploadedFile) assert body["file_a"].filename == "orange.gif" assert body["file_b"].filename == "yellow.gif" assert body.get("test2", "default") == "default" assert len(body["file_a"]) == 49
def test_can_copy() -> None: # given headers = { "Header-1": "value-1", "heaDer-2": 13, } request = HttpRequest(HttpMethod.GET, body=b"test body", headers=headers) # when request_copy = copy(request) assert request_copy == request request_copy.headers.set("header-1", "new-value") # then assert request.headers.get("header-1") == "value-1"
def test_can_create_binary_message() -> None: # given request = HttpRequest( HttpMethod.POST, "/test", body=binary_body ) # when parsed_body = request.parsed_body binary_data = request.parsed_body.read() # then assert isinstance(parsed_body, BytesIO) assert isinstance(parsed_body, BinaryHttpMessage) assert binary_data == binary_body
def test_parse_multipart_body() -> None: request = HttpRequest( multipart_body["REQUEST_METHOD"], body=multipart_body["wsgi.input"], headers={"content-type": multipart_body["CONTENT_TYPE"]}, ) body = request.parsed_body assert isinstance(body, MultipartHttpMessage) assert str(body["id"]) == "51b8a72aaaf909e303000034" assert str(body["test_1"]) == "only string value" assert str(body["test_2"]) == "1232" assert isinstance(body["file_a"], UploadedFile) assert isinstance(body["file_b"], UploadedFile) assert body["file_a"].filename == "orange.gif" assert body["file_b"].filename == "yellow.gif" assert body.get("test2", "default") == "default" assert len(body["file_a"]) == 49
def test_empty_pipeline(): pipeline = MiddlewarePipeline() with pytest.raises(RuntimeError): pipeline(HttpRequest("get"))
def authorise_user(req: HttpRequest, next) -> HttpResponse: ... req.authorised = True return next(req)
def test_post_body(): request = HttpRequest.from_wsgi(test_wsgi_body) body = request.parsed_body assert isinstance(body, FormBody) assert str(body["test_1"]) == "1" assert body.get("test2", "default") == "default"
def test_can_instantiate() -> None: instance = HttpRequest(HttpMethod.GET) assert isinstance(instance, HttpRequest)
def test_http_request_as_dict() -> None: body = '{"a": 1}' request = HttpRequest(HttpMethod.POST, body=body) assert request.as_str() == body assert request.as_dict() == {"a": 1}
def test_create_json_request() -> None: json_data = json.dumps({"test": "OK"}).encode("utf8") instance = HttpRequest( HttpMethod.GET, "/json", BytesIO(json_data), headers=HttpHeaders({"Content-Type": "application/json"}), ) assert dict(instance.parsed_body) == {"test": "OK"} @pytest.mark.parametrize( "a, b", [ [HttpRequest(HttpMethod.GET), HttpRequest(HttpMethod.GET)], [ HttpRequest(HttpMethod.GET, headers={"test": "1"}), HttpRequest(HttpMethod.GET, headers={"test": "1"}), ], [ HttpRequest(HttpMethod.GET, path="/test/1"), HttpRequest(HttpMethod.GET, path="/test/1"), ], ], ) def test_compare_equal_http_request(a: HttpRequest, b: HttpRequest) -> None: assert a == b
def test_can_instantiate(): instance = HttpRequest("GET") assert isinstance(instance, HttpRequest)
def test_http_request_body_type(data: Union[bytes, bytearray, BytesIO, str, None], expected: str) -> None: request = HttpRequest(HttpMethod.GET, body=data) assert str(request) == expected