def test_str_response_validation_failed(self): response = Response("1234") with self.assertRaises(ValidationError): ResponseSerializerService.validate_and_serialize_response_body( response, HttpAccept.from_accept_string("text/plain"), ResponseStatusParameter(200, "", {"text/plain": fields.Email()}))
def test_register_response_serializer(self): class MyResponseSerializer(ResponseSerializer): def get_response_data_type(self) -> type: return str def get_media_type_strings(self) -> List[str]: return ["my/type"] def validate_and_serialize( self, response_input: Any, response_status_parameter: Union[None, ResponseStatusParameter], can_handle_result: CanHandleResultType ) -> Tuple[bytes, str]: assert can_handle_result.mime_type.charset == "ascii" return "".join(reversed(response_input)).encode( encoding=can_handle_result.mime_type.charset), "my/type" ResponseSerializerService.register_response_serializer( MyResponseSerializer()) response = Response("Test") ResponseSerializerService.validate_and_serialize_response_body( response, HttpAccept.from_accept_string("my/type; charset=ascii")) self.assertEqual(b'tseT', response.content)
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 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 test_make_html_response_no_debug(self): response = self.response_maker.create_response( HttpAccept.from_accept_string("text/html")) self.assertEqual(500, response.status_code) self.assertIn("<title>500 Internal Server Error</title>", response.text) self.assertIn("<h1>Internal Server Error</h1>", response.text) self.assertIn("<p>Something is not working</p>", response.text)
def test_bytes_response_body_with_schema(self): response = Response(b"1234") ResponseSerializerService.validate_and_serialize_response_body( response, HttpAccept.from_accept_string("text/plain"), ResponseStatusParameter(200, "", {"text/plain": fields.Integer()})) self.assertEqual(b"1234", response.content) self.assertEqual("1234", response.text) self.assertEqual("text/plain", response.headers["Content-Type"])
def test_make_html_response_debug(self): response = HttpErrorResponseMaker( InternalServerError("Something is not working", traceback="traceback"), debug=True).create_response( HttpAccept.from_accept_string("text/html")) self.assertEqual(500, response.status_code) self.assertIn("<title>500 Internal Server Error</title>", response.text) self.assertIn("<h1>Internal Server Error</h1>", response.text) self.assertIn("Something is not working", response.text) self.assertIn("traceback", response.text)
def test_make_application_json_error(self): response = self.response_maker.create_response( HttpAccept.from_accept_string("application/json")) self.assertEqual(500, response.status_code) self.assertEqual( { 'detail': 'Something is not working', 'instance': None, 'status': 500, 'title': 'Internal Server Error', 'type': 'https://developer.mozilla.org/de/docs/Web/HTTP/Status/500' }, response.json())
def test_create_http_accept_from_string(self): http_accept_string = \ 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,' \ '*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' http_accept = HttpAccept.from_accept_string(http_accept_string) self.assertEqual(7, len(http_accept.mime_types)) self.assertEqual(MIMEType("text", "html"), http_accept.mime_types[0]) self.assertEqual(MIMEType("application", "xhtml+xml"), http_accept.mime_types[1]) self.assertEqual(MIMEType("image", "webp"), http_accept.mime_types[2]) self.assertEqual(MIMEType("image", "apng"), http_accept.mime_types[3]) self.assertEqual(MIMEType("application", "xml", 0.9, {"q": "0.9"}), http_accept.mime_types[4]) self.assertEqual( MIMEType("application", "signed-exchange", 0.9, { "v": "b3", "q": "0.9" }), http_accept.mime_types[5]) self.assertEqual(MIMEType("*", "*", 0.8, {"q": "0.8"}), http_accept.mime_types[6])
def test_request_attributes(self): request = Request(self.wsgi_environment, {}) self.assertEqual(b"1234567890", request.body) self.assertEqual("text/plain", request.content_type.to_string()) self.assertEqual("gzip, deflate", request.content_encoding) self.assertEqual("utf-8", request.headers["Accept-Charset"]) self.assertEqual("*/*", request.headers["Accept"]) self.assertEqual("gzip, deflate", request.headers["Accept-Encoding"]) self.assertEqual(HttpAccept.from_accept_string("*/*"), request.http_accept_object) self.assertEqual("/path", request.path) self.assertEqual({ "param": "value", "param2": "value2" }, request.query_parameters) self.assertEqual("http://localhost:8080", request.host) self.assertEqual( "http://localhost:8080/path?param=value¶m2=value2", request.original_url) self.assertEqual("GET", request.request_method_name)
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 http_accept_object(self) -> HttpAccept: return HttpAccept.from_accept_string(self.headers.get("Accept", "*/*"))
def test_fallback_text_error_response(self): response = self.response_maker.create_response( HttpAccept.from_accept_string("unknown/muh")) self.assertEqual(500, response.status_code) self.assertEqual("500 Internal Server Error: Something is not working", response.text)
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 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 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 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"))