]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
Add a read_body helper function
authorDiaoul <diaoulael@gmail.com>
Tue, 14 Jul 2015 23:16:10 +0000 (01:16 +0200)
committerDiaoul <diaoulael@gmail.com>
Tue, 14 Jul 2015 23:16:10 +0000 (01:16 +0200)
vcr/matchers.py
vcr/util.py

index 94a91874c527772dad3997efe30dc4b90979e777..39c5949fd84a6bbc679849d9a7ecb036ea8b9e51 100644 (file)
@@ -1,6 +1,6 @@
 import json
 from six.moves import urllib, xmlrpc_client
-from .util import CaseInsensitiveDict
+from .util import CaseInsensitiveDict, read_body
 import logging
 log = logging.getLogger(__name__)
 
@@ -34,18 +34,12 @@ def query(r1, r2):
 
 
 def raw_body(r1, r2):
-    if hasattr(r1.body, 'read') and hasattr(r2.body, 'read'):
-        return r1.body.read() == r2.body.read()
-    return r1.body == r2.body
+    return read_body(r1) == read_body(r2)
 
 
 def body(r1, r2):
-    if hasattr(r1.body, 'read') and hasattr(r2.body, 'read'):
-        r1_body = r1.body.read()
-        r2_body  = r2.body.read()
-    else:
-        r1_body = r1.body
-        r2_body  = r2.body
+    r1_body = read_body(r1)
+    r2_body  = read_body(r2)
     r1_headers = CaseInsensitiveDict(r1.headers)
     r2_headers = CaseInsensitiveDict(r2.headers)
     if r1_headers.get('Content-Type') == r2_headers.get('Content-Type') == 'application/x-www-form-urlencoded':
index e3b05d8549715e26c6f28fce53620a8c26a714cd..8c5bd9472aa911d69475bab50343247ab5b4c9bf 100644 (file)
@@ -85,3 +85,8 @@ def compose(*functions):
             res = function(res)
         return res
     return composed
+
+def read_body(request):
+    if hasattr(request.body, 'read'):
+        return request.body.read()
+    return request.body