Skip to content

Commit

Permalink
more REST API error handling for url input: bad urls, connection fail…
Browse files Browse the repository at this point in the history
  • Loading branch information
snarfed committed Aug 7, 2016
1 parent eb8eb29 commit 9235863
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 10 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

__author__ = 'Ryan Barrett <granary@ryanb.org>'

import httplib
import json
import logging
import urllib
Expand Down Expand Up @@ -99,7 +100,15 @@ def get(self):

# fetch url
url = util.get_required_param(self, 'url')
resp = util.urlopen(url)
try:
resp = util.urlopen(url)
except httplib.InvalidURL as e:
self.abort(400, str(e))
except Exception as e:
if util.is_connection_failure(e):
self.abort(502, str(e))
raise

if url != resp.geturl():
url = resp.geturl()
logging.info('Redirected to %s', url)
Expand Down
16 changes: 15 additions & 1 deletion test_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Unit tests for app.py.
"""

import httplib
import json
import socket
import xml.sax.saxutils

import oauth_dropins.webutil.test
Expand Down Expand Up @@ -182,6 +183,19 @@ def test_url_bad_input(self):
resp = app.application.get_response('/url?url=http://my/posts.json&input=foo')
self.assert_equals(400, resp.status_int)

def test_url_bad_url(self):
self.expect_urlopen('http://astralandopal.com\\').AndRaise(httplib.InvalidURL(''))
self.mox.ReplayAll()
resp = app.application.get_response(
'/url?url=http://astralandopal.com\\&input=html')
self.assert_equals(400, resp.status_int)

def test_url_fetch_fails(self):
self.expect_urlopen('http://my/posts.html').AndRaise(socket.error(''))
self.mox.ReplayAll()
resp = app.application.get_response('/url?url=http://my/posts.html&input=html')
self.assert_equals(502, resp.status_int)

def test_hub(self):
self.expect_urlopen('http://my/posts.html', HTML % {
'body_class': '',
Expand Down

0 comments on commit 9235863

Please sign in to comment.