def test_django_response_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 import django.http.response 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'} raw_response = django.http.response.HttpResponse(b'', content_type='application/json', status=200) response = normalize_response(raw_response, raw_request) assert response.path == '/get' assert response.content_type == 'application/json' assert response.url == httpbin.url + '/get?key=val' assert response.status_code == '200' redirect_url = 'http://www.example.org' raw_response = django.http.response.HttpResponseRedirect(redirect_url) response = normalize_response(raw_response) assert response.url == redirect_url
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_response_normalization(httpbin): raw_response = requests.get(httpbin.url + '/get') response = normalize_response(raw_response) assert response.path == '/get' assert response.content_type == 'application/json' assert response.url == httpbin.url + '/get' assert response.status_code == '200'
def test_response_normalization(httpbin): raw_response = requests.get(httpbin.url + '/get') response = normalize_response(raw_response) assert response.path == '/get' assert response.content_type == 'application/json' assert response.url == httpbin.url + '/get' assert response.status_code == 200
def test_urllib2_response_normalization(httpbin): import urllib2 raw_response = urllib2.urlopen(httpbin.url + '/get') response = normalize_response(raw_response) assert response.path == '/get' assert response.content_type == 'application/json' assert response.url == httpbin.url + '/get' assert response.status_code == '200'
def test_urllib2_response_normalization(httpbin): import urllib2 raw_response = urllib2.urlopen(httpbin.url + '/get') response = normalize_response(raw_response) assert response.path == '/get' assert response.content_type == 'application/json' assert response.url == httpbin.url + '/get' assert response.status_code == 200
def test_urllib_response_normalization(httpbin): if six.PY2: raw_response = urllib.urlopen(httpbin.url + '/get') else: raw_response = urllib.request.urlopen(httpbin.url + '/get') response = normalize_response(raw_response) assert response.path == '/get' assert response.content_type == 'application/json' assert response.url == httpbin.url + '/get' assert response.status_code == '200'
def test_urllib_response_normalization(httpbin): if six.PY2: raw_response = urllib.urlopen(httpbin.url + '/get') else: raw_response = urllib.request.urlopen(httpbin.url + '/get') response = normalize_response(raw_response) assert response.path == '/get' assert response.content_type == 'application/json' assert response.url == httpbin.url + '/get' assert response.status_code == 200
def validate_api_response(schema, raw_response, request_method='get'): """ Validate the response of an api call against a swagger schema. """ response = None if raw_response is not None: response = normalize_response(raw_response) if response is not None: validate_response(response=response, request_method=request_method, schema=schema)
def test_tornado_response_normalization(httpbin): import tornado.httpclient with closing(tornado.httpclient.HTTPClient()) as client: raw_response = client.fetch( httpbin.url + '/get', headers={'Content-Type': 'application/json'}) response = normalize_response(raw_response) assert response.path == '/get' assert response.content_type == 'application/json' assert response.url == httpbin.url + '/get' assert response.status_code == '200'
def test_django_response_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 import django.http.response 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'} raw_response = django.http.response.HttpResponse(b'', content_type='application/json', status=200) response = normalize_response(raw_response, raw_request) assert response.path == '/get' assert response.content_type == 'application/json' assert response.url == httpbin.url + '/get?key=val' assert response.status_code == '200' del raw_response._headers['content-type'] response = normalize_response(raw_response, raw_request) assert response.content_type is None redirect_url = 'http://www.example.org' raw_response = django.http.response.HttpResponseRedirect(redirect_url) response = normalize_response(raw_response) assert response.url == redirect_url
def test_tornado_response_normalization(httpbin): import tornado.httpclient with closing(tornado.httpclient.HTTPClient()) as client: raw_response = client.fetch( httpbin.url + '/get', headers={'Content-Type': 'application/json'} ) response = normalize_response(raw_response) assert response.path == '/get' assert response.content_type == 'application/json' assert response.url == httpbin.url + '/get' assert response.status_code == '200'
def test_webob_response_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' raw_response = webob.Response() raw_response.content_type = 'application/json' response = normalize_response(raw_response, raw_request) assert response.path == '/get' assert response.content_type == 'application/json' assert response.url == httpbin.url + '/get?key=val' assert response.status_code == '200'
def test_werkzeug_response_normalization(httpbin): from werkzeug.wrappers import Request, Response from werkzeug.test import create_environ raw_request = Request(create_environ( path='/get', base_url=httpbin.url, query_string='key=val', method='GET', )) raw_response = Response( response=b'{"key2": "val2"}', content_type='application/json', ) response = normalize_response(raw_response, raw_request) assert response.path == '/get' assert response.content_type == 'application/json' assert response.url == httpbin.url + '/get?key=val' assert response.status_code == '200'