Skip to content

Commit

Permalink
fix incorrect content-length of data set to a Buffer object
Browse files Browse the repository at this point in the history
Update basic.js

removed a it.only call from tests

add missing dependency

fix first test to send an actual string
  • Loading branch information
psirenny committed Apr 22, 2015
1 parent f7bb769 commit 7e771f5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/node/index.js
Expand Up @@ -854,7 +854,7 @@ Request.prototype.end = function(fn){

// content-length
if (data && !req.getHeader('Content-Length')) {
this.set('Content-Length', Buffer.byteLength(data));
this.set('Content-Length', Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data));
}
}

Expand Down
36 changes: 36 additions & 0 deletions test/node/basic.js
@@ -1,5 +1,7 @@
var EventEmitter = require('events').EventEmitter;
var assert = require('better-assert');
var fs = require('fs');
var StringDecoder = require('string_decoder').StringDecoder;
var url = require('url');

var request = require('../../');
Expand Down Expand Up @@ -241,4 +243,38 @@ describe('[node] request', function(){
});
})
})

describe('content-length', function() {
it('should be set to the byte length of a non-buffer object', function (done) {
var decoder = new StringDecoder('utf8');
var img = fs.readFileSync(__dirname + '/fixtures/test.png');
img = decoder.write(img);
request
.post('http://localhost:5000/echo')
.type('application/x-image')
.send(img)
.buffer(false)
.end(function(err, res){
assert(null == err);
assert(!res.buffered);
assert(res.header['content-length'] == Buffer.byteLength(img));
done();
});
})

it('should be set to the length of a buffer object', function(done){
var img = fs.readFileSync(__dirname + '/fixtures/test.png');
request
.post('http://localhost:5000/echo')
.type('application/x-image')
.send(img)
.buffer(true)
.end(function(err, res){
assert(null == err);
assert(res.buffered);
assert(res.header['content-length'] == img.length);
done();
});
})
})
});

0 comments on commit 7e771f5

Please sign in to comment.