@@ -5,6 +5,7 @@ const Fastify = require('fastify')
55const proxy = require ( '.' )
66const got = require ( 'got' )
77const { Unauthorized } = require ( 'http-errors' )
8+ const Transform = require ( 'stream' ) . Transform
89
910async function run ( ) {
1011 const origin = Fastify ( )
@@ -32,7 +33,7 @@ async function run () {
3233
3334 tearDown ( origin . close . bind ( origin ) )
3435
35- test ( 'basic proxy' , async ( t ) => {
36+ test ( 'basic proxy' , async t => {
3637 const server = Fastify ( )
3738 server . register ( proxy , {
3839 upstream : `http://localhost:${ origin . server . address ( ) . port } `
@@ -41,14 +42,18 @@ async function run () {
4142 await server . listen ( 0 )
4243 t . tearDown ( server . close . bind ( server ) )
4344
44- const resultRoot = await got ( `http://localhost:${ server . server . address ( ) . port } ` )
45+ const resultRoot = await got (
46+ `http://localhost:${ server . server . address ( ) . port } `
47+ )
4548 t . equal ( resultRoot . body , 'this is root' )
4649
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+ )
4853 t . equal ( resultA . body , 'this is a' )
4954 } )
5055
51- test ( 'no upstream will throw' , async ( t ) => {
56+ test ( 'no upstream will throw' , async t => {
5257 const server = Fastify ( )
5358 server . register ( proxy )
5459 try {
@@ -60,7 +65,7 @@ async function run () {
6065 t . fail ( )
6166 } )
6267
63- test ( 'prefixed proxy' , async ( t ) => {
68+ test ( 'prefixed proxy' , async t => {
6469 const server = Fastify ( )
6570 server . register ( proxy , {
6671 upstream : `http://localhost:${ origin . server . address ( ) . port } ` ,
@@ -70,17 +75,23 @@ async function run () {
7075 await server . listen ( 0 )
7176 t . tearDown ( server . close . bind ( server ) )
7277
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+ )
7481 t . equal ( resultRoot . body , 'this is root' )
7582
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+ )
7786 t . equal ( withoutSlash . body , 'this is root' )
7887
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+ )
8091 t . equal ( resultA . body , 'this is a' )
8192 } )
8293
83- test ( 'posting stuff' , async ( t ) => {
94+ test ( 'posting stuff' , async t => {
8495 const server = Fastify ( )
8596 server . register ( proxy , {
8697 upstream : `http://localhost:${ origin . server . address ( ) . port } `
@@ -89,14 +100,17 @@ async function run () {
89100 await server . listen ( 0 )
90101 t . tearDown ( server . close . bind ( server ) )
91102
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+ )
96110 t . deepEqual ( resultRoot . body , { something : 'posted' } )
97111 } )
98112
99- test ( 'preHandler' , async ( t ) => {
113+ test ( 'preHandler' , async t => {
100114 const server = Fastify ( )
101115 server . register ( proxy , {
102116 upstream : `http://localhost:${ origin . server . address ( ) . port } ` ,
@@ -127,7 +141,7 @@ async function run () {
127141 t . ok ( errored )
128142 } )
129143
130- test ( 'beforeHandler(deprecated)' , async ( t ) => {
144+ test ( 'beforeHandler(deprecated)' , async t => {
131145 const server = Fastify ( )
132146 server . register ( proxy , {
133147 upstream : `http://localhost:${ origin . server . address ( ) . port } ` ,
@@ -158,7 +172,7 @@ async function run () {
158172 t . ok ( errored )
159173 } )
160174
161- test ( 'multiple prefixes with multiple plugins' , async ( t ) => {
175+ test ( 'multiple prefixes with multiple plugins' , async t => {
162176 const origin2 = Fastify ( )
163177
164178 origin2 . get ( '/' , async ( request , reply ) => {
@@ -188,14 +202,18 @@ async function run () {
188202 proxyServer . close ( )
189203 } )
190204
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+ )
192208 t . equal ( firstProxyPrefix . body , 'this is root' )
193209
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+ )
195213 t . equal ( secondProxyPrefix . body , 'this is root for origin2' )
196214 } )
197215
198- test ( 'passes replyOptions object to reply.from() calls' , async ( t ) => {
216+ test ( 'passes replyOptions object to reply.from() calls' , async t => {
199217 const proxyServer = Fastify ( )
200218
201219 proxyServer . register ( proxy , {
@@ -212,11 +230,13 @@ async function run () {
212230 proxyServer . close ( )
213231 } )
214232
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+ )
216236 t . match ( headers , { 'x-test' : 'test' } )
217237 } )
218238
219- test ( 'rewritePrefix' , async ( t ) => {
239+ test ( 'rewritePrefix' , async t => {
220240 const proxyServer = Fastify ( )
221241
222242 proxyServer . register ( proxy , {
@@ -231,11 +251,13 @@ async function run () {
231251 proxyServer . close ( )
232252 } )
233253
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+ )
235257 t . equal ( firstProxyPrefix . body , 'this is /api2/a' )
236258 } )
237259
238- test ( 'rewrite location headers' , async ( t ) => {
260+ test ( 'rewrite location headers' , async t => {
239261 const proxyServer = Fastify ( )
240262
241263 proxyServer . register ( proxy , {
@@ -249,12 +271,52 @@ async function run () {
249271 proxyServer . close ( )
250272 } )
251273
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+ )
256283 t . equal ( location , '/api/something' )
257284 } )
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+ } )
258320}
259321
260322run ( )
0 commit comments