-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
127 lines (107 loc) · 3.91 KB
/
tests.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
/* eslint no-undef:0, import/no-extraneous-dependencies:0,
import/no-unresolved:0, global-require:0 */
const HydraHttpPlugin = require('./index').HydraHttpPlugin
const expect = require('chai').expect
const hydra = require('hydra')
describe('Hydra HTTP plugin', () => {
it('init hydra', async () => {
hydra.use(new HydraHttpPlugin())
await hydra.init({
hydra: {
serviceName: 'hydra-router',
serviceDescription: 'Native service on top of Hydra',
serviceIP: '127.0.0.1',
servicePort: 3000,
serviceType: 'native',
serviceVersion: '1.0.0',
redis: {
host: '127.0.0.1',
port: 6379,
db: 15
},
plugins: {
'hydra-plugin-http': {
lb: {
strategy: {
name: 'race', // strategy name
timeout: 3000, // call timeout
nodes: 3, // number of nodes to call
healthPath: '_health' // health check endpoint, for example: http://127.0.0.1:3000/_health
}
}
}
}
}
})
await hydra.registerService()
})
it('lb.translate should succeed', async () => {
const baseUrl = await hydra.http.lb.translate('express-service-test')
expect(baseUrl).to.equal('http://127.0.0.1:3000')
})
it('lb.translate should fail', async () => {
try {
await hydra.http.lb.translate('unavailable-service')
} catch (err) {
expect(err instanceof Error).to.equal(true)
}
})
it('request should succeed - http://127.0.0.1:3000/v1/welcome', async () => {
const res = await hydra.http.request('http://127.0.0.1:3000/v1/welcome')
expect(res.status).to.equal(200)
})
it('request should succeed - /express-service-test/v1/welcome', async () => {
const res = await hydra.http.request('/express-service-test/v1/welcome')
expect(res.status).to.equal(200)
})
it('request should succeed - /v1/welcome', async () => {
const res = await hydra.http.request('/v1/welcome')
expect(res.status).to.equal(200)
})
it('request should fail - /v1/welcome222', async () => {
try {
await hydra.http.request('/v1/welcome222')
} catch (err) {
expect(err instanceof Error).to.equal(true)
}
})
it('proxy.findService should succeed - /v1/welcome?age=31', async () => {
const res = await hydra.http.proxy.findService('/v1/welcome?age=31', 'get')
expect(res.name).to.equal('express-service-test')
expect(res.path).to.equal('/v1/welcome?age=31')
})
it('proxy.findService should succeed - /express-service-test/v1/welcome', async () => {
const res = await hydra.http.proxy.findService('/express-service-test/v1/welcome', 'get')
expect(res.name).to.equal('express-service-test')
})
it('proxy.findService should fail - /express-service-test/v1/welcome222', async () => {
const res = await hydra.http.proxy.findService('/express-service-test/v1/welcome222', 'get')
expect(res).to.equal(null)
})
it('proxy.findService should fail - /unexisting-service/v1/welcome', async () => {
const res = await hydra.http.proxy.findService('/unexisting-service/v1/welcome', 'get')
expect(res).to.equal(null)
})
it('proxy.findService should fail - /v1/welcome222', async () => {
const res = await hydra.http.proxy.findService('/v1/welcome222', 'get')
expect(res).to.equal(null)
})
it('proxy.attach', async () => {
const http = require('http')
const server = http.createServer()
hydra.http.proxy.attach(server)
server.listen(5050)
let res = await hydra.http.request('http://localhost:5050/v1/welcome')
expect(res.status).to.equal(200)
expect(res.data).to.equal('Hello World!')
try {
res = await hydra.http.request('http://localhost:5050/v1/welcome222')
} catch (err) {
expect(err.response.status).to.equal(503)
}
server.close()
})
it('shutdown hydra', async () => {
hydra.shutdown()
})
})