]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Add Exception when JSON Serializing Binary Data
authorKevin McCarthy <me@kevinmccarthy.org>
Sun, 27 Apr 2014 00:31:40 +0000 (14:31 -1000)
committerKevin McCarthy <me@kevinmccarthy.org>
Sun, 27 Apr 2014 00:31:40 +0000 (14:31 -1000)
Since I can't think of a good way to deal with this, let's just give a
nice error message to point people in the right direction.

Closes #51

tests/unit/test_json_serializer.py [new file with mode: 0644]
vcr/serializers/jsonserializer.py

diff --git a/tests/unit/test_json_serializer.py b/tests/unit/test_json_serializer.py
new file mode 100644 (file)
index 0000000..fe043c2
--- /dev/null
@@ -0,0 +1,14 @@
+import pytest
+from vcr.serializers.jsonserializer import serialize
+from vcr.request import Request
+
+
+def test_serialize_binary():
+    request = Request('http','localhost',80,'GET','/',{},{})
+    cassette = {'requests': [request], 'responses': [{'body':b'\x8c'}]}
+
+    with pytest.raises(Exception) as e:
+        serialize(cassette)
+        assert e.message == "Error serializing cassette to JSON. Does this \
+            HTTP interaction contain binary data? If so, use a different \
+            serializer (like the yaml serializer) for this request"
index d10a1382c017fbeac3cf369de2b2e93b1352185c..eeaa91be85c875b976cabb489a64791d283bf717 100644 (file)
@@ -27,4 +27,12 @@ def serialize(cassette_dict):
         cassette_dict['requests'],
         cassette_dict['responses']
     )])
-    return json.dumps(data, indent=4, default=_json_default)
+    try:
+        return json.dumps(data, indent=4, default=_json_default)
+    except UnicodeDecodeError as e:
+        raise UnicodeDecodeError(
+            "Error serializing cassette to JSON. ",
+            "Does this HTTP interaction contain binary data? ",
+            "If so, use a different serializer (like the yaml serializer) for",
+            "this request"
+        )