This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathconstructor.spec.js
208 lines (180 loc) · 6.55 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* eslint-env mocha, browser */
import { multiaddr } from '@multiformats/multiaddr'
import { expect } from 'aegir/chai'
import { factory } from './utils/factory.js'
import { create as ipfsClient } from '../src/index.js'
import { isBrowser } from 'ipfs-utils/src/env.js'
const f = factory()
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('opts with URL components from URL', () => {
const host = 'wizard.world'
const port = '999'
const protocol = 'https'
const url = new URL(`${protocol}://${host}:${port}`)
const ipfs = ipfsClient({ host: url.host, port: url.port, protocol: url.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('URL as string', () => {
const host = '10.100.100.255'
const port = '9999'
const apiPath = '/future/api/v1/'
const ipfs = ipfsClient(`http://${host}:${port}${apiPath}`)
expectConfig(ipfs, { host, port, apiPath })
})
it('URL as URL', () => {
const host = '10.100.100.255'
const port = '9999'
const apiPath = '/future/api/v1/'
const ipfs = ipfsClient(new URL(`http://${host}:${port}${apiPath}`))
expectConfig(ipfs, { host, port, apiPath })
})
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 })
})
it('options.url as URL string', () => {
const host = '10.100.100.255'
const port = '9999'
const apiPath = '/future/api/v1/'
const ipfs = ipfsClient({ url: `http://${host}:${port}${apiPath}` })
expectConfig(ipfs, { host, port, apiPath })
})
it('options.url as URL', () => {
const host = '10.100.100.255'
const port = '9999'
const apiPath = '/future/api/v1/'
const ipfs = ipfsClient({ url: new URL(`http://${host}:${port}${apiPath}`) })
expectConfig(ipfs, { host, port, apiPath })
})
it('options.url as multiaddr (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({ url: multiaddr(addr) })
expectConfig(ipfs, { host, port, protocol })
})
it('options.url as multiaddr (explicit https)', () => {
const host = 'foo.com'
const port = '1001'
const protocol = 'https'
const addr = `/dns4/${host}/tcp/${port}/https`
const ipfs = ipfsClient({ url: multiaddr(addr) })
expectConfig(ipfs, { host, port, protocol })
})
it('options.url as multiaddr 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({ url: addr })
expectConfig(ipfs, { host, port, protocol })
})
it('options.url as multiaddr string (explicit https)', () => {
const host = 'foo.com'
const port = '1001'
const protocol = 'https'
const addr = `/dns4/${host}/tcp/${port}/https`
const ipfs = ipfsClient({ url: addr })
expectConfig(ipfs, { host, port, protocol })
})
})
describe('integration', () => {
let ipfsd
before(async function () {
this.timeout(60 * 1000) // slow CI
ipfsd = await f.spawn()
})
after(() => f.clean())
it('can connect to an ipfs http api', async () => {
await clientWorks(ipfsClient(ipfsd.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()
if (protocol) {
protocol = protocol + ':'
}
if (isBrowser) {
expect(conf.host).to.be.oneOf([host, globalThis.location.hostname, ''])
expect(conf.port).to.be.oneOf([port, globalThis.location.port, '80'])
expect(conf.protocol).to.equal(protocol || 'http:')
expect(conf.pathname).to.equal(apiPath || '/api/v0')
} else {
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.pathname).to.equal(apiPath || '/api/v0')
}
}