Permalink
Browse files

Merge pull request #7 from pandemicsyn/master

stats jsonp support
  • Loading branch information...
2 parents f951bc8 + cda00e9 commit fda207171fa4a614fc7444bd71dc13c2bda4f3f3 @gholt committed Aug 17, 2012
Showing with 62 additions and 5 deletions.
  1. +16 −4 brim/stats.py
  2. +46 −1 brim/test/unit/test_stats.py
View
@@ -15,18 +15,22 @@
"""
Reports the brimd server stats as a JSON reponse. The stats
contain basic things like the server start time and request counts.
+You may also add a jsonp or callback query variable for JSONP
+support.
See Stats.parse_conf for configuration options.
"""
from sys import maxint
+from brim.http import QueryParser
class Stats(object):
"""
A WSGI application that reports the brimd server stats as a
JSON reponse. The stats contain basic things like the server
- start time and request counts.
+ start time and request counts. You may also add a jsonp or
+ callback query variable for JSONP support.
:param name: The name of the app, indicates the app's section in
the overall configuration for the WSGI server.
@@ -84,9 +88,17 @@ def __call__(self, env, start_response):
stats.bucket_names[i],
{})[name] = stats.get(i, name)
body['start_time'] = server.start_time
- body = env['brim.json_dumps'](body) + '\n'
- start_response('200 OK', [('Content-Length', str(len(body))),
- ('Content-Type', 'application/json')])
+ qp = QueryParser(env['QUERY_STRING'])
+ callback = qp.get('jsonp', default=qp.get('callback', default=False))
+ if callback:
+ body = '%s(%s)' % (callback, env['brim.json_dumps'](body))
+ start_response('200 OK', [('Content-Length', str(len(body))),
+ ('Content-Type',
+ 'application/javascript')])
+ else:
+ body = env['brim.json_dumps'](body) + '\n'
+ start_response('200 OK', [('Content-Length', str(len(body))),
+ ('Content-Type', 'application/json')])
if env['REQUEST_METHOD'] == 'HEAD':
return []
return [body]
@@ -103,7 +103,8 @@ def _start_response(*args):
'brim': FakeServer().subservers[0],
'wsgi.input': StringIO('testbody'),
'brim.json_dumps': dumps,
- 'brim.json_loads': loads}
+ 'brim.json_loads': loads,
+ 'QUERY_STRING': ''}
self.parsed_conf = {'path': '/testpath'}
def test_init_attrs(self):
@@ -166,6 +167,50 @@ def test_call_stats_zeroed(self):
self.assertEquals(body['daemons'], a['daemons'])
self.assertEquals(body, a)
+ def test_jsonp(self):
+ self.env['QUERY_STRING'] = 'jsonp=jptest1'
+ body = ''.join(stats.Stats(
+ 'test', self.parsed_conf, self.next_app)(
+ self.env, self.start_response))
+ self.assertEquals(self.start_response_calls,
+ [('200 OK', [('Content-Length', '819'),
+ ('Content-Type',
+ 'application/javascript')])])
+ self.assertEquals(True, body.startswith('jptest1('))
+
+ def test_jsonp_callback(self):
+ self.env['QUERY_STRING'] = 'callback=jptest2'
+ body = ''.join(stats.Stats(
+ 'test', self.parsed_conf, self.next_app)(
+ self.env, self.start_response))
+ self.assertEquals(self.start_response_calls,
+ [('200 OK', [('Content-Length', '819'),
+ ('Content-Type',
+ 'application/javascript')])])
+ self.assertEquals(True, body.startswith('jptest2('))
+
+ def test_jsonp_over_callback1(self):
+ self.env['QUERY_STRING'] = 'jsonp=jptest3&callback=jptest4'
+ body = ''.join(stats.Stats(
+ 'test', self.parsed_conf, self.next_app)(
+ self.env, self.start_response))
+ self.assertEquals(self.start_response_calls,
+ [('200 OK', [('Content-Length', '819'),
+ ('Content-Type',
+ 'application/javascript')])])
+ self.assertEquals(True, body.startswith('jptest3('))
+
+ def test_jsonp_over_callback2(self):
+ self.env['QUERY_STRING'] = 'callback=jptest3&jsonp=jptest4'
+ body = ''.join(stats.Stats(
+ 'test', self.parsed_conf, self.next_app)(
+ self.env, self.start_response))
+ self.assertEquals(self.start_response_calls,
+ [('200 OK', [('Content-Length', '819'),
+ ('Content-Type',
+ 'application/javascript')])])
+ self.assertEquals(True, body.startswith('jptest4('))
+
def test_call_stats_zeroed_head(self):
self.env['REQUEST_METHOD'] = 'HEAD'
body = ''.join(stats.Stats(

0 comments on commit fda2071

Please sign in to comment.