Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix to jsonp handler

  • Loading branch information...
commit a99d391dacb294e15925438c286a42771c286d36 1 parent 9997ee3
@peterbe authored
Showing with 25 additions and 11 deletions.
  1. +15 −11 handlers.py
  2. +10 −0 tests/test_handlers.py
View
26 handlers.py
@@ -77,18 +77,20 @@ def get(self):
# make sure it's a unique list
usernames = set(usernames)
- if not usernames:
- self.write_json({
- 'ERROR': 'No usernames asked for'
- })
- self.finish()
- return
-
if jsonp:
self.jsonp = self.get_argument('callback', 'callback')
else:
self.jsonp = False
+ if not usernames:
+ msg = {'ERROR': 'No usernames asked for'}
+ if jsonp:
+ self.write_jsonp(self.jsonp, msg)
+ else:
+ self.write_json(msg)
+ self.finish()
+ return
+
#print "USERNAMES"
#print usernames
@@ -112,10 +114,12 @@ def get(self):
if access_token:
access_token = json_decode(access_token)
if not access_token:
- self.write_json({
- 'ERROR': ('Not authorized with Twitter for %s' %
- self.request.host)
- })
+ msg = {'ERROR': ('Not authorized. Go to http://%s and sign in' %
+ self.request.host)}
+ if self.jsonp:
+ self.write_jsonp(self.jsonp, msg)
+ else:
+ self.write_json(msg)
self.finish()
return
#print "USERNAMES"
View
10 tests/test_handlers.py
@@ -127,6 +127,16 @@ def test_jsonp(self):
self.assertEqual(response.code, 200)
self.assertEqual(response.body, 'FOO({"obama": false})')
+ def test_jsonp(self):
+ url = self.reverse_url('jsonp')
+ response = self.client.get(url, {'username': 'obama'})
+ self.assertEqual(response.code, 200)
+ self.assertTrue(response.body.startswith('callback({"ERROR":'))
+ response = self.client.get(url, {'username': 'obama', 'callback': 'FOO'})
+ self.assertEqual(response.code, 200)
+ self.assertTrue(response.body.startswith('FOO({"ERROR":'))
+
+
def test_following_none_cached(self):
FollowsHandler.twitter_request = \
make_mock_twitter_request([
Please sign in to comment.
Something went wrong with that request. Please try again.