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

Avoid crash for responses without 'content-type' header #222

Merged
merged 1 commit into from
Mar 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/request-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function onRes (buffer, cb) {

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

if (res.statusCode >= 400 || !res.statusCode) {
const error = new Error(`Server responded with ${res.statusCode}`)
Expand Down
19 changes: 19 additions & 0 deletions test/request-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,24 @@ describe('ipfsAPI request tests', () => {
protocol: 'http'
}).id(noop)
})

it('does not crash if no content-type header is provided', (done) => {
if (!isNode) {
return done()
}

// go-ipfs always (currently) adds a content-type header, even if no content is present,
// the standard behaviour for an http-api is to omit this header if no content is present
const server = require('http').createServer((req, res) => {
res.writeHead(200)
res.end()
}).listen(6001, () => {
ipfsAPI('/ip4/127.0.0.1/tcp/6001')
.config.replace('test/r-config.json', (err) => {
expect(err).to.be.equal(null)
server.close(done)
})
})
})
})
})