Skip to content

Commit

Permalink
Fixup GAE urlfetch tests
Browse files Browse the repository at this point in the history
Change-Id: I12bfc352915e00892bc3595739559465017f5163
  • Loading branch information
Jon Wayne Parrott committed Aug 16, 2016
1 parent 2d783c4 commit ce22f0d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
14 changes: 8 additions & 6 deletions appengine/standard/urlfetch/async/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]


Expand All @@ -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',
Expand All @@ -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]


Expand Down
43 changes: 35 additions & 8 deletions appengine/standard/urlfetch/async/rpc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ce22f0d

Please sign in to comment.