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
150 lines (129 loc) · 4.37 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
/* eslint-env mocha, browser */
'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('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 dns4 string, explicit https in 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('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, { 'api-path': apiPath })
expectConfig(ipfs, { host, port, apiPath })
})
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('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.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/')
}