Repository: cdent/gabbi

Issue:
If content-type is not set, but data is, the error message is incomplete
If you try to write `data` without setting a content-type header, there's no content handler and the error message is not very useful:

```
      File "/mnt/share/cdentsrc/nova/.tox/functional/local/lib/python2.7/site-packages/gabbi/suitemaker.py", line 95, in do_test
        return test_method(*args, **kwargs)
      File "/mnt/share/cdentsrc/nova/.tox/functional/local/lib/python2.7/site-packages/gabbi/case.py", line 94, in wrapper
        func(self)
      File "/mnt/share/cdentsrc/nova/.tox/functional/local/lib/python2.7/site-packages/gabbi/case.py", line 148, in test_request
        self._run_test()
      File "/mnt/share/cdentsrc/nova/.tox/functional/local/lib/python2.7/site-packages/gabbi/case.py", line 524, in _run_test
        utils.extract_content_type(headers, default='')[0])
      File "/mnt/share/cdentsrc/nova/.tox/functional/local/lib/python2.7/site-packages/gabbi/case.py", line 587, in _test_data_to_string
        'unable to process data to %s' % content_type)
    ValueError: unable to process data to 
```

If content_type is None, we should provide a different message.

Discussion:

Patch:
diff --git a/gabbi/case.py b/gabbi/case.py
index 6b7823c..c1c0dec 100644
--- a/gabbi/case.py
+++ b/gabbi/case.py
@@ -595,8 +595,12 @@ class HTTPTestCase(unittest.TestCase):
                 data = self.replace_template(data)
                 data = dumper_class.dumps(data, test=self)
             else:
-                raise ValueError(
-                    'unable to process data to %s' % content_type)
+                if content_type:
+                    raise ValueError(
+                        'unable to process data to %s' % content_type)
+                else:
+                    raise ValueError(
+                        'no content-type available for processing data')
 
         data = self.replace_template(data)
 


Test patch:
diff --git a/gabbi/tests/test_data_to_string.py b/gabbi/tests/test_data_to_string.py
new file mode 100644
index 0000000..d3150ba
--- /dev/null
+++ b/gabbi/tests/test_data_to_string.py
@@ -0,0 +1,54 @@
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+"""Test handling of data field in tests.
+"""
+
+import unittest
+
+from gabbi import case
+from gabbi import handlers
+
+
+class TestDataToString(unittest.TestCase):
+
+    def setUp(self):
+        self.case = case.HTTPTestCase('test_request')
+        self.case.content_handlers = []
+        for handler in handlers.RESPONSE_HANDLERS:
+            h = handler()
+            if hasattr(h, 'content_handler') and h.content_handler:
+                self.case.content_handlers.append(h)
+
+    def testHappyPath(self):
+        data = [{"hi": "low"}, {"yes": "no"}]
+        content_type = 'application/json'
+        body = self.case._test_data_to_string(data, content_type)
+        self.assertEqual('[{"hi": "low"}, {"yes": "no"}]', body)
+
+    def testNoContentType(self):
+        data = [{"hi": "low"}, {"yes": "no"}]
+        content_type = ''
+        with self.assertRaises(ValueError) as exc:
+            self.case._test_data_to_string(data, content_type)
+        self.assertEqual(
+            'no content-type available for processing data',
+            str(exc.exception))
+
+    def testNoHandler(self):
+        data = [{"hi": "low"}, {"yes": "no"}]
+        content_type = 'application/xml'
+        with self.assertRaises(ValueError) as exc:
+            self.case._test_data_to_string(data, content_type)
+        self.assertEqual(
+            'unable to process data to application/xml',
+            str(exc.exception))