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

Commit 524a110

Browse files
lidelAlan Shaw
authored and
Alan Shaw
committed
refactor: tests of addFromURL in browsers (#514)
> Required by ipfs/js-ipfs#2304 Old version of `addFromURL` tests was Node-centric and did not work in web browser contexts. This PR: - moves the HTTP server to a separate file to enable spawning it from aegir hook (see .aegir.js/hooks/browser/pre|post in js-ipfs from ipfs/js-ipfs#2304) - simplifies the way we use HTTP Echo Server by moving it to aegir hooks and removing HTTPS version - #514 (comment) - removes overhead of starting and stopping HTTP servers multiple times - shortens/simplifies test code a bit License: MIT Signed-off-by: Marcin Rataj <[email protected]>
1 parent 56caa89 commit 524a110

File tree

3 files changed

+120
-231
lines changed

3 files changed

+120
-231
lines changed

src/files-regular/add-from-url.js

+58-227
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const { fixtures } = require('./utils')
54
const { getDescribe, getIt, expect } = require('../utils/mocha')
6-
const http = require('http')
7-
const https = require('https')
8-
const each = require('async/each')
9-
const waterfall = require('async/waterfall')
105
const parallel = require('async/parallel')
6+
const { echoUrl, redirectUrl } = require('../utils/echo-http-server')
117

128
module.exports = (createCommon, options) => {
139
const describe = getDescribe(options)
@@ -23,10 +19,8 @@ module.exports = (createCommon, options) => {
2319
// CI takes longer to instantiate the daemon, so we need to increase the
2420
// timeout for the before step
2521
this.timeout(60 * 1000)
26-
2722
common.setup((err, factory) => {
2823
expect(err).to.not.exist()
29-
3024
factory.spawnNode((err, node) => {
3125
expect(err).to.not.exist()
3226
ipfs = node
@@ -37,260 +31,97 @@ module.exports = (createCommon, options) => {
3731

3832
after((done) => common.teardown(done))
3933

40-
let testServers = []
41-
42-
const sslOpts = fixtures.sslOpts
43-
44-
const startTestServer = (handler, opts, cb) => {
45-
if (typeof opts === 'function') {
46-
cb = opts
47-
opts = {}
48-
}
49-
50-
const server = opts.secure
51-
? https.createServer(sslOpts, handler)
52-
: http.createServer(handler)
53-
54-
server.listen((err) => {
55-
if (err) return cb(err)
56-
testServers.push(server)
57-
cb(null, server)
58-
})
59-
}
60-
61-
beforeEach(() => {
62-
// Instructs node to not reject our snake oil SSL certificate when it
63-
// can't verify the certificate authority
64-
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
65-
})
66-
67-
afterEach((done) => {
68-
// Reinstate unauthorised SSL cert rejection
69-
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1
70-
71-
each(testServers, (server, cb) => server.close(cb), (err) => {
72-
testServers = []
73-
done(err)
74-
})
75-
})
76-
7734
it('should add from a HTTP URL', (done) => {
78-
const data = Buffer.from(`TEST${Date.now()}`)
79-
35+
const text = `TEST${Date.now()}`
36+
const url = echoUrl(text)
8037
parallel({
81-
server: (cb) => {
82-
const handler = (req, res) => {
83-
res.write(data)
84-
res.end()
85-
}
86-
startTestServer(handler, cb)
87-
},
88-
expectedResult: (cb) => ipfs.add(data, cb)
89-
}, (err, taskResult) => {
38+
result: (cb) => ipfs.addFromURL(url, cb),
39+
expectedResult: (cb) => ipfs.add(Buffer.from(text), cb)
40+
}, (err, { result, expectedResult }) => {
9041
expect(err).to.not.exist()
91-
const { server, expectedResult } = taskResult
92-
93-
const url = `http://127.0.0.1:${server.address().port}/`
94-
ipfs.addFromURL(url, (err, result) => {
95-
expect(err).to.not.exist()
96-
expect(result).to.deep.equal(expectedResult)
97-
done()
98-
})
99-
})
100-
})
101-
102-
it('should add from a HTTPS URL', (done) => {
103-
const data = Buffer.from(`TEST${Date.now()}`)
104-
105-
parallel({
106-
server: (cb) => {
107-
const handler = (req, res) => {
108-
res.write(data)
109-
res.end()
110-
}
111-
startTestServer(handler, { secure: true }, cb)
112-
},
113-
expectedResult: (cb) => ipfs.add(data, cb)
114-
}, (err, taskResult) => {
115-
expect(err).to.not.exist()
116-
const { server, expectedResult } = taskResult
117-
118-
const url = `https://127.0.0.1:${server.address().port}/`
119-
ipfs.addFromURL(url, (err, result) => {
120-
expect(err).to.not.exist()
121-
expect(result).to.deep.equal(expectedResult)
122-
done()
123-
})
42+
expect(result.err).to.not.exist()
43+
expect(expectedResult.err).to.not.exist()
44+
expect(result[0].hash).to.equal(expectedResult[0].hash)
45+
expect(result[0].size).to.equal(expectedResult[0].size)
46+
expect(result[0].path).to.equal(text)
47+
done()
12448
})
12549
})
12650

12751
it('should add from a HTTP URL with redirection', (done) => {
128-
const data = Buffer.from(`TEST${Date.now()}`)
129-
130-
waterfall([
131-
(cb) => {
132-
const handler = (req, res) => {
133-
res.write(data)
134-
res.end()
135-
}
136-
startTestServer(handler, cb)
137-
},
138-
(serverA, cb) => {
139-
const url = `http://127.0.0.1:${serverA.address().port}`
140-
const handler = (req, res) => {
141-
res.statusCode = 302
142-
res.setHeader('Location', url)
143-
res.end()
144-
}
145-
startTestServer(handler, (err, serverB) => {
146-
if (err) return cb(err)
147-
cb(null, { a: serverA, b: serverB })
148-
})
149-
}
150-
], (err, servers) => {
151-
expect(err).to.not.exist()
152-
153-
ipfs.add(data, (err, res) => {
154-
expect(err).to.not.exist()
155-
156-
const expectedHash = res[0].hash
157-
const url = `http://127.0.0.1:${servers.b.address().port}`
52+
const text = `TEST${Date.now()}`
53+
const url = echoUrl(text) + '?foo=bar#buzz'
15854

159-
ipfs.addFromURL(url, (err, result) => {
160-
expect(err).to.not.exist()
161-
expect(result[0].hash).to.equal(expectedHash)
162-
done()
163-
})
164-
})
165-
})
166-
})
167-
168-
it('should add from a HTTPS URL with redirection', (done) => {
169-
const data = Buffer.from(`TEST${Date.now()}`)
170-
171-
waterfall([
172-
(cb) => {
173-
const handler = (req, res) => {
174-
res.write(data)
175-
res.end()
176-
}
177-
startTestServer(handler, { secure: true }, cb)
178-
},
179-
(serverA, cb) => {
180-
const url = `https://127.0.0.1:${serverA.address().port}`
181-
const handler = (req, res) => {
182-
res.statusCode = 302
183-
res.setHeader('Location', url)
184-
res.end()
185-
}
186-
startTestServer(handler, { secure: true }, (err, serverB) => {
187-
if (err) return cb(err)
188-
cb(null, { a: serverA, b: serverB })
189-
})
190-
}
191-
], (err, servers) => {
55+
parallel({
56+
result: (cb) => ipfs.addFromURL(redirectUrl(url), cb),
57+
expectedResult: (cb) => ipfs.add(Buffer.from(text), cb)
58+
}, (err, { result, expectedResult }) => {
19259
expect(err).to.not.exist()
193-
194-
ipfs.add(data, (err, res) => {
195-
expect(err).to.not.exist()
196-
197-
const expectedHash = res[0].hash
198-
const url = `https://127.0.0.1:${servers.b.address().port}`
199-
200-
ipfs.addFromURL(url, (err, result) => {
201-
expect(err).to.not.exist()
202-
expect(result[0].hash).to.equal(expectedHash)
203-
done()
204-
})
205-
})
60+
expect(result.err).to.not.exist()
61+
expect(expectedResult.err).to.not.exist()
62+
expect(result[0].hash).to.equal(expectedResult[0].hash)
63+
expect(result[0].size).to.equal(expectedResult[0].size)
64+
expect(result[0].path).to.equal(text)
65+
done()
20666
})
20767
})
20868

20969
it('should add from a URL with only-hash=true', (done) => {
210-
const handler = (req, res) => {
211-
res.write(`TEST${Date.now()}`)
212-
res.end()
213-
}
214-
215-
startTestServer(handler, (err, server) => {
70+
const text = `TEST${Date.now()}`
71+
const url = echoUrl(text)
72+
ipfs.addFromURL(url, { onlyHash: true }, (err, res) => {
21673
expect(err).to.not.exist()
21774

218-
const url = `http://127.0.0.1:${server.address().port}/`
219-
220-
ipfs.addFromURL(url, { onlyHash: true }, (err, res) => {
221-
expect(err).to.not.exist()
222-
223-
// A successful object.get for this size data took my laptop ~14ms
224-
let didTimeout = false
225-
const timeoutId = setTimeout(() => {
226-
didTimeout = true
227-
done()
228-
}, 500)
75+
// A successful object.get for this size data took my laptop ~14ms
76+
let didTimeout = false
77+
const timeoutId = setTimeout(() => {
78+
didTimeout = true
79+
done()
80+
}, 500)
22981

230-
ipfs.object.get(res[0].hash, () => {
231-
clearTimeout(timeoutId)
232-
if (didTimeout) return
233-
expect(new Error('did not timeout')).to.not.exist()
234-
})
82+
ipfs.object.get(res[0].hash, () => {
83+
clearTimeout(timeoutId)
84+
if (didTimeout) return
85+
expect(new Error('did not timeout')).to.not.exist()
23586
})
23687
})
23788
})
23889

23990
it('should add from a URL with wrap-with-directory=true', (done) => {
240-
const filename = `TEST${Date.now()}.txt`
241-
const data = Buffer.from(`TEST${Date.now()}`)
242-
91+
const filename = `TEST${Date.now()}.txt` // also acts as data
92+
const url = echoUrl(filename) + '?foo=bar#buzz'
93+
const addOpts = { wrapWithDirectory: true }
24394
parallel({
244-
server: (cb) => startTestServer((req, res) => {
245-
res.write(data)
246-
res.end()
247-
}, cb),
248-
expectedResult: (cb) => {
249-
ipfs.add([{ path: filename, content: data }], { wrapWithDirectory: true }, cb)
250-
}
251-
}, (err, taskResult) => {
95+
result: (cb) => ipfs.addFromURL(url, addOpts, cb),
96+
expectedResult: (cb) => ipfs.add([{ path: filename, content: Buffer.from(filename) }], addOpts, cb)
97+
}, (err, { result, expectedResult }) => {
25298
expect(err).to.not.exist()
253-
254-
const { server, expectedResult } = taskResult
255-
const url = `http://127.0.0.1:${server.address().port}/${filename}?foo=bar#buzz`
256-
257-
ipfs.addFromURL(url, { wrapWithDirectory: true }, (err, result) => {
258-
expect(err).to.not.exist()
259-
expect(result).to.deep.equal(expectedResult)
260-
done()
261-
})
99+
expect(result.err).to.not.exist()
100+
expect(expectedResult.err).to.not.exist()
101+
expect(result).to.deep.equal(expectedResult)
102+
done()
262103
})
263104
})
264105

265106
it('should add from a URL with wrap-with-directory=true and URL-escaped file name', (done) => {
266-
const filename = '320px-Domažlice,_Jiráskova_43_(9102).jpg'
267-
const data = Buffer.from(`TEST${Date.now()}`)
268-
107+
const filename = `320px-Domažlice,_Jiráskova_43_(${Date.now()}).jpg` // also acts as data
108+
const url = echoUrl(filename) + '?foo=bar#buzz'
109+
const addOpts = { wrapWithDirectory: true }
269110
parallel({
270-
server: (cb) => startTestServer((req, res) => {
271-
res.write(data)
272-
res.end()
273-
}, cb),
274-
expectedResult: (cb) => {
275-
ipfs.add([{ path: filename, content: data }], { wrapWithDirectory: true }, cb)
276-
}
277-
}, (err, taskResult) => {
111+
result: (cb) => ipfs.addFromURL(url, addOpts, cb),
112+
expectedResult: (cb) => ipfs.add([{ path: filename, content: Buffer.from(filename) }], addOpts, cb)
113+
}, (err, { result, expectedResult }) => {
278114
expect(err).to.not.exist()
279-
280-
const { server, expectedResult } = taskResult
281-
const url = `http://127.0.0.1:${server.address().port}/${encodeURIComponent(filename)}?foo=bar#buzz`
282-
283-
ipfs.addFromURL(url, { wrapWithDirectory: true }, (err, result) => {
284-
expect(err).to.not.exist()
285-
expect(result).to.deep.equal(expectedResult)
286-
done()
287-
})
115+
expect(result.err).to.not.exist()
116+
expect(expectedResult.err).to.not.exist()
117+
expect(result).to.deep.equal(expectedResult)
118+
done()
288119
})
289120
})
290121

291122
it('should not add from an invalid url', (done) => {
292123
ipfs.addFromURL('http://invalid', (err, result) => {
293-
expect(err.code).to.equal('ENOTFOUND')
124+
expect(err).to.exist()
294125
expect(result).to.not.exist()
295126
done()
296127
})

src/files-regular/utils.js

-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,5 @@ exports.fixtures = Object.freeze({
2121
bigFile: Object.freeze({
2222
cid: 'Qme79tX2bViL26vNjPsF3DP1R9rMKMvnPYJiKTTKPrXJjq',
2323
data: loadFixture('test/fixtures/15mb.random', 'interface-ipfs-core')
24-
}),
25-
sslOpts: Object.freeze({
26-
key: loadFixture('test/fixtures/ssl/privkey.pem', 'interface-ipfs-core'),
27-
cert: loadFixture('test/fixtures/ssl/cert.pem', 'interface-ipfs-core')
2824
})
2925
})

0 commit comments

Comments
 (0)