This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
218 lines (196 loc) · 6.35 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const stream = require('stream')
const test = require('ava')
const koa = require('koa')
const axios = require('axios')
const tunnelAgent = require('tunnel-agent')
const kroxy = require('.')
const generalKroxyApp = (() => {
const app = koa()
app.use(kroxy())
return app
})()
const axiosServer = ({ context }, app = generalKroxyApp) =>
new Promise(resolve => {
const server = app.listen(() => resolve(axios.create({
baseURL: 'http://httpbin.org/',
proxy: {
host: '127.0.0.1',
port: server.address().port
}
})))
server.on('connect', kroxy.tunnel())
context.server = server
})
test.afterEach(({ context: { server } }) => server && server.close())
test('Method', t => axiosServer(t)
.then(axios => axios.patch('/patch'))
.then(response => t.is(response.status, 200)))
test('Query String', t => axiosServer(t)
.then(axios => axios.get('/get', { params: { foo: 'bar' } })
.then(response => t.is(response.data.args.foo, 'bar'))))
test('Request Header', t => axiosServer(t)
.then(axios => axios.get('/headers', { headers: { 'x-foo': 'bar' } })
.then(response => t.is(response.data.headers['X-Foo'], 'bar'))))
test('Request Body', t => axiosServer(t)
.then(axios => axios.post('/post', { foo: 'bar' }))
.then(response => t.is(response.data.json.foo, 'bar')))
test('Status Code', t => axiosServer(t)
.then(axios => axios.get('/status/418'))
.catch(error => t.is(error.response.status, 418)))
test('Response Header', t => axiosServer(t)
.then(axios => axios.get('/response-headers', { params: { 'x-foo': 'bar' } }))
.then(response => t.is(response.headers['x-foo'], 'bar')))
test('general http request', t => {
const app = koa()
app.use(kroxy())
app.use(function * () { this.body = 'foo' })
return axiosServer(t, app)
.then(axios => {
const port = t.context.server.address().port
return axios.get(`http://localhost:${port}/`)
})
.then((response) => t.is(response.data, 'foo'))
})
test('http tunnel', t => axiosServer(t)
.then(() => axios.get('https://httpbin.org/', {
httpsAgent: tunnelAgent.httpsOverHttp({
proxy: {
host: '127.0.0.1',
port: t.context.server.address().port
}
})
}).then(() => t.pass())))
test('with untrusted request body', t => {
const app = koa()
app.use(function * (next) {
delete this.request.headers['content-type']
this.request.body = 'foo'
yield next
})
app.use(kroxy())
return axiosServer(t, app)
.then(axios => axios.post('/post', 'bar=1')
.then(response => t.not(response.data.data, 'foo')))
})
test('with request body (null)', t => {
const app = koa()
app.use(function * (next) {
this.request.body = null
yield next
})
app.use(kroxy({ trustRequestBody: true }))
return axiosServer(t, app)
.then(axios => axios.post('/post', 'bar=1')
.then(response => t.false('bar' in response.data.form)))
})
test('with request body (string)', t => {
const app = koa()
app.use(function * (next) {
delete this.request.headers['content-type']
this.request.body = 'foo'
yield next
})
app.use(kroxy({ trustRequestBody: true }))
return axiosServer(t, app)
.then(axios => axios.post('/post', 'bar=1')
.then(response => {
t.is(response.data.headers['Content-Type'], 'text/plain;charset=utf-8')
t.is(response.data.data, 'foo')
}))
})
test('with request body (string), keep content type', t => {
const app = koa()
app.use(function * (next) {
this.request.body = 'bar=2'
yield next
})
app.use(kroxy({ trustRequestBody: true }))
return axiosServer(t, app)
.then(axios => axios.post('/post', 'bar=1')
.then(response => {
t.is(response.data.form.bar, '2')
}))
})
test('with request body (buffer)', t => {
const app = koa()
app.use(function * (next) {
this.request.body = Buffer.from('bar=3', 'utf-8')
yield next
})
app.use(kroxy({ trustRequestBody: true }))
return axiosServer(t, app)
.then(axios => axios.post('/post', 'bar=1')
.then(response => t.is(response.data.form.bar, '3')))
})
test('with request body (stream)', t => {
const app = koa()
app.use(function * (next) {
this.request.body = new stream.PassThrough()
this.request.body.end('bar=4', 'utf-8')
yield next
})
app.use(kroxy({ trustRequestBody: true }))
return axiosServer(t, app)
.then(axios => axios.post('/post', 'bar=1')
.then(response => t.is(response.data.form.bar, '4')))
})
test('with request body (JSON)', t => {
const app = koa()
app.use(function * (next) {
delete this.request.headers['content-type']
this.request.body = { bar: 5 }
yield next
})
app.use(kroxy({ trustRequestBody: true }))
return axiosServer(t, app)
.then(axios => axios.post('/post', 'bar=1')
.then(response => t.is(response.data.json.bar, 5)))
})
test('with request body (JSON), keep content type', t => {
const app = koa()
app.use(function * (next) {
this.request.body = { bar: '=5' } // {"bar":"=5"}
yield next
})
app.use(kroxy({ trustRequestBody: true }))
return axiosServer(t, app)
.then(axios => axios.post('/post', 'bar=1')
.then(response => t.is(response.data.form['{"bar":"'], '5"}')))
})
test('do not parse response body', t => {
const app = koa()
app.use(function * (next) {
yield next
t.is(typeof this.response.body.pipe, 'function')
})
app.use(kroxy())
return axiosServer(t, app)
.then(axios => axios.post('/post', 'bar=1')
.then(response => t.is(response.data.form.bar, '1')))
})
test('parse and modify response body', t => {
const app = koa()
app.use(function * (next) {
yield next
this.response.body = JSON.parse(this.response.body.toString('utf-8'))
t.is(this.response.body.form.bar, '1')
this.response.body.form.bar = '2'
})
app.use(kroxy({ parseResponseBody: true }))
return axiosServer(t, app)
.then(axios => axios.post('/post', 'bar=1')
.then(response => t.is(response.data.form.bar, '2')))
})
test('parse gzipped response body', t => {
const app = koa()
app.use(function * (next) {
yield next
this.response.body = JSON.parse(this.response.body.toString('utf-8'))
t.falsy(this.response.get('content-encoding'),
'Remove Content-Encoding header')
t.true(this.response.body.gzipped)
})
app.use(kroxy({ parseResponseBody: true }))
return axiosServer(t, app)
.then(axios => axios.get('/gzip'))
})