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

Commit a810adf

Browse files
committed
refactor: remove HTTPS server
This simplifies the way we use HTTP Echo Server by moving it to aegir hooks and removing HTTPS version. Context: #514 (comment) License: MIT Signed-off-by: Marcin Rataj <[email protected]>
1 parent b11151a commit a810adf

File tree

2 files changed

+25
-107
lines changed

2 files changed

+25
-107
lines changed

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

+15-73
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33

44
const { getDescribe, getIt, expect } = require('../utils/mocha')
55
const parallel = require('async/parallel')
6-
const EchoHttpServer = require('../utils/echo-http-server')
7-
const { isNode } = require('ipfs-utils/src/env')
8-
9-
const httpServer = EchoHttpServer.createServer()
10-
const httpsServer = EchoHttpServer.createServer({ secure: true })
6+
const { echoUrl, redirectUrl } = require('../utils/echo-http-server')
117

128
module.exports = (createCommon, options) => {
139
const describe = getDescribe(options)
@@ -23,54 +19,21 @@ 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-
// Instructs node to not reject our snake oil SSL certificate when it
27-
// can't verify the certificate authority
28-
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
29-
parallel([
30-
cb => common.setup((err, factory) => {
22+
common.setup((err, factory) => {
23+
expect(err).to.not.exist()
24+
factory.spawnNode((err, node) => {
3125
expect(err).to.not.exist()
32-
factory.spawnNode((err, node) => {
33-
expect(err).to.not.exist()
34-
ipfs = node
35-
cb()
36-
})
37-
}),
38-
cb => httpServer.start(cb),
39-
cb => httpsServer.start(cb)
40-
], done)
26+
ipfs = node
27+
done()
28+
})
29+
})
4130
})
4231

43-
after((done) => {
44-
// Reinstate unauthorised SSL cert rejection
45-
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 1
46-
parallel([
47-
cb => httpServer.stop(cb),
48-
cb => httpsServer.stop(cb),
49-
cb => common.teardown(cb)
50-
], done)
51-
})
32+
after((done) => common.teardown(done))
5233

