Repository: cdent/gabbi

Issue:
Regression with 1.39.0 and empty reponse_headers field
One of the tests in Gnocchi has an empty `reponse_headers` field:
https://github.com/gnocchixyz/gnocchi/blob/master/gnocchi/tests/functional/gabbits/resource.yaml#L503

This broke the test suite in some way as one behaviour changed between Gabbi 1.38.0 and 1.39.0. I've bisected and the culprit is 8de923ac825bfeb34cd8c9384634c68ebda345bd.

This is not critical as the fix is easy on Gnocchi's side: remove the empty and useless `reponse_headers` field in the YAML file https://github.com/gnocchixyz/gnocchi/pull/599. Still, I thought it might be useful to report it just in case there's something bigger behind that we would have missed.

Discussion:

Patch:
diff --git a/gabbi/case.py b/gabbi/case.py
index a7a0f49..2403859 100644
--- a/gabbi/case.py
+++ b/gabbi/case.py
@@ -481,15 +481,15 @@ class HTTPTestCase(testtools.TestCase):
     def _replace_headers_template(self, test_name, headers):
         replaced_headers = {}
 
-        for name in headers:
-            try:
+        try:
+            for name in headers:
                 replaced_name = self.replace_template(name)
                 replaced_headers[replaced_name] = self.replace_template(
                     headers[name]
                 )
-            except TypeError as exc:
-                raise exception.GabbiFormatError(
-                    'malformed headers in test %s: %s' % (test_name, exc))
+        except TypeError as exc:
+            raise exception.GabbiFormatError(
+                'malformed headers in test %s: %s' % (test_name, exc))
 
         return replaced_headers
 


Test patch:
diff --git a/gabbi/tests/test_replacers.py b/gabbi/tests/test_replacers.py
index 65fdc3f..9505e60 100644
--- a/gabbi/tests/test_replacers.py
+++ b/gabbi/tests/test_replacers.py
@@ -18,6 +18,7 @@ import os
 import unittest
 
 from gabbi import case
+from gabbi import exception
 
 
 class EnvironReplaceTest(unittest.TestCase):
@@ -56,3 +57,13 @@ class EnvironReplaceTest(unittest.TestCase):
 
         os.environ['moo'] = "True"
         self.assertEqual(True, http_case._environ_replace(message))
+
+
+class TestReplaceHeaders(unittest.TestCase):
+
+    def test_empty_headers(self):
+        """A None value in headers should cause a GabbiFormatError."""
+        http_case = case.HTTPTestCase('test_request')
+        self.assertRaises(
+                exception.GabbiFormatError,
+                http_case._replace_headers_template, 'foo', None)