Comparing version 1.0.1 to 1.1.0
@@ -159,9 +159,46 @@ var foldHeaderLine = require('./foldHeaderLine'), | ||
Headers.prototype.remove = function (headerName, valueNumber) { | ||
var headerNameLowerCase = headerName.toLowerCase(), | ||
Headers.prototype.remove = function (headerNameOrObj, valueOrValuesOrValueNumber) { | ||
if (headerNameOrObj && typeof headerNameOrObj === 'object') { | ||
var numRemoved = 0; | ||
Object.keys(headerNameOrObj).forEach(function (headerName) { | ||
numRemoved += this.remove(headerName, headerNameOrObj[headerName]); | ||
}, this); | ||
return numRemoved; | ||
} | ||
var headerNameLowerCase = headerNameOrObj.toLowerCase(), | ||
values = this.valuesByName[headerNameLowerCase]; | ||
if (typeof valueNumber === 'undefined' || (values.length === 1 && valueNumber === 0)) { | ||
if (!values) { | ||
return 0; | ||
} else if (typeof valueOrValuesOrValueNumber === 'undefined') { | ||
delete this.valuesByName[headerNameLowerCase]; | ||
} else if (values) { | ||
values.splice(valueNumber, 1); | ||
return values.length; | ||
} else if (Array.isArray(valueOrValuesOrValueNumber)) { | ||
var numRemoved = 0; | ||
valueOrValuesOrValueNumber.forEach(function (value) { | ||
numRemoved += this.remove(headerNameLowerCase, value); | ||
}, this); | ||
return numRemoved; | ||
} else if (typeof valueOrValuesOrValueNumber === 'number') { | ||
if (values.length === 1 && valueOrValuesOrValueNumber === 0) { | ||
delete this.valuesByName[headerNameLowerCase]; | ||
return 1; | ||
} else if (valueOrValuesOrValueNumber < values.length) { | ||
values.splice(valueOrValuesOrValueNumber, 1); | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
} else { | ||
var value = String(valueOrValuesOrValueNumber), | ||
index = values.indexOf(value); | ||
if (index === -1) { | ||
return 0; | ||
} else { | ||
if (index === 0 && values.length === 1) { | ||
delete this.valuesByName[headerNameLowerCase]; | ||
} else { | ||
values.splice(index, 1); | ||
} | ||
return 1; | ||
} | ||
} | ||
@@ -168,0 +205,0 @@ }; |
@@ -109,3 +109,3 @@ /*global unescape*/ | ||
result += '\r\n'; | ||
if (this.body && typeof this.body === 'object' && isNonBufferNonRegExpObject(this.body) && /^application\/json\b/i.test(this.headers.get('Content-Type'))) { | ||
if (this.body && typeof this.body === 'object' && isNonBufferNonRegExpObject(this.body) && /^application\/json\b|\+json/i.test(this.headers.get('Content-Type'))) { | ||
result += JSON.stringify(this.body); | ||
@@ -112,0 +112,0 @@ } else { |
{ | ||
"name": "messy", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Object model for HTTP and RFC822 messages", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
/*global describe, it*/ | ||
var expect = require('unexpected'), | ||
var unexpected = require('unexpected'), | ||
Headers = require('../lib/Headers'); | ||
describe('Headers', function () { | ||
var expect = unexpected.clone().installPlugin(require('unexpected-messy')); | ||
it('should accept a string', function () { | ||
@@ -28,2 +30,68 @@ var headers = new Headers('Subject: hey, dude!'); | ||
}); | ||
describe('#remove', function () { | ||
it('should remove all header values for the given header when only passed one argument', function () { | ||
var headers = new Headers({foo: ['bla', 'bar'], quux: 'baz'}); | ||
headers.remove('foo'); | ||
expect(headers, 'to equal', new Headers({quux: 'baz'})); | ||
}); | ||
it('should remove a single header value', function () { | ||
var headers = new Headers({foo: 'bar', quux: 'baz'}); | ||
headers.remove('foo', 'bar'); | ||
expect(headers, 'to equal', new Headers({quux: 'baz'})); | ||
}); | ||
it('should remove one out of multiple values', function () { | ||
var headers = new Headers({foo: ['bar', 'bla'], quux: 'baz'}); | ||
headers.remove('foo', 'bar'); | ||
expect(headers, 'to equal', new Headers({foo: 'bla', quux: 'baz'})); | ||
}); | ||
it('should remove multiple values, leaving one', function () { | ||
var headers = new Headers({foo: ['bar', 'bla', 'hey'], quux: 'baz'}); | ||
headers.remove('foo', ['bar', 'hey']); | ||
expect(headers, 'to equal', new Headers({foo: 'bla', quux: 'baz'})); | ||
}); | ||
it('should remove multiple values, leaving none', function () { | ||
var headers = new Headers({foo: ['bla', 'hey'], quux: 'baz'}); | ||
headers.remove('foo', ['hey', 'bla']); | ||
expect(headers, 'to equal', new Headers({quux: 'baz'})); | ||
expect(headers.valuesByName.foo, 'to be undefined'); | ||
}); | ||
it('should remove all header values found in object', function () { | ||
var headers = new Headers({foo: ['bla', 'bar'], quux: 'baz'}); | ||
expect(headers.remove({foo: 'bar', quux: 'baz'}), 'to equal', 2); | ||
expect(headers, 'to equal', new Headers({foo: 'bla'})); | ||
}); | ||
it('should remove header value specified by number', function () { | ||
var headers = new Headers({foo: ['bla', 'bar'], quux: 'baz'}); | ||
expect(headers.remove('foo', 1), 'to equal', 1); | ||
expect(headers.remove('foo', 1), 'to equal', 0); | ||
expect(headers, 'to equal', new Headers({foo: 'bla', quux: 'baz'})); | ||
}); | ||
it('should return the number of removed values when removing all values of a header', function () { | ||
expect(new Headers({foo: ['bla', 'hey'], quux: 'baz'}).remove('foo'), 'to equal', 2); | ||
}); | ||
it('should return the number of removed values when removing one out of two', function () { | ||
expect(new Headers({foo: ['bla', 'hey'], quux: 'baz'}).remove('foo', 'hey'), 'to equal', 1); | ||
}); | ||
it('should return the number of removed values when removing two out of two', function () { | ||
expect(new Headers({foo: ['bla', 'hey'], quux: 'baz'}).remove('foo', ['bla', 'hey']), 'to equal', 2); | ||
}); | ||
it('should return 0 when the attempting to remove a single value that was not found', function () { | ||
expect(new Headers({foo: 'hey', quux: 'baz'}).remove('foo', 'dah'), 'to equal', 0); | ||
}); | ||
it('should return 0 when the attempting to remove multiple value that were not found', function () { | ||
expect(new Headers({foo: 'hey', quux: 'baz'}).remove('foo', ['dah', 'bla']), 'to equal', 0); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75161
1609