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
 

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.
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)

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest-cov hacking sphinx pytest
3.6
requirements.txt requirements-dev.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
alabaster==0.7.13
attrs==22.2.0
Babel==2.11.0
certifi==2021.5.30
charset-normalizer==2.0.12
colorama==0.4.5
coverage==6.2
decorator==5.1.1
distlib==0.3.9
docutils==0.18.1
filelock==3.4.1
fixtures==4.0.1
flake8==3.8.4
-e git+https://github.com/cdent/gabbi.git@d91d8ec9a5516615289d77a30529f9ea496b98c4#egg=gabbi
hacking==4.1.0
idna==3.10
imagesize==1.4.1
importlib-metadata==4.8.3
importlib-resources==5.4.0
iniconfig==1.1.1
Jinja2==3.0.3
jsonpath-rw==1.4.0
jsonpath-rw-ext==1.2.2
MarkupSafe==2.0.1
mccabe==0.6.1
packaging==21.3
pbr==6.1.1
platformdirs==2.4.0
pluggy==1.0.0
ply==3.11
py==1.11.0
pycodestyle==2.6.0
pyflakes==2.2.0
Pygments==2.14.0
pyparsing==3.1.4
pytest==7.0.1
pytest-cov==4.0.0
pytz==2025.2
PyYAML==6.0.1
requests==2.27.1
six==1.17.0
snowballstemmer==2.2.0
Sphinx==5.3.0
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
testtools==2.6.0
toml==0.10.2
tomli==1.2.3
tox==3.28.0
typing_extensions==4.1.1
urllib3==1.26.20
virtualenv==20.17.1
wsgi_intercept==1.13.1
zipp==3.6.0

name: gabbi
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - alabaster==0.7.13
      - attrs==22.2.0
      - babel==2.11.0
      - charset-normalizer==2.0.12
      - colorama==0.4.5
      - coverage==6.2
      - decorator==5.1.1
      - distlib==0.3.9
      - docutils==0.18.1
      - filelock==3.4.1
      - fixtures==4.0.1
      - flake8==3.8.4
      - hacking==4.1.0
      - idna==3.10
      - imagesize==1.4.1
      - importlib-metadata==4.8.3
      - importlib-resources==5.4.0
      - iniconfig==1.1.1
      - jinja2==3.0.3
      - jsonpath-rw==1.4.0
      - jsonpath-rw-ext==1.2.2
      - markupsafe==2.0.1
      - mccabe==0.6.1
      - packaging==21.3
      - pbr==6.1.1
      - platformdirs==2.4.0
      - pluggy==1.0.0
      - ply==3.11
      - py==1.11.0
      - pycodestyle==2.6.0
      - pyflakes==2.2.0
      - pygments==2.14.0
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pytest-cov==4.0.0
      - pytz==2025.2
      - pyyaml==6.0.1
      - requests==2.27.1
      - six==1.17.0
      - snowballstemmer==2.2.0
      - sphinx==5.3.0
      - sphinxcontrib-applehelp==1.0.2
      - sphinxcontrib-devhelp==1.0.2
      - sphinxcontrib-htmlhelp==2.0.0
      - sphinxcontrib-jsmath==1.0.1
      - sphinxcontrib-qthelp==1.0.3
      - sphinxcontrib-serializinghtml==1.1.5
      - testtools==2.6.0
      - toml==0.10.2
      - tomli==1.2.3
      - tox==3.28.0
      - typing-extensions==4.1.1
      - urllib3==1.26.20
      - virtualenv==20.17.1
      - wsgi-intercept==1.13.1
      - zipp==3.6.0
prefix: /opt/conda/envs/gabbi

gabbi/tests/test_replacers.py::TestReplaceHeaders::test_empty_headers
gabbi/tests/test_replacers.py::EnvironReplaceTest::test_environ_boolean
gabbi/case.py
gabbi/case.py