Repository: cdent/gabbi

Issue:
missing response header raises error rather than failure
The following reports an error when it should report a failure instead:

```yaml
tests:

  - name: failure
    url: http://google.com

    status: 302
    response_headers:
        x-foo: bar
```

AFAICT that's because internally a `KeyError` is raised ("'x-foo' header not available in response keys: dict_keys(...)"): `testtools.TestCase`'s default `exception_handlers` doesn't have a mapping for that particular exception, so it defaults to `_report_error` rather than using `_report_failure`.


Discussion:

Patch:
diff --git a/gabbi/handlers.py b/gabbi/handlers.py
index d39a987..0b7a71d 100644
--- a/gabbi/handlers.py
+++ b/gabbi/handlers.py
@@ -128,9 +128,9 @@ class HeadersResponseHandler(ResponseHandler):
         try:
             response_value = response[header]
         except KeyError:
-            # Reform KeyError to something more debuggable.
-            raise KeyError("'%s' header not available in response keys: %s"
-                           % (header, response.keys()))
+            raise AssertionError(
+                "'%s' header not present in response: %s" % (
+                    header, response.keys()))
 
         if header_value.startswith('/') and header_value.endswith('/'):
             header_value = header_value.strip('/').rstrip('/')


Test patch:
diff --git a/gabbi/tests/test_handlers.py b/gabbi/tests/test_handlers.py
index b647e80..af27359 100644
--- a/gabbi/tests/test_handlers.py
+++ b/gabbi/tests/test_handlers.py
@@ -133,9 +133,9 @@ class HandlersTest(unittest.TestCase):
             'location': '/somewhere',
         }}
         self.test.response = {'content-type': 'application/json'}
-        with self.assertRaises(KeyError) as failure:
+        with self.assertRaises(AssertionError) as failure:
             self._assert_handler(handler)
-        self.assertIn("'location' header not available in response keys:",
+        self.assertIn("'location' header not present in response:",
                       str(failure.exception))
 
     def _assert_handler(self, handler):