Skip to content

Commit 2bbcf44

Browse files
authored
Merge pull request #351 from yetzt/patch-1
treat xml and json mime types as text
2 parents b694f30 + f56b49a commit 2bbcf44

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

lib/needle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ Needle.prototype.send_request = function(count, method, uri, config, post_data,
576576

577577
var pipeline = [],
578578
mime = parse_content_type(headers['content-type']),
579-
text_response = mime.type && mime.type.indexOf('text/') != -1;
579+
text_response = mime.type && (mime.type.indexOf('text/') != -1 || !!mime.type.match(/(\/|\+)(xml|json)$/));
580580

581581
// To start, if our body is compressed and we're able to inflate it, do it.
582582
if (headers['content-encoding'] && decompressors[headers['content-encoding']]) {

test/mimetype.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
var should = require('should'),
2+
needle = require('./../'),
3+
helpers = require('./helpers');
4+
5+
describe('receiving json and xml content as string', function() {
6+
7+
this.timeout(5000);
8+
9+
["text/plain", "application/json", "application/ld+json", "application/xml", "image/svg+xml"].forEach(function(mimetype, offset){
10+
11+
describe('Given content-type: "'+mimetype+'"', function () {
12+
13+
var server, port = 54330+offset;
14+
15+
before(function(done) {
16+
server = helpers.server({
17+
port: port,
18+
response: 'content',
19+
headers: { 'Content-Type': mimetype }
20+
}, done);
21+
})
22+
23+
after(function(done) {
24+
server.close(done)
25+
})
26+
27+
describe('with parse = false', function () {
28+
it('delivers by default as string', function (done) {
29+
30+
needle.get('http://localhost:' + port, { parse: false }, function (err, resp) {
31+
32+
resp.body.should.be.a.String;
33+
(typeof resp.body).should.eql('string')
34+
done();
35+
})
36+
37+
})
38+
39+
})
40+
41+
})
42+
43+
});
44+
45+
["application/octet-stream", "image/png"].forEach(function(mimetype, offset){
46+
47+
describe('Given content-type: "'+mimetype+'"', function () {
48+
49+
var server, port = 54340+offset;
50+
51+
before(function(done) {
52+
server = helpers.server({
53+
port: port,
54+
response: 'content',
55+
headers: { 'Content-Type': mimetype }
56+
}, done);
57+
})
58+
59+
after(function(done) {
60+
server.close(done)
61+
})
62+
63+
describe('with parse = false', function () {
64+
it('delivers by default as Buffer', function (done) {
65+
66+
needle.get('http://localhost:' + port, { parse: false }, function (err, resp) {
67+
68+
resp.body.should.be.a.Buffer;
69+
(resp.body instanceof Buffer).should.eql(true)
70+
done();
71+
})
72+
73+
})
74+
75+
})
76+
77+
})
78+
79+
})
80+
81+
})

0 commit comments

Comments
 (0)