Example #1
0
    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
Example #2
0
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
Example #3
0
 def test_callback(request: HttpRequest) -> HttpResponse:
     return HttpResponse(
         request.path,
         headers={
             "Content-Type": content_type,
         },
     )
Example #4
0
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
        )
Example #5
0
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
Example #6
0
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"
Example #7
0
def test_http_response_as_dict() -> None:
    body = '{"a": 1}'
    response = HttpResponse(body)

    assert response.as_str() == body
    assert response.as_dict() == {"a": 1}
Example #8
0
 def handle(self, request: HttpRequest,
            next: MiddlewareHandler) -> HttpResponse:
     response = HttpResponse(status=201)
     response.write("Created")
     return response
Example #9
0
def test_set_cookie() -> None:
    instance = HttpResponse()
    instance.cookies.append(HttpCookie("name", "value"))

    assert "name" in instance.cookies
Example #10
0
def test_can_instantiate():
    instance = HttpResponse(200)
    assert isinstance(instance, HttpResponse)
Example #11
0
def test_headers():
    instance = HttpResponse()
    with pytest.raises(AttributeError):
        instance.headers = None

    assert isinstance(instance.headers, Headers)
Example #12
0
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"
Example #13
0
 def test_callaback(request: HttpRequest) -> HttpResponse:
     return HttpResponse(request.path)
Example #14
0
 def _serve_response(request: HttpRequest, next: Callable) -> HttpResponse:
     assert request.method == HttpMethod.POST
     return HttpResponse("OK", headers=request.headers)
Example #15
0
 def one(request: HttpRequest) -> HttpResponse:
     return HttpResponse("group b.1", status=200)
Example #16
0
 def three(request: HttpRequest) -> HttpResponse:
     return HttpResponse("group a.3.1", status=200)
Example #17
0
 def two(request: HttpRequest) -> HttpResponse:
     return HttpResponse("group a.2", status=200)
Example #18
0
def test_can_instantiate() -> None:
    instance = HttpResponse(status=200)
    assert isinstance(instance, HttpResponse)
Example #19
0
def default(request: HttpRequest) -> HttpResponse:
    return HttpResponse("Not found", HttpStatus.NOT_FOUND)
Example #20
0
    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
Example #21
0
def hello(request: HttpRequest) -> HttpResponse:
    return HttpResponse(body=f"Hello {request.attributes['name']}!")
Example #22
0
def test_patch(request: HttpRequest) -> HttpResponse:
    return HttpResponse(status=HttpStatus.OK, body="test put")
Example #23
0
 def ok_handler(request: HttpRequest) -> HttpResponse:
     return HttpResponse(status=200)
Example #24
0
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)
Example #25
0
def test_can_close_body():
    instance = HttpResponse(200)
    instance.write("Test")
    assert instance.writable
    instance.close()
    assert not instance.writable
Example #26
0
def test_get(request: HttpRequest) -> HttpResponse:
    return HttpResponse(status=HttpStatus.OK, body="test get")
Example #27
0
def test_set_cookie():
    instance = HttpResponse()
    instance.cookies.append(Cookie("name", "value"))
    assert instance.headers.get("Set-Cookie") == "name=value"
Example #28
0
 def handle(self, request: HttpRequest, next: MiddlewareHandler) -> HttpResponse:
     response = HttpResponse(200)
     response.write("OK")
     return response
Example #29
0
def test_delete(request: HttpRequest) -> HttpResponse:
    return HttpResponse(status=HttpStatus.OK, body="test head")
Example #30
0
def test_can_close_body() -> None:
    instance = HttpResponse(status=HttpStatus.OK)
    instance.write("Test")
    assert instance.writable
    instance.close()
    assert not instance.writable