Skip to content

Commit 6c2c0b2

Browse files
committed
wip! it's working!!!
1 parent eac7040 commit 6c2c0b2

File tree

2 files changed

+38
-20
lines changed

2 files changed

+38
-20
lines changed

src/node/proxy.ts

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { HttpCode } from "../common/http"
44
export const proxy = proxyServer.createProxyServer({})
55

66
proxy.on("error", (error, _, res) => {
7-
console.log("yoooo")
87
res.writeHead(HttpCode.ServerError)
98
res.end(error.message)
109
})

test/unit/proxy.test.ts

+38-19
Original file line numberDiff line numberDiff line change
@@ -1,9 +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"
36
import { proxy } from "../../src/node/proxy"
47
// import { proxy } from "../../src/node/proxy"
58
import * as httpserver from "../utils/httpserver"
69
import * as integration from "../utils/integration"
10+
// import * as EventEmitter from "events"
711

812
describe("proxy", () => {
913
const nhooyrDevServer = new httpserver.HttpServer()
@@ -191,33 +195,48 @@ describe("proxy (standalone)", () => {
191195
})
192196
})
193197

194-
describe("test dummy", () => {
195-
it("should return a 200", async () => {
196-
const testServer = new httpserver.HttpServer()
197-
198-
await testServer.listen(function (req, res) {
198+
describe("code-server proxy", () => {
199+
const PORT = 9003
200+
const PROXY_PORT = 8003
201+
const URL = `http://localhost:${PORT}`
202+
const PROXY_URL = `http://localhost:${PROXY_PORT}`
203+
let testServer: http.Server
204+
let proxyTarget: http.Server
205+
206+
beforeEach(async () => {
207+
// Define server and a proxy server
208+
testServer = http.createServer((req, res) => {
199209
proxy.web(req, res, {
200-
target: `http://localhost:${testServer.port()}`,
210+
target: PROXY_URL,
201211
})
212+
})
202213

203-
if (req.url === "/error") {
204-
res.statusCode = 500
205-
res.end("ERROR")
206-
return
207-
}
208-
214+
proxyTarget = http.createServer((req, res) => {
209215
res.writeHead(200, { "Content-Type": "text/plain" })
210-
res.write("nice joe")
211216
res.end()
212217
})
213218

214-
const resp = await testServer.fetch(`/wsup`)
215-
expect(resp.status).toBe(200)
216-
expect(resp.statusText).toBe("OK")
217-
218-
const errorResp = await testServer.fetch(`/error`)
219-
expect(errorResp.status).toBe(500)
219+
// Start both servers
220+
await proxyTarget.listen(PROXY_PORT)
221+
await testServer.listen(PORT)
222+
})
220223

224+
afterEach(async () => {
221225
await testServer.close()
226+
await proxyTarget.close()
227+
})
228+
229+
it("should return a 500 when proxy target errors ", async () => {
230+
// Close the proxy target so that proxy errors
231+
await proxyTarget.close()
232+
const errorResp = await nodeFetch.default(`${URL}/error`)
233+
expect(errorResp.status).toBe(HttpCode.ServerError)
234+
expect(errorResp.statusText).toBe("Internal Server Error")
235+
})
236+
237+
it("should proxy correctly", async () => {
238+
const resp = await nodeFetch.default(`${URL}/route`)
239+
expect(resp.status).toBe(200)
240+
expect(resp.statusText).toBe("OK")
222241
})
223242
})

0 commit comments

Comments
 (0)