def test_django_request_normalization(httpbin): from django.conf import settings if not settings.configured: settings.configure() settings.ALLOWED_HOSTS.append('127.0.0.1') import django.http.request url = urlparse.urlparse(httpbin.url + '/get') raw_request = django.http.request.HttpRequest() raw_request.method = 'GET' raw_request.path = url.path raw_request._body = None raw_request.META = { 'CONTENT_TYPE': 'application/json', 'HTTP_HOST': url.netloc, 'QUERY_STRING': 'key=val' } request = normalize_request(raw_request) assert request.path == '/get' assert request.content_type == 'application/json' assert request.url == httpbin.url + '/get?key=val' assert request.method == 'get' del raw_request.META['CONTENT_TYPE'] request = normalize_request(raw_request) assert request.content_type is None
def validate_api_call(schema, raw_request, raw_response): """ Validate the request/response cycle of an api call against a swagger schema. Request/Response objects from the `requests` and `urllib` library are supported. """ request = normalize_request(raw_request) response = normalize_response(raw_response) with ErrorCollection() as errors: try: validate_request( request=request, schema=schema, ) except ValidationError as err: errors['request'].add_error(err.messages or getattr(err, 'detail')) return try: validate_response( response=response, request_method=request.method, schema=schema, ) except ValidationError as err: errors['response'].add_error(err.messages or getattr(err, 'detail'))
def test_request_normalization(httpbin): raw_response = requests.post(httpbin.url + '/post') request = normalize_request(raw_response.request) assert request.path == '/post' assert request.content_type is None assert request.url == httpbin.url + '/post' assert request.method == 'post'
def test_python3_urllib_request_normalization(httpbin): raw_request = urllib.request.Request( httpbin.url + '/get', headers={'Content-Type': 'application/json'}, ) request = normalize_request(raw_request) assert request.path == '/get' assert request.content_type == 'application/json' assert request.url == httpbin.url + '/get' assert request.method == 'get'
def test_request_normalization_with_content_type(httpbin): raw_response = requests.post( httpbin.url + '/post', headers={'Content-Type': 'application/json'}, ) request = normalize_request(raw_response.request) assert request.path == '/post' assert request.content_type == 'application/json' assert request.url == httpbin.url + '/post' assert request.method == 'post'
def test_tornado_client_request_normalization(httpbin): import tornado.httpclient raw_request = tornado.httpclient.HTTPRequest( httpbin.url + '/get?key=val', headers={'Content-Type': 'application/json'}) request = normalize_request(raw_request) assert request.path == '/get' assert request.content_type == 'application/json' assert request.url == httpbin.url + '/get?key=val' assert request.method == 'get'
def test_tornado_request_normalization(httpbin): import tornado.httpclient raw_request = tornado.httpclient.HTTPRequest( httpbin.url + '/get', headers={'Content-Type': 'application/json'} ) request = normalize_request(raw_request) assert request.path == '/get' assert request.content_type == 'application/json' assert request.url == httpbin.url + '/get' assert request.method == 'get'
def test_webob_client_request_normalization(httpbin): import webob raw_request = webob.Request.blank(httpbin.url + '/get') raw_request.query_string = 'key=val' raw_request.method = 'GET' raw_request.content_type = 'application/json' request = normalize_request(raw_request) assert request.path == '/get' assert request.content_type == 'application/json' assert request.url == httpbin.url + '/get?key=val' assert request.method == 'get'
def validate_api_response(schema, raw_response, request_method='get', raw_request=None): """ Validate the response of an api call against a swagger schema. """ request = None if raw_request is not None: request = normalize_request(raw_request) response = None if raw_response is not None: response = normalize_response(raw_response, request=request) if response is not None: validate_response( response=response, request_method=request_method, schema=schema )
def test_werkzeug_request_normalization(httpbin): from werkzeug.test import create_environ from werkzeug.wrappers import Request env = create_environ( path='/put', base_url=httpbin.url, query_string='key=val', headers={'Content-Type': 'application/json'}, data=b'{"key2": "val2"}', method='PUT', ) raw_request = Request(env) request = normalize_request(raw_request) assert request.path == '/put' assert request.content_type == 'application/json' assert request.url == httpbin.url + '/put?key=val' assert request.method == 'put' assert request.data == {'key2': 'val2'}
def test_falcon_request_normalization(httpbin): import falcon from falcon.testing.helpers import create_environ env = create_environ( path='/put', query_string='key=val', host=httpbin.host, port=httpbin.port, headers={'Content-Type': 'application/json'}, body=b'{"key2": "val2"}', method='PUT', ) raw_request = falcon.Request(env) request = normalize_request(raw_request) assert request.path == '/put' assert request.content_type == 'application/json' assert request.url == httpbin.url + '/put?key=val' assert request.method == 'put' assert request.body == '{"key2": "val2"}'
def validate_api_request(schema, raw_request): request = normalize_request(raw_request) with ErrorDict(): validate_request(request=request, schema=schema)