|
1 | 1 | import bodyParser from "body-parser"
|
2 | 2 | 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" |
3 | 6 | import { proxy } from "../../src/node/proxy"
|
4 | 7 | // import { proxy } from "../../src/node/proxy"
|
5 | 8 | import * as httpserver from "../utils/httpserver"
|
6 | 9 | import * as integration from "../utils/integration"
|
| 10 | +// import * as EventEmitter from "events" |
7 | 11 |
|
8 | 12 | describe("proxy", () => {
|
9 | 13 | const nhooyrDevServer = new httpserver.HttpServer()
|
@@ -191,33 +195,48 @@ describe("proxy (standalone)", () => {
|
191 | 195 | })
|
192 | 196 | })
|
193 | 197 |
|
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) => { |
199 | 209 | proxy.web(req, res, {
|
200 |
| - target: `http://localhost:${testServer.port()}`, |
| 210 | + target: PROXY_URL, |
201 | 211 | })
|
| 212 | + }) |
202 | 213 |
|
203 |
| - if (req.url === "/error") { |
204 |
| - res.statusCode = 500 |
205 |
| - res.end("ERROR") |
206 |
| - return |
207 |
| - } |
208 |
| - |
| 214 | + proxyTarget = http.createServer((req, res) => { |
209 | 215 | res.writeHead(200, { "Content-Type": "text/plain" })
|
210 |
| - res.write("nice joe") |
211 | 216 | res.end()
|
212 | 217 | })
|
213 | 218 |
|
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 | + }) |
220 | 223 |
|
| 224 | + afterEach(async () => { |
221 | 225 | 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") |
222 | 241 | })
|
223 | 242 | })
|
0 commit comments