@@ -5,6 +5,7 @@ const Fastify = require('fastify')
5
5
const proxy = require ( '.' )
6
6
const got = require ( 'got' )
7
7
const { Unauthorized } = require ( 'http-errors' )
8
+ const Transform = require ( 'stream' ) . Transform
8
9
9
10
async function run ( ) {
10
11
const origin = Fastify ( )
@@ -32,7 +33,7 @@ async function run () {
32
33
33
34
tearDown ( origin . close . bind ( origin ) )
34
35
35
- test ( 'basic proxy' , async ( t ) => {
36
+ test ( 'basic proxy' , async t => {
36
37
const server = Fastify ( )
37
38
server . register ( proxy , {
38
39
upstream : `http://localhost:${ origin . server . address ( ) . port } `
@@ -41,14 +42,18 @@ async function run () {
41
42
await server . listen ( 0 )
42
43
t . tearDown ( server . close . bind ( server ) )
43
44
44
- const resultRoot = await got ( `http://localhost:${ server . server . address ( ) . port } ` )
45
+ const resultRoot = await got (
46
+ `http://localhost:${ server . server . address ( ) . port } `
47
+ )
45
48
t . equal ( resultRoot . body , 'this is root' )
46
49
47
- const resultA = await got ( `http://localhost:${ server . server . address ( ) . port } /a` )
50
+ const resultA = await got (
51
+ `http://localhost:${ server . server . address ( ) . port } /a`
52
+ )
48
53
t . equal ( resultA . body , 'this is a' )
49
54
} )
50
55
51
- test ( 'no upstream will throw' , async ( t ) => {
56
+ test ( 'no upstream will throw' , async t => {
52
57
const server = Fastify ( )
53
58
server . register ( proxy )
54
59
try {
@@ -60,7 +65,7 @@ async function run () {
60
65
t . fail ( )
61
66
} )
62
67
63
- test ( 'prefixed proxy' , async ( t ) => {
68
+ test ( 'prefixed proxy' , async t => {
64
69
const server = Fastify ( )
65
70
server . register ( proxy , {
66
71
upstream : `http://localhost:${ origin . server . address ( ) . port } ` ,
@@ -70,17 +75,23 @@ async function run () {
70
75
await server . listen ( 0 )
71
76
t . tearDown ( server . close . bind ( server ) )
72
77
73
- const resultRoot = await got ( `http://localhost:${ server . server . address ( ) . port } /my-prefix/` )
78
+ const resultRoot = await got (
79
+ `http://localhost:${ server . server . address ( ) . port } /my-prefix/`
80
+ )
74
81
t . equal ( resultRoot . body , 'this is root' )
75
82
76
- const withoutSlash = await got ( `http://localhost:${ server . server . address ( ) . port } /my-prefix` )
83
+ const withoutSlash = await got (
84
+ `http://localhost:${ server . server . address ( ) . port } /my-prefix`
85
+ )
77
86
t . equal ( withoutSlash . body , 'this is root' )
78
87
79
- const resultA = await got ( `http://localhost:${ server . server . address ( ) . port } /my-prefix/a` )
88
+ const resultA = await got (
89
+ `http://localhost:${ server . server . address ( ) . port } /my-prefix/a`
90
+ )
80
91
t . equal ( resultA . body , 'this is a' )
81
92
} )
82
93
83
- test ( 'posting stuff' , async ( t ) => {
94
+ test ( 'posting stuff' , async t => {
84
95
const server = Fastify ( )
85
96
server . register ( proxy , {
86
97
upstream : `http://localhost:${ origin . server . address ( ) . port } `
@@ -89,14 +100,17 @@ async function run () {
89
100
await server . listen ( 0 )
90
101
t . tearDown ( server . close . bind ( server ) )
91
102
92
- const resultRoot = await got ( `http://localhost:${ server . server . address ( ) . port } /this-has-data` , {
93
- body : { hello : 'world' } ,
94
- json : true
95
- } )
103
+ const resultRoot = await got (
104
+ `http://localhost:${ server . server . address ( ) . port } /this-has-data` ,
105
+ {
106
+ body : { hello : 'world' } ,
107
+ json : true
108
+ }
109
+ )
96
110
t . deepEqual ( resultRoot . body , { something : 'posted' } )
97
111
} )
98
112
99
- test ( 'preHandler' , async ( t ) => {
113
+ test ( 'preHandler' , async t => {
100
114
const server = Fastify ( )
101
115
server . register ( proxy , {
102
116
upstream : `http://localhost:${ origin . server . address ( ) . port } ` ,
@@ -127,7 +141,7 @@ async function run () {
127
141
t . ok ( errored )
128
142
} )
129
143
130
- test ( 'beforeHandler(deprecated)' , async ( t ) => {
144
+ test ( 'beforeHandler(deprecated)' , async t => {
131
145
const server = Fastify ( )
132
146
server . register ( proxy , {
133
147
upstream : `http://localhost:${ origin . server . address ( ) . port } ` ,
@@ -158,7 +172,7 @@ async function run () {
158
172
t . ok ( errored )
159
173
} )
160
174
161
- test ( 'multiple prefixes with multiple plugins' , async ( t ) => {
175
+ test ( 'multiple prefixes with multiple plugins' , async t => {
162
176
const origin2 = Fastify ( )
163
177
164
178
origin2 . get ( '/' , async ( request , reply ) => {
@@ -188,14 +202,18 @@ async function run () {
188
202
proxyServer . close ( )
189
203
} )
190
204
191
- const firstProxyPrefix = await got ( `http://localhost:${ proxyServer . server . address ( ) . port } /api` )
205
+ const firstProxyPrefix = await got (
206
+ `http://localhost:${ proxyServer . server . address ( ) . port } /api`
207
+ )
192
208
t . equal ( firstProxyPrefix . body , 'this is root' )
193
209
194
- const secondProxyPrefix = await got ( `http://localhost:${ proxyServer . server . address ( ) . port } /api2` )
210
+ const secondProxyPrefix = await got (
211
+ `http://localhost:${ proxyServer . server . address ( ) . port } /api2`
212
+ )
195
213
t . equal ( secondProxyPrefix . body , 'this is root for origin2' )
196
214
} )
197
215
198
- test ( 'passes replyOptions object to reply.from() calls' , async ( t ) => {
216
+ test ( 'passes replyOptions object to reply.from() calls' , async t => {
199
217
const proxyServer = Fastify ( )
200
218
201
219
proxyServer . register ( proxy , {
@@ -212,11 +230,13 @@ async function run () {
212
230
proxyServer . close ( )
213
231
} )
214
232
215
- const { headers } = await got ( `http://localhost:${ proxyServer . server . address ( ) . port } /api` )
233
+ const { headers } = await got (
234
+ `http://localhost:${ proxyServer . server . address ( ) . port } /api`
235
+ )
216
236
t . match ( headers , { 'x-test' : 'test' } )
217
237
} )
218
238
219
- test ( 'rewritePrefix' , async ( t ) => {
239
+ test ( 'rewritePrefix' , async t => {
220
240
const proxyServer = Fastify ( )
221
241
222
242
proxyServer . register ( proxy , {
@@ -231,11 +251,13 @@ async function run () {
231
251
proxyServer . close ( )
232
252
} )
233
253
234
- const firstProxyPrefix = await got ( `http://localhost:${ proxyServer . server . address ( ) . port } /api/a` )
254
+ const firstProxyPrefix = await got (
255
+ `http://localhost:${ proxyServer . server . address ( ) . port } /api/a`
256
+ )
235
257
t . equal ( firstProxyPrefix . body , 'this is /api2/a' )
236
258
} )
237
259
238
- test ( 'rewrite location headers' , async ( t ) => {
260
+ test ( 'rewrite location headers' , async t => {
239
261
const proxyServer = Fastify ( )
240
262
241
263
proxyServer . register ( proxy , {
@@ -249,12 +271,52 @@ async function run () {
249
271
proxyServer . close ( )
250
272
} )
251
273
252
- const { headers : { location } } = await got ( `http://localhost:${ proxyServer . server . address ( ) . port } /api/this-has-data` , {
253
- body : { hello : 'world' } ,
254
- json : true
255
- } )
274
+ const {
275
+ headers : { location }
276
+ } = await got (
277
+ `http://localhost:${ proxyServer . server . address ( ) . port } /api/this-has-data` ,
278
+ {
279
+ body : { hello : 'world' } ,
280
+ json : true
281
+ }
282
+ )
256
283
t . equal ( location , '/api/something' )
257
284
} )
285
+
286
+ test ( 'passes onResponse option to reply.from() calls' , async t => {
287
+ const proxyServer = Fastify ( )
288
+
289
+ proxyServer . register ( proxy , {
290
+ upstream : `http://localhost:${ origin . server . address ( ) . port } ` ,
291
+ prefix : '/api' ,
292
+ replyOptions : {
293
+ onResponse ( request , reply , stream ) {
294
+ return reply . send (
295
+ stream . pipe (
296
+ new Transform ( {
297
+ transform : function ( chunk , enc , cb ) {
298
+ this . push ( chunk . toString ( ) . toUpperCase ( ) )
299
+ cb ( )
300
+ }
301
+ } )
302
+ )
303
+ )
304
+ }
305
+ }
306
+ } )
307
+
308
+ await proxyServer . listen ( 0 )
309
+
310
+ t . tearDown ( ( ) => {
311
+ proxyServer . close ( )
312
+ } )
313
+
314
+ const { body } = await got (
315
+ `http://localhost:${ proxyServer . server . address ( ) . port } /api`
316
+ )
317
+
318
+ t . match ( body , 'THIS IS ROOT' )
319
+ } )
258
320
}
259
321
260
322
run ( )
0 commit comments