@@ -2,28 +2,29 @@ import * as assert from "assert"
2
2
import * as express from "express"
3
3
import * as httpserver from "./httpserver"
4
4
import * as integration from "./integration"
5
+ import bodyParser from "body-parser"
5
6
6
7
describe ( "proxy" , ( ) => {
7
- let codeServer : httpserver . HttpServer | undefined
8
8
const nhooyrDevServer = new httpserver . HttpServer ( )
9
+ let codeServer : httpserver . HttpServer | undefined
9
10
let proxyPath : string
11
+ let e : express . Express
10
12
11
13
before ( async ( ) => {
12
- const e = express . default ( )
13
- await nhooyrDevServer . listen ( e )
14
- e . get ( "/wsup" , ( req , res ) => {
15
- res . json ( "asher is the best" )
14
+ await nhooyrDevServer . listen ( ( req , res ) => {
15
+ e ( req , res )
16
16
} )
17
17
proxyPath = `/proxy/${ nhooyrDevServer . port ( ) } /wsup`
18
- e . get ( proxyPath , ( req , res ) => {
19
- res . json ( "joe is the best" )
20
- } )
21
18
} )
22
19
23
20
after ( async ( ) => {
24
21
await nhooyrDevServer . close ( )
25
22
} )
26
23
24
+ beforeEach ( ( ) => {
25
+ e = express . default ( )
26
+ } )
27
+
27
28
afterEach ( async ( ) => {
28
29
if ( codeServer ) {
29
30
await codeServer . close ( )
@@ -32,16 +33,70 @@ describe("proxy", () => {
32
33
} )
33
34
34
35
it ( "should rewrite the base path" , async ( ) => {
36
+ e . get ( "/wsup" , ( req , res ) => {
37
+ res . json ( "asher is the best" )
38
+ } )
35
39
; [ , , codeServer ] = await integration . setup ( [ "--auth=none" ] , "" )
36
40
const resp = await codeServer . fetch ( proxyPath )
37
41
assert . equal ( resp . status , 200 )
38
42
assert . equal ( await resp . json ( ) , "asher is the best" )
39
43
} )
40
44
41
45
it ( "should not rewrite the base path" , async ( ) => {
46
+ e . get ( proxyPath , ( req , res ) => {
47
+ res . json ( "joe is the best" )
48
+ } )
42
49
; [ , , codeServer ] = await integration . setup ( [ "--auth=none" , "--proxy-path-passthrough=true" ] , "" )
43
50
const resp = await codeServer . fetch ( proxyPath )
44
51
assert . equal ( resp . status , 200 )
45
52
assert . equal ( await resp . json ( ) , "joe is the best" )
46
53
} )
54
+
55
+ it ( "should rewrite redirects" , async ( ) => {
56
+ e . post ( "/wsup" , ( req , res ) => {
57
+ res . redirect ( 307 , "/finale" )
58
+ } )
59
+ e . post ( "/finale" , ( req , res ) => {
60
+ res . json ( "redirect success" )
61
+ } )
62
+ ; [ , , codeServer ] = await integration . setup ( [ "--auth=none" ] , "" )
63
+ const resp = await codeServer . fetch ( proxyPath , {
64
+ method : "POST" ,
65
+ } )
66
+ assert . equal ( resp . status , 200 )
67
+ assert . equal ( await resp . json ( ) , "redirect success" )
68
+ } )
69
+
70
+ it ( "should not rewrite redirects" , async ( ) => {
71
+ const finalePath = proxyPath . replace ( "/wsup" , "/finale" )
72
+ e . post ( proxyPath , ( req , res ) => {
73
+ res . redirect ( 307 , finalePath )
74
+ } )
75
+ e . post ( finalePath , ( req , res ) => {
76
+ res . json ( "redirect success" )
77
+ } )
78
+ ; [ , , codeServer ] = await integration . setup ( [ "--auth=none" , "--proxy-path-passthrough=true" ] , "" )
79
+ const resp = await codeServer . fetch ( proxyPath , {
80
+ method : "POST" ,
81
+ } )
82
+ assert . equal ( resp . status , 200 )
83
+ assert . equal ( await resp . json ( ) , "redirect success" )
84
+ } )
85
+
86
+ it ( "should allow post bodies" , async ( ) => {
87
+ e . use ( bodyParser . json ( { strict : false } ) )
88
+ e . post ( "/wsup" , ( req , res ) => {
89
+ res . json ( req . body )
90
+ } )
91
+ ; [ , , codeServer ] = await integration . setup ( [ "--auth=none" ] , "" )
92
+ const resp = await codeServer . fetch ( proxyPath , {
93
+ method : "post" ,
94
+ body : JSON . stringify ( "coder is the best" ) ,
95
+ headers : {
96
+ "Content-Type" : "application/json" ,
97
+ } ,
98
+ } )
99
+ assert . equal ( resp . status , 200 )
100
+ assert . equal ( await resp . json ( ) , "coder is the best" )
101
+ } )
47
102
} )
0 commit comments