Skip to content

Commit a86f3df

Browse files
committed
feat: add tests for proxy
1 parent 36be0b7 commit a86f3df

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

test/unit/proxy.test.ts

+109
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
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"
7+
// import { proxy } from "../../src/node/proxy"
38
import * as httpserver from "../utils/httpserver"
49
import * as integration from "../utils/integration"
10+
// import * as EventEmitter from "events"
511

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

0 commit comments

Comments
 (0)