Skip to content

Commit 1fcacd2

Browse files
committed
Merge pull request #9 from alexcouper/alex/check-headers
Add support for testing headers.
2 parents 3db3a29 + e6ebc60 commit 1fcacd2

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

abe/unittest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ def assert_data_list_equal(self, data1, data2):
8989
)
9090
raise type(exceptions[0])(message)
9191

92+
def assert_headers_contain(self, response_data, spec_data):
93+
"""
94+
response_data headers contain all headers defined in spec_data
95+
"""
96+
for expected_header, expected_value in spec_data.items():
97+
django_expected_header = (
98+
'HTTP_' + expected_header.upper().replace('-', '_')
99+
)
100+
actual_val = response_data.get(
101+
expected_header,
102+
response_data.get(django_expected_header)
103+
)
104+
self.assertEqual(
105+
expected_value, actual_val,
106+
"Incorrect or missing value specified for "
107+
"header {0}".format(expected_header)
108+
)
109+
92110
def assert_matches_sample(self, path, label, url, response):
93111
"""
94112
Check a URL and response against a sample.
@@ -108,6 +126,11 @@ def assert_matches_sample(self, path, label, url, response):
108126
sample_response = sample.examples[label].response
109127

110128
self.assertEqual(url, sample_request.url)
129+
if 'headers' in sample_request:
130+
self.assert_headers_contain(
131+
response.request, sample_request.headers
132+
)
133+
111134
self.assertEqual(response.status_code, sample_response.status)
112135
if 'body' in sample_response:
113136
response_parsed = response.data

tests/test_assertions.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,37 @@ def test_list_equality_ordering_nested_items(self):
3737
AssertionError,
3838
self.assert_data_list_equal, data1, data2
3939
)
40+
41+
42+
class TestAssertHeadersEqual(TestCase, AbeTestMixin):
43+
44+
def test_matches_django_test_format(self):
45+
self.assert_headers_contain(
46+
{'HTTP_THIS_CUSTOM_HEADER': 'Foo'},
47+
{'This-Custom-Header': 'Foo'}
48+
)
49+
50+
def test_matches_normal_header_format(self):
51+
self.assert_headers_contain(
52+
{'This-Custom-Header': 'Foo'},
53+
{'This-Custom-Header': 'Foo'},
54+
)
55+
56+
def test_matches_only_sample_defined_headers(self):
57+
self.assert_headers_contain(
58+
{'HTTP_THIS_CUSTOM_HEADER': 'Foo',
59+
'HTTP_SOME_UNDEFINED_IN_SPEC_HEADER': 'Bar'},
60+
{'This-Custom-Header': 'Foo'},
61+
)
62+
63+
def test_requires_key_and_value_match(self):
64+
with self.assertRaises(AssertionError):
65+
self.assert_headers_contain(
66+
{'HTTP_THIS_CUSTOM_HEADER': 'Bar'},
67+
{'This-Custom-Header': 'Foo'},
68+
)
69+
with self.assertRaises(AssertionError):
70+
self.assert_headers_contain(
71+
{'HTTP_THIS_CUSTOM': 'Foo'},
72+
{'This-Custom-Header': 'Foo'},
73+
)

0 commit comments

Comments
 (0)