1
+ import bodyParser from "body-parser"
1
2
import * as express from "express"
2
3
import * as httpserver from "./httpserver"
3
4
import * as integration from "./integration"
4
5
5
6
describe ( "proxy" , ( ) => {
6
- let codeServer : httpserver . HttpServer | undefined
7
7
const nhooyrDevServer = new httpserver . HttpServer ( )
8
+ let codeServer : httpserver . HttpServer | undefined
8
9
let proxyPath : string
10
+ let e : express . Express
9
11
10
12
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 )
15
15
} )
16
16
proxyPath = `/proxy/${ nhooyrDevServer . port ( ) } /wsup`
17
- e . get ( proxyPath , ( req , res ) => {
18
- res . json ( "joe is the best" )
19
- } )
20
17
} )
21
18
22
19
afterAll ( async ( ) => {
23
20
await nhooyrDevServer . close ( )
24
21
} )
25
22
23
+ beforeEach ( ( ) => {
24
+ e = express . default ( )
25
+ } )
26
+
26
27
afterEach ( async ( ) => {
27
28
if ( codeServer ) {
28
29
await codeServer . close ( )
@@ -31,6 +32,9 @@ describe("proxy", () => {
31
32
} )
32
33
33
34
it ( "should rewrite the base path" , async ( ) => {
35
+ e . get ( "/wsup" , ( req , res ) => {
36
+ res . json ( "asher is the best" )
37
+ } )
34
38
; [ , , codeServer ] = await integration . setup ( [ "--auth=none" ] , "" )
35
39
const resp = await codeServer . fetch ( proxyPath )
36
40
expect ( resp . status ) . toBe ( 200 )
@@ -39,10 +43,61 @@ describe("proxy", () => {
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
expect ( resp . status ) . toBe ( 200 )
45
52
const json = await resp . json ( )
46
53
expect ( json ) . toBe ( "joe is the best" )
47
54
} )
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
+ } )
48
103
} )
0 commit comments