예제 #1
0
 def get(self, request: Request, **kwargs) -> Response:
     headers = request.headers
     return Response({
         'Accept': headers["Accept"],
         'Accept-Encoding': headers["Accept-Encoding"],
         'Content-Type': headers["Content-Type"],
         'Content-Encoding': headers["Content-Encoding"],
         'Accept-Charset': headers["Accept-Charset"]
     })
예제 #2
0
    def test_dict_fallback_response_serializer(self):
        ResponseSerializerService.register_response_serializer(
            DictFallbackResponseSerializer())
        response = Response({"key": "value"})
        ResponseSerializerService.validate_and_serialize_response_body(
            response, HttpAccept.from_accept_string("wuff/miau"))

        self.assertEqual(b'{"key": "value"}', response.content)
        self.assertEqual("application/json", response._headers["Content-Type"])
예제 #3
0
 def get(self, request: Request) -> Response:
     return Response({
         "hyperlink_with_path_params":
         Hyperlink(ResourceWithPathParams, request).generate(id=10),
         "hyperlink":
         Hyperlink(MyResource, request).generate(),
         "hyperlink_with_path":
         Hyperlink("/path/to/hyperlink/:id", request).generate(id=10)
     })
예제 #4
0
    def test_string_fallback_response_serializer(self):
        ResponseSerializerService.register_response_serializer(
            StringFallbackResponseSerializer())
        response = Response("huhu")
        ResponseSerializerService.validate_and_serialize_response_body(
            response, HttpAccept.from_accept_string("wuff/miau"))

        self.assertEqual(b'huhu', response.content)
        self.assertEqual("text/plain", response._headers["Content-Type"])
예제 #5
0
 def create_rfc7807_json_response(self) -> Response:
     """https://tools.ietf.org/html/rfc7807
     https://tools.ietf.org/html/rfc7807#section-3.1
     """
     response = Response(
         response_body=self.http_error.to_rfc7807_json(),
         status_code=self.http_error.status_code,
         headers={"Content-Type": "application/problem+json"}
     )
     return response
예제 #6
0
    def test_prioritize_media_type(self):
        http_accept = HttpAccept(
            [MIMEType("application", "json"),
             MIMEType("text", "plain", 0.7)])

        ResponseSerializerService.clear_all_response_serializer()

        ResponseSerializerService.register_response_serializer(
            DefaultDictJsonResponseSerializer())
        ResponseSerializerService.register_response_serializer(
            DefaultDictTextResponseSerializer())

        response = Response({"key": "value"})
        ResponseSerializerService.validate_and_serialize_response_body(
            response, http_accept)

        self.assertEqual(b'{"key": "value"}', response.content)
        self.assertEqual("application/json", response._headers["Content-Type"])
예제 #7
0
    def test_response_serializer_with_schema(self):
        class MySchema(Schema):
            field1 = fields.Integer()
            field2 = fields.String()

        response = Response({
            "field1": 1,
            "field2": "hello",
            "not_expected": "wuff"
        })
        ResponseSerializerService.validate_and_serialize_response_body(
            response, HttpAccept.from_accept_string("application/json"),
            ResponseStatusParameter(200, "", {"application/json": MySchema()}))

        self.assertIn('"field1": 1', response.text)
        self.assertIn('"field2": "hello"', response.text)
        self.assertNotIn("not_expected", response.text)
        self.assertIn(b'"field1": 1', response.content)
        self.assertIn(b'"field2": "hello"', response.content)
        self.assertNotIn(b"not_expected", response.content)
        self.assertEqual(200, response.status_code)
        self.assertEqual("application/json", response.headers["Content-Type"])
예제 #8
0
    def test_default_list_to_json(self):
        response = Response(["1", 2])
        ResponseSerializerService.validate_and_serialize_response_body(
            response, HttpAccept([MIMEType("application", "json")]))

        self.assertEqual(b'["1", 2]', response.content)
예제 #9
0
    def test_default_dict_to_json(self):
        response = Response({"key": "value"})
        ResponseSerializerService.validate_and_serialize_response_body(
            response, HttpAccept([MIMEType("application", "json")]))

        self.assertEqual(b'{"key": "value"}', response.content)
예제 #10
0
 def test_response_body_type_not_supported(self):
     response = Response(1.0)
     with self.assertRaises(Response.ResponseBodyTypeNotSupportedException):
         ResponseSerializerService.validate_and_serialize_response_body(
             response, HttpAccept.from_accept_string("application/json"))
예제 #11
0
 def post(self, request: Request) -> Response:
     schema_object: SchemaClass = request.deserialized_body
     return Response({
         "param1": schema_object.param1,
         "param2": schema_object.param2
     })
예제 #12
0
 def get(self, request: Request, **kwargs) -> Response:
     return Response(request.typed_body[dict])
예제 #13
0
 def create_plain_text_response(self) -> Response:
     return Response(
         response_body=self.http_error.to_text(self.debug),
         status_code=self.http_error.status_code
     )
예제 #14
0
 def test_clear_all_default_serializer(self):
     ResponseSerializerService.clear_all_response_serializer()
     response = Response("Test")
     with self.assertRaises(NotAcceptable):
         ResponseSerializerService.validate_and_serialize_response_body(
             response, HttpAccept.from_accept_string("text/plain"))
예제 #15
0
 def get(self, request: Request, **path_params) -> Response:
     return Response("")
예제 #16
0
    def get(self, request: Request) -> Response:
        if request.path_parameters["file_name"] == "swagger.json":
            return Response(self.open_api_documentation.generate_spec())

        return super().get(request)
예제 #17
0
 def post(self, request: Request) -> Response:
     return Response("", 204)
예제 #18
0
 def get(self, request: Request) -> Response:
     return Response("wuff")
예제 #19
0
 def get(self, request: Request, **kwargs) -> Response:
     return Response("Hallo")
예제 #20
0
    def test_default_dict_to_text(self):
        response = Response({"key": "value"})
        ResponseSerializerService.validate_and_serialize_response_body(
            response, HttpAccept.from_accept_string("text/plain"))

        self.assertEqual(b'{"key": "value"}', response.content)
예제 #21
0
 def get(self, request: Request) -> Response:
     return Response("Hallo from subpath 2")
예제 #22
0
    def test_default_str_to_text(self):
        response = Response("Test")
        ResponseSerializerService.validate_and_serialize_response_body(
            response, HttpAccept.from_accept_string("text/plain"))

        self.assertEqual(b'Test', response.content)
예제 #23
0
 def delete(self, request: Request, **kwargs) -> Response:
     return Response(request.typed_body[dict], 201)
예제 #24
0
 def get(self, request: Request) -> Response:
     return Response({
         "hyperlink_with_path_params":
         Hyperlink(ResourceWithPathParams, request).generate(not_there=10),
     })
예제 #25
0
 def post(self, request: Request) -> Response:
     return Response(request.deserialized_body)
예제 #26
0
 def get(self, request: Request) -> Response:
     return Response(request.query_parameters)
예제 #27
0
 def get(self, request: Request) -> Response:
     return Response("Hallo")