Skip to content

Commit 42cfa4a

Browse files
committed
feat: add tests for proxy
1 parent 7e43f7d commit 42cfa4a

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

test/unit/node/proxy.test.ts

+107
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import bodyParser from "body-parser"
22
import * as express from "express"
3+
import * as nodeFetch from "node-fetch"
4+
import * as http from "http"
5+
import { HttpCode } from "../../../src/common/http"
6+
import { proxy } from "../../../src/node/proxy"
37
import * as httpserver from "../../utils/httpserver"
48
import * as integration from "../../utils/integration"
59

@@ -102,4 +106,107 @@ describe("proxy", () => {
102106
expect(resp.status).toBe(200)
103107
expect(await resp.json()).toBe("coder is the best")
104108
})
109+
110+
it("should handle bad requests", async () => {
111+
e.use(bodyParser.json({ strict: false }))
112+
e.post("/wsup", (req, res) => {
113+
res.json(req.body)
114+
})
115+
codeServer = await integration.setup(["--auth=none"], "")
116+
const resp = await codeServer.fetch(proxyPath, {
117+
method: "post",
118+
body: "coder is the best",
119+
headers: {
120+
"Content-Type": "application/json",
121+
},
122+
})
123+
expect(resp.status).toBe(400)
124+
expect(resp.statusText).toMatch("Bad Request")
125+
})
126+
127+
it("should handle invalid routes", async () => {
128+
e.post("/wsup", (req, res) => {
129+
res.json(req.body)
130+
})
131+
codeServer = await integration.setup(["--auth=none"], "")
132+
const resp = await codeServer.fetch(`${proxyPath}/hello`)
133+
expect(resp.status).toBe(404)
134+
expect(resp.statusText).toMatch("Not Found")
135+
})
136+
137+
it("should handle errors", async () => {
138+
e.use(bodyParser.json({ strict: false }))
139+
e.post("/wsup", (req, res) => {
140+
throw new Error("BROKEN")
141+
})
142+
codeServer = await integration.setup(["--auth=none"], "")
143+
const resp = await codeServer.fetch(proxyPath, {
144+
method: "post",
145+
body: JSON.stringify("coder is the best"),
146+
headers: {
147+
"Content-Type": "application/json",
148+
},
149+
})
150+
expect(resp.status).toBe(500)
151+
expect(resp.statusText).toMatch("Internal Server Error")
152+
})
153+
})
154+
155+
// NOTE@jsjoeio
156+
// Both this test suite and the one above it are very similar
157+
// The main difference is this one uses http and node-fetch
158+
// and specifically tests the proxy in isolation vs. using
159+
// the httpserver abstraction we've built.
160+
//
161+
// Leaving this as a separate test suite for now because
162+
// we may consider refactoring the httpserver abstraction
163+
// in the future.
164+
//
165+
// If you're writing a test specifically for code in
166+
// src/node/proxy.ts, you should probably add it to
167+
// this test suite.
168+
describe("proxy (standalone)", () => {
169+
const PORT = 9003
170+
const PROXY_PORT = 8003
171+
const URL = `http://localhost:${PORT}`
172+
const PROXY_URL = `http://localhost:${PROXY_PORT}`
173+
let testServer: http.Server
174+
let proxyTarget: http.Server
175+
176+
beforeEach(async () => {
177+
// Define server and a proxy server
178+
testServer = http.createServer((req, res) => {
179+
proxy.web(req, res, {
180+
target: PROXY_URL,
181+
})
182+
})
183+
184+
proxyTarget = http.createServer((req, res) => {
185+
res.writeHead(200, { "Content-Type": "text/plain" })
186+
res.end()
187+
})
188+
189+
// Start both servers
190+
await proxyTarget.listen(PROXY_PORT)
191+
await testServer.listen(PORT)
192+
})
193+
194+
afterEach(async () => {
195+
await testServer.close()
196+
await proxyTarget.close()
197+
})
198+
199+
it("should return a 500 when proxy target errors ", async () => {
200+
// Close the proxy target so that proxy errors
201+
await proxyTarget.close()
202+
const errorResp = await nodeFetch.default(`${URL}/error`)
203+
expect(errorResp.status).toBe(HttpCode.ServerError)
204+
expect(errorResp.statusText).toBe("Internal Server Error")
205+
})
206+
207+
it("should proxy correctly", async () => {
208+
const resp = await nodeFetch.default(`${URL}/route`)
209+
expect(resp.status).toBe(200)
210+
expect(resp.statusText).toBe("OK")
211+
})
105212
})

0 commit comments

Comments
 (0)