-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
192 lines (162 loc) · 5.82 KB
/
test.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
import os from 'os'
import fsProm from 'fs/promises'
import path from 'path'
import { expect } from 'chai'
import ram from 'random-access-memory'
import axios from 'axios'
import createTestnet from 'hyperdht/testnet.js'
import Hyperswarm from 'hyperswarm'
import Corestore from 'corestore'
import Rehoster from 'hypercore-rehoster'
import setupRehostServer from './lib/server.js'
import SwarmManager from 'swarm-manager'
import safetyCatch from 'safety-catch'
describe('Rehost server tests', function () {
let app
let testnet, swarm, swarmManager
let url
let rehoster
const key = 'a'.repeat(64)
this.beforeEach(async function () {
testnet = await createTestnet(3)
const bootstrap = testnet.bootstrap
swarm = new Hyperswarm({ bootstrap })
const corestore = new Corestore(ram)
swarmManager = new SwarmManager(swarm)
swarm.on('connection', socket => {
corestore.replicate(socket)
corestore.on('error', safetyCatch)
})
rehoster = new Rehoster(corestore, swarmManager)
})
this.afterEach(async function () {
if (app) await app.close()
// Need to clear the metrics, because without clearing
// the server crashes on the second test, attempting
// to re-register the metrics
app._promClientRegister.clear()
await swarmManager.close()
await testnet.destroy()
})
it('Can put key with info', async function () {
app = await setupRehostServer(rehoster, { logger: false, host: '127.0.0.1' })
url = `http://127.0.0.1:${app.server.address().port}/`
const putRes = await axios.put(`${url}${key}`, { info: 'A key' })
expect(putRes.status).to.equal(200)
const getRes = await axios.get(url)
expect(getRes.status).to.equal(200)
expect(getRes.data).to.deep.equal([{
key, info: 'A key'
}])
})
it('can use the api', async function () {
app = await setupRehostServer(rehoster, { logger: false, host: '127.0.0.1' })
url = `http://127.0.0.1:${app.server.address().port}/`
let res = await axios.get(url)
expect(res.status).to.equal(200)
expect(res.data).to.deep.equal([])
res = await axios.put(`${url}${key}`, {})
expect(res.status).to.equal(200)
res = await axios.get(url)
expect(res.status).to.equal(200)
expect(res.data).to.deep.equal([{ key }])
res = await axios.delete(`${url}${key}`)
expect(res.status).to.equal(204)
// Note: not usually necessary to wait
// but saw it fail once
await new Promise((resolve) => setTimeout(resolve, 100))
res = await axios.get(url)
expect(res.status).to.equal(200)
expect(res.data).to.deep.equal([])
res = await axios.get(`${url}info`)
expect(res.status).to.equal(200)
expect(Object.keys(res.data)).to.deep.have.same.members(['info', 'details'])
})
it('Can access metrics', async function () {
app = await setupRehostServer(rehoster, { logger: false, host: '127.0.0.1' })
url = `http://127.0.0.1:${app.server.address().port}/`
const res = await axios.get(`${url}metrics`)
expect(res.status).to.equal(200)
})
it('Can access health', async function () {
app = await setupRehostServer(rehoster, { logger: false, host: '127.0.0.1' })
url = `http://127.0.0.1:${app.server.address().port}/`
const res = await axios.get(`${url}health`)
expect(res.status).to.equal(200)
})
it('can update from a config file', async function () {
const workDir = path.join(os.tmpdir(), Math.random().toString().slice(2))
await fsProm.mkdir(workDir)
const key1 = 'a'.repeat(64)
const key2 = 'b'.repeat(64)
try {
const configPath = path.join(workDir, 'config.json')
await fsProm.writeFile(
configPath,
JSON.stringify({
[key1]: {},
[key2]: { info: 'the b key' }
})
)
app = await setupRehostServer(rehoster, {
logger: false,
host: '127.0.0.1',
configPath
})
url = `http://127.0.0.1:${app.server.address().port}/`
{
// Applies init config
const res = await axios.get(url)
expect(res.status).to.equal(200)
expect(res.data).to.deep.equal([
{ key: key1 },
{ key: key2, info: 'the b key' }
])
}
await fsProm.writeFile(
configPath,
JSON.stringify({ [key1]: {} })
)
{
const res = await axios.post(`${url}sync`, {})
expect(res.status).to.equal(200)
}
{
// Synced with the new config
const res = await axios.get(url)
expect(res.status).to.equal(200)
expect(res.data).to.deep.equal([
{ key: key1 }
])
}
} finally {
await fsProm.rm(workDir, { recursive: true })
}
})
it('sync returns 400 if no configFile specified', async function () {
app = await setupRehostServer(rehoster, { logger: false, host: '127.0.0.1' })
url = `http://127.0.0.1:${app.server.address().port}/`
const res = await axios.post(`${url}sync`, {}, { validateStatus: false })
expect(res.status).to.equal(400)
expect(res.data).to.equal('No config path was specified during server startup')
})
it('Returns 400 if the configFile does not exist', async function () {
const workDir = path.join(os.tmpdir(), Math.random().toString().slice(2))
await fsProm.mkdir(workDir)
try {
const configPath = path.join(workDir, 'config.json')
await fsProm.writeFile(
configPath,
'{}'
)
app = await setupRehostServer(rehoster, { logger: false, host: '127.0.0.1', configPath })
url = `http://127.0.0.1:${app.server.address().port}/`
await fsProm.rm(configPath)
const res = await axios.post(`${url}sync`, {}, { validateStatus: false })
expect(res.status).to.equal(400)
expect(res.data).to.include('Could not apply the config at')
} finally {
await fsProm.rm(workDir, { recursive: true })
}
})
})