Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup GAE urlfetch tests #457

Merged
merged 2 commits into from
Aug 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -12,6 +12,8 @@
# 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
Expand All @@ -23,25 +25,50 @@ 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