def handle(self, request: HttpRequest, next: MiddlewareHandler) -> HttpResponse: try: assert isinstance(next, MiddlewareHandler) assert len(next.queue) == 2 response = next(request) except Exception as e: response = HttpResponse(status=500) response.write("Error") return response
def default(request: HttpRequest) -> HttpResponse: """Shows how cookie work""" client_cookies = request.cookies response = HttpResponse("Not found", HttpStatus.NOT_FOUND) response.cookies["simple_cookie"] = "value" response.cookies.append( HttpCookie( "advanced_cookie", "Advanced value {test 1}", expires=datetime(2021, 1, 1) ) ) return response
def test_callback(request: HttpRequest) -> HttpResponse: return HttpResponse( request.path, headers={ "Content-Type": content_type, }, )
def error_handler(request, next) -> HttpResponse: try: return next(request) except ValidationError as error: json_error = {"error_code": error.code, "error_message": str(error)} return HttpResponse( json.dumps(json_error), status=HttpStatus.UNPROCESSABLE_ENTITY )
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_can_write_bytesio_body() -> None: body = BytesIO() body.write(b"example text") instance = HttpResponse(status=200) instance.write(body) assert str(instance) == "example text" instance.body.seek(0) assert instance.body.read() == b"example text" instance.write(" and string") assert str(instance) == "example text and string"
def test_http_response_as_dict() -> None: body = '{"a": 1}' response = HttpResponse(body) assert response.as_str() == body assert response.as_dict() == {"a": 1}
def handle(self, request: HttpRequest, next: MiddlewareHandler) -> HttpResponse: response = HttpResponse(status=201) response.write("Created") return response
def test_set_cookie() -> None: instance = HttpResponse() instance.cookies.append(HttpCookie("name", "value")) assert "name" in instance.cookies
def test_can_instantiate(): instance = HttpResponse(200) assert isinstance(instance, HttpResponse)
def test_headers(): instance = HttpResponse() with pytest.raises(AttributeError): instance.headers = None assert isinstance(instance.headers, Headers)
def test_can_write_and_read_body(): instance = HttpResponse(200) instance.write("Example text") assert str(instance) == "Example text" instance.body.seek(0) assert instance.body.read() == b"Example text"
def test_callaback(request: HttpRequest) -> HttpResponse: return HttpResponse(request.path)
def _serve_response(request: HttpRequest, next: Callable) -> HttpResponse: assert request.method == HttpMethod.POST return HttpResponse("OK", headers=request.headers)
def one(request: HttpRequest) -> HttpResponse: return HttpResponse("group b.1", status=200)
def three(request: HttpRequest) -> HttpResponse: return HttpResponse("group a.3.1", status=200)
def two(request: HttpRequest) -> HttpResponse: return HttpResponse("group a.2", status=200)
def test_can_instantiate() -> None: instance = HttpResponse(status=200) assert isinstance(instance, HttpResponse)
def default(request: HttpRequest) -> HttpResponse: return HttpResponse("Not found", HttpStatus.NOT_FOUND)
def _serve_response(request: HttpRequest, next: Callable) -> HttpResponse: assert request.method == HttpMethod.POST response = HttpResponse("OK", headers=request.headers) response.cookies.append(HttpCookie(name="test", value="SuperCookie")) return response
def hello(request: HttpRequest) -> HttpResponse: return HttpResponse(body=f"Hello {request.attributes['name']}!")
def test_patch(request: HttpRequest) -> HttpResponse: return HttpResponse(status=HttpStatus.OK, body="test put")
def ok_handler(request: HttpRequest) -> HttpResponse: return HttpResponse(status=200)
def test_http_response_parsed_body_as_json_message() -> None: body = '{"a": 1}' response = HttpResponse(body=body, headers={"content-type": "application/json"}) assert isinstance(response.parsed_body, JsonHttpMessage)
def test_can_close_body(): instance = HttpResponse(200) instance.write("Test") assert instance.writable instance.close() assert not instance.writable
def test_get(request: HttpRequest) -> HttpResponse: return HttpResponse(status=HttpStatus.OK, body="test get")
def test_set_cookie(): instance = HttpResponse() instance.cookies.append(Cookie("name", "value")) assert instance.headers.get("Set-Cookie") == "name=value"
def handle(self, request: HttpRequest, next: MiddlewareHandler) -> HttpResponse: response = HttpResponse(200) response.write("OK") return response
def test_delete(request: HttpRequest) -> HttpResponse: return HttpResponse(status=HttpStatus.OK, body="test head")
def test_can_close_body() -> None: instance = HttpResponse(status=HttpStatus.OK) instance.write("Test") assert instance.writable instance.close() assert not instance.writable