Comparing version 0.9.2 to 0.10.0
@@ -54,2 +54,3 @@ ////////////////////////////////////////// | ||
encoding : 'utf8', | ||
parse : 'all', // same as true. valid options: 'json', 'xml' or false/null | ||
@@ -67,3 +68,2 @@ // headers | ||
// booleans | ||
parse_response : true, | ||
decode_response : true, | ||
@@ -132,8 +132,9 @@ follow_set_cookies : false, | ||
function get_option(key) { | ||
function get_option(key, fallback) { | ||
// if original is in options, return that value | ||
if (typeof options[key] != 'undefined') return options[key]; | ||
// otherwise, return value from alias, if present | ||
return options[aliased.inverted[key]]; | ||
// otherwise, return value from alias or fallback/undefined | ||
return typeof options[aliased.inverted[key]] != 'undefined' | ||
? options[aliased.inverted[key]] : fallback; | ||
} | ||
@@ -158,2 +159,3 @@ | ||
output : options.output, | ||
parser : get_option('parse_response', true), | ||
encoding : options.encoding || (options.multipart ? 'binary' : defaults.encoding) | ||
@@ -245,3 +247,3 @@ } | ||
if (uri.indexOf('http') === -1) | ||
uri = 'http://' + uri; | ||
uri = uri.replace(/^(\/\/)?/, 'http://'); | ||
@@ -396,3 +398,3 @@ var config = this.setup(uri, options); | ||
// unless follow.keep_method was set to true, rewrite the request to GET before continuing | ||
// unless follow_keep_method was set to true, rewrite the request to GET before continuing | ||
if (!config.follow_keep_method) { | ||
@@ -444,10 +446,17 @@ method = 'GET'; | ||
// If parse is enabled and we have a parser for it, then go for it. | ||
if (config.parse_response && parsers[mime.type]) { | ||
out.parser = parsers[mime.type].name; | ||
pipeline.push(parsers[mime.type].fn()); | ||
if (config.parser && parsers[mime.type]) { | ||
// set objectMode on out stream to improve performance | ||
out._writableState.objectMode = true; | ||
out._readableState.objectMode = true; | ||
// If a specific parser was requested, make sure we don't parse other types. | ||
var parser_name = config.parser.toString().toLowerCase(); | ||
if (['xml', 'json'].indexOf(parser_name) == -1 || parsers[mime.type].name == parser_name) { | ||
// OK, so either we're parsing all content types or the one requested matches. | ||
out.parser = parsers[mime.type].name; | ||
pipeline.push(parsers[mime.type].fn()); | ||
// Set objectMode on out stream to improve performance. | ||
out._writableState.objectMode = true; | ||
out._readableState.objectMode = true; | ||
} | ||
// If we're not parsing, and unless decoding was disabled, we'll try | ||
@@ -454,0 +463,0 @@ // decoding non UTF-8 bodies to UTF-8, using the iconv-lite library. |
{ | ||
"name": "needle", | ||
"version": "0.9.2", | ||
"version": "0.10.0", | ||
"description": "The leanest and most handsome HTTP client in the Nodelands.", | ||
@@ -65,2 +65,3 @@ "keywords": [ | ||
}, | ||
"license": "MIT", | ||
"engines": { | ||
@@ -67,0 +68,0 @@ "node": ">= 0.10.x" |
@@ -214,3 +214,3 @@ Needle | ||
- `decode_response` : (or `decode`) Whether to decode the text responses to UTF-8, if Content-Type header shows a different charset. Defaults to `true`. | ||
- `parse_response` : (or `parse`) Whether to parse XML or JSON response bodies automagically. Defaults to `true`. | ||
- `parse_response` : (or `parse`) Whether to parse XML or JSON response bodies automagically. Defaults to `true`. You can also set this to 'xml' or 'json' in which case Needle will *only* parse the response if the content type matches. | ||
- `output` : Dump response output to file. This occurs after parsing and charset decoding is done. | ||
@@ -217,0 +217,0 @@ |
@@ -20,50 +20,3 @@ var needle = require('../'), | ||
describe('null URL', function(){ | ||
it('throws', function(){ | ||
var ex = get_catch(); // null | ||
should.exist(ex); | ||
ex.should.be.an.instanceOf(TypeError); | ||
ex.message.should.containEql('URL must be a string'); | ||
}) | ||
}) | ||
/* | ||
describe('invalid protocol', function(){ | ||
var url = 'foo://www.google.com/what' | ||
it('throws', function(){ | ||
var ex = get_catch(url); | ||
should.exist(ex); | ||
}) | ||
}) | ||
describe('invalid host', function(){ | ||
var url = 'http://s1\\\2.com/' | ||
it('throws', function(){ | ||
var ex = get_catch(url); | ||
should.exist(ex); | ||
}) | ||
}) | ||
describe('invalid path', function(){ | ||
var url = 'http://www.google.com\\\/x\\\ /x2.com/' | ||
it('throws', function(){ | ||
var ex = get_catch(url); | ||
should.exist(ex); | ||
}) | ||
}) | ||
*/ | ||
describe('when host does not exist', function(){ | ||
@@ -70,0 +23,0 @@ |
@@ -134,5 +134,25 @@ var should = require('should'), | ||
describe('and parse option is "xml"', function() { | ||
it('does NOT return object', function(done){ | ||
needle.get('localhost:' + port, { parse: 'xml' }, function(err, response, body) { | ||
should.not.exist(err); | ||
body.should.be.an.instanceof(Buffer) | ||
body.toString().should.eql('{"foo":"bar"}'); | ||
done(); | ||
}) | ||
}) | ||
it('should NOT have a .parser = json property', function(done) { | ||
needle.get('localhost:' + port, { parse: 'xml' }, function(err, resp) { | ||
should.not.exist(err); | ||
should.not.exist(resp.parser); | ||
done(); | ||
}) | ||
}) | ||
}) | ||
}); | ||
describe('when response is JSON \'false\'', function(){ | ||
@@ -247,2 +267,16 @@ | ||
describe('and parse option is "xml"', function() { | ||
it('does NOT return object', function(done){ | ||
needle.get('localhost:' + port, { parse: 'xml' }, function(err, response, body) { | ||
should.not.exist(err); | ||
body.should.be.an.instanceof(Buffer) | ||
body.toString().should.eql('false'); | ||
done(); | ||
}) | ||
}) | ||
}) | ||
}); | ||
@@ -249,0 +283,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2696
114940
16