Commits

Miki Tebeka committed 4d2d78f

json tool to work with cached (fixing #1237)

Comments (0)

Files changed (2)

cherrypy/lib/jsontools.py

         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):

cherrypy/test/test_json.py

             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)
         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"')
+
+
+