This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy pathconstructor.spec.js
122 lines (104 loc) · 3.36 KB
/
constructor.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* eslint-env mocha */
'use strict'
const multiaddr = require('multiaddr')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const f = require('./utils/factory')
const ipfsClient = require('../src/index.js')
describe('ipfs-http-client constructor tests', () => {
describe('parameter permuations', () => {
it('none', () => {
const ipfs = ipfsClient()
if (typeof self !== 'undefined') {
const { hostname, port } = self.location
expectConfig(ipfs, { host: hostname, port })
} else {
expectConfig(ipfs, {})
}
})
it('opts', () => {
const host = 'wizard.world'
const port = '999'
const protocol = 'https'
const ipfs = ipfsClient({ host, port, protocol })
expectConfig(ipfs, { host, port, protocol })
})
it('mutliaddr dns4 string, opts', () => {
const host = 'foo.com'
const port = '1001'
const protocol = 'https'
const addr = `/dns4/${host}/tcp/${port}`
const ipfs = ipfsClient(addr, { protocol })
expectConfig(ipfs, { host, port, protocol })
})
it('mutliaddr ipv4 string', () => {
const host = '101.101.101.101'
const port = '1001'
const addr = `/ip4/${host}/tcp/${port}`
const ipfs = ipfsClient(addr)
expectConfig(ipfs, { host, port })
})
it('mutliaddr instance', () => {
const host = 'ace.place'
const port = '1001'
const addr = multiaddr(`/dns4/${host}/tcp/${port}`)
const ipfs = ipfsClient(addr)
expectConfig(ipfs, { host, port })
})
it('host and port strings', () => {
const host = '1.1.1.1'
const port = '9999'
const ipfs = ipfsClient(host, port)
expectConfig(ipfs, { host, port })
})
it('host, port and api path', () => {
const host = '10.100.100.255'
const port = '9999'
const apiPath = '/future/api/v1/'
const ipfs = ipfsClient(host, port, { 'api-path': apiPath })
expectConfig(ipfs, { host, port, apiPath })
})
it('throws on invalid mutliaddr', () => {
expect(() => ipfsClient('/dns4')).to.throw('invalid address')
expect(() => ipfsClient('/hello')).to.throw('no protocol with name')
expect(() => ipfsClient('/dns4/ipfs.io')).to.throw('multiaddr must have a valid format')
})
})
describe('integration', () => {
let apiAddr
let ipfsd
before(function (done) {
this.timeout(60 * 1000) // slow CI
f.spawn({ initOptions: { bits: 1024, profile: 'test' } }, (err, node) => {
expect(err).to.not.exist()
ipfsd = node
apiAddr = node.apiAddr.toString()
done()
})
})
after((done) => {
if (!ipfsd) return done()
ipfsd.stop(done)
})
it('can connect to an ipfs http api', (done) => {
clientWorks(ipfsClient(apiAddr), done)
})
})
})
function clientWorks (client, done) {
client.id((err, id) => {
expect(err).to.not.exist()
expect(id).to.have.a.property('id')
expect(id).to.have.a.property('publicKey')
done()
})
}
function expectConfig (ipfs, { host, port, protocol, apiPath }) {
const conf = ipfs.util.getEndpointConfig()
expect(conf.host).to.equal(host || 'localhost')
expect(conf.port).to.equal(port || '5001')
expect(conf.protocol).to.equal(protocol || 'http')
expect(conf['api-path']).to.equal(apiPath || '/api/v0/')
}