Skip to content

Commit

Permalink
Improved User-Agent handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shogun committed Mar 7, 2016
1 parent 1e9fc07 commit b0f28db
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 81 deletions.
35 changes: 17 additions & 18 deletions lib/core.js
Expand Up @@ -17,6 +17,7 @@ if(typeof Promise === "undefined")
var errors = require("./errors");
var parseUrl = require("url").parse;

var userAgent = null;
var originalThreshold = 4096;
var threshold = originalThreshold;

Expand Down Expand Up @@ -60,17 +61,17 @@ if(typeof Promise === "undefined")
};

/**
Gets or sets the user agent string to send when connecting to a host.
* Gets or sets the user agent string to send when connecting to a host.
*
* @alias module:fastimage.userAget
* @param {string} [value] - The new value to set. Passing `null` will restore the default value (Fast Image - Node Image Lookup).
* @alias module:fastimage.userAgent
* @param {string} [value] - The new value to set. Passing `null` will delete the current value and not send any User-Agent.
* @returns {number} The current value
*/
var manageUserAgent = function(value){
if(typeof value === "string")
userAgent = value;
else if(value === null)
userAgent = originalUserAgent;
userAgent = null;

return userAgent;
};
Expand Down Expand Up @@ -171,17 +172,15 @@ if(typeof Promise === "undefined")
var unsupportedError = new errors.FastImageError("Unsupported image file.", {url: url, code: "UNSUPPORTED_TYPE"});

// When a response is received, control the HTTP code. If is not in the 2xx range, return a error.
request.get({
url: url,
encoding: null,
timeout: timeout,
headers: {
"user-agent": userAgent
}
})
.on("response", function(response){
realUrl = response.request.uri.href;
size = parseFloat(response.headers["content-length"]);
var params = {url: url, encoding: null, timeout: timeout};

if(userAgent)
params.headers = {"User-Agent": userAgent};

request.get(params)
.on("response", function(response){
realUrl = response.request.uri.href;
size = parseFloat(response.headers["content-length"]);

if(response.statusCode / HTTP_RESPONSE_CLASS_FACTOR !== 2){
this.abort();
Expand All @@ -194,9 +193,9 @@ if(typeof Promise === "undefined")
buffer = Buffer.concat([buffer, chunk]);
info = identify(buffer, url, realUrl, size, time);

// Informations found or threshold reached. End the transfer and call the callback.
if(info || buffer.length >= threshold){
this.abort();
// Informations found or threshold reached. End the transfer and call the callback.
if(info || buffer.length >= threshold){
this.abort();

if(info)
return callback(null, info);
Expand Down
99 changes: 93 additions & 6 deletions test/general.js
Expand Up @@ -12,7 +12,8 @@
var path = require("path");
var net = require("net");
var crypto = require("crypto");

var sinon = require("sinon");
var request = require("request");
var fastimage = require("../main");

sepia.fixtureDir(path.join(__dirname, "cassettes"));
Expand Down Expand Up @@ -53,12 +54,11 @@

describe("user agent", function(){
it("should set and get the value", function(){
var defaultValue = "Fast Image - Node Image Lookup - https://www.npmjs.com/package/fastimage";
expect(fastimage.userAgent()).to.equal(defaultValue);
expect(fastimage.userAgent()).to.equal(null);
expect(fastimage.userAgent("new value")).to.equal("new value");
expect(fastimage.userAgent()).to.equal("new value");
expect(fastimage.userAgent(null)).to.equal(defaultValue);
expect(fastimage.userAgent()).to.equal(defaultValue);
expect(fastimage.userAgent(null)).to.equal(null);
expect(fastimage.userAgent()).to.equal(null);
});
});

Expand All @@ -78,7 +78,6 @@
});
});


describe("URL", function(){
it("should return the information of a image", function(done){
fastimage.info("http://placehold.it/200x100.png", function(error, info){
Expand Down Expand Up @@ -291,6 +290,37 @@
});
});
});

describe("it should handle the User-Agent", function(){
var requestParams = {};

beforeEach(function(){
sinon.stub(request, "get", function(params){
requestParams = params;

return {
on: function (){}
};
});
});

afterEach(function(){
request.get.restore();
});

it("not sending any header if no agent has been set", function(done){
fastimage.info("http://placehold.it/100x100.png");
expect(requestParams.headers).not.to.be.ok();
done(null);
});

it("sending the header if the agent has been set", function(done){
fastimage.userAgent("AGENT");
fastimage.info("http://placehold.it/100x100.png");
expect(requestParams.headers["User-Agent"]).to.equal("AGENT");
done(null);
});
});
});

describe("filteredInfo", function(){
Expand Down Expand Up @@ -385,5 +415,62 @@
});
});
});
/*
describe("outgoing request", function(){
var waitForTestToCallRequest, defaultUserAgent;
beforeEach(function(){
defaultUserAgent = "Fast Image - Node Image Lookup - https://www.npmjs.com/package/fastimage";
waitForTestToCallRequest = new Promise(function (res) {
sinon.stub(request, "get", function (outgoingRequest) {
res(outgoingRequest);
var ret = {
on: function () {
return ret;
}
};
return ret;
});
});
});
afterEach(function(){
request.get.restore();
});
it("should contain a default user agent string", function (done) {
waitForTestToCallRequest.then(function (outgoingRequest) {
expect(outgoingRequest.headers["user-agent"]).to.be(defaultUserAgent);
done();
}).catch(done);
fastimage.info("http://example.com/");
});
it("should contain a default user agent string", function (done) {
var newUserAgentString = "Hello World!";
waitForTestToCallRequest.then(function (outgoingRequest) {
expect(outgoingRequest.headers["user-agent"]).to.be(newUserAgentString);
done();
}).catch(done);
fastimage.userAgent(newUserAgentString);
fastimage.info("http://example.com/");
});
it("should have a standard setter/getter for user agent string", function (done) {
waitForTestToCallRequest.then(function (outgoingRequest) {
expect(outgoingRequest.headers["user-agent"]).to.be(defaultUserAgent);
done();
}).catch(done);
fastimage.userAgent("this will never actually be used");
expect(fastimage.userAgent()).to.eql("this will never actually be used");
fastimage.userAgent(null);
expect(fastimage.userAgent()).to.eql(defaultUserAgent);
fastimage.info("http://example.com/");
});
});
*/
});
})();
57 changes: 0 additions & 57 deletions test/promises.js
Expand Up @@ -8,7 +8,6 @@

