From ce22f0d524f26d1bff5ff1580bca484be72e0a7c Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Tue, 16 Aug 2016 13:38:06 -0700 Subject: [PATCH 1/2] Fixup GAE urlfetch tests Change-Id: I12bfc352915e00892bc3595739559465017f5163 --- appengine/standard/urlfetch/async/rpc.py | 14 +++--- appengine/standard/urlfetch/async/rpc_test.py | 43 +++++++++++++++---- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/appengine/standard/urlfetch/async/rpc.py b/appengine/standard/urlfetch/async/rpc.py index 79bd7c5398ec..297e1466b651 100644 --- a/appengine/standard/urlfetch/async/rpc.py +++ b/appengine/standard/urlfetch/async/rpc.py @@ -27,7 +27,7 @@ class UrlFetchRpcHandler(webapp2.RequestHandler): def get(self): # [START urlfetch-rpc] rpc = urlfetch.create_rpc() - urlfetch.make_fetch_call(rpc, "http://www.google.com/") + urlfetch.make_fetch_call(rpc, 'http://www.google.com/') # ... do other things ... try: @@ -36,10 +36,12 @@ def get(self): text = result.content self.response.write(text) else: - self.response.status_code = result.status_code - logging.error("Error making RPC request") + self.response.status_int = result.status_code + self.response.write('URL returned status code {}'.format( + result.status_code)) except urlfetch.DownloadError: - logging.error("Error fetching URL0") + self.response.status_int = 500 + self.response.write('Error fetching URL') # [END urlfetch-rpc] @@ -52,7 +54,7 @@ def get(self): def handle_result(rpc): result = rpc.get_result() self.response.write(result.content) - logging.info("Handling RPC in callback: result {}".format(result)) + logging.info('Handling RPC in callback: result {}'.format(result)) urls = ['http://www.google.com', 'http://www.github.com', @@ -71,7 +73,7 @@ def handle_result(rpc): for rpc in rpcs: rpc.wait() - logging.info("Done waiting for RPCs") + logging.info('Done waiting for RPCs') # [END urlfetch-rpc-callback] diff --git a/appengine/standard/urlfetch/async/rpc_test.py b/appengine/standard/urlfetch/async/rpc_test.py index b97ff98c86d9..45ad677ef01b 100644 --- a/appengine/standard/urlfetch/async/rpc_test.py +++ b/appengine/standard/urlfetch/async/rpc_test.py @@ -17,31 +17,58 @@ import rpc import webtest +from google.appengine.api import urlfetch + @pytest.fixture def app(): return webtest.TestApp(rpc.app) -@mock.patch("rpc.urlfetch") +@mock.patch('rpc.urlfetch') def test_url_fetch(urlfetch_mock, app): get_result_mock = mock.Mock( - return_value=mock.Mock(status_code=200, - content="I'm Feeling Lucky")) + return_value=mock.Mock( + status_code=200, + content='I\'m Feeling Lucky')) urlfetch_mock.create_rpc = mock.Mock( return_value=mock.Mock(get_result=get_result_mock)) response = app.get('/') assert response.status_int == 200 - assert "I'm Feeling Lucky" in response.body + assert 'I\'m Feeling Lucky' in response.body + + +@mock.patch('rpc.urlfetch') +def test_url_fetch_rpc_error(urlfetch_mock, app): + urlfetch_mock.DownloadError = urlfetch.DownloadError + get_result_mock = mock.Mock( + side_effect=urlfetch.DownloadError()) + urlfetch_mock.create_rpc = mock.Mock( + return_value=mock.Mock(get_result=get_result_mock)) + response = app.get('/', status=500) + assert 'Error fetching URL' in response.body -@mock.patch("rpc.urlfetch") +@mock.patch('rpc.urlfetch') +def test_url_fetch_http_error(urlfetch_mock, app): + get_result_mock = mock.Mock( + return_value=mock.Mock( + status_code=404, + content='Not Found')) + urlfetch_mock.create_rpc = mock.Mock( + return_value=mock.Mock(get_result=get_result_mock)) + response = app.get('/', status=404) + assert '404' in response.body + + +@mock.patch('rpc.urlfetch') def test_url_post(urlfetch_mock, app): get_result_mock = mock.Mock( - return_value=mock.Mock(status_code=200, - content="I'm Feeling Lucky")) + return_value=mock.Mock( + status_code=200, + content='I\'m Feeling Lucky')) urlfetch_mock.create_rpc = mock.Mock( - return_value=mock.Mock(get_result=get_result_mock)) + return_value=mock.Mock(get_result=get_result_mock)) rpc_mock = mock.Mock() urlfetch_mock.create_rpc.return_value = rpc_mock From 1ae2596ec2a2fd7886545ad95681a2ff3b816bd8 Mon Sep 17 00:00:00 2001 From: Jon Wayne Parrott Date: Tue, 16 Aug 2016 14:58:28 -0700 Subject: [PATCH 2/2] Fix lint. Change-Id: Idd758ddb7e5beb6d16489955cbf794011aa145cc --- appengine/standard/urlfetch/async/rpc_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/appengine/standard/urlfetch/async/rpc_test.py b/appengine/standard/urlfetch/async/rpc_test.py index 45ad677ef01b..cff5c985f78f 100644 --- a/appengine/standard/urlfetch/async/rpc_test.py +++ b/appengine/standard/urlfetch/async/rpc_test.py @@ -12,13 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. + +from google.appengine.api import urlfetch import mock import pytest import rpc import webtest -from google.appengine.api import urlfetch - @pytest.fixture def app():