user should be able to resolve cloned body before original body
This commit is contained in:
parent
8b8309fe0f
commit
419597fe13
13
lib/body.js
13
lib/body.js
|
@ -205,7 +205,7 @@ Body.prototype._convert = function(encoding) {
|
||||||
* @return Mixed
|
* @return Mixed
|
||||||
*/
|
*/
|
||||||
Body.prototype._clone = function(instance) {
|
Body.prototype._clone = function(instance) {
|
||||||
var pass;
|
var p1, p2;
|
||||||
var body = instance.body;
|
var body = instance.body;
|
||||||
|
|
||||||
// don't allow cloning a used body
|
// don't allow cloning a used body
|
||||||
|
@ -216,9 +216,14 @@ Body.prototype._clone = function(instance) {
|
||||||
// check that body is a stream and not form-data object
|
// check that body is a stream and not form-data object
|
||||||
// note: we can't clone the form-data object without having it as a dependency
|
// note: we can't clone the form-data object without having it as a dependency
|
||||||
if (bodyStream(body) && typeof body.getBoundary !== 'function') {
|
if (bodyStream(body) && typeof body.getBoundary !== 'function') {
|
||||||
pass = new PassThrough();
|
// tee instance body
|
||||||
body.pipe(pass);
|
p1 = new PassThrough();
|
||||||
body = pass;
|
p2 = new PassThrough();
|
||||||
|
body.pipe(p1);
|
||||||
|
body.pipe(p2);
|
||||||
|
// set instance body to teed body and return the other teed body
|
||||||
|
instance.body = p1;
|
||||||
|
body = p2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return body;
|
return body;
|
||||||
|
|
13
test/test.js
13
test/test.js
|
@ -939,6 +939,19 @@ describe('node-fetch', function() {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should allow cloning a json response, first log as text response, then return json object', function() {
|
||||||
|
url = base + '/json';
|
||||||
|
return fetch(url).then(function(res) {
|
||||||
|
var r1 = res.clone();
|
||||||
|
return r1.text().then(function(result) {
|
||||||
|
expect(result).to.equal('{"name":"value"}');
|
||||||
|
return res.json().then(function(result) {
|
||||||
|
expect(result).to.deep.equal({name: 'value'});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should not allow cloning a response after its been used', function() {
|
it('should not allow cloning a response after its been used', function() {
|
||||||
url = base + '/hello';
|
url = base + '/hello';
|
||||||
return fetch(url).then(function(res) {
|
return fetch(url).then(function(res) {
|
||||||
|
|
Loading…
Reference in New Issue