From 4ebb2e0f1cdf9e25434ab2768d85b2f55ce5da7b Mon Sep 17 00:00:00 2001 From: Orie Steele Date: Sun, 15 Apr 2018 13:31:39 -0500 Subject: [PATCH 1/4] add support for custom headers to send-request --- src/utils/send-request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/send-request.js b/src/utils/send-request.js index 90f436d47..49fadc8f5 100644 --- a/src/utils/send-request.js +++ b/src/utils/send-request.js @@ -106,7 +106,7 @@ function requestAPI (config, options, callback) { delete options.qs.followSymlinks const method = 'POST' - const headers = {} + const headers = config.headers || {} if (isNode) { // Browsers do not allow you to modify the user agent From d8301a5c3526893f60885081a95b7e528229c9b3 Mon Sep 17 00:00:00 2001 From: Orie Steele Date: Sun, 15 Apr 2018 13:31:39 -0500 Subject: [PATCH 2/4] add custom headers test --- src/utils/send-request.js | 2 +- test/custom-headers.spec.js | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 test/custom-headers.spec.js diff --git a/src/utils/send-request.js b/src/utils/send-request.js index 90f436d47..49fadc8f5 100644 --- a/src/utils/send-request.js +++ b/src/utils/send-request.js @@ -106,7 +106,7 @@ function requestAPI (config, options, callback) { delete options.qs.followSymlinks const method = 'POST' - const headers = {} + const headers = config.headers || {} if (isNode) { // Browsers do not allow you to modify the user agent diff --git a/test/custom-headers.spec.js b/test/custom-headers.spec.js new file mode 100644 index 000000000..acf43464a --- /dev/null +++ b/test/custom-headers.spec.js @@ -0,0 +1,62 @@ +/* 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)) +}) From 9311394d7c53b8c260df14becf58140a2e773d25 Mon Sep 17 00:00:00 2001 From: Orie Steele Date: Thu, 26 Apr 2018 11:06:22 -0500 Subject: [PATCH 3/4] add custom header documentation --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/README.md b/README.md index 5b9446a59..3bc2126b6 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,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 From cf96ceefb50848f7e8648e74e17e068c5eadefa5 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 18 Jun 2018 11:33:38 +0100 Subject: [PATCH 4/4] fix: clone config.headers to avoid prev headers in next req License: MIT Signed-off-by: Alan Shaw --- src/utils/send-request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/send-request.js b/src/utils/send-request.js index 49fadc8f5..eb9ea6adc 100644 --- a/src/utils/send-request.js +++ b/src/utils/send-request.js @@ -106,7 +106,7 @@ function requestAPI (config, options, callback) { delete options.qs.followSymlinks const method = 'POST' - const headers = config.headers || {} + const headers = Object.assign({}, config.headers) if (isNode) { // Browsers do not allow you to modify the user agent