added suggestCompression config
This commit is contained in:
@ -34,6 +34,7 @@ function Host(config) {
|
||||
this.auth = null;
|
||||
this.query = null;
|
||||
this.headers = null;
|
||||
this.suggestCompression = false;
|
||||
|
||||
if (typeof config === 'string') {
|
||||
if (!startsWithProtocolRE.test(config)) {
|
||||
@ -137,7 +138,7 @@ Host.prototype.makeUrl = function (params) {
|
||||
function objectPropertyGetter(prop, preOverride) {
|
||||
return function (overrides) {
|
||||
if (preOverride) {
|
||||
overrides = preOverride(overrides);
|
||||
overrides = preOverride.call(this, overrides);
|
||||
}
|
||||
|
||||
var obj = this[prop];
|
||||
@ -153,7 +154,16 @@ function objectPropertyGetter(prop, preOverride) {
|
||||
};
|
||||
}
|
||||
|
||||
Host.prototype.getHeaders = objectPropertyGetter('headers');
|
||||
Host.prototype.getHeaders = objectPropertyGetter('headers', function (overrides) {
|
||||
if (!this.suggestCompression) {
|
||||
return overrides;
|
||||
}
|
||||
|
||||
return _.defaults(overrides || {}, {
|
||||
'Accept-Encoding': 'gzip,deflate'
|
||||
});
|
||||
});
|
||||
|
||||
Host.prototype.getQuery = objectPropertyGetter('query', function (query) {
|
||||
return typeof query === 'string' ? qs.parse(query) : query;
|
||||
});
|
||||
|
||||
@ -11,7 +11,8 @@ var hostDefaults = {
|
||||
path: '',
|
||||
auth: null,
|
||||
query: {},
|
||||
headers: null
|
||||
headers: null,
|
||||
suggestCompression: false
|
||||
};
|
||||
|
||||
describe('Host class', function () {
|
||||
@ -169,4 +170,36 @@ describe('Host class', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getHeaders', function () {
|
||||
it('merges the passed in headers with the default headers', function () {
|
||||
var host = new Host({ headers: { 'Joe-Smith': 'present' } });
|
||||
|
||||
expect(host.getHeaders({
|
||||
'John-Smith': 'present'
|
||||
})).to.eql({
|
||||
'John-Smith': 'present',
|
||||
'Joe-Smith': 'present'
|
||||
});
|
||||
});
|
||||
|
||||
it('overrides the default headers with the passed in headers', function () {
|
||||
var host = new Host({ headers: { 'Joe-Smith': 'present' } });
|
||||
|
||||
expect(host.getHeaders({
|
||||
'John-Smith': 'present',
|
||||
'Joe-Smith': 'absent'
|
||||
})).to.eql({
|
||||
'John-Smith': 'present',
|
||||
'Joe-Smith': 'absent'
|
||||
});
|
||||
});
|
||||
|
||||
it('adds Accept-Encoding header when the suggestCompression setting is true', function () {
|
||||
var host = new Host({ suggestCompression: true });
|
||||
expect(host.getHeaders()).to.eql({
|
||||
'Accept-Encoding': 'gzip,deflate'
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user