Skip to content

Commit ce22f0d

Browse files
author
Jon Wayne Parrott
committed
Fixup GAE urlfetch tests
Change-Id: I12bfc352915e00892bc3595739559465017f5163
1 parent 2d783c4 commit ce22f0d

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

appengine/standard/urlfetch/async/rpc.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class UrlFetchRpcHandler(webapp2.RequestHandler):
2727
def get(self):
2828
# [START urlfetch-rpc]
2929
rpc = urlfetch.create_rpc()
30-
urlfetch.make_fetch_call(rpc, "http://www.google.com/")
30+
urlfetch.make_fetch_call(rpc, 'http://www.google.com/')
3131

3232
# ... do other things ...
3333
try:
@@ -36,10 +36,12 @@ def get(self):
3636
text = result.content
3737
self.response.write(text)
3838
else:
39-
self.response.status_code = result.status_code
40-
logging.error("Error making RPC request")
39+
self.response.status_int = result.status_code
40+
self.response.write('URL returned status code {}'.format(
41+
result.status_code))
4142
except urlfetch.DownloadError:
42-
logging.error("Error fetching URL0")
43+
self.response.status_int = 500
44+
self.response.write('Error fetching URL')
4345
# [END urlfetch-rpc]
4446

4547

@@ -52,7 +54,7 @@ def get(self):
5254
def handle_result(rpc):
5355
result = rpc.get_result()
5456
self.response.write(result.content)
55-
logging.info("Handling RPC in callback: result {}".format(result))
57+
logging.info('Handling RPC in callback: result {}'.format(result))
5658

5759
urls = ['http://www.google.com',
5860
'http://www.github.com',
@@ -71,7 +73,7 @@ def handle_result(rpc):
7173
for rpc in rpcs:
7274
rpc.wait()
7375

74-
logging.info("Done waiting for RPCs")
76+
logging.info('Done waiting for RPCs')
7577
# [END urlfetch-rpc-callback]
7678

7779

appengine/standard/urlfetch/async/rpc_test.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,58 @@
1717
import rpc
1818
import webtest
1919

20+
from google.appengine.api import urlfetch
21+
2022

2123
@pytest.fixture
2224
def app():
2325
return webtest.TestApp(rpc.app)
2426

2527

26-
@mock.patch("rpc.urlfetch")
28+
@mock.patch('rpc.urlfetch')
2729
def test_url_fetch(urlfetch_mock, app):
2830
get_result_mock = mock.Mock(
29-
return_value=mock.Mock(status_code=200,
30-
content="I'm Feeling Lucky"))
31+
return_value=mock.Mock(
32+
status_code=200,
33+
content='I\'m Feeling Lucky'))
3134
urlfetch_mock.create_rpc = mock.Mock(
3235
return_value=mock.Mock(get_result=get_result_mock))
3336
response = app.get('/')
3437
assert response.status_int == 200
35-
assert "I'm Feeling Lucky" in response.body
38+
assert 'I\'m Feeling Lucky' in response.body
39+
40+
41+
@mock.patch('rpc.urlfetch')
42+
def test_url_fetch_rpc_error(urlfetch_mock, app):
43+
urlfetch_mock.DownloadError = urlfetch.DownloadError
44+
get_result_mock = mock.Mock(
45+
side_effect=urlfetch.DownloadError())
46+
urlfetch_mock.create_rpc = mock.Mock(
47+
return_value=mock.Mock(get_result=get_result_mock))
48+
response = app.get('/', status=500)
49+
assert 'Error fetching URL' in response.body
3650

3751

38-
@mock.patch("rpc.urlfetch")
52+
@mock.patch('rpc.urlfetch')
53+
def test_url_fetch_http_error(urlfetch_mock, app):
54+
get_result_mock = mock.Mock(
55+
return_value=mock.Mock(
56+
status_code=404,
57+
content='Not Found'))
58+
urlfetch_mock.create_rpc = mock.Mock(
59+
return_value=mock.Mock(get_result=get_result_mock))
60+
response = app.get('/', status=404)
61+
assert '404' in response.body
62+
63+
64+
@mock.patch('rpc.urlfetch')
3965
def test_url_post(urlfetch_mock, app):
4066
get_result_mock = mock.Mock(
41-
return_value=mock.Mock(status_code=200,
42-
content="I'm Feeling Lucky"))
67+
return_value=mock.Mock(
68+
status_code=200,
69+
content='I\'m Feeling Lucky'))
4370
urlfetch_mock.create_rpc = mock.Mock(
44-
return_value=mock.Mock(get_result=get_result_mock))
71+
return_value=mock.Mock(get_result=get_result_mock))
4572

4673
rpc_mock = mock.Mock()
4774
urlfetch_mock.create_rpc.return_value = rpc_mock

0 commit comments

Comments
 (0)