Skip to content

Commit 01c646e

Browse files
committed
proxy.test.ts: Add POST body test and redirection tests
Closes #2377
1 parent c8274ed commit 01c646e

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

test/proxy.test.ts

+63-8
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,29 @@ import * as assert from "assert"
22
import * as express from "express"
33
import * as httpserver from "./httpserver"
44
import * as integration from "./integration"
5+
import bodyParser from "body-parser"
56

67
describe("proxy", () => {
7-
let codeServer: httpserver.HttpServer | undefined
88
const nhooyrDevServer = new httpserver.HttpServer()
9+
let codeServer: httpserver.HttpServer | undefined
910
let proxyPath: string
11+
let e: express.Express
1012

1113
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)
1616
})
1717
proxyPath = `/proxy/${nhooyrDevServer.port()}/wsup`
18-
e.get(proxyPath, (req, res) => {
19-
res.json("joe is the best")
20-
})
2118
})
2219

2320
after(async () => {
2421
await nhooyrDevServer.close()
2522
})
2623

24+
beforeEach(() => {
25+
e = express.default()
26+
})
27+
2728
afterEach(async () => {
2829
if (codeServer) {
2930
await codeServer.close()
@@ -32,16 +33,70 @@ describe("proxy", () => {
3233
})
3334

3435
it("should rewrite the base path", async () => {
36+
e.get("/wsup", (req, res) => {
37+
res.json("asher is the best")
38+
})
3539
;[, , codeServer] = await integration.setup(["--auth=none"], "")
3640
const resp = await codeServer.fetch(proxyPath)
3741
assert.equal(resp.status, 200)
3842
assert.equal(await resp.json(), "asher is the best")
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
assert.equal(resp.status, 200)
4552
assert.equal(await resp.json(), "joe is the best")
4653
})
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+
})
47102
})

0 commit comments

Comments
 (0)