From e8806426cec3dfaf020fd13a7870801b129e1b6f Mon Sep 17 00:00:00 2001 From: sneakypete81 Date: Fri, 6 Sep 2013 18:20:03 +0100 Subject: [PATCH 1/2] Use DDT to simplify http_error testcases --- tests/unit/test_http_errors.py | 143 ++++++++++----------------------- 1 file changed, 41 insertions(+), 102 deletions(-) diff --git a/tests/unit/test_http_errors.py b/tests/unit/test_http_errors.py index 4a1845b..a4ec376 100644 --- a/tests/unit/test_http_errors.py +++ b/tests/unit/test_http_errors.py @@ -1,6 +1,8 @@ from __future__ import unicode_literals import json import httpretty +from httpretty import GET, POST +from ddt import ddt, data # TEMP: Temporary hack until httpretty string checking is fixed if httpretty.compat.PY3: @@ -11,8 +13,10 @@ try: except ImportError: import unittest +from test_http import GetOrPost import trovebox +@ddt class TestHttpErrors(unittest.TestCase): test_host = "test.example.com" test_endpoint = "test.json" @@ -42,146 +46,81 @@ class TestHttpErrors(unittest.TestCase): **kwds) @httpretty.activate - def test_get_with_error_status(self): + @data(GET, POST) + def test_error_status(self, method): """ - Check that an error status causes the get method + Check that an error status causes the get/post methods to raise an exception """ - self._register_uri(httpretty.GET, status=500) + self._register_uri(method, status=500) with self.assertRaises(trovebox.TroveboxError): - self.client.get(self.test_endpoint) + GetOrPost(self.client, method).call(self.test_endpoint) @httpretty.activate - def test_post_with_error_status(self): + @data(GET, POST) + def test_404_status(self, method): """ - Check that an error status causes the post method - to raise an exception - """ - self._register_uri(httpretty.POST, status=500) - with self.assertRaises(trovebox.TroveboxError): - self.client.post(self.test_endpoint) - - @httpretty.activate - def test_get_with_404_status(self): - """ - Check that a 404 status causes the get method + Check that a 404 status causes the get/post methods to raise a 404 exception """ - self._register_uri(httpretty.GET, status=404) + self._register_uri(method, status=404) with self.assertRaises(trovebox.Trovebox404Error): - self.client.get(self.test_endpoint) + GetOrPost(self.client, method).call(self.test_endpoint) @httpretty.activate - def test_post_with_404_status(self): + @data(GET, POST) + def test_with_invalid_json(self, method): """ - Check that a 404 status causes the post method - to raise a 404 exception - """ - self._register_uri(httpretty.POST, status=404) - with self.assertRaises(trovebox.Trovebox404Error): - self.client.post(self.test_endpoint) - - @httpretty.activate - def test_get_with_invalid_json(self): - """ - Check that invalid JSON causes the get method to + Check that invalid JSON causes the get/post methods to raise an exception """ - self._register_uri(httpretty.GET, body="Invalid JSON") + self._register_uri(method, body="Invalid JSON") with self.assertRaises(ValueError): - self.client.get(self.test_endpoint) + GetOrPost(self.client, method).call(self.test_endpoint) @httpretty.activate - def test_post_with_invalid_json(self): + @data(GET, POST) + def test_with_error_status_and_invalid_json(self, method): """ - Check that invalid JSON causes the post method to - raise an exception + Check that invalid JSON causes the get/post methods to raise + an exception, even with an error status is returned """ - self._register_uri(httpretty.POST, body="Invalid JSON") - with self.assertRaises(ValueError): - self.client.post(self.test_endpoint) - - @httpretty.activate - def test_get_with_error_status_and_invalid_json(self): - """ - Check that invalid JSON causes the get method to raise an exception, - even with an error status is returned - """ - self._register_uri(httpretty.GET, body="Invalid JSON", status=500) + self._register_uri(method, body="Invalid JSON", status=500) with self.assertRaises(trovebox.TroveboxError): - self.client.get(self.test_endpoint) + GetOrPost(self.client, method).call(self.test_endpoint) @httpretty.activate - def test_post_with_error_status_and_invalid_json(self): + @data(GET, POST) + def test_with_404_status_and_invalid_json(self, method): """ - Check that invalid JSON causes the post method to raise an exception, - even with an error status is returned + Check that invalid JSON causes the get/post methods to raise + an exception, even with a 404 status is returned """ - self._register_uri(httpretty.POST, body="Invalid JSON", status=500) - with self.assertRaises(trovebox.TroveboxError): - self.client.post(self.test_endpoint) - - @httpretty.activate - def test_get_with_404_status_and_invalid_json(self): - """ - Check that invalid JSON causes the get method to raise an exception, - even with a 404 status is returned - """ - self._register_uri(httpretty.GET, body="Invalid JSON", status=404) + self._register_uri(method, body="Invalid JSON", status=404) with self.assertRaises(trovebox.Trovebox404Error): - self.client.get(self.test_endpoint) + GetOrPost(self.client, method).call(self.test_endpoint) @httpretty.activate - def test_post_with_404_status_and_invalid_json(self): + @data(GET, POST) + def test_with_duplicate_status(self, method): """ - Check that invalid JSON causes the post method to raise an exception, - even with a 404 status is returned - """ - self._register_uri(httpretty.POST, body="Invalid JSON", status=404) - with self.assertRaises(trovebox.Trovebox404Error): - self.client.post(self.test_endpoint) - - @httpretty.activate - def test_get_with_duplicate_status(self): - """ - Check that a get with a duplicate status + Check that a get/post with a duplicate status raises a duplicate exception """ data = {"message": "This photo already exists", "code": 409} - self._register_uri(httpretty.GET, data=data, status=409) + self._register_uri(method, data=data, status=409) with self.assertRaises(trovebox.TroveboxDuplicateError): - self.client.get(self.test_endpoint) + GetOrPost(self.client, method).call(self.test_endpoint) @httpretty.activate - def test_post_with_duplicate_status(self): - """ - Check that a post with a duplicate status - raises a duplicate exception - """ - data = {"message": "This photo already exists", "code": 409} - self._register_uri(httpretty.POST, data=data, status=409) - with self.assertRaises(trovebox.TroveboxDuplicateError): - self.client.post(self.test_endpoint) - - @httpretty.activate - def test_get_with_status_code_mismatch(self): + @data(GET, POST) + def test_with_status_code_mismatch(self, method): """ Check that a mismatched HTTP status code still returns the - JSON status code for get requests. + JSON status code. """ data = {"message": "Test Message", "code": 202} - self._register_uri(httpretty.GET, data=data, status=200) - response = self.client.get(self.test_endpoint) - self.assertEqual(response["code"], 202) - - @httpretty.activate - def test_post_with_status_code_mismatch(self): - """ - Check that a mismatched HTTP status code still returns the - JSON status code for post requests. - """ - data = {"message": "Test Message", "code": 202} - self._register_uri(httpretty.POST, data=data, status=200) - response = self.client.post(self.test_endpoint) + self._register_uri(method, data=data, status=200) + response = GetOrPost(self.client, method).call(self.test_endpoint) self.assertEqual(response["code"], 202) From 533c2661b18f2e6032eb713049749d7f1f12a482 Mon Sep 17 00:00:00 2001 From: sneakypete81 Date: Fri, 6 Sep 2013 18:21:21 +0100 Subject: [PATCH 2/2] Ensure HTTP errors raise exceptions even when response processing is disabled --- tests/unit/test_http_errors.py | 12 ++++++++++++ trovebox/http.py | 12 ++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/unit/test_http_errors.py b/tests/unit/test_http_errors.py index a4ec376..f2a7893 100644 --- a/tests/unit/test_http_errors.py +++ b/tests/unit/test_http_errors.py @@ -124,3 +124,15 @@ class TestHttpErrors(unittest.TestCase): response = GetOrPost(self.client, method).call(self.test_endpoint) self.assertEqual(response["code"], 202) + @httpretty.activate + @data(GET, POST) + def test_http_error_with_no_response_processing(self, method): + """ + Check that get/post methods work with response processing disabled + when an HTTP error code is returned. + """ + httpretty.register_uri(method, self.test_uri, status=500) + with self.assertRaises(trovebox.TroveboxError): + response = GetOrPost(self.client, method).call(self.test_endpoint, + process_response=False) + diff --git a/trovebox/http.py b/trovebox/http.py index dd33917..ffeb487 100644 --- a/trovebox/http.py +++ b/trovebox/http.py @@ -113,7 +113,11 @@ class Http(object): if process_response: return self._process_response(response) else: - return response.text + if 200 <= response.status_code < 300: + return response.text + else: + raise TroveboxError("HTTP Error %d: %s" % + (response.status_code, response.reason)) def post(self, endpoint, process_response=True, files=None, **params): """ @@ -163,7 +167,11 @@ class Http(object): if process_response: return self._process_response(response) else: - return response.text + if 200 <= response.status_code < 300: + return response.text + else: + raise TroveboxError("HTTP Error %d: %s" % + (response.status_code, response.reason)) def _construct_url(self, endpoint): """Return the full URL to the specified endpoint"""