5334
it('should add from a HTTP URL', (done) => {
5435
const text = `TEST${Date.now()}`
55-
const url = httpServer.echoUrl(text)
56-
parallel({
57-
result: (cb) => ipfs.addFromURL(url, cb),
58-
expectedResult: (cb) => ipfs.add(Buffer.from(text), cb)
59-
}, (err, { result, expectedResult }) => {
60-
expect(err).to.not.exist()
61-
expect(result.err).to.not.exist()
62-
expect(expectedResult.err).to.not.exist()
63-
expect(result[0].hash).to.equal(expectedResult[0].hash)
64-
expect(result[0].size).to.equal(expectedResult[0].size)
65-
expect(result[0].path).to.equal(text)
66-
done()
67-
})
68-
})
69-
70-
it('should add from a HTTPS URL', function (done) {
71-
if (!isNode) return this.skip() // unable to do self-signed HTTPS in browser
72-
const text = `TEST${Date.now()}`
73-
const url = httpsServer.echoUrl(text)
36+
const url = echoUrl(text)
7437
parallel({
7538
result: (cb) => ipfs.addFromURL(url, cb),
7639
expectedResult: (cb) => ipfs.add(Buffer.from(text), cb)
@@ -87,31 +50,10 @@ module.exports = (createCommon, options) => {
8750

8851
it('should add from a HTTP URL with redirection', (done) => {
8952
const text = `TEST${Date.now()}`
90-
const url = httpServer.echoUrl(text) + '?foo=bar#buzz'
91-
const redirectUrl = httpServer.redirectUrl(url)
92-
93-
parallel({
94-
result: (cb) => ipfs.addFromURL(redirectUrl, cb),
95-
expectedResult: (cb) => ipfs.add(Buffer.from(text), cb)
96-
}, (err, { result, expectedResult }) => {
97-
expect(err).to.not.exist()
98-
expect(result.err).to.not.exist()
99-
expect(expectedResult.err).to.not.exist()
100-
expect(result[0].hash).to.equal(expectedResult[0].hash)
101-
expect(result[0].size).to.equal(expectedResult[0].size)
102-
expect(result[0].path).to.equal(text)
103-
done()
104-
})
105-
})
106-
107-
it('should add from a HTTPS URL with redirection', function (done) {
108-
if (!isNode) return this.skip() // unable to do self-signed HTTPS in browser
109-
const text = `TEST${Date.now()}`
110-
const url = httpsServer.echoUrl(text) + '?foo=bar#buzz'
111-
const redirectUrl = httpsServer.redirectUrl(url)
53+
const url = echoUrl(text) + '?foo=bar#buzz'
11254

11355
parallel({
114-
result: (cb) => ipfs.addFromURL(redirectUrl, cb),
56+
result: (cb) => ipfs.addFromURL(redirectUrl(url), cb),
11557
expectedResult: (cb) => ipfs.add(Buffer.from(text), cb)
11658
}, (err, { result, expectedResult }) => {
11759
expect(err).to.not.exist()
@@ -126,7 +68,7 @@ module.exports = (createCommon, options) => {
12668

12769
it('should add from a URL with only-hash=true', (done) => {
12870
const text = `TEST${Date.now()}`
129-
const url = httpServer.echoUrl(text)
71+
const url = echoUrl(text)
13072
ipfs.addFromURL(url, { onlyHash: true }, (err, res) => {
13173
expect(err).to.not.exist()
13274

@@ -147,7 +89,7 @@ module.exports = (createCommon, options) => {
14789

14890
it('should add from a URL with wrap-with-directory=true', (done) => {
14991
const filename = `TEST${Date.now()}.txt` // also acts as data
150-
const url = httpServer.echoUrl(filename) + '?foo=bar#buzz'
92+
const url = echoUrl(filename) + '?foo=bar#buzz'
15193
const addOpts = { wrapWithDirectory: true }
15294
parallel({
15395
result: (cb) => ipfs.addFromURL(url, addOpts, cb),
@@ -163,7 +105,7 @@ module.exports = (createCommon, options) => {
163105

164106
it('should add from a URL with wrap-with-directory=true and URL-escaped file name', (done) => {
165107
const filename = `320px-Domažlice,_Jiráskova_43_(${Date.now()}).jpg` // also acts as data
166-
const url = httpServer.echoUrl(filename) + '?foo=bar#buzz'
108+
const url = echoUrl(filename) + '?foo=bar#buzz'
167109
const addOpts = { wrapWithDirectory: true }
168110
parallel({
169111
result: (cb) => ipfs.addFromURL(url, addOpts, cb),

src/utils/echo-http-server.js

+10-34
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,15 @@
22
'use strict'
33

44
const http = require('http')
5-
const https = require('https')
65
const URL = require('url').URL || self.URL
7-
const { isNode } = require('ipfs-utils/src/env')
86

9-
const loadFixture = require('aegir/fixtures')
10-
const sslOpts = Object.freeze({
11-
key: loadFixture('test/fixtures/ssl/privkey.pem', 'interface-ipfs-core'),
12-
cert: loadFixture('test/fixtures/ssl/cert.pem', 'interface-ipfs-core')
13-
})
14-
15-
const httpPort = 11080
16-
const httpsPort = 11443
7+
const defaultPort = 11080
178

189
// Create a mock of remote HTTP server that can return arbitrary text in response
1910
// or redirect to other URL. Used in tests of ipfs.addFromURL etc
20-
module.exports.createServer = (opts) => {
21-
const secure = opts && opts.secure
22-
const defaultPort = secure ? httpsPort : httpPort
23-
24-
// Web browser is not able to start HTTP server
25-
// We return noop here and start it from Node via .aegir.js/hooks/browser/pre|post instead (eg. in js-ipfs)
26-
if (!isNode) {
27-
const noopServer = {
28-
start: (cb) => cb(),
29-
stop: (cb) => cb(),
30-
url: () => `${secure ? 'https' : 'http'}://127.0.0.1:${defaultPort}`,
31-
echoUrl: (text) => `${noopServer.url()}/echo/${encodeURIComponent(text)}`,
32-
redirectUrl: (url) => `${noopServer.url()}/302/${encodeURI(url)}`
33-
}
34-
return Object.freeze(noopServer)
35-
}
36-
11+
// It needs to be available to tests run in browsers:
12+
// start it from Node via .aegir.js/hooks/browser/pre|post (example in js-ipfs)
13+
module.exports.createServer = () => {
3714
const handler = (req, res) => {
3815
// Relaxed CORS to enable use in tests in web browser with fetch
3916
res.setHeader('Access-Control-Allow-Origin', '*')
@@ -64,9 +41,7 @@ module.exports.createServer = (opts) => {
6441
res.end()
6542
}
6643

67-
const server = secure
68-
? https.createServer(sslOpts, handler)
69-
: http.createServer(handler)
44+
const server = http.createServer(handler)
7045

7146
server.start = (opts, cb) => {
7247
if (typeof opts === 'function') {
@@ -78,9 +53,10 @@ module.exports.createServer = (opts) => {
7853

7954
server.stop = (cb) => server.close(cb)
8055

81-
server.url = () => `${secure ? 'https' : 'http'}://127.0.0.1:${server.address().port}`
82-
server.echoUrl = (text) => `${server.url()}/echo/${encodeURIComponent(text)}`
83-
server.redirectUrl = (url) => `${server.url()}/302/${encodeURI(url)}`
84-
8556
return server
8657
}
58+
59+
module.exports.defaultPort = defaultPort
60+
module.exports.url = `http://127.0.0.1:${module.exports.defaultPort}`
61+
module.exports.echoUrl = (text) => `${module.exports.url}/echo/${encodeURIComponent(text)}`
62+
module.exports.redirectUrl = (url) => `${module.exports.url}/302/${encodeURI(url)}`

0 commit comments

Comments
 (0)