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

Commit 285a7e3

Browse files
committed
Merge pull request #162 from alexmingoia/fix-port-default
Exclude port from path if `config.port` not set.
2 parents 37be296 + c30afb4 commit 285a7e3

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/request-api.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ function requestAPI (config, path, args, qs, files, buffer, cb) {
8787
// this option is only used internally, not passed to daemon
8888
delete qs.followSymlinks
8989

90+
const port = config.port ? `:${config.port}` : ''
91+
9092
const opts = {
9193
method: files ? 'POST' : 'GET',
92-
uri: `${config.protocol}://${config.host}:${config.port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`,
94+
uri: `${config.protocol}://${config.host}${port}${config['api-path']}${path}?${Qs.stringify(qs, {arrayFormat: 'repeat'})}`,
9395
headers: {}
9496
}
9597

test/request-api.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict'
2+
3+
const ipfsAPI = require('../src/index.js')
4+
const noop = () => {}
5+
6+
describe('ipfsAPI request tests', () => {
7+
describe('requestAPI', () => {
8+
const apiAddrs = require('./tmp-disposable-nodes-addrs.json')
9+
const apiAddr = apiAddrs.a.split('/')
10+
11+
it('excludes port from URL if config.port is falsy', (done) => {
12+
const Wreck = require('wreck')
13+
const request = Wreck.request
14+
15+
Wreck.request = (method, uri, opts, cb) => {
16+
Wreck.request = request
17+
expect(uri).to.not.contain(/:\d/)
18+
done()
19+
}
20+
21+
ipfsAPI({
22+
host: apiAddr[2],
23+
port: null,
24+
protocol: 'http'
25+
}).id(noop)
26+
})
27+
28+
it('includes port in URL if config.port is truthy', (done) => {
29+
const Wreck = require('wreck')
30+
const request = Wreck.request
31+
32+
Wreck.request = (method, uri, opts, cb) => {
33+
Wreck.request = request
34+
expect(uri).to.contain(':' + apiAddr[4])
35+
done()
36+
}
37+
38+
ipfsAPI({
39+
host: apiAddr[2],
40+
port: apiAddr[4],
41+
protocol: 'http'
42+
}).id(noop)
43+
})
44+
})
45+
})

0 commit comments

Comments
 (0)