Skip to content

Commit

Permalink
Test query params
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcouper committed Oct 12, 2016
1 parent 2963d43 commit d482121
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
19 changes: 19 additions & 0 deletions abe/unittest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import urlparse

from .mocks import AbeMock
from .utils import normalize, subkeys
Expand Down Expand Up @@ -125,6 +126,18 @@ def assert_headers_contain(self, response_data, spec_data):
"header {0}".format(expected_header)
)

def assert_query_params_equal(self, request_data, spec_data):
qs = urlparse.parse_qs(request_data)
for k, expected_value in spec_data.items():
try:
actual_value = qs[k]
except KeyError:
raise AssertionError('Missing {0} from request'.format(k))

if len(actual_value) == 1:
actual_value = actual_value[0]
self.assertEqual(expected_value, actual_value)

def assert_matches_request(self, sample_request, wsgi_request,
non_strict=None):
"""
Expand All @@ -144,6 +157,12 @@ def assert_matches_request(self, sample_request, wsgi_request,
wsgi_request.META, sample_request['headers']
)

if 'queryParams' in sample_request and 'queryParams' not in non_strict:
self.assert_query_params_equal(
wsgi_request.META['QUERY_STRING'],
sample_request['queryParams']
)

if 'body' in sample_request:
self.assert_data_equal(
wsgi_request.POST, sample_request['body'], non_strict)
Expand Down
15 changes: 13 additions & 2 deletions tests/test_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ def setUp(self):
"examples": {
"OK": {
"request": {
"queryParams": {},
"queryParams": {
"foo": "1",
"bar": "2"
},
"body": {
"name": "My Resource"
},
Expand All @@ -133,7 +136,8 @@ def setUp(self):
META={
'HTTP_THIS_CUSTOM_HEADER': 'Foo',
'REQUEST_METHOD': 'POST',
'PATH_INFO': '/resource/'
'PATH_INFO': '/resource/',
'QUERY_STRING': 'foo=1&bar=2'
},
)

Expand Down Expand Up @@ -172,6 +176,13 @@ def test_matches_non_strict(self):
self.sample_request, self.mock_wsgi_request, non_strict=['name']
)

def test_assertion_error_if_query_params_mismatch(self):
self.mock_wsgi_request.META['QUERY_STRING'] = "foo=1"
with self.assertRaises(AssertionError):
self.assert_matches_request(
self.sample_request, self.mock_wsgi_request
)


def _abe_wrap_response(response):
abe_mock = AbeMock({
Expand Down

0 comments on commit d482121

Please sign in to comment.