Skip to content

Commit

Permalink
fix: keep exists accept header on dataType = json (#289)
Browse files Browse the repository at this point in the history
* test: add ungzip error test case
  • Loading branch information
fengmk2 committed Jul 27, 2018
1 parent 7e529f9 commit ab39245
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/urllib.js
Expand Up @@ -361,7 +361,9 @@ exports.requestWithCallback = function requestWithCallback(url, args, callback)
}

if (args.dataType === 'json') {
options.headers.Accept = 'application/json';
if (!options.headers.Accept && !options.headers.accept) {
options.headers.Accept = 'application/json';
}
}

if (typeof args.beforeRequest === 'function') {
Expand Down Expand Up @@ -585,7 +587,12 @@ exports.requestWithCallback = function requestWithCallback(url, args, callback)
case 'gzip':
case 'deflate':
debug('unzip %d length body', body.length);
zlib.unzip(body, cb);
zlib.unzip(body, function(err, data) {
if (err && err.name === 'Error') {
err.name = 'UnzipError';
}
cb(err, data);
});
break;
default:
cb(null, body, encoding);
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/server.js
Expand Up @@ -181,6 +181,10 @@ var server = http.createServer(function (req, res) {
} else if (req.url.indexOf('/no-gzip') === 0) {
fs.createReadStream(__filename).pipe(res);
return;
} else if (req.url.indexOf('/error-gzip') === 0) {
res.setHeader('Content-Encoding', 'gzip');
fs.createReadStream(__filename).pipe(res);
return;
} else if (req.url.indexOf('/gzip') === 0) {
res.setHeader('Content-Encoding', 'gzip');
fs.createReadStream(__filename).pipe(zlib.createGzip()).pipe(res);
Expand Down Expand Up @@ -217,6 +221,10 @@ var server = http.createServer(function (req, res) {
}));
} else if (req.url === '/bigfile') {
return res.end(new Buffer(1024 * 1024 * 100));
} else if (req.url === '/headers/accept') {
return res.end(JSON.stringify({
accept: req.headers.accept,
}));
}

var url = req.url.split('?');
Expand Down
38 changes: 38 additions & 0 deletions test/urllib.test.js
Expand Up @@ -693,6 +693,31 @@ describe('test/urllib.test.js', function () {
});
});

it('should auto set accept headers when dataType = json and accept not exists', function(done) {
urllib.request(host + '/headers/accept', {
dataType: 'json',
}, function (err, data, res) {
assert(!err);
assert(res.statusCode === 200);
assert(data.accept === 'application/json');
done();
});
});

it('should keep exists accept headers when dataType = json', function(done) {
urllib.request(host + '/headers/accept', {
dataType: 'json',
headers: {
accept: 'foo/json',
},
}, function (err, data, res) {
assert(!err);
assert(res.statusCode === 200);
assert(data.accept === 'foo/json');
done();
});
});

describe('mock sockets full', function () {
var agent = new http.Agent({
maxSockets: 1,
Expand Down Expand Up @@ -1272,6 +1297,19 @@ describe('test/urllib.test.js', function () {
});
});

it('should throw on error gzip content', function (done) {
urllib.request(host + '/error-gzip',
{
gzip: true,
timeout: 25000,
}, function (err, data, res) {
assert(err);
assert(err.name === 'UnzipError');
assert(res.headers['content-encoding'] === 'gzip');
done();
});
});

it('should auto accept and custom decode gzip response content', function (done) {
done = pedding(4, done);

Expand Down

0 comments on commit ab39245

Please sign in to comment.