1+ import bodyParser from "body-parser"
12import * as express from "express"
23import * as httpserver from "./httpserver"
34import * as integration from "./integration"
45
56describe ( "proxy" , ( ) => {
6- let codeServer : httpserver . HttpServer | undefined
77 const nhooyrDevServer = new httpserver . HttpServer ( )
8+ let codeServer : httpserver . HttpServer | undefined
89 let proxyPath : string
10+ let e : express . Express
911
1012 beforeAll ( async ( ) => {
11- const e = express . default ( )
12- await nhooyrDevServer . listen ( e )
13- e . get ( "/wsup" , ( req , res ) => {
14- res . json ( "asher is the best" )
13+ await nhooyrDevServer . listen ( ( req , res ) => {
14+ e ( req , res )
1515 } )
1616 proxyPath = `/proxy/${ nhooyrDevServer . port ( ) } /wsup`
17- e . get ( proxyPath , ( req , res ) => {
18- res . json ( "joe is the best" )
19- } )
2017 } )
2118
2219 afterAll ( async ( ) => {
2320 await nhooyrDevServer . close ( )
2421 } )
2522
23+ beforeEach ( ( ) => {
24+ e = express . default ( )
25+ } )
26+
2627 afterEach ( async ( ) => {
2728 if ( codeServer ) {
2829 await codeServer . close ( )
@@ -31,6 +32,9 @@ describe("proxy", () => {
3132 } )
3233
3334 it ( "should rewrite the base path" , async ( ) => {
35+ e . get ( "/wsup" , ( req , res ) => {
36+ res . json ( "asher is the best" )
37+ } )
3438 ; [ , , codeServer ] = await integration . setup ( [ "--auth=none" ] , "" )
3539 const resp = await codeServer . fetch ( proxyPath )
3640 expect ( resp . status ) . toBe ( 200 )
@@ -39,10 +43,61 @@ describe("proxy", () => {
3943 } )
4044
4145 it ( "should not rewrite the base path" , async ( ) => {
46+ e . get ( proxyPath , ( req , res ) => {
47+ res . json ( "joe is the best" )
48+ } )
4249 ; [ , , codeServer ] = await integration . setup ( [ "--auth=none" , "--proxy-path-passthrough=true" ] , "" )
4350 const resp = await codeServer . fetch ( proxyPath )
4451 expect ( resp . status ) . toBe ( 200 )
4552 const json = await resp . json ( )
4653 expect ( json ) . toBe ( "joe is the best" )
4754 } )
55+
56+ it ( "should rewrite redirects" , async ( ) => {
57+ e . post ( "/wsup" , ( req , res ) => {
58+ res . redirect ( 307 , "/finale" )
59+ } )
60+ e . post ( "/finale" , ( req , res ) => {
61+ res . json ( "redirect success" )
62+ } )
63+ ; [ , , codeServer ] = await integration . setup ( [ "--auth=none" ] , "" )
64+ const resp = await codeServer . fetch ( proxyPath , {
65+ method : "POST" ,
66+ } )
67+ expect ( resp . status ) . toBe ( 200 )
68+ expect ( await resp . json ( ) ) . toBe ( "redirect success" )
69+ } )
70+
71+ it ( "should not rewrite redirects" , async ( ) => {
72+ const finalePath = proxyPath . replace ( "/wsup" , "/finale" )
73+ e . post ( proxyPath , ( req , res ) => {
74+ res . redirect ( 307 , finalePath )
75+ } )
76+ e . post ( finalePath , ( req , res ) => {
77+ res . json ( "redirect success" )
78+ } )
79+ ; [ , , codeServer ] = await integration . setup ( [ "--auth=none" , "--proxy-path-passthrough=true" ] , "" )
80+ const resp = await codeServer . fetch ( proxyPath , {
81+ method : "POST" ,
82+ } )
83+ expect ( resp . status ) . toBe ( 200 )
84+ expect ( await resp . json ( ) ) . toBe ( "redirect success" )
85+ } )
86+
87+ it ( "should allow post bodies" , async ( ) => {
88+ e . use ( bodyParser . json ( { strict : false } ) )
89+ e . post ( "/wsup" , ( req , res ) => {
90+ res . json ( req . body )
91+ } )
92+ ; [ , , codeServer ] = await integration . setup ( [ "--auth=none" ] , "" )
93+ const resp = await codeServer . fetch ( proxyPath , {
94+ method : "post" ,
95+ body : JSON . stringify ( "coder is the best" ) ,
96+ headers : {
97+ "Content-Type" : "application/json" ,
98+ } ,
99+ } )
100+ expect ( resp . status ) . toBe ( 200 )
101+ expect ( await resp . json ( ) ) . toBe ( "coder is the best" )
102+ } )
48103} )
0 commit comments