From 9ea09fb3f6fdbb24ad40d645542d26a6f2725f5f Mon Sep 17 00:00:00 2001 From: Alex Mingoia Date: Tue, 8 Dec 2015 09:50:13 -0800 Subject: [PATCH 1/2] Exclude port from path if `config.port` not set. Fixes ipfs/js-ipfs-api#161. --- src/request-api.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/request-api.js b/src/request-api.js index 3419536bf..36577da3e 100644 --- a/src/request-api.js +++ b/src/request-api.js @@ -90,9 +90,11 @@ function requestAPI (config, path, args, qs, files, buffer, cb) { // this option is only used internally, not passed to daemon delete qs.followSymlinks + const port = config.port ? `:${config.port}` : '' + const opts = { method: files ? 'POST' : 'GET', - uri: `${config.protocol}://${config.host}:${config.port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`, + uri: `${config.protocol}://${config.host}${port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`, headers: {} } From c30afb4242365410cfeac8c1245e37117c694db6 Mon Sep 17 00:00:00 2001 From: Alex Mingoia Date: Tue, 8 Dec 2015 11:31:35 -0800 Subject: [PATCH 2/2] Add tests for port component of request API URL. --- test/request-api.spec.js | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 test/request-api.spec.js diff --git a/test/request-api.spec.js b/test/request-api.spec.js new file mode 100644 index 000000000..d50126853 --- /dev/null +++ b/test/request-api.spec.js @@ -0,0 +1,45 @@ +'use strict' + +const ipfsAPI = require('../src/index.js') +const noop = () => {} + +describe('ipfsAPI request tests', () => { + describe('requestAPI', () => { + const apiAddrs = require('./tmp-disposable-nodes-addrs.json') + const apiAddr = apiAddrs.a.split('/') + + it('excludes port from URL if config.port is falsy', (done) => { + const Wreck = require('wreck') + const request = Wreck.request + + Wreck.request = (method, uri, opts, cb) => { + Wreck.request = request + expect(uri).to.not.contain(/:\d/) + done() + } + + ipfsAPI({ + host: apiAddr[2], + port: null, + protocol: 'http' + }).id(noop) + }) + + it('includes port in URL if config.port is truthy', (done) => { + const Wreck = require('wreck') + const request = Wreck.request + + Wreck.request = (method, uri, opts, cb) => { + Wreck.request = request + expect(uri).to.contain(':' + apiAddr[4]) + done() + } + + ipfsAPI({ + host: apiAddr[2], + port: apiAddr[4], + protocol: 'http' + }).id(noop) + }) + }) +})