diff options
author | Miki Tebeka <miki.tebeka@gmail.com> | 2013-04-02 10:31:58 -0700 |
---|---|---|
committer | Miki Tebeka <miki.tebeka@gmail.com> | 2013-04-02 10:31:58 -0700 |
commit | d8b245fdd0a5119755f4fb811b025d800d927863 (patch) | |
tree | 84ebee59a462730c79afc96a301e374e27529212 | |
parent | 5229f2fe9d096e3af9e354976e7e9422d392bf06 (diff) | |
download | cherrypy-d8b245fdd0a5119755f4fb811b025d800d927863.tar.gz |
json tool to work with cached (fixing #1237)
-rw-r--r-- | cherrypy/lib/jsontools.py | 6 | ||||
-rw-r--r-- | cherrypy/test/test_json.py | 21 |
2 files changed, 26 insertions, 1 deletions
diff --git a/cherrypy/lib/jsontools.py b/cherrypy/lib/jsontools.py index 776bddf6..354c903d 100644 --- a/cherrypy/lib/jsontools.py +++ b/cherrypy/lib/jsontools.py @@ -58,7 +58,11 @@ def json_in(content_type=[ntou('application/json'), ntou('text/javascript')], request.body.processors[ct] = processor def json_handler(*args, **kwargs): - value = cherrypy.serving.request._json_inner_handler(*args, **kwargs) + request = cherrypy.serving.request + if getattr(request, 'cached', False): + return cherrypy.serving.response.body + + value = request._json_inner_handler(*args, **kwargs) return json_encode(value) def json_out(content_type='application/json', debug=False, handler=json_handler): diff --git a/cherrypy/test/test_json.py b/cherrypy/test/test_json.py index c7e7f17a..3f30fa23 100644 --- a/cherrypy/test/test_json.py +++ b/cherrypy/test/test_json.py @@ -33,6 +33,14 @@ class JsonTest(helper.CPWebCase): json_post.exposed = True json_post._cp_config = {'tools.json_in.on': True} + def json_cached(self): + return 'hello there' + json_cached.exposed = True + json_cached._cp_config = { + 'tools.json_out.on': True, + 'tools.caching.on': True, + } + root = Root() cherrypy.tree.mount(root) setup_server = staticmethod(setup_server) @@ -77,3 +85,16 @@ class JsonTest(helper.CPWebCase): self.getPage("/json_post", method="POST", headers=headers, body=body) self.assertStatus(400, 'Invalid JSON document') + def test_cached(self): + if json is None: + self.skip("json not found ") + return + + self.getPage("/json_cached") + self.assertStatus(200, '"hello"') + + self.getPage("/json_cached") # 2'nd time to hit cache + self.assertStatus(200, '"hello"') + + + |