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

Commit e295e8f

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

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,24 @@ 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+
// go-ipfs always (currently) adds a content-type header, even if no content is present,
51+
// the standard behaviour for an http-api is to omit this header if no content is present
52+
const server = require('http').createServer((req, res) => {
53+
res.writeHead(200)
54+
res.end()
55+
}).listen(6001, () => {
56+
ipfsAPI('/ip4/127.0.0.1/tcp/6001')
57+
.config.replace('test/r-config.json', (err) => {
58+
expect(err).to.be.equal(null)
59+
server.close(done)
60+
})
61+
})
62+
})
4463
})
4564
})

0 commit comments

Comments
 (0)