cached-request
Advanced tools
Comparing version 0.1.4 to 1.0.0
@@ -34,2 +34,3 @@ /* | ||
this.cacheDirectory = "/tmp/"; | ||
this.ttl = 0; | ||
@@ -40,2 +41,8 @@ function _request () { | ||
_request.get = function () { | ||
arguments[0].method = 'GET'; | ||
return self.cachedRequest.apply(self, arguments); | ||
} | ||
_request.setCacheDirectory = function (cacheDirectory) { | ||
@@ -45,7 +52,7 @@ self.setCacheDirectory(cacheDirectory); | ||
_request.set = function (key, value) { | ||
_request.setValue = function (key, value) { | ||
self[key] = value; | ||
} | ||
_request.get = function (key) { | ||
_request.getValue = function (key) { | ||
return self[key]; | ||
@@ -109,3 +116,3 @@ } | ||
, options = args[0] | ||
, ttl = options.ttl || 0 | ||
, ttl = options.ttl || this.ttl | ||
, mustParseJSON = false | ||
@@ -112,0 +119,0 @@ , callback |
{ | ||
"name": "cached-request", | ||
"version": "0.1.4", | ||
"version": "1.0.0", | ||
"description": "Node.js module to perform HTTP requests with caching support", | ||
@@ -5,0 +5,0 @@ "author": "Daniel López <danypype@gmail.com>", |
@@ -29,3 +29,3 @@ #cached-request | ||
cacheRequest.setCacheDirectory(cacheDirectory); | ||
cachedRequest.setCacheDirectory(cacheDirectory); | ||
``` | ||
@@ -64,2 +64,9 @@ _Note_: You have to ensure the user that launches the process has read+write permissions over `cacheDirectory`, otherwise the program will fail. | ||
You can also set a global ttl option for all requests: | ||
```javascript | ||
cachedRequest.set('ttl', 1000); | ||
cachedRequest({url: 'https://www.google.com'}, callback); // should benefit from the cache if previously cached | ||
``` | ||
##Can I use everything that comes with **request**? | ||
@@ -66,0 +73,0 @@ No, there's some things you can't use. For example, the shortcut functions `.get`, `.post`, `.put`, etc. are not available in **cached-request**. If you'd like to have them, this is a great opportunity to contribute! |
@@ -58,2 +58,15 @@ var CachedRequest = require("../") | ||
it("makes the request when the response isn't cached using the get extension method", function (done) { | ||
mock("GET", 1, function () { | ||
return new MockedResponseStream({}, "pong"); | ||
}); | ||
this.cachedRequest.get({uri: "http://ping.com/", ttl: 0, method: 'GET'}, function (error, response, body) { | ||
if (error) return done(error); | ||
expect(response.statusCode).to.equal(200); | ||
expect(response.headers["x-from-cache"]).to.not.exist; | ||
expect(body).to.equal("pong"); | ||
done(); | ||
}); | ||
}); | ||
it("responds from the cache", function (done) { | ||
@@ -91,2 +104,34 @@ var self = this; | ||
it("responds from the cache using get extension method", function (done) { | ||
var self = this; | ||
var responseBody = {"a": 1, "b": {"c": 2}}; | ||
var options = { | ||
uri: "http://ping.com/", | ||
method: "POST", | ||
json: { | ||
a: 1 | ||
}, | ||
ttl: 5000 | ||
}; | ||
mock(options.method, 1, function () { | ||
return new MockedResponseStream({}, JSON.stringify(responseBody)); | ||
}); | ||
this.cachedRequest(options, function (error, response, body) { | ||
if (error) return done(error); | ||
expect(response.statusCode).to.equal(200); | ||
expect(response.headers["x-from-cache"]).to.not.exist; | ||
expect(body).to.deep.equal(responseBody); | ||
self.cachedRequest(options, function (error, response, body) { | ||
if (error) return done(error); | ||
expect(response.statusCode).to.equal(200); | ||
expect(response.headers["x-from-cache"]).to.equal(1); | ||
expect(body).to.deep.equal(responseBody); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it("responds the same from the cache if gzipped", function (done) { | ||
@@ -173,2 +218,41 @@ var self = this; | ||
it("allows to use request with get extension method as a stream", function (done) { | ||
var self = this; | ||
var responseBody = ""; | ||
for (var i = 0; i < 1000; i++) { | ||
responseBody += "this is a long response body"; | ||
}; | ||
mock("GET", 1, function () { | ||
return new MockedResponseStream({}, responseBody); | ||
}); | ||
var options = {url: "http://ping.com/", ttl: 5000}; | ||
var body = ""; | ||
//Make fresh request | ||
this.cachedRequest.get(options) | ||
.on("data", function (data) { | ||
body += data; | ||
}) | ||
.on("end", function () { | ||
expect(body).to.equal(responseBody); | ||
body = ""; | ||
//Make cached request | ||
self.cachedRequest(options) | ||
.on("response", function (response) { | ||
expect(response.statusCode).to.equal(200); | ||
expect(response.headers["x-from-cache"]).to.equal(1); | ||
response.on("data", function (data) { | ||
body += data; | ||
}) | ||
.on("end", function () { | ||
expect(body).to.equal(responseBody); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
it("handles gzip response", function (done) { | ||
@@ -175,0 +259,0 @@ var self = this; |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
25903
615
0
80