Shiran Pasternak avatar Shiran Pasternak committed 020d5b6

Not caching error responses.

Comments (0)

Files changed (4)

 * No limit on size or count of cached items.
 * Not using Cache-Control headers at the moment.
 
+##Changelog
+####v0.0.3
+* Only caches responses with non-error statuses (200).
+
 ##License
     Copyright (c) 2013 Shiran Pasternak <shiranpasternak@gmail.com>
 
         if (contentType) {
             ctKey = contentType.match(CONTENT_TYPE_REGEX)[1];
         }
-        // Will not cache empty values
-        if (body) {
+        // Will only cache valid, non-empty responses
+        if (body && res.statusCode == 200) {
             cache.lruSet(key, ctKey, body, function (err, reply) {});
         }
     }
             "name": "Shiran Pasternak",
            "email": "shiranpasternak@gmail.com"
           },
-         "version": "0.0.2",
+         "version": "0.0.3",
         "keywords": [ "server", "cache", "redis" ],
     "dependencies": {
            "redis": ">=0.8.2"
         }
         async.series(reqs, function () { done() });
     });
+    it("should not cache error responses", function (done) {
+        var errRequestCounter = 1;
+        app.get('/err500', function (req, res) {
+            res.send(500, {
+                error: "Error" + errRequestCounter 
+            });
+            errRequestCounter++;
+        })
+        async.series([
+            function (callback) {
+                // First request
+                request(app).get('/err500')
+                    .expect(500)
+                    .expect({ error: "Error1" }, function (err) {
+                        callback(err);
+                    }
+                );
+            },
+            function (callback) {  
+                // Second request
+                request(app).get('/err500')
+                    .expect(500).expect({ error: "Error2" }, function (err) {
+                        callback(err);
+                    }
+                )
+            }
+        ], function (err, results) {
+            if (err) {
+                throw err;
+            }
+            done();
+        });
+    })
 });
 
 describe("HTML content", function () {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.