Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit c7b2d86

Browse files
committed
Avoid crash for responses without 'content-type' header
1 parent 2e67784 commit c7b2d86

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/request-api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function onRes (buffer, cb) {
2525

2626
const stream = !!res.headers['x-stream-output']
2727
const chunkedObjects = !!res.headers['x-chunked-output']
28-
const isJson = res.headers['content-type'].indexOf('application/json') === 0
28+
const isJson = res.headers['content-type'] && res.headers['content-type'].indexOf('application/json') === 0
2929

3030
if (res.statusCode >= 400 || !res.statusCode) {
3131
const error = new Error(`Server responded with ${res.statusCode}`)

test/request-api.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,21 @@ describe('ipfsAPI request tests', () => {
4141
protocol: 'http'
4242
}).id(noop)
4343
})
44+
45+
it('does not crash if no content-type header is provided', (done) => {
46+
if (!isNode) {
47+
return done()
48+
}
49+
50+
const server = require('http').createServer((req, res) => {
51+
res.writeHead(200)
52+
res.end()
53+
}).listen(6001, () => {
54+
ipfsAPI('/ip4/127.0.0.1/tcp/6001').config.replace('test/r-config.json', (err) => {
55+
expect(err).to.be.equal(null)
56+
server.close(done)
57+
})
58+
})
59+
})
4460
})
4561
})

0 commit comments

Comments
 (0)