Esempio n. 1
0
    def test_request_deserializer_content_type_fallback(self):
        json_bytes = json.dumps({"key": "value"}).encode()
        with self.assertLogs(level="WARNING") as log:
            RequestDeserializerService.deserialize_request_body(
                json_bytes, MIMEType.from_string("whats/up"), dict)

        self.assertIn(
            'WARNING:restit.internal.default_request_deserializer.default_fallback_dict_deserializer:Trying to '
            'parse JSON from content type != application/json', log.output)
Esempio n. 2
0
    def test_request_deserializer_not_found_for_python_type(self):
        json_bytes = json.dumps({"key": "value"}).encode()
        with self.assertRaises(
                RequestDeserializerService.NoRequestDeserializerFoundException
        ) as exception:
            RequestDeserializerService.deserialize_request_body(
                json_bytes, MIMEType.from_string("application/json"), datetime)

        self.assertEqual(
            "Unable to find a request deserializer for content type MIMEType(type=application, subtype=json, "
            "quality=1.0, details={}) to type "
            "<class 'datetime.datetime'>", str(exception.exception))
Esempio n. 3
0
 def __getitem__(self, python_type: type):
     value = self.cache.get(
         python_type,
         RequestDeserializerService.deserialize_request_body(self.body, self.content_type, python_type)
     )
     self.cache[python_type] = value
     return value
Esempio n. 4
0
    def test_application_json_to_dict(self):
        json_bytes = json.dumps({"key": "value"}).encode()

        json_dict = RequestDeserializerService.deserialize_request_body(
            json_bytes, MIMEType.from_string("application/json"), dict)

        self.assertEqual(json_dict, {"key": "value"})
Esempio n. 5
0
    def test_custom_request_deserializer(self):
        class MyRequestDeserializer(RequestDeserializer):
            def get_content_type_list(self) -> Union[List[str], None]:
                return ["whats/up"]

            def get_deserialized_python_type(self) -> Type:
                return str

            def deserialize(self,
                            request_input: bytes,
                            encoding: str = None) -> str:
                return "".join(reversed(request_input.decode()))

        RequestDeserializerService.register_request_deserializer(
            MyRequestDeserializer())

        deserialized_value = RequestDeserializerService.deserialize_request_body(
            b"hello", MIMEType.from_string("whats/up"), str)

        self.assertEqual("olleh", deserialized_value)
Esempio n. 6
0
    def __init__(self, wsgi_environment: dict, path_params: dict):
        self._wsgi_environment = wsgi_environment
        self._path_params = path_params
        self._query_string = wsgi_environment["QUERY_STRING"]
        self._path = wsgi_environment["PATH_INFO"]
        self._method_name = wsgi_environment["REQUEST_METHOD"]

        self._query_parameters: dict = create_dict_from_assignment_syntax(
            self._query_string)
        self._headers = self._create_headers()

        self._forwarded = ForwardedHeader.from_headers(self._headers)

        self._typed_body = TypedBody(self.body, self.content_type)
        self._deserialized_body = None

        self._request_deserializer_service = RequestDeserializerService()
Esempio n. 7
0
    def test_form_data_to_dict(self):
        json_dict = RequestDeserializerService.deserialize_request_body(
            b"key=value&key2=value",
            MIMEType.from_string("application/x-www-form-urlencoded"), dict)

        self.assertEqual({'key': 'value', 'key2': 'value'}, json_dict)
Esempio n. 8
0
 def tearDown(self) -> None:
     RequestDeserializerService.clear_all_request_deserializers()
     RequestDeserializerService.restore_default_request_deserializers()