def test_type_casting_errors(rf, router): request = rf.get('/add-numbers?a=foo&b=8') request.api_info = APIInfo( router.get_path('/add-numbers').get_operation('get')) with pytest.raises(ErroneousParameters) as ei: read_parameters(request, capture_errors=True) assert 'a' in ei.value.errors assert 'b' in ei.value.parameters
def test_malformed_complex_parameter(): request = make_request_for_operation( doc.get_path('/complex-parameter').get_operation('get'), query_string=urlencode({ 'coordinates': '{{{{{{{{{{{{{it\'s so cold and miserable', }), ) with pytest.raises(ErroneousParameters) as ei: read_parameters(request, capture_errors=True) assert isinstance(ei.value.errors['coordinates'], InvalidComplexContent)
def test_complex_parameter_fails_validation(): request = make_request_for_operation( doc.get_path('/complex-parameter').get_operation('get'), query_string=urlencode({ 'coordinates': json.dumps({'lat': 8, 'long': 'hoi there'}), }), ) with pytest.raises(ErroneousParameters) as ei: read_parameters(request, capture_errors=True) assert isinstance(ei.value.errors['coordinates'], ValidationError)
def test_parameter_cascade(rf, router): request = rf.get('/cascade-parameters?a=7&b=10') request.api_info = APIInfo( router.get_path('/cascade-parameters').get_operation('get')) assert read_parameters(request) == {'a': 7, 'b': 10} request = rf.get('/cascade-parameter-override?a=yylmao') request.api_info = APIInfo( router.get_path('/cascade-parameter-override').get_operation('get')) assert read_parameters(request) == { 'a': 'yylmao' } # this would fail in the typecast if override didn't work
def test_header_parameter(rf, router): # Too bad there isn't a "requests"-like interface for testing that didn't # work by creating a `WSGIRequest` environment... Would be more truthful to test with something like that. request = rf.get('/header-parameter?blep=foo', HTTP_TOKEN='foo') request.api_info = APIInfo( router.get_path('/header-parameter').get_operation('get')) assert read_parameters(request)['token'] == 'foo'
def test_text_body_type(rf): apidoc = APIDefinition.from_data(yaml.safe_load(JSONIFY_DOC)) operation = apidoc.get_path('/jsonify').get_operation('post') request = rf.post('/jsonify', 'henlo worl', content_type='text/plain') request.api_info = APIInfo(operation=operation) params = read_parameters(request, {}) assert params['text'] == 'henlo worl'
def test_schema_refs(rf, doc_version): router = get_router(f'{doc_version}/schema-refs.yaml') request = rf.post('/cat', json.dumps(lil_bub), content_type='application/json') request.api_info = APIInfo(router.get_path('/cat').get_operation('post')) params = read_parameters(request, {}) assert params['cat'] == lil_bub
def test_polymorphism(rf, doc_version, object): router = get_router(f'{doc_version}/schema-refs.yaml') request = rf.post('/pet', json.dumps(object), content_type='application/json') request.api_info = APIInfo(router.get_path('/pet').get_operation('post')) params = read_parameters(request, {}) assert params['pet'] == object
def test_complex_parameter(): coords_obj = {'lat': 8, 'long': 7} request = make_request_for_operation( doc.get_path('/complex-parameter').get_operation('get'), query_string=urlencode({ 'coordinates': json.dumps(coords_obj), }), ) params = read_parameters(request) assert params == {'coordinates': coords_obj}
def test_files(rf, router): request = rf.post('/upload', { 'file': ContentFile(b'foo', name='foo.txt'), }) request.api_info = APIInfo( router.get_path('/upload').get_operation('post')) parameters = read_parameters(request) if OpenAPI3BodyParameter.name in parameters: # Peel into the body parameter parameters = parameters[OpenAPI3BodyParameter.name] assert isinstance(parameters['file'], UploadedFile)
def test_label_parameter(): request = make_request_for_operation( doc.get_path('/single/{thing}/data{format}').get_operation('get')) params = read_parameters(request, { 'thing': 'blorp', 'format': '.json', }) assert params == { 'thing': 'blorp', 'format': 'json', # Notice the missing dot }
def test_label_array_parameter(): request = make_request_for_operation( doc.get_path('/array/{thing}/data{format}').get_operation('get')) params = read_parameters(request, { 'thing': 'blorp', 'format': '.json.yaml.xml.pdf', }) assert params == { 'thing': 'blorp', 'format': ['json', 'yaml', 'xml', 'pdf'], # An eldritch monstrosity }
def test_label_object_parameter(): request = make_request_for_operation( doc.get_path('/object/{thing}/data{format}').get_operation('get')) params = read_parameters(request, { 'thing': 'blorp', 'format': '.some,body,once,told', }) assert params == { 'thing': 'blorp', 'format': { 'some': 'body', 'once': 'told' }, }
def dispatch(self, request, **kwargs): try: operation = self.path.get_operation(request.method) except InvalidOperation: return self.http_method_not_allowed(request, **kwargs) request.api_info = APIInfo( operation=operation, router=self.router, ) params = dict( (snake_case(name), value) for (name, value) in read_parameters(request, kwargs, capture_errors=True).items() ) handler = request.api_info.router.get_handler(operation.id) try: response = handler(request, **params) except ExceptionalResponse as er: response = er.response return self.transform_response(response)
def test_parameter_ref(rf, router): request = rf.get('/parameter-reference?age=86') request.api_info = APIInfo( router.get_path('/parameter-reference').get_operation('get')) assert read_parameters(request) == {'age': 86}
def test_parameters_ref(rf, router): # /parameters-reference refers the entire parameters object from parameter-reference, so the test is equivalent request = rf.get('/parameters-reference?age=86') request.api_info = APIInfo( router.get_path('/parameters-reference').get_operation('get')) assert read_parameters(request) == {'age': 86}
def test_invalid_collection_format(rf, router): request = rf.get('/invalid-collection-format?blep=foo') request.api_info = APIInfo( router.get_path('/invalid-collection-format').get_operation('get')) with pytest.raises((NotImplementedError, ImproperlyConfigured)): read_parameters(request)
def test_required(rf, router): request = rf.get('/greet') request.api_info = APIInfo(router.get_path('/greet').get_operation('get')) with pytest.raises(ErroneousParameters) as ei: read_parameters(request) assert isinstance(ei.value.errors['greetee'], MissingParameter)
def test_default(rf, router): request = rf.get('/greet?greetee=doggo') request.api_info = APIInfo(router.get_path('/greet').get_operation('get')) parameters = read_parameters(request) assert parameters == {'greeting': 'henlo', 'greetee': 'doggo'}
def test_multi(rf, router): request = rf.get('/multiple-tags?tag=a&tag=b&tag=c') request.api_info = APIInfo( router.get_path('/multiple-tags').get_operation('get')) parameters = read_parameters(request) assert parameters['tag'] == ['a', 'b', 'c']