From 2bcc4a774a8ca1556bd95c32f044d818f3547a40 Mon Sep 17 00:00:00 2001 From: Scott Burns Date: Wed, 23 Jan 2013 15:25:48 -0600 Subject: [PATCH 1/4] Pass kwargs to json.loads Not all JSON is created equally. This commit addresses when users want to take control of the json decode process. --- requests/models.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/requests/models.py b/requests/models.py index 8885569a..131bcd5a 100644 --- a/requests/models.py +++ b/requests/models.py @@ -596,8 +596,11 @@ class Response(object): return content - def json(self): - """Returns the json-encoded content of a response, if any.""" + def json(self, **kwargs): + """Returns the json-encoded content of a response, if any. + + :param \*\*kwargs: Optional arguments that ``json.loads`` takes. + """ if not self.encoding and len(self.content) > 3: # No encoding set. JSON RFC 4627 section 3 states we should expect @@ -606,8 +609,8 @@ class Response(object): # a best guess). encoding = guess_json_utf(self.content) if encoding is not None: - return json.loads(self.content.decode(encoding)) - return json.loads(self.text or self.content) + return json.loads(self.content.decode(encoding), **kwargs) + return json.loads(self.text or self.content, **kwargs) @property def links(self): From 0af8ff940538357d3357814430be78dfdbdc4a94 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 23 Jan 2013 20:03:43 -0500 Subject: [PATCH 2/4] remove silly assert --- test_requests.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/test_requests.py b/test_requests.py index 425e3570..32340ae3 100644 --- a/test_requests.py +++ b/test_requests.py @@ -34,9 +34,6 @@ class RequestsTestCase(unittest.TestCase): """Teardown.""" pass - def test_assertion(self): - assert 1 - def test_entry_points(self): requests.session From ccea28db071e057d161567329d0844357789a5c3 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 23 Jan 2013 20:31:05 -0500 Subject: [PATCH 3/4] introduce a test for link headers. --- test_requests.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test_requests.py b/test_requests.py index 32340ae3..d47c8fed 100644 --- a/test_requests.py +++ b/test_requests.py @@ -265,6 +265,11 @@ class RequestsTestCase(unittest.TestCase): self.assertEqual(r.status_code, 200) self.assertTrue(b"text/py-content-type" in r.request.body) + def test_links(self): + url = 'https://api.github.com/users/kennethreitz/repos?page=1&per_page=10' + r = requests.head(url=url) + self.assertEqual(r.links['next']['rel'], 'next') + if __name__ == '__main__': unittest.main() From ef52044c5f0411307ef2200fcb950bd84879266f Mon Sep 17 00:00:00 2001 From: Chad Whitacre Date: Wed, 23 Jan 2013 20:32:03 -0500 Subject: [PATCH 4/4] Remove a test per @kennethreitz in IRC The test suite is moving from the httpbin pattern (which hits the network) to depending on the request.prepare method (which doesn't). Here's a start ... --- test_requests.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test_requests.py b/test_requests.py index 425e3570..e3f8627f 100644 --- a/test_requests.py +++ b/test_requests.py @@ -76,10 +76,6 @@ class RequestsTestCase(unittest.TestCase): self.assertEqual(request.url, "http://example.com/path?key=value&a=b#fragment") - def test_HTTP_200_OK_GET(self): - r = requests.get(httpbin('get')) - self.assertEqual(r.status_code, 200) - def test_HTTP_200_OK_GET_ALTERNATIVE(self): r = requests.Request('GET', httpbin('get')) s = requests.Session()