var expect = require("expect.js");
var path = require("path");
var sinon = require("sinon");
var request = require("request");

var fastimage = require("../main");
Expand Down Expand Up @@ -122,61 +121,5 @@
});
});
});

describe("outgoing request", function(){
var waitForTestToCallRequest, defaultUserAgent;

beforeEach(function(){
defaultUserAgent = "Fast Image - Node Image Lookup - https://www.npmjs.com/package/fastimage";
waitForTestToCallRequest = new Promise(function (res) {
sinon.stub(request, "get", function (outgoingRequest) {
res(outgoingRequest);
var ret = {
on: function () {
return ret;
}
};
return ret;
});
});
});
afterEach(function(){
request.get.restore();
});

it("should contain a default user agent string", function (done) {
waitForTestToCallRequest.then(function (outgoingRequest) {
expect(outgoingRequest.headers["user-agent"]).to.be(defaultUserAgent);
done();
}).catch(done);

fastimage.info("http://example.com/");
});

it("should contain a default user agent string", function (done) {
var newUserAgentString = "Hello World!";

waitForTestToCallRequest.then(function (outgoingRequest) {
expect(outgoingRequest.headers["user-agent"]).to.be(newUserAgentString);
done();
}).catch(done);

fastimage.userAgent(newUserAgentString);
fastimage.info("http://example.com/");
});

it("should have a standard setter/getter for user agent string", function (done) {
waitForTestToCallRequest.then(function (outgoingRequest) {
expect(outgoingRequest.headers["user-agent"]).to.be(defaultUserAgent);
done();
}).catch(done);

fastimage.userAgent("this will never actually be used");
expect(fastimage.userAgent()).to.eql("this will never actually be used");
fastimage.userAgent(null);
expect(fastimage.userAgent()).to.eql(defaultUserAgent);
fastimage.info("http://example.com/");
});
});
});
})();

0 comments on commit b0f28db

Please sign in to comment.