Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

if body not decode, dont touch it #26

  • Loading branch information...
commit a881cbd7da70e952b141a5baa2aa6a61995037ef 1 parent 0609a3b
@fengmk2 fengmk2 authored
Showing with 37 additions and 8 deletions.
  1. +13 −6 lib/urllib.js
  2. +24 −2 test/urllib.test.js
View
19 lib/urllib.js
@@ -215,7 +215,13 @@ exports.request = function (url, args, callback) {
var headers = JSON.stringify(res && res.headers || {});
err.message += ', ' + options.method + ' ' + url + ' ' + statusCode
+ '\nheaders: ' + headers;
- err.res = res;
+ if (res) {
+ err.res = {
+ statusCode: res.statusCode,
+ headers: res.headers,
+ aborted: res.aborted,
+ };
+ }
err.data = data;
}
cb(err, data, res);
@@ -264,13 +270,13 @@ exports.request = function (url, args, callback) {
}
var decodeContent = function (res, body, cb) {
+ var encoding = res.headers['content-encoding'];
if (body.length === 0 || !enableGzip) {
- return cb(null, body);
+ return cb(null, body, encoding);
}
- var encoding = res.headers['content-encoding'];
if (!encoding || encoding.toLowerCase() !== 'gzip') {
- return cb(null, body);
+ return cb(null, body, encoding);
}
debug('gunzip %d length body', body.length);
@@ -367,12 +373,13 @@ exports.request = function (url, args, callback) {
return;
}
- decodeContent(res, body, function (err, data) {
+ decodeContent(res, body, function (err, data, encoding) {
if (err) {
return done(err, body, res);
}
- if (args.dataType === 'json') {
+ // if body not decode, dont touch it
+ if (!encoding && args.dataType === 'json') {
try {
data = size > 0 ? JSON.parse(data) : null;
} catch (e) {
View
26 test/urllib.test.js
@@ -17,6 +17,7 @@
var should = require('should');
var http = require('http');
var https = require('https');
+var zlib = require('zlib');
var querystring = require('querystring');
var urlutil = require('url');
var KeepAliveAgent = require('agentkeepalive');
@@ -781,16 +782,37 @@ describe('urllib.test.js', function () {
describe('gzip content', function () {
it('should auto accept and decode gzip response content', function (done) {
- urllib.request('http://r.cnpmjs.org/npm',
+ urllib.request('http://r.cnpmjs.org/byte',
{dataType: 'json', gzip: true, timeout: 10000}, function (err, data, res) {
should.not.exist(err);
- data.name.should.equal('npm');
+ data.name.should.equal('byte');
res.should.have.header('content-encoding', 'gzip');
res.should.have.header('content-type', 'application/json');
done();
});
});
+ it('should auto accept and custom decode gzip response content', function (done) {
+ urllib.request('http://r.cnpmjs.org/byte', {
+ dataType: 'json', gzip: true, timeout: 10000,
+ headers: {
+ 'accept-encoding': 'gzip'
+ }
+ }, function (err, data, res) {
+ should.not.exist(err);
+ data.should.be.a.Buffer;
+ data.length.should.above(0);
+ res.should.have.header('content-encoding', 'gzip');
+ res.should.have.header('content-type', 'application/json');
+ zlib.gunzip(data, function (err, buf) {
+ should.not.exist(err);
+ buf.should.be.a.Buffer;
+ JSON.parse(buf).name.should.equal('byte');
+ done();
+ });
+ });
+ });
+
it('should redirect and gzip', function (done) {
urllib.request('http://dist.cnpmjs.org/v0.10.1/SHASUMS.txt',
{followRedirect: true, gzip: true, timeout: 10000}, function (err, data, res) {
Please sign in to comment.
Something went wrong with that request. Please try again.