This repository was archived by the owner on Feb 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy path4.opts.test.js
107 lines (91 loc) · 2.57 KB
/
4.opts.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
/* global describe, it */
'use strict'
const request = require('supertest')
const bodyParser = require('body-parser')
const expect = require('chai').expect
const pump = require('pump')
const queryString = require('node:querystring')
let gateway, service, close, proxy, gHttpServer
const nock = require('nock')
nock('http://dev.com')
.get('/service/headers?age=33')
.reply(200, {
}, {
url: 'http://dev.com'
})
describe('fast-proxy smoke', () => {
it('init', async () => {
const fastProxy = require('../index')({
base: 'http://127.0.0.1:3000',
queryString
})
close = fastProxy.close
proxy = fastProxy.proxy
expect(proxy instanceof Function).to.equal(true)
expect(close instanceof Function).to.equal(true)
})
it('init & start gateway', async () => {
// init gateway
gateway = require('restana')()
gateway.all('/service/*', function (req, res) {
proxy(req, res, req.url, {
base: req.headers.base,
queryString: { age: 33 },
onResponse (req, res, stream) {
pump(stream, res)
}
})
})
gHttpServer = await gateway.start(8080)
})
it('init & start remote service', async () => {
// init remote service
service = require('restana')()
service.use(bodyParser.json())
service
.get('/service/headers', (req, res) => {
res.setHeader('url', req.url)
res.send()
})
.get('/service/302', (req, res) => {
res.statusCode = 302
res.end()
})
await service.start(3000)
})
it('should 200 on GET /servive/headers + opts', async () => {
await request(gHttpServer)
.get('/service/headers')
.expect(200)
.then((response) => {
expect(response.headers.url).to.equal('/service/headers?age=33')
})
})
it('should overwrite global base', async () => {
await request(gHttpServer)
.get('/service/headers')
.set('base', 'http://localhost:3000')
.expect(200)
.then((response) => {
expect(response.headers.url).to.equal('/service/headers?age=33')
})
})
it('should overwrite global base 2', async () => {
await request(gHttpServer)
.get('/service/headers')
.set('base', 'http://dev.com')
.then((response) => {
expect(response.headers.url).to.equal('http://dev.com')
})
})
it('should retrieve http status from origin', async () => {
await request(gHttpServer)
.get('/service/302')
.expect(302)
})
it('close all', async () => {
close()
await gateway.close()
await service.close()
})
})