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"] })
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"])
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) })
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"])
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
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"])
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"])
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)
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)
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"))
def post(self, request: Request) -> Response: schema_object: SchemaClass = request.deserialized_body return Response({ "param1": schema_object.param1, "param2": schema_object.param2 })
def get(self, request: Request, **kwargs) -> Response: return Response(request.typed_body[dict])
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 )
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"))
def get(self, request: Request, **path_params) -> Response: return Response("")
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)
def post(self, request: Request) -> Response: return Response("", 204)
def get(self, request: Request) -> Response: return Response("wuff")
def get(self, request: Request, **kwargs) -> Response: return Response("Hallo")
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)
def get(self, request: Request) -> Response: return Response("Hallo from subpath 2")
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)
def delete(self, request: Request, **kwargs) -> Response: return Response(request.typed_body[dict], 201)
def get(self, request: Request) -> Response: return Response({ "hyperlink_with_path_params": Hyperlink(ResourceWithPathParams, request).generate(not_there=10), })
def post(self, request: Request) -> Response: return Response(request.deserialized_body)
def get(self, request: Request) -> Response: return Response(request.query_parameters)
def get(self, request: Request) -> Response: return Response("Hallo")