def test_empty(self): media_type = MediaType('application/json') value = '' result = media_type.cast(value) assert result == value
def test_schema_type_invalid(self): schema = Schema('integer', _source={'type': 'integer'}) media_type = MediaType('application/json', schema=schema) value = 'test' with pytest.raises(InvalidMediaTypeValue): media_type.unmarshal(value)
def test_schema_type_invalid(self, unmarshaller_factory): schema = Schema('integer', _source={'type': 'integer'}) media_type = MediaType('application/json', schema=schema) value = 'test' with pytest.raises(InvalidSchemaFormatValue): unmarshaller_factory(media_type.schema).unmarshal(value)
def test_no_schema_deserialised(self, deserializer_factory): media_type = MediaType('application/json') value = "{}" result = deserializer_factory(media_type)(value) assert result == {}
def test_schema_custom_format_invalid(self, unmarshaller_factory): class CustomFormatter(Formatter): def unmarshal(self, value): raise ValueError formatter = CustomFormatter() custom_format = 'custom' custom_formatters = { custom_format: formatter, } schema = Schema( 'string', schema_format=custom_format, _source={ 'type': 'string', 'format': 'custom' }, ) media_type = MediaType('application/json', schema=schema) value = 'test' with pytest.raises(InvalidSchemaFormatValue): unmarshaller_factory( media_type.schema, custom_formatters=custom_formatters, ).unmarshal(value)
def test_urlencoded_form_simple(self, deserializer_factory): media_type = MediaType('application/x-www-form-urlencoded') value = 'param1=test' result = deserializer_factory(media_type)(value) assert result == {'param1': 'test'}
def test_urlencoded_form_empty(self, deserializer_factory): media_type = MediaType('application/x-www-form-urlencoded') value = '' result = deserializer_factory(media_type)(value) assert result == {}
def test_json_empty_object(self, deserializer_factory): media_type = MediaType('application/json') value = "{}" result = deserializer_factory(media_type)(value) assert result == {}
def generate(self, content, in_request_body=False): for mimetype, media_type in iteritems(content): schema_spec = media_type.get('schema') schema = None if schema_spec: schema, _ = self.schemas_registry.get_or_create( schema_spec, in_request_body=in_request_body) yield mimetype, MediaType(mimetype, schema)
def test_schema_custom_format_invalid(self): def custom_formatter(value): raise ValueError schema = Schema( 'string', schema_format='custom', _source={ 'type': 'string', 'format': 'custom' }, ) custom_formatters = { 'custom': custom_formatter, } media_type = MediaType('application/json', schema=schema) value = 'test' with pytest.raises(InvalidMediaTypeValue): media_type.unmarshal(value, custom_formatters=custom_formatters)
def test_data_form_simple(self, deserializer_factory): media_type = MediaType('multipart/form-data') value = b('Content-Type: multipart/form-data; boundary="' '===============2872712225071193122=="\n' 'MIME-Version: 1.0\n\n' '--===============2872712225071193122==\n' 'Content-Type: text/plain\nMIME-Version: 1.0\n' 'Content-Disposition: form-data; name="param1"\n\ntest\n' '--===============2872712225071193122==--\n') result = deserializer_factory(media_type)(value) assert result == {'param1': b('test')}
def test_no_schema_custom_deserialiser(self, deserializer_factory): custom_mimetype = 'application/custom' media_type = MediaType(custom_mimetype) value = "{}" def custom_deserializer(value): return 'custom' custom_deserializers = { custom_mimetype: custom_deserializer, } result = deserializer_factory( media_type, custom_deserializers=custom_deserializers)(value) assert result == 'custom'
def generate(self, content): for mimetype, media_type in iteritems(content): schema_spec = media_type.get('schema') example_spec = media_type.get('example') example_type = type(example_spec) if example_type is dict: example = self.dereferencer.dereference(example_spec) else: example = example_spec schema = None if schema_spec: schema, _ = self.schemas_registry.get_or_create(schema_spec) yield mimetype, MediaType(mimetype, schema=schema, example=example)
def test_no_schema(self, unmarshaller_factory): media_type = MediaType('application/json') value = 'test' with pytest.raises(TypeError): unmarshaller_factory(media_type.schema).unmarshal(value)
def test_empty(self, deserializer_factory): media_type = MediaType('application/json') value = '' with pytest.raises(DeserializeError): deserializer_factory(media_type)(value)
def test_data_form_empty(self, deserializer_factory, value): media_type = MediaType('multipart/form-data') result = deserializer_factory(media_type)(value) assert result == {}