Commits

Bruno Renié committed 1c37e6b

Added case-insensitive header access in DjangoWebtestResponse

  • Participants
  • Parent commits b8f3bb0

Comments (0)

Files changed (2)

django_webtest/response.py

         return client
 
     def __getitem__(self, item):
-        if item != 'Location':
-            raise TypeError('Keys other than "Location" are unsupported')
-
-        # django's test response returns location as http://testserver/,
-        # WebTest returns it as http://localhost:80/
-        e_scheme, e_netloc, e_path, e_query, e_fragment = urlparse.urlsplit(self.location)
-        if e_netloc == 'localhost:80':
-            e_netloc = 'testserver'
-        return urlparse.urlunsplit([e_scheme, e_netloc, e_path, e_query, e_fragment])
+        item = item.lower()
+        if item == 'location':
+            # django's test response returns location as http://testserver/,
+            # WebTest returns it as http://localhost:80/
+            e_scheme, e_netloc, e_path, e_query, e_fragment = urlparse.urlsplit(self.location)
+            if e_netloc == 'localhost:80':
+                e_netloc = 'testserver'
+            return urlparse.urlunsplit([e_scheme, e_netloc, e_path, e_query, e_fragment])
+        for header, value in self.headerlist:
+            if header.lower() == item:
+                return value
+        raise KeyError(item)

django_webtest_tests/testapp_tests/tests.py

     def test_session_not_empty(self):
         response = self.app.get(reverse('set_session'))
         self.assertEqual('foo', self.app.session['test'])
+
+
+class TestHeaderAccess(WebTest):
+    def test_headers(self):
+        response = self.app.get('/')
+        self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
+        self.assertEqual(response['content-type'], 'text/html; charset=utf-8')
+
+    def test_bad_header(self):
+        response = self.app.get('/')
+        with self.assertRaises(KeyError):
+            response['X-Unknown-Header']
+