Skip to content

Commit

Permalink
feat: add support for custom headers to send-request (ipfs-inactive#741)
Browse files Browse the repository at this point in the history
* add support for custom headers to send-request

* add custom headers test

* add custom header documentation

* fix: clone config.headers to avoid prev headers in next req

License: MIT
Signed-off-by: Alan Shaw <alan@tableflip.io>
  • Loading branch information
Orie Steele authored and danieldaf committed Jul 21, 2018
1 parent 63d1e22 commit 9aff99e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Credentials "[\"true\"]"
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods "[\"PUT\", \"POST\", \"GET\"]"
```

### Custom Headers

If you wish to send custom headers with each request made by this library, for example, the Authorization header. You can use the config to do so:

```
const ipfs = IpfsApi({
host: 'localhost',
port: 5001,
protocol: 'http',
headers: {
authorization: 'Bearer ' + TOKEN
}
})
```

## Usage

### API
Expand Down
2 changes: 1 addition & 1 deletion src/utils/send-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function requestAPI (config, options, callback) {
delete options.qs.followSymlinks

const method = 'POST'
const headers = {}
const headers = Object.assign({}, config.headers)

if (isNode) {
// Browsers do not allow you to modify the user agent
Expand Down
61 changes: 61 additions & 0 deletions test/custom-headers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-env mocha */
'use strict'

const isNode = require('detect-node')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const IPFSApi = require('../src')
const f = require('./utils/factory')

describe('custom headers', function () {
// do not test in browser
if (!isNode) { return }
this.timeout(50 * 1000) // slow CI
let ipfs
let ipfsd
// initialize ipfs with custom headers
before(done => {
f.spawn({ initOptions: { bits: 1024 } }, (err, _ipfsd) => {
expect(err).to.not.exist()
ipfsd = _ipfsd
ipfs = IPFSApi({
host: 'localhost',
port: 6001,
protocol: 'http',
headers: {
authorization: 'Bearer ' + 'YOLO'
}
})
done()
})
})

it('are supported', done => {
// spin up a test http server to inspect the requests made by the library
const server = require('http').createServer((req, res) => {
req.on('data', () => {})
req.on('end', () => {
res.writeHead(200)
res.end()
// ensure custom headers are present
expect(req.headers.authorization).to.equal('Bearer ' + 'YOLO')
server.close()
done()
})
})

server.listen(6001, () => {
ipfs.id((err, res) => {
if (err) {
throw new Error('Unexpected error.')
}
// this call is used to test that headers are being sent.
})
})
})

after(done => ipfsd.stop(done))
})

0 comments on commit 9aff99e

Please sign in to comment.