-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented support for chunked transfer-encoding
- Loading branch information
1 parent
0170178
commit 25f6da9
Showing
6 changed files
with
263 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
var common = require('../common'); | ||
var assert = common.assert; | ||
var mime = require('mime-types'); | ||
var request = require('request'); | ||
var FormData = require(common.dir.lib + '/form_data'); | ||
|
||
var remoteFile = 'http://localhost:' + common.staticPort + '/unicycle.jpg'; | ||
|
||
// wrap non simple values into function | ||
// just to deal with ReadStream "autostart" | ||
var FIELDS = { | ||
'remote_file': { | ||
type: mime.lookup(common.dir.fixture + '/unicycle.jpg'), | ||
value: function() { | ||
return request(remoteFile) | ||
.on('response', function(response) { | ||
// Remove content-length header from response, force it to be chunked | ||
delete response.headers['content-length']; | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
// count total | ||
var fieldsPassed = Object.keys(FIELDS).length; | ||
|
||
// prepare form-receiving http server | ||
var server = common.testFields(FIELDS, function(fields){ | ||
fieldsPassed = fields; | ||
}); | ||
|
||
server.listen(common.port, function() { | ||
|
||
var form = new FormData(); | ||
|
||
common.actions.populateFields(form, FIELDS); | ||
|
||
// custom params object passed to submit | ||
form.submit({ | ||
port: common.port, | ||
path: '/' | ||
}, function(err, res) { | ||
|
||
if (err) { | ||
throw err; | ||
} | ||
|
||
// Only now we know that it's a chunked stream, as form.submit() called getLength() | ||
|
||
assert.strictEqual(form.getHeaders()['transfer-encoding'], 'chunked'); | ||
assert.strictEqual(form.isChunked(), true); | ||
|
||
// Verify status code | ||
assert.strictEqual(res.statusCode, 200); | ||
|
||
// Try to get length again - that should take an already cached value and cover the _calculatedLength code branch | ||
form.getLength(function (ex, length) { | ||
assert.ok(isNaN(length)); | ||
}); | ||
|
||
res.resume(); | ||
server.close(); | ||
}); | ||
|
||
}); | ||
|
||
process.on('exit', function() { | ||
assert.strictEqual(fieldsPassed, 0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
var common = require('../common'); | ||
var assert = common.assert; | ||
var mime = require('mime-types'); | ||
var fs = require('fs'); | ||
var Stream = require('stream'); | ||
var FormData = require(common.dir.lib + '/form_data'); | ||
|
||
// wrap non simple values into function | ||
// just to deal with ReadStream "autostart" | ||
var FIELDS = { | ||
'my_field': { | ||
value: 'my_value' | ||
}, | ||
'my_empty_field': { | ||
value: '' // Will cover the (!value) code branch | ||
}, | ||
'my_buffer': { | ||
type: FormData.DEFAULT_CONTENT_TYPE, | ||
value: common.defaultTypeValue | ||
}, | ||
'my_file': { | ||
type: mime.lookup(common.dir.fixture + '/unicycle.jpg'), | ||
value: function() { return fs.createReadStream(common.dir.fixture + '/unicycle.jpg').pipe(new Stream.PassThrough()); }, | ||
options: { | ||
filename: 'unicycle.jpg', | ||
} | ||
} | ||
}; | ||
|
||
var TEST_FIELDS = { | ||
'my_field': { | ||
value: 'my_value' | ||
}, | ||
'my_empty_field': { | ||
value: '' | ||
}, | ||
'my_buffer': { | ||
type: FormData.DEFAULT_CONTENT_TYPE, | ||
value: common.defaultTypeValue() | ||
}, | ||
'my_file': { | ||
type: mime.lookup(common.dir.fixture + '/unicycle.jpg'), | ||
value: fs.createReadStream(common.dir.fixture + '/unicycle.jpg') | ||
} | ||
}; | ||
|
||
// count total | ||
var fieldsPassed = Object.keys(FIELDS).length; | ||
|
||
// prepare form-receiving http server | ||
var server = common.testFields(TEST_FIELDS, function(fields){ | ||
fieldsPassed = fields; | ||
}); | ||
|
||
server.listen(common.port, function() { | ||
|
||
var form = new FormData(); | ||
|
||
common.actions.populateFields(form, FIELDS); | ||
|
||
assert.strictEqual(form.getHeaders()['transfer-encoding'], 'chunked'); | ||
assert.strictEqual(form.isChunked(), true); | ||
|
||
// custom params object passed to submit | ||
form.submit({ | ||
port: common.port, | ||
path: '/' | ||
}, function(err, res) { | ||
|
||
if (err) { | ||
throw err; | ||
} | ||
|
||
assert.strictEqual(res.statusCode, 200); | ||
|
||
res.resume(); | ||
server.close(); | ||
}); | ||
|
||
}); | ||
|
||
process.on('exit', function() { | ||
assert.strictEqual(fieldsPassed, 0); | ||
}); |