Permalink
Browse files

Fixes #59 - duplicate headers because of Corset-Casing

  • Loading branch information...
1 parent 6cdce25 commit 6ce425809ea45518f40ff8c03baa5d87711534d9 @automatthew automatthew committed Nov 12, 2013
Showing with 37 additions and 28 deletions.
  1. +33 −25 lib/shred/mixins/headers.js
  2. +1 −1 lib/shred/response.js
  3. +3 −2 test/shred_test.coffee
@@ -26,46 +26,54 @@ var corsetCase = function(string) {
// We suspect that `initializeHeaders` was once more complicated ...
var initializeHeaders = function(object) {
- return {};
+ return {input: {}, normalized: {}};
};
// Access the `_headers` property using lazy initialization. **Warning:** If you
// mix this into an object that is using the `_headers` property already, you're
// going to have trouble.
var $H = function(object) {
- return object._headers||(object._headers=initializeHeaders(object));
+ if (typeof(object._headers) === "undefined") {
+ object._headers = initializeHeaders(object);
+ return object._headers;
+ } else {
+ return object._headers;
+ }
};
// Hide the implementations as private functions, separate from how we expose them.
// The "real" `getHeader` function: get the header after normalizing the name.
-var getHeader = function(object,name) {
- return $H(object)[corsetCase(name)];
+var getHeader = function(object, name) {
+ var headers = $H(object);
+ return headers.normalized[corsetCase(name)] || headers.input[name]
};
// The "real" `getHeader` function: get one or more headers, or all of them
// if you don't ask for any specifics.
-var getHeaders = function(object,names) {
- var keys = (names && names.length>0) ? names : Object.keys($H(object));
- var hash = {};
+var getHeaders = function(object, names) {
+ var headers = $H(object);
+ var keys = (names && names.length>0) ? names : Object.keys(headers.input);
+ var output = {};
for (var i=0,l=keys.length;i<l;i++) {
var key = keys[i];
- hash[key] = getHeader(object, key);
+ output[key] = getHeader(object, key);
}
- Object.freeze(hash);
- return hash;
+ Object.freeze(output);
+ return output;
};
// The "real" `setHeader` function: set a header, after normalizing the name.
-var setHeader = function(object,name,value) {
- $H(object)[name] = value;
- $H(object)[corsetCase(name)] = value;
+var setHeader = function(object, name, value) {
+ var headers = $H(object);
+ headers.input[name] = value;
+ headers.normalized[corsetCase(name)] = value;
return object;
};
// The "real" `setHeaders` function: set multiple headers based on a hash.
-var setHeaders = function(object,hash) {
- for( var key in hash ) { setHeader(object,key,hash[key]); };
+var setHeaders = function(object, hash) {
+ for( var key in hash ) { setHeader(object, key, hash[key]); };
return this;
};
@@ -75,24 +83,24 @@ module.exports = {
// Add getters.
getters: function(constructor) {
- constructor.prototype.getHeader = function(name) { return getHeader(this,name); };
- constructor.prototype.getHeaders = function() { return getHeaders(this,arguments); };
+ constructor.prototype.getHeader = function(name) { return getHeader(this, name); };
+ constructor.prototype.getHeaders = function() { return getHeaders(this, arguments); };
},
// Add setters but as "private" methods.
privateSetters: function(constructor) {
- constructor.prototype._setHeader = function(key,value) { return setHeader(this,key,value); };
- constructor.prototype._setHeaders = function(hash) { return setHeaders(this,hash); };
+ constructor.prototype._setHeader = function(key, value) { return setHeader(this, key, value); };
+ constructor.prototype._setHeaders = function(hash) { return setHeaders(this, hash); };
},
// Add setters.
setters: function(constructor) {
- constructor.prototype.setHeader = function(key,value) { return setHeader(this,key,value); };
- constructor.prototype.setHeaders = function(hash) { return setHeaders(this,hash); };
+ constructor.prototype.setHeader = function(key, value) { return setHeader(this, key, value); };
+ constructor.prototype.setHeaders = function(hash) { return setHeaders(this, hash); };
},
// Add both getters and setters.
gettersAndSetters: function(constructor) {
- constructor.prototype.getHeader = function(name) { return getHeader(this,name); };
- constructor.prototype.getHeaders = function() { return getHeaders(this,arguments); };
- constructor.prototype.setHeader = function(key,value) { return setHeader(this,key,value); };
- constructor.prototype.setHeaders = function(hash) { return setHeaders(this,hash); };
+ constructor.prototype.getHeader = function(name) { return getHeader(this, name); };
+ constructor.prototype.getHeaders = function() { return getHeaders(this, arguments); };
+ constructor.prototype.setHeader = function(key, value) { return setHeader(this, key, value); };
+ constructor.prototype.setHeaders = function(hash) { return setHeaders(this, hash); };
},
};
View
@@ -171,7 +171,7 @@ Object.defineProperties(Response.prototype, {
headers: {
get: function() {
- return this._headers;
+ return this.getHeaders();
},
enumerable: true
},
View
@@ -2,6 +2,7 @@ Shred = require "../lib/shred"
Testify = require "testify"
assert = require "assert"
+#shred = new Shred(logCurl: true)
shred = new Shred()
request_error_handler = (error) ->
@@ -32,10 +33,10 @@ Testify.test "Shred", (context) ->
assert.equal(response.content.body.constructor, String)
context.test "response.content.body.length is correct", ->
- assert.equal response.headers["Content-Length"], response.content.body.length
+ assert.equal response.getHeader("Content-Length"), response.content.body.length
context.test "response.content.length is correct", ->
- assert.equal response.headers["Content-Length"], response.content.length
+ assert.equal response.getHeader("Content-Length"), response.content.length
context.test "response.content._body is a Buffer", ->
assert.ok(Buffer.isBuffer(response.content._body))

0 comments on commit 6ce4258

Please sign in to comment.