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
131 lines (112 loc) · 3.87 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
123
124
125
126
127
128
129
130
131
/* eslint-env mocha, browser */
'use strict'
const multiaddr = require('multiaddr')
const { expect } = require('interface-ipfs-core/src/utils/mocha')
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('multiaddr dns4 string (implicit http)', () => {
const host = 'foo.com'
const port = '1001'
const protocol = 'http' // default to http if not specified in multiaddr
const addr = `/dns4/${host}/tcp/${port}`
const ipfs = ipfsClient(addr)
expectConfig(ipfs, { host, port, protocol })
})
it('multiaddr dns4 string (explicit https)', () => {
const host = 'foo.com'
const port = '1001'
const protocol = 'https'
const addr = `/dns4/${host}/tcp/${port}/${protocol}`
const ipfs = ipfsClient(addr)
expectConfig(ipfs, { host, port, protocol })
})
it('multiaddr ipv4 string (implicit http)', () => {
const host = '101.101.101.101'
const port = '1001'
const protocol = 'http'
const addr = `/ip4/${host}/tcp/${port}`
const ipfs = ipfsClient(addr)
expectConfig(ipfs, { host, port, protocol })
})
it('multiaddr ipv4 string (explicit https)', () => {
const host = '101.101.101.101'
const port = '1001'
const protocol = 'https'
const addr = `/ip4/${host}/tcp/${port}/${protocol}`
const ipfs = ipfsClient(addr)
expectConfig(ipfs, { host, port, protocol })
})
it('multiaddr 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, apiPath })
expectConfig(ipfs, { host, port, apiPath: apiPath.slice(0, -1) })
})
it('throws on invalid multiaddr', () => {
expect(() => ipfsClient('/dns4')).to.throw('invalid address')
expect(() => ipfsClient('/hello')).to.throw('no protocol with name')
expect(() => ipfsClient('/dns4/ipfs.io')).to.throw()
})
})
describe('integration', () => {
let apiAddr
let ipfsd
before(async function () {
this.timeout(60 * 1000) // slow CI
ipfsd = await f.spawn({ initOptions: { bits: 1024, profile: 'test' } })
apiAddr = ipfsd.apiAddr.toString()
})
after(async () => {
if (ipfsd) {
await ipfsd.stop()
}
})
it('can connect to an ipfs http api', async () => {
await clientWorks(ipfsClient(apiAddr))
})
})
})
async function clientWorks (client) {
const id = await client.id()
expect(id).to.have.a.property('id')
expect(id).to.have.a.property('publicKey')
}
function expectConfig (ipfs, { host, port, protocol, apiPath }) {
const conf = ipfs.getEndpointConfig()
expect(conf.host).to.be.oneOf([host, 'localhost', ''])
expect(conf.port).to.be.oneOf([port, '5001', '80'])
expect(conf.protocol).to.equal(protocol || 'http')
expect(conf['api-path']).to.equal(apiPath || '/api/v0')
}