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

Commit 0ebb098

Browse files
committed
fix: improve constructor param handling tests
- ensure explicitly passed parameters are used; dont pass in the default values and assume the are being set correctly TODO: needs #935 to expose the protocol and api-path properties License: MIT Signed-off-by: Oli Evans <oli@tableflip.io>
1 parent 75d76a7 commit 0ebb098

File tree

2 files changed

+101
-57
lines changed

2 files changed

+101
-57
lines changed

src/index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,28 @@ function ipfsClient (hostOrMultiaddr, port, opts) {
1010
// convert all three params to objects that we can merge.
1111
let hostAndPort = {}
1212

13-
if (hostOrMultiaddr.host) {
14-
hostAndPort = hostOrMultiaddr
13+
if (!hostOrMultiaddr) {
14+
// autoconfigure host and port in browser
15+
if (typeof self !== 'undefined') {
16+
const split = self.location.host.split(':')
17+
hostAndPort.host = split[0]
18+
hostAndPort.port = split[1]
19+
}
1520

1621
} else if (multiaddr.isMultiaddr(hostOrMultiaddr)) {
1722
hostAndPort = toHostAndPort(hostOrMultiaddr)
1823

24+
} else if (typeof hostOrMultiaddr === 'object') {
25+
hostAndPort = hostOrMultiaddr
26+
1927
} else if (typeof hostOrMultiaddr === 'string') {
2028
if (hostOrMultiaddr[0] === '/') {
2129
// throws if multiaddr is malformed or can't be converted to a nodeAddress
2230
hostAndPort = toHostAndPort(multiaddr(hostOrMultiaddr))
2331
} else {
24-
// hostAndPort is domain or ip address as a string
25-
hostAndPort = hostOrMultiaddr
32+
// hostOrMultiaddr is domain or ip address as a string
33+
hostAndPort.host = hostOrMultiaddr
2634
}
27-
28-
// autoconfigure host and port in browser
29-
} else if (!hostOrMultiaddr && typeof self !== 'undefined') {
30-
const split = self.location.host.split(':')
31-
hostAndPort.host = split[0]
32-
hostAndPort.port = split[1]
3335
}
3436

3537
if (port && typeof port !== 'object') {

test/constructor.spec.js

Lines changed: 89 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-env mocha */
22
'use strict'
33

4+
const multiaddr = require('multiaddr')
45
const chai = require('chai')
56
const dirtyChai = require('dirty-chai')
67
const expect = chai.expect
@@ -9,18 +10,73 @@ chai.use(dirtyChai)
910
const f = require('./utils/factory')
1011
const ipfsClient = require('../src/index.js')
1112

12-
function clientWorks (client, done) {
13-
client.id((err, id) => {
14-
expect(err).to.not.exist()
13+
describe('ipfs-http-client constructor tests', () => {
14+
describe('parameter permuations', () => {
15+
it('none', () => {
16+
const host = 'localhost'
17+
const port = '5001'
18+
const protocol = 'http'
19+
const ipfs = ipfsClient()
20+
expectConfig(ipfs, { host, port, protocol })
21+
})
1522

16-
expect(id).to.have.a.property('id')
17-
expect(id).to.have.a.property('publicKey')
18-
done()
23+
it('opts', () => {
24+
const host = 'wizard.world'
25+
const port = '999'
26+
const protocol = 'https'
27+
const ipfs = ipfsClient({ host, port, protocol })
28+
expectConfig(ipfs, { host, port })
29+
})
30+
31+
it('mutliaddr dns4 string, opts', () => {
32+
const host = 'foo.com'
33+
const port = '1001'
34+
const protocol = 'https'
35+
const addr = `/dns4/${host}/tcp/${port}`
36+
const ipfs = ipfsClient(addr, { protocol })
37+
expectConfig(ipfs, { host, port, protocol })
38+
})
39+
40+
it('mutliaddr ipv4 string', () => {
41+
const host = '101.101.101.101'
42+
const port = '1001'
43+
const addr = `/ip4/${host}/tcp/${port}`
44+
const ipfs = ipfsClient(addr)
45+
expectConfig(ipfs, { host, port })
46+
})
47+
48+
it('mutliaddr instance', () => {
49+
const host = 'ace.place'
50+
const port = '1001'
51+
const addr = multiaddr(`/dns4/${host}/tcp/${port}`)
52+
const ipfs = ipfsClient(addr)
53+
expectConfig(ipfs, { host, port })
54+
})
55+
56+
it('host and port strings', () => {
57+
const host = '1.1.1.1'
58+
const port = '9999'
59+
const ipfs = ipfsClient(host, port)
60+
expectConfig(ipfs, { host, port })
61+
})
62+
63+
// TOOD: need to add `api-path` to `getEndpointConfig()` so we can test that setting a non-default value works
64+
it.skip('host, port and api path', () => {
65+
const host = '10.100.100.255'
66+
const port = '9999'
67+
const apiPath = '/future/api/v1'
68+
const ipfs = ipfsClient(host, port, {'api-path': apiPath})
69+
expectConfig(ipfs, { host, port, apiPath })
70+
})
71+
72+
it('throws on invalid mutliaddr', () => {
73+
expect(() => ipfsClient('/dns4')).to.throw('invalid address')
74+
expect(() => ipfsClient('/hello')).to.throw('no protocol with name')
75+
expect(() => ipfsClient('/dns4/ipfs.io')).to.throw('multiaddr must have a valid format')
76+
})
1977
})
20-
}
2178

22-
describe('ipfs-http-client constructor tests', () => {
23-
describe('parameter permuations', () => {
79+
describe('integration', () => {
2480
let apiAddr
2581
let ipfsd
2682

@@ -40,45 +96,31 @@ describe('ipfs-http-client constructor tests', () => {
4096
ipfsd.stop(done)
4197
})
4298

43-
it('opts', (done) => {
44-
const splitted = apiAddr.split('/')
45-
clientWorks(ipfsClient({
46-
host: splitted[2],
47-
port: splitted[4],
48-
protocol: 'http'
49-
}), done)
50-
})
51-
52-
it('mutliaddr, opts', (done) => {
53-
clientWorks(ipfsClient(apiAddr, { protocol: 'http' }), done)
54-
})
55-
56-
it('host, port', (done) => {
57-
const splitted = apiAddr.split('/')
58-
59-
clientWorks(ipfsClient(splitted[2], splitted[4]), done)
60-
})
61-
62-
it('specify host, port and api path', (done) => {
63-
const splitted = apiAddr.split('/')
64-
65-
clientWorks(ipfsClient({
66-
host: splitted[2],
67-
port: splitted[4],
68-
'api-path': '/api/v0/'
69-
}), done)
99+
it('can connect to an ipfs http api', (done) => {
100+
clientWorks(ipfsClient(apiAddr), done)
70101
})
102+
})
103+
})
71104

72-
it('host, port, opts', (done) => {
73-
const splitted = apiAddr.split('/')
74-
75-
clientWorks(ipfsClient(splitted[2], splitted[4], { protocol: 'http' }), done)
76-
})
105+
function clientWorks (client, done) {
106+
client.id((err, id) => {
107+
expect(err).to.not.exist()
77108

78-
it('error in invalid mutliaddr', () => {
79-
expect(() => ipfsClient('/dns4')).to.throw('invalid address')
80-
expect(() => ipfsClient('/hello')).to.throw('no protocol with name')
81-
expect(() => ipfsClient('/dns4/ipfs.io')).to.throw('multiaddr must have a valid format')
82-
})
109+
expect(id).to.have.a.property('id')
110+
expect(id).to.have.a.property('publicKey')
111+
done()
83112
})
84-
})
113+
}
114+
115+
function expectConfig (ipfs, {host, port, protocol, apiPath}) {
116+
const conf = ipfs.util.getEndpointConfig()
117+
expect(conf.host).to.equal(host)
118+
expect(conf.port).to.equal(port)
119+
// TODO: needs https://github.com/ipfs/js-ipfs-http-client/pull/935
120+
if (protocol) {
121+
expect(conf.protocol).to.equal(protocol)
122+
}
123+
if (apiPath) {
124+
expect(conf['api-path']).to.equal(apiPath)
125+
}
126+
}

0 commit comments

Comments
 (0)