def test_get_method_serializers(app): """Test get request method serializers.""" v = ContentNegotiatedMethodView( method_serializers=dict( DELETE={'*/*': 'any-delete', }, GET={'text/plain': 'plain-get', 'text/html': 'html-get'}, POST={'text/plain': 'plain-post', 'text/html': 'html-post'} ), serializers={ 'text/plain': 'plain', 'text/html': 'html', }, default_media_type='application/json', default_method_media_type=dict( GET='text/plain', POST='text/html', ) ) # Default serializers assert v.get_method_serializers('GET')[0] == v.method_serializers['GET'] assert v.get_method_serializers('HEAD')[0] == v.method_serializers['GET'] assert v.get_method_serializers('POST')[0] == v.method_serializers['POST'] assert v.get_method_serializers('PUT')[0] == v.serializers # Default media type assert v.get_method_serializers('GET')[1] == \ v.default_method_media_type['GET'] assert v.get_method_serializers('HEAD')[1] == \ v.default_method_media_type['GET'] assert v.get_method_serializers('POST')[1] == \ v.default_method_media_type['POST'] assert v.get_method_serializers('PUT')[1] == v.default_media_type assert v.get_method_serializers('DELETE')[0] == \ v.method_serializers['DELETE']
def test_match_serializers_query_arg(app): """Test match serializers query argument.""" v = ContentNegotiatedMethodView( method_serializers=dict( GET={ 'application/json': 'json-get', 'application/marcxml+xml': 'xml-get'} ), serializers_query_aliases={ 'json': 'application/json', 'marcxml': 'application/marcxml+xml', }, default_media_type='application/marcxml+xml', default_method_media_type=dict( GET='application/marcxml+xml', ) ) # enable serialization by query arg app.config['REST_MIMETYPE_QUERY_ARG_NAME'] = 'format' InvenioREST(app) arg_name = app.config['REST_MIMETYPE_QUERY_ARG_NAME'] tests = [ # Should serialize to marcxml (urlencode({arg_name: 'marcxml'}), 'application/json', 'xml-get'), # Should serialize to json (urlencode({arg_name: 'json'}), 'application/marcxml+xml', 'json-get'), # Should serialize to json (urlencode({}), 'application/json', 'json-get'), # Should serialize to default (marcxml) (urlencode({'wrong_name': 'marcxml'}), None, 'xml-get'), ] for arg, accept, expected_serializer in tests: params = dict(headers=[('Accept', accept)], query_string=arg) if accept else {} _test_march_serializers(app, v, params, 'GET', accept, expected_serializer) # disable query arg, should serialize by headers even if query arg passed app.config['REST_MIMETYPE_QUERY_ARG_NAME'] = None InvenioREST(app) accept = 'application/json' params = dict(headers=[('Accept', accept)], query_string='format=marcxml') _test_march_serializers(app, v, params, 'GET', accept, 'json-get')
def test_match_serializers(app): """Test match serializers.""" v = ContentNegotiatedMethodView(method_serializers=dict( DELETE={ '*/*': 'any-delete', }, GET={ 'application/json': 'json-get', 'application/marcxml+xml': 'xml-get' }, POST={ 'application/json': 'json-post', 'application/marcxml+xml': 'xml-post' }), serializers={ 'application/json': 'json', 'application/marcxml+xml': 'xml', }, default_media_type='application/json', default_method_media_type=dict( GET='application/json', POST='application/marcxml+xml', )) tests = [ # Should choose application/json ('text/plain,application/json,*/*', 'GET', 'json-get'), ('text/plain,application/json,*/*', 'HEAD', 'json-get'), ('text/plain,application/json,*/*', 'POST', 'json-post'), ('text/plain,application/json,*/*', 'PUT', 'json'), # Should choose application/json (even with lower quality) ('text/plain,application/json; q=0.5, */*', 'GET', 'json-get'), ('text/plain,application/json; q=0.5, */*', 'HEAD', 'json-get'), ('text/plain,application/json; q=0.5, */*', 'POST', 'json-post'), ('text/plain,application/json; q=0.5, */*', 'PUT', 'json'), # Should choose application/marcxml+xml ('text/plain,application/marcxml+xml,*/*', 'GET', 'xml-get'), ('text/plain,application/marcxml+xml,*/*', 'HEAD', 'xml-get'), ('text/plain,application/marcxml+xml,*/*', 'POST', 'xml-post'), ('text/plain,application/marcxml+xml,*/*', 'PUT', 'xml'), # Should choose default defined by default_media_type and # default_method_media_type ('text/plain,*/*', 'GET', 'json-get'), ('text/plain,*/*', 'HEAD', 'json-get'), ('text/plain,*/*', 'POST', 'xml-post'), ('text/plain,*/*', 'PUT', 'json'), # Should choose none ('text/plain', 'GET', None), ('text/plain', 'HEAD', None), ('text/plain', 'POST', None), ('text/plain', 'PUT', None), # Should choose default (None, 'GET', 'json-get'), (None, 'HEAD', 'json-get'), (None, 'POST', 'xml-post'), (None, 'PUT', 'json'), # Should choose application/json ('application/json', 'GET', 'json-get'), # Should choose highest quality ('application/json; q=0.5, application/marcxml+xml', 'GET', 'xml-get'), ('application/json, application/marcxml+xml; q=0.5', 'GET', 'json-get'), ('application/json; q=0.4, application/marcxml+xml; q=0.6', 'GET', 'xml-get'), ('application/marcxml+xml; q=0.6, application/json; q=0.4', 'GET', 'xml-get'), # Test delete ('application/marcxml+xml; q=0.6, application/json; q=0.4', 'DELETE', 'any-delete'), ('video/mp4', 'DELETE', 'any-delete'), ] for accept, method, expected_serializer in tests: params = dict(headers=[('Accept', accept)]) if accept else {} with app.test_request_context(**params): assert v.match_serializers(*v.get_method_serializers(method)) == \ expected_serializer, "Accept: {0}, Method: {1}".format( accept, method)
def test_match_serializers(app): """Test match serializers.""" v = ContentNegotiatedMethodView( method_serializers=dict( DELETE={ '*/*': 'any-delete', }, GET={ 'application/json': 'json-get', 'application/marcxml+xml': 'xml-get'}, POST={ 'application/json': 'json-post', 'application/marcxml+xml': 'xml-post'} ), serializers={ 'application/json': 'json', 'application/marcxml+xml': 'xml', }, default_media_type='application/json', default_method_media_type=dict( GET='application/json', POST='application/marcxml+xml', ) ) tests = [ # Should choose application/json ('text/plain,application/json,*/*', 'GET', 'json-get'), ('text/plain,application/json,*/*', 'HEAD', 'json-get'), ('text/plain,application/json,*/*', 'POST', 'json-post'), ('text/plain,application/json,*/*', 'PUT', 'json'), # Should choose application/json (even with lower quality) ('text/plain,application/json; q=0.5, */*', 'GET', 'json-get'), ('text/plain,application/json; q=0.5, */*', 'HEAD', 'json-get'), ('text/plain,application/json; q=0.5, */*', 'POST', 'json-post'), ('text/plain,application/json; q=0.5, */*', 'PUT', 'json'), # Should choose application/marcxml+xml ('text/plain,application/marcxml+xml,*/*', 'GET', 'xml-get'), ('text/plain,application/marcxml+xml,*/*', 'HEAD', 'xml-get'), ('text/plain,application/marcxml+xml,*/*', 'POST', 'xml-post'), ('text/plain,application/marcxml+xml,*/*', 'PUT', 'xml'), # Should choose default defined by default_media_type and # default_method_media_type ('text/plain,*/*', 'GET', 'json-get'), ('text/plain,*/*', 'HEAD', 'json-get'), ('text/plain,*/*', 'POST', 'xml-post'), ('text/plain,*/*', 'PUT', 'json'), # Should choose none ('text/plain', 'GET', None), ('text/plain', 'HEAD', None), ('text/plain', 'POST', None), ('text/plain', 'PUT', None), # Should choose default (None, 'GET', 'json-get'), (None, 'HEAD', 'json-get'), (None, 'POST', 'xml-post'), (None, 'PUT', 'json'), # Should choose application/json ('application/json', 'GET', 'json-get'), # Should choose highest quality ('application/json; q=0.5, application/marcxml+xml', 'GET', 'xml-get'), ('application/json, application/marcxml+xml; q=0.5', 'GET', 'json-get'), ('application/json; q=0.4, application/marcxml+xml; q=0.6', 'GET', 'xml-get'), ('application/marcxml+xml; q=0.6, application/json; q=0.4', 'GET', 'xml-get'), # Test delete ('application/marcxml+xml; q=0.6, application/json; q=0.4', 'DELETE', 'any-delete'), ('video/mp4', 'DELETE', 'any-delete'), ] for accept, method, expected_serializer in tests: params = dict(headers=[('Accept', accept)]) if accept else {} with app.test_request_context(**params): assert v.match_serializers(*v.get_method_serializers(method)) == \ expected_serializer, "Accept: {0}, Method: {1}".format( accept, method)