From 10c43233c83218170bcbd2f95f67b638cba0f256 Mon Sep 17 00:00:00 2001 From: Simone Sanfratello Date: Thu, 20 Nov 2025 08:54:54 +0100 Subject: [PATCH 1/4] chore: update deps --- package.json | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 1f9d60f..5fe8af4 100644 --- a/package.json +++ b/package.json @@ -57,33 +57,33 @@ } ], "devDependencies": { - "@fastify/websocket": "^11.0.2", + "@fastify/websocket": "^11.2.0", "@jsumners/line-reporter": "^1.0.1", - "@types/node": "^24.0.8", - "@types/ws": "^8.5.14", + "@types/node": "^24.10.1", + "@types/ws": "^8.18.1", "borp": "^0.21.0", - "eslint": "^9.20.1", + "eslint": "^9.39.1", "express": "^5.1.0", - "express-http-proxy": "^2.1.1", + "express-http-proxy": "^2.1.2", "fast-proxy": "^2.2.0", - "fastify": "^5.2.1", + "fastify": "^5.6.2", "got": "^11.8.6", "http-errors": "^2.0.0", "http-proxy": "^1.18.1", - "neostandard": "^0.12.1", + "neostandard": "^0.12.2", "pino": "^10.1.0", "pino-test": "^1.1.0", "socket.io": "^4.8.1", "socket.io-client": "^4.8.1", "tsd": "^0.33.0", - "typescript": "~5.9.2", + "typescript": "~5.9.3", "why-is-node-running": "^3.2.2" }, "dependencies": { - "@fastify/reply-from": "^12.0.2", + "@fastify/reply-from": "^12.4.0", "fast-querystring": "^1.1.2", - "fastify-plugin": "^5.0.1", - "ws": "^8.18.0" + "fastify-plugin": "^5.1.0", + "ws": "^8.18.3" }, "tsd": { "directory": "test/types" From b0c84543d1b1711eab5590f52a2cc8a0f1a8864e Mon Sep 17 00:00:00 2001 From: Simone Sanfratello Date: Thu, 20 Nov 2025 09:13:21 +0100 Subject: [PATCH 2/4] chore: remove got in tests --- package.json | 1 - test/helper/helper.js | 52 ++++++++++++++++- test/test.js | 100 ++++++++++++++++----------------- test/ws-prefix-rewrite-core.js | 4 +- test/ws-prefix-rewrite.js | 4 +- 5 files changed, 105 insertions(+), 56 deletions(-) diff --git a/package.json b/package.json index 5fe8af4..d2f9eb2 100644 --- a/package.json +++ b/package.json @@ -67,7 +67,6 @@ "express-http-proxy": "^2.1.2", "fast-proxy": "^2.2.0", "fastify": "^5.6.2", - "got": "^11.8.6", "http-errors": "^2.0.0", "http-proxy": "^1.18.1", "neostandard": "^0.12.2", diff --git a/test/helper/helper.js b/test/helper/helper.js index c216fb1..a7d037d 100644 --- a/test/helper/helper.js +++ b/test/helper/helper.js @@ -76,8 +76,58 @@ async function createServices ({ t, wsReconnectOptions, wsTargetOptions, wsServe } } +// Helper function to wrap fetch +async function request (url, options = {}) { + // Handle got's object format: request({ url: '...', headers: {...} }) + if (typeof url === 'object' && url.url) { + options = url + url = options.url + } + + const fetchOptions = { + method: options.method || 'GET', + headers: options.headers || {}, + redirect: options.followRedirect === false ? 'manual' : 'follow' + } + + if (options.json) { + fetchOptions.headers['content-type'] = 'application/json' + fetchOptions.body = JSON.stringify(options.json) + } + + if (options.retry !== undefined) { + // Native fetch doesn't have retry, just ignore it + } + + const response = await fetch(url, fetchOptions) + + // Convert fetch Response to got-like response + const result = { + statusCode: response.status, + statusMessage: response.statusText, + headers: Object.fromEntries(response.headers.entries()) + } + + // Handle response body based on responseType + if (options.responseType === 'json') { + result.body = await response.json() + } else { + result.body = await response.text() + } + + // Throw on HTTP errors like got does + if (!response.ok && response.status !== 302) { + const error = new Error(`Response code ${response.status} (${response.statusText})`) + error.response = result + throw error + } + + return result +} + module.exports = { waitForLogMessage, createTargetServer, - createServices + createServices, + request } diff --git a/test/test.js b/test/test.js index f7d07d2..55ef6b2 100644 --- a/test/test.js +++ b/test/test.js @@ -3,10 +3,10 @@ const { after, test } = require('node:test') const Fastify = require('fastify') const proxy = require('../') -const got = require('got') const { Unauthorized } = require('http-errors') const Transform = require('node:stream').Transform const qs = require('fast-querystring') +const { request } = require('./helper/helper') async function run () { const origin = Fastify() @@ -65,12 +65,12 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await got( + const resultRoot = await request( `http://localhost:${server.server.address().port}` ) t.assert.strictEqual(resultRoot.body, 'this is root') - const resultA = await got( + const resultA = await request( `http://localhost:${server.server.address().port}/a` ) t.assert.strictEqual(resultA.body, 'this is a') @@ -93,12 +93,12 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await got( + const resultRoot = await request( `http://localhost:${server.server.address().port}` ) t.assert.strictEqual(resultRoot.body, 'this is root') - const resultA = await got( + const resultA = await request( `http://localhost:${server.server.address().port}/a` ) t.assert.strictEqual(resultA.body, 'this is a') @@ -116,7 +116,7 @@ async function run () { const { headers: { location }, statusCode - } = await got( + } = await request( `http://localhost:${server.server.address().port}/redirect`, { followRedirect: false } @@ -142,7 +142,7 @@ async function run () { const { headers: { location }, statusCode - } = await got( + } = await request( `http://localhost:${server.server.address().port}/redirect`, { followRedirect: false } @@ -173,17 +173,17 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await got( + const resultRoot = await request( `http://localhost:${server.server.address().port}/my-prefix/` ) t.assert.strictEqual(resultRoot.body, 'this is root') - const withoutSlash = await got( + const withoutSlash = await request( `http://localhost:${server.server.address().port}/my-prefix` ) t.assert.strictEqual(withoutSlash.body, 'this is root') - const resultA = await got( + const resultA = await request( `http://localhost:${server.server.address().port}/my-prefix/a` ) t.assert.strictEqual(resultA.body, 'this is a') @@ -204,17 +204,17 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await got( + const resultRoot = await request( `http://localhost:${server.server.address().port}/my-prefix/` ) t.assert.strictEqual(resultRoot.body, 'this is root') - const withoutSlash = await got( + const withoutSlash = await request( `http://localhost:${server.server.address().port}/my-prefix` ) t.assert.strictEqual(withoutSlash.body, 'this is root') - const resultA = await got( + const resultA = await request( `http://localhost:${server.server.address().port}/my-prefix/a` ) t.assert.strictEqual(resultA.body, 'this is a') @@ -229,7 +229,7 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await got( + const resultRoot = await request( `http://localhost:${server.server.address().port}/this-has-data`, { method: 'POST', @@ -255,7 +255,7 @@ async function run () { t.after(() => server.close()) try { - await got( + await request( `http://localhost:${server.server.address().port}/this-has-data`, { method: 'POST', @@ -285,7 +285,7 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await got( + const resultRoot = await request( `http://localhost:${server.server.address().port}/this-has-data`, { method: 'POST', @@ -310,7 +310,7 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await got( + const resultRoot = await request( `http://localhost:${server.server.address().port}/this-has-data`, { method: 'POST', @@ -335,7 +335,7 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - await got( + await request( `http://localhost:${server.server.address().port}/this-has-data`, { method: 'POST', @@ -359,7 +359,7 @@ async function run () { let errored = false try { - await got(`http://localhost:${server.server.address().port}`) + await request(`http://localhost:${server.server.address().port}`) } catch (err) { t.assert.strictEqual(err.response.statusCode, 401) errored = true @@ -368,7 +368,7 @@ async function run () { errored = false try { - await got(`http://localhost:${server.server.address().port}/a`) + await request(`http://localhost:${server.server.address().port}/a`) } catch (err) { t.assert.strictEqual(err.response.statusCode, 401) errored = true @@ -404,7 +404,7 @@ async function run () { let errored = false try { - await got(`http://localhost:${server.server.address().port}`) + await request(`http://localhost:${server.server.address().port}`) } catch (err) { t.assert.strictEqual(err.response.statusCode, 401) errored = true @@ -442,12 +442,12 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await got( + const firstProxyPrefix = await request( `http://localhost:${proxyServer.server.address().port}/api` ) t.assert.strictEqual(firstProxyPrefix.body, 'this is root') - const secondProxyPrefix = await got( + const secondProxyPrefix = await request( `http://localhost:${proxyServer.server.address().port}/api2` ) t.assert.strictEqual(secondProxyPrefix.body, 'this is root for origin2') @@ -473,7 +473,7 @@ async function run () { proxyServer.close() }) - const { headers } = await got({ + const { headers } = await request({ url: `http://localhost:${proxyServer.server.address().port}/api`, headers: { 'x-req': 'from-header' @@ -501,7 +501,7 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await got( + const firstProxyPrefix = await request( `http://localhost:${proxyServer.server.address().port}/api/a` ) t.assert.strictEqual(firstProxyPrefix.body, 'this is /api2/a') @@ -521,7 +521,7 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await got( + const firstProxyPrefix = await request( `http://localhost:${proxyServer.server.address().port}/a` ) t.assert.strictEqual(firstProxyPrefix.body, 'this is /api2/a') @@ -542,7 +542,7 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await got( + const firstProxyPrefix = await request( `http://localhost:${proxyServer.server.address().port}/api/123/static/a` ) t.assert.strictEqual(firstProxyPrefix.body, 'this is /api2/a') @@ -563,7 +563,7 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await got( + const firstProxyPrefix = await request( `http://localhost:${proxyServer.server.address().port}/api/123/endpoint` ) t.assert.strictEqual(firstProxyPrefix.body, 'this is "variable-api" endpoint with id 123') @@ -584,7 +584,7 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await got( + const firstProxyPrefix = await request( `http://localhost:${proxyServer.server.address().port}/api/123/static` ) t.assert.strictEqual(firstProxyPrefix.body, 'this is "variable-api" endpoint with id 123') @@ -605,7 +605,7 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await got( + const firstProxyPrefix = await request( `http://localhost:${proxyServer.server.address().port}/123` ) t.assert.strictEqual(firstProxyPrefix.body, 'this is "variable-api" endpoint with id 123') @@ -627,7 +627,7 @@ async function run () { const { headers: { location } - } = await got( + } = await request( `http://localhost:${proxyServer.server.address().port}/api/this-has-data`, { method: 'POST', @@ -654,7 +654,7 @@ async function run () { const { headers: { location } - } = await got( + } = await request( `http://localhost:${proxyServer.server.address().port}/my-prefix/redirect-to-relative-url`, { method: 'POST' @@ -691,7 +691,7 @@ async function run () { proxyServer.close() }) - const { body } = await got( + const { body } = await request( `http://localhost:${proxyServer.server.address().port}/api` ) @@ -714,7 +714,7 @@ async function run () { const { headers: { location } - } = await got( + } = await request( `http://localhost:${proxyServer.server.address().port}/this-has-data`, { method: 'POST', @@ -739,7 +739,7 @@ async function run () { t.after(() => server.close()) try { - await got( + await request( `http://localhost:${server.server.address().port}/timeout`, { retry: 0 } ) @@ -761,12 +761,12 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await got(`http://localhost:${server.server.address().port}/a`) + const resultRoot = await request(`http://localhost:${server.server.address().port}/a`) t.assert.deepStrictEqual(resultRoot.statusCode, 200) let errored = false try { - await await got(`http://localhost:${server.server.address().port}/api2/a`) + await await request(`http://localhost:${server.server.address().port}/api2/a`) } catch (err) { t.assert.strictEqual(err.response.statusCode, 404) errored = true @@ -784,7 +784,7 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await got( + const resultRoot = await request( `http://localhost:${server.server.address().port}/this-has-data`, { method: 'POST', @@ -796,7 +796,7 @@ async function run () { let errored = false try { - await await got(`http://localhost:${server.server.address().port}/a`) + await await request(`http://localhost:${server.server.address().port}/a`) } catch (err) { t.assert.strictEqual(err.response.statusCode, 404) errored = true @@ -834,14 +834,14 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - await got(`http://localhost:${server.server.address().port}/a`, { + await request(`http://localhost:${server.server.address().port}/a`, { headers: { 'test-header': 'valid-value' } }) try { - await got(`http://localhost:${server.server.address().port}/a`, { + await request(`http://localhost:${server.server.address().port}/a`, { headers: { 'test-header': 'invalid-value' } @@ -852,7 +852,7 @@ async function run () { } try { - await got(`http://localhost:${server.server.address().port}/a`) + await request(`http://localhost:${server.server.address().port}/a`) t.assert.fail() } catch (err) { t.assert.strictEqual(err.response.statusCode, 404) @@ -876,14 +876,14 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultProxied = await got(`http://localhost:${server.server.address().port}/a`, { + const resultProxied = await request(`http://localhost:${server.server.address().port}/a`, { headers: { 'test-header': 'with-proxy' } }) t.assert.strictEqual(resultProxied.body, 'this is a') - const resultUnproxied = await got(`http://localhost:${server.server.address().port}/a`, { + const resultUnproxied = await request(`http://localhost:${server.server.address().port}/a`, { headers: { 'test-header': 'without-proxy' } @@ -912,12 +912,12 @@ async function run () { t.after(() => { proxyServer.close() }) t.after(() => { appServer.close() }) - const resultRoot = await got( + const resultRoot = await request( `${proxyAddress}/second-service?lang=en` ) t.assert.strictEqual(resultRoot.body, 'Hello World - lang = en') - const resultFooRoute = await got( + const resultFooRoute = await request( `${proxyAddress}/second-service/foo?lang=en` ) t.assert.strictEqual(resultFooRoute.body, 'Hello World (foo) - lang = en') @@ -938,7 +938,7 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await got( + const firstProxyPrefix = await request( `http://localhost:${proxyServer.server.address().port}/api/123/endpoint?foo=bar&foo=baz&abc=qux` ) const queryParams = JSON.stringify(qs.parse('foo=bar&foo=baz&abc=qux')) @@ -967,7 +967,7 @@ async function run () { const { statusCode, body - } = await got(`http://localhost:${server.server.address().port}/`) + } = await request(`http://localhost:${server.server.address().port}/`) t.assert.strictEqual(statusCode, 200) t.assert.strictEqual(body, 'this is root') } @@ -976,7 +976,7 @@ async function run () { const { statusCode, body - } = await got(`http://localhost:${server.server.address().port}/fake-a`) + } = await request(`http://localhost:${server.server.address().port}/fake-a`) t.assert.strictEqual(statusCode, 200) t.assert.strictEqual(body, 'this is a') } @@ -1004,7 +1004,7 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await got( + const firstProxyPrefix = await request( `http://localhost:${proxyServer.server.address().port}/api/abc` ) t.assert.strictEqual(firstProxyPrefix.body, 'this is /api2/a') diff --git a/test/ws-prefix-rewrite-core.js b/test/ws-prefix-rewrite-core.js index 722d549..c5fb7cd 100644 --- a/test/ws-prefix-rewrite-core.js +++ b/test/ws-prefix-rewrite-core.js @@ -7,8 +7,8 @@ const Fastify = require('fastify') const fastifyWebSocket = require('@fastify/websocket') const proxy = require('..') const WebSocket = require('ws') -const got = require('got') const { convertUrlToWebSocket } = require('../utils') +const { request } = require('./helper/helper') const level = 'warn' @@ -53,7 +53,7 @@ async function processRequest (t, frontendURL, path, expected) { } try { - const result = await got(url) + const result = await request(url) gotResult = result.body } catch { gotResult = 'error' diff --git a/test/ws-prefix-rewrite.js b/test/ws-prefix-rewrite.js index c72a63e..53ee5fc 100644 --- a/test/ws-prefix-rewrite.js +++ b/test/ws-prefix-rewrite.js @@ -7,7 +7,7 @@ const Fastify = require('fastify') const fastifyWebSocket = require('@fastify/websocket') const proxy = require('..') const WebSocket = require('ws') -const got = require('got') +const { request } = require('./helper/helper') const level = 'warn' @@ -51,7 +51,7 @@ async function processRequest (t, frontendURL, path, expected) { } try { - const result = await got(url) + const result = await request(url) gotResult = result.body } catch { gotResult = 'error' From 8834a65a88ea1710299b3c2c9e712c8033b03f8c Mon Sep 17 00:00:00 2001 From: Simone Sanfratello Date: Thu, 20 Nov 2025 09:17:54 +0100 Subject: [PATCH 3/4] chore: remove test reporter --- package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index d2f9eb2..c2e2a65 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "lint": "eslint", "lint:fix": "eslint --fix", "lint:typescript": "npm run lint:fix - --parser @typescript-eslint/parser --plugin typescript \"test/types/*.ts\"", - "test": "npm run lint && borp -C --check-coverage --reporter=@jsumners/line-reporter && npm run typescript", + "test": "npm run lint && borp -C --check-coverage && npm run typescript", "typescript": "tsd" }, "repository": { @@ -58,7 +58,6 @@ ], "devDependencies": { "@fastify/websocket": "^11.2.0", - "@jsumners/line-reporter": "^1.0.1", "@types/node": "^24.10.1", "@types/ws": "^8.18.1", "borp": "^0.21.0", From 66eeba056c7ae5e02c88a386e005970e2fd2ac72 Mon Sep 17 00:00:00 2001 From: Simone Sanfratello Date: Thu, 20 Nov 2025 13:39:22 +0100 Subject: [PATCH 4/4] test: remove helper request fn --- test/helper/helper.js | 52 +--- test/test.js | 456 +++++++++++++-------------------- test/ws-prefix-rewrite-core.js | 10 +- test/ws-prefix-rewrite.js | 9 +- 4 files changed, 189 insertions(+), 338 deletions(-) diff --git a/test/helper/helper.js b/test/helper/helper.js index a7d037d..c216fb1 100644 --- a/test/helper/helper.js +++ b/test/helper/helper.js @@ -76,58 +76,8 @@ async function createServices ({ t, wsReconnectOptions, wsTargetOptions, wsServe } } -// Helper function to wrap fetch -async function request (url, options = {}) { - // Handle got's object format: request({ url: '...', headers: {...} }) - if (typeof url === 'object' && url.url) { - options = url - url = options.url - } - - const fetchOptions = { - method: options.method || 'GET', - headers: options.headers || {}, - redirect: options.followRedirect === false ? 'manual' : 'follow' - } - - if (options.json) { - fetchOptions.headers['content-type'] = 'application/json' - fetchOptions.body = JSON.stringify(options.json) - } - - if (options.retry !== undefined) { - // Native fetch doesn't have retry, just ignore it - } - - const response = await fetch(url, fetchOptions) - - // Convert fetch Response to got-like response - const result = { - statusCode: response.status, - statusMessage: response.statusText, - headers: Object.fromEntries(response.headers.entries()) - } - - // Handle response body based on responseType - if (options.responseType === 'json') { - result.body = await response.json() - } else { - result.body = await response.text() - } - - // Throw on HTTP errors like got does - if (!response.ok && response.status !== 302) { - const error = new Error(`Response code ${response.status} (${response.statusText})`) - error.response = result - throw error - } - - return result -} - module.exports = { waitForLogMessage, createTargetServer, - createServices, - request + createServices } diff --git a/test/test.js b/test/test.js index 55ef6b2..4df07eb 100644 --- a/test/test.js +++ b/test/test.js @@ -6,7 +6,6 @@ const proxy = require('../') const { Unauthorized } = require('http-errors') const Transform = require('node:stream').Transform const qs = require('fast-querystring') -const { request } = require('./helper/helper') async function run () { const origin = Fastify() @@ -65,15 +64,13 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await request( - `http://localhost:${server.server.address().port}` - ) - t.assert.strictEqual(resultRoot.body, 'this is root') + const responseRoot = await fetch(`http://localhost:${server.server.address().port}`) + const bodyRoot = await responseRoot.text() + t.assert.strictEqual(bodyRoot, 'this is root') - const resultA = await request( - `http://localhost:${server.server.address().port}/a` - ) - t.assert.strictEqual(resultA.body, 'this is a') + const responseA = await fetch(`http://localhost:${server.server.address().port}/a`) + const bodyA = await responseA.text() + t.assert.strictEqual(bodyA, 'this is a') }) test('dynamic upstream for basic proxy', async t => { @@ -93,15 +90,13 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await request( - `http://localhost:${server.server.address().port}` - ) - t.assert.strictEqual(resultRoot.body, 'this is root') + const responseRoot = await fetch(`http://localhost:${server.server.address().port}`) + const bodyRoot = await responseRoot.text() + t.assert.strictEqual(bodyRoot, 'this is root') - const resultA = await request( - `http://localhost:${server.server.address().port}/a` - ) - t.assert.strictEqual(resultA.body, 'this is a') + const responseA = await fetch(`http://localhost:${server.server.address().port}/a`) + const bodyA = await responseA.text() + t.assert.strictEqual(bodyA, 'this is a') }) test('redirects passthrough', async t => { @@ -113,14 +108,11 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const { - headers: { location }, - statusCode - } = await request( - `http://localhost:${server.server.address().port}/redirect`, { - followRedirect: false - } - ) + const response = await fetch(`http://localhost:${server.server.address().port}/redirect`, { + redirect: 'manual' + }) + const location = response.headers.get('location') + const statusCode = response.status t.assert.strictEqual(location, 'https://fastify.dev') t.assert.strictEqual(statusCode, 302) }) @@ -139,14 +131,11 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const { - headers: { location }, - statusCode - } = await request( - `http://localhost:${server.server.address().port}/redirect`, { - followRedirect: false - } - ) + const response = await fetch(`http://localhost:${server.server.address().port}/redirect`, { + redirect: 'manual' + }) + const location = response.headers.get('location') + const statusCode = response.status t.assert.strictEqual(location, 'https://fastify.dev') t.assert.strictEqual(statusCode, 302) }) @@ -173,20 +162,17 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await request( - `http://localhost:${server.server.address().port}/my-prefix/` - ) - t.assert.strictEqual(resultRoot.body, 'this is root') + const responseRoot = await fetch(`http://localhost:${server.server.address().port}/my-prefix/`) + const bodyRoot = await responseRoot.text() + t.assert.strictEqual(bodyRoot, 'this is root') - const withoutSlash = await request( - `http://localhost:${server.server.address().port}/my-prefix` - ) - t.assert.strictEqual(withoutSlash.body, 'this is root') + const responseWithoutSlash = await fetch(`http://localhost:${server.server.address().port}/my-prefix`) + const bodyWithoutSlash = await responseWithoutSlash.text() + t.assert.strictEqual(bodyWithoutSlash, 'this is root') - const resultA = await request( - `http://localhost:${server.server.address().port}/my-prefix/a` - ) - t.assert.strictEqual(resultA.body, 'this is a') + const responseA = await fetch(`http://localhost:${server.server.address().port}/my-prefix/a`) + const bodyA = await responseA.text() + t.assert.strictEqual(bodyA, 'this is a') }) test('dynamic upstream for prefixed proxy', async t => { @@ -204,20 +190,17 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await request( - `http://localhost:${server.server.address().port}/my-prefix/` - ) - t.assert.strictEqual(resultRoot.body, 'this is root') + const responseRoot = await fetch(`http://localhost:${server.server.address().port}/my-prefix/`) + const bodyRoot = await responseRoot.text() + t.assert.strictEqual(bodyRoot, 'this is root') - const withoutSlash = await request( - `http://localhost:${server.server.address().port}/my-prefix` - ) - t.assert.strictEqual(withoutSlash.body, 'this is root') + const responseWithoutSlash = await fetch(`http://localhost:${server.server.address().port}/my-prefix`) + const bodyWithoutSlash = await responseWithoutSlash.text() + t.assert.strictEqual(bodyWithoutSlash, 'this is root') - const resultA = await request( - `http://localhost:${server.server.address().port}/my-prefix/a` - ) - t.assert.strictEqual(resultA.body, 'this is a') + const responseA = await fetch(`http://localhost:${server.server.address().port}/my-prefix/a`) + const bodyA = await responseA.text() + t.assert.strictEqual(bodyA, 'this is a') }) test('posting stuff', async t => { @@ -229,15 +212,13 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await request( - `http://localhost:${server.server.address().port}/this-has-data`, - { - method: 'POST', - json: { hello: 'world' }, - responseType: 'json' - } - ) - t.assert.deepStrictEqual(resultRoot.body, { something: 'posted' }) + const response = await fetch(`http://localhost:${server.server.address().port}/this-has-data`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ hello: 'world' }) + }) + const body = await response.json() + t.assert.deepStrictEqual(body, { something: 'posted' }) }) test('preValidation post payload contains invalid data', async t => { @@ -254,21 +235,14 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - try { - await request( - `http://localhost:${server.server.address().port}/this-has-data`, - { - method: 'POST', - json: { hello: 'invalid' }, - responseType: 'json' - } - ) - } catch (err) { - t.assert.strictEqual(err.response.statusCode, 400) - t.assert.deepStrictEqual(err.response.body, { message: 'invalid body.hello value' }) - return - } - t.assert.fail() + const response = await fetch(`http://localhost:${server.server.address().port}/this-has-data`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ hello: 'invalid' }) + }) + t.assert.strictEqual(response.status, 400) + const body = await response.json() + t.assert.deepStrictEqual(body, { message: 'invalid body.hello value' }) }) test('preValidation post payload contains valid data', async t => { @@ -285,15 +259,13 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await request( - `http://localhost:${server.server.address().port}/this-has-data`, - { - method: 'POST', - json: { hello: 'world' }, - responseType: 'json' - } - ) - t.assert.deepStrictEqual(resultRoot.body, { something: 'posted' }) + const response = await fetch(`http://localhost:${server.server.address().port}/this-has-data`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ hello: 'world' }) + }) + const body = await response.json() + t.assert.deepStrictEqual(body, { something: 'posted' }) }) test('dynamic upstream for posting stuff', async t => { @@ -310,15 +282,13 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await request( - `http://localhost:${server.server.address().port}/this-has-data`, - { - method: 'POST', - json: { hello: 'world' }, - responseType: 'json' - } - ) - t.assert.deepStrictEqual(resultRoot.body, { something: 'posted' }) + const response = await fetch(`http://localhost:${server.server.address().port}/this-has-data`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ hello: 'world' }) + }) + const body = await response.json() + t.assert.deepStrictEqual(body, { something: 'posted' }) }) test('skip proxying the incoming payload', async t => { @@ -335,14 +305,11 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - await request( - `http://localhost:${server.server.address().port}/this-has-data`, - { - method: 'POST', - json: { hello: 'world' }, - responseType: 'json' - } - ) + await fetch(`http://localhost:${server.server.address().port}/this-has-data`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ hello: 'world' }) + }) }) test('preHandler', async t => { @@ -357,23 +324,11 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - let errored = false - try { - await request(`http://localhost:${server.server.address().port}`) - } catch (err) { - t.assert.strictEqual(err.response.statusCode, 401) - errored = true - } - t.assert.ok(errored) + const response1 = await fetch(`http://localhost:${server.server.address().port}`) + t.assert.strictEqual(response1.status, 401) - errored = false - try { - await request(`http://localhost:${server.server.address().port}/a`) - } catch (err) { - t.assert.strictEqual(err.response.statusCode, 401) - errored = true - } - t.assert.ok(errored) + const response2 = await fetch(`http://localhost:${server.server.address().port}/a`) + t.assert.strictEqual(response2.status, 401) }) test('preHandler gets config', async t => { @@ -402,14 +357,8 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - let errored = false - try { - await request(`http://localhost:${server.server.address().port}`) - } catch (err) { - t.assert.strictEqual(err.response.statusCode, 401) - errored = true - } - t.assert.ok(errored) + const response = await fetch(`http://localhost:${server.server.address().port}`) + t.assert.strictEqual(response.status, 401) }) test('multiple prefixes with multiple plugins', async t => { @@ -442,15 +391,13 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await request( - `http://localhost:${proxyServer.server.address().port}/api` - ) - t.assert.strictEqual(firstProxyPrefix.body, 'this is root') + const response1 = await fetch(`http://localhost:${proxyServer.server.address().port}/api`) + const body1 = await response1.text() + t.assert.strictEqual(body1, 'this is root') - const secondProxyPrefix = await request( - `http://localhost:${proxyServer.server.address().port}/api2` - ) - t.assert.strictEqual(secondProxyPrefix.body, 'this is root for origin2') + const response2 = await fetch(`http://localhost:${proxyServer.server.address().port}/api2`) + const body2 = await response2.text() + t.assert.strictEqual(body2, 'this is root for origin2') }) test('passes replyOptions object to reply.from() calls', async t => { @@ -473,16 +420,16 @@ async function run () { proxyServer.close() }) - const { headers } = await request({ - url: `http://localhost:${proxyServer.server.address().port}/api`, + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/api`, { headers: { 'x-req': 'from-header' } }) + const headers = response.headers const expected = { 'x-test': 'test', 'x-req': 'from-header' } for (const [key, value] of Object.entries(expected)) { - t.assert.strictEqual(headers[key], value, `Header ${key} does not match`) + t.assert.strictEqual(headers.get(key), value, `Header ${key} does not match`) } }) @@ -501,10 +448,9 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await request( - `http://localhost:${proxyServer.server.address().port}/api/a` - ) - t.assert.strictEqual(firstProxyPrefix.body, 'this is /api2/a') + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/api/a`) + const body = await response.text() + t.assert.strictEqual(body, 'this is /api2/a') }) test('rewritePrefix without prefix', async t => { @@ -521,10 +467,9 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await request( - `http://localhost:${proxyServer.server.address().port}/a` - ) - t.assert.strictEqual(firstProxyPrefix.body, 'this is /api2/a') + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/a`) + const body = await response.text() + t.assert.strictEqual(body, 'this is /api2/a') }) test('prefix with variables', async t => { @@ -542,10 +487,9 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await request( - `http://localhost:${proxyServer.server.address().port}/api/123/static/a` - ) - t.assert.strictEqual(firstProxyPrefix.body, 'this is /api2/a') + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/api/123/static/a`) + const body = await response.text() + t.assert.strictEqual(body, 'this is /api2/a') }) test('prefix and rewritePrefix with variables', async t => { @@ -563,10 +507,9 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await request( - `http://localhost:${proxyServer.server.address().port}/api/123/endpoint` - ) - t.assert.strictEqual(firstProxyPrefix.body, 'this is "variable-api" endpoint with id 123') + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/api/123/endpoint`) + const body = await response.text() + t.assert.strictEqual(body, 'this is "variable-api" endpoint with id 123') }) test('prefix (complete path) and rewritePrefix with variables and similar path', async t => { @@ -584,10 +527,9 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await request( - `http://localhost:${proxyServer.server.address().port}/api/123/static` - ) - t.assert.strictEqual(firstProxyPrefix.body, 'this is "variable-api" endpoint with id 123') + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/api/123/static`) + const body = await response.text() + t.assert.strictEqual(body, 'this is "variable-api" endpoint with id 123') }) test('prefix and rewritePrefix with variables with different paths', async t => { @@ -605,10 +547,9 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await request( - `http://localhost:${proxyServer.server.address().port}/123` - ) - t.assert.strictEqual(firstProxyPrefix.body, 'this is "variable-api" endpoint with id 123') + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/123`) + const body = await response.text() + t.assert.strictEqual(body, 'this is "variable-api" endpoint with id 123') }) test('rewrite location headers', async t => { @@ -625,15 +566,12 @@ async function run () { proxyServer.close() }) - const { - headers: { location } - } = await request( - `http://localhost:${proxyServer.server.address().port}/api/this-has-data`, - { - method: 'POST', - json: { hello: 'world' } - } - ) + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/api/this-has-data`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ hello: 'world' }) + }) + const location = response.headers.get('location') t.assert.strictEqual(location, '/api/something') }) @@ -652,14 +590,10 @@ async function run () { proxyServer.close() }) - const { - headers: { location } - } = await request( - `http://localhost:${proxyServer.server.address().port}/my-prefix/redirect-to-relative-url`, - { - method: 'POST' - } - ) + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/my-prefix/redirect-to-relative-url`, { + method: 'POST' + }) + const location = response.headers.get('location') t.assert.strictEqual(location, '/relative-url') }) @@ -691,9 +625,8 @@ async function run () { proxyServer.close() }) - const { body } = await request( - `http://localhost:${proxyServer.server.address().port}/api` - ) + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/api`) + const body = await response.text() t.assert.match(body, /THIS IS ROOT/) }) @@ -712,15 +645,12 @@ async function run () { proxyServer.close() }) - const { - headers: { location } - } = await request( - `http://localhost:${proxyServer.server.address().port}/this-has-data`, - { - method: 'POST', - json: { hello: 'world' } - } - ) + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/this-has-data`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ hello: 'world' }) + }) + const location = response.headers.get('location') t.assert.strictEqual(location, '/something') }) @@ -738,17 +668,9 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - try { - await request( - `http://localhost:${server.server.address().port}/timeout`, - { retry: 0 } - ) - } catch (err) { - t.assert.strictEqual(err.response.statusCode, 504) - t.assert.strictEqual(err.response.statusMessage, 'Gateway Timeout') - return - } - t.assert.fail() + const response = await fetch(`http://localhost:${server.server.address().port}/timeout`) + t.assert.strictEqual(response.status, 504) + t.assert.strictEqual(response.statusText, 'Gateway Timeout') }) test('settings of routes', async t => { @@ -761,17 +683,11 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await request(`http://localhost:${server.server.address().port}/a`) - t.assert.deepStrictEqual(resultRoot.statusCode, 200) + const response = await fetch(`http://localhost:${server.server.address().port}/a`) + t.assert.deepStrictEqual(response.status, 200) - let errored = false - try { - await await request(`http://localhost:${server.server.address().port}/api2/a`) - } catch (err) { - t.assert.strictEqual(err.response.statusCode, 404) - errored = true - } - t.assert.ok(errored) + const response2 = await fetch(`http://localhost:${server.server.address().port}/api2/a`) + t.assert.strictEqual(response2.status, 404) }) test('settings of method types', async t => { @@ -784,24 +700,16 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultRoot = await request( - `http://localhost:${server.server.address().port}/this-has-data`, - { - method: 'POST', - json: { hello: 'world' }, - responseType: 'json' - } - ) - t.assert.deepStrictEqual(resultRoot.body, { something: 'posted' }) + const response = await fetch(`http://localhost:${server.server.address().port}/this-has-data`, { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ hello: 'world' }) + }) + const body = await response.json() + t.assert.deepStrictEqual(body, { something: 'posted' }) - let errored = false - try { - await await request(`http://localhost:${server.server.address().port}/a`) - } catch (err) { - t.assert.strictEqual(err.response.statusCode, 404) - errored = true - } - t.assert.ok(errored) + const response2 = await fetch(`http://localhost:${server.server.address().port}/a`) + t.assert.strictEqual(response2.status, 404) }) const getTestConstraint = () => ({ @@ -834,29 +742,23 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - await request(`http://localhost:${server.server.address().port}/a`, { + + const response1 = await fetch(`http://localhost:${server.server.address().port}/a`, { headers: { 'test-header': 'valid-value' } }) + t.assert.strictEqual(response1.status, 200) - try { - await request(`http://localhost:${server.server.address().port}/a`, { - headers: { - 'test-header': 'invalid-value' - } - }) - t.assert.fail() - } catch (err) { - t.assert.strictEqual(err.response.statusCode, 404) - } + const response2 = await fetch(`http://localhost:${server.server.address().port}/a`, { + headers: { + 'test-header': 'invalid-value' + } + }) + t.assert.strictEqual(response2.status, 404) - try { - await request(`http://localhost:${server.server.address().port}/a`) - t.assert.fail() - } catch (err) { - t.assert.strictEqual(err.response.statusCode, 404) - } + const response3 = await fetch(`http://localhost:${server.server.address().port}/a`) + t.assert.strictEqual(response3.status, 404) }) test('constraints with unconstrained routes', async t => { @@ -876,19 +778,21 @@ async function run () { await server.listen({ port: 0 }) t.after(() => server.close()) - const resultProxied = await request(`http://localhost:${server.server.address().port}/a`, { + const responseProxied = await fetch(`http://localhost:${server.server.address().port}/a`, { headers: { 'test-header': 'with-proxy' } }) - t.assert.strictEqual(resultProxied.body, 'this is a') + const bodyProxied = await responseProxied.text() + t.assert.strictEqual(bodyProxied, 'this is a') - const resultUnproxied = await request(`http://localhost:${server.server.address().port}/a`, { + const responseUnproxied = await fetch(`http://localhost:${server.server.address().port}/a`, { headers: { 'test-header': 'without-proxy' } }) - t.assert.strictEqual(resultUnproxied.body, 'this is unproxied a') + const bodyUnproxied = await responseUnproxied.text() + t.assert.strictEqual(bodyUnproxied, 'this is unproxied a') }) test('prefixed proxy with query search', async t => { @@ -912,15 +816,13 @@ async function run () { t.after(() => { proxyServer.close() }) t.after(() => { appServer.close() }) - const resultRoot = await request( - `${proxyAddress}/second-service?lang=en` - ) - t.assert.strictEqual(resultRoot.body, 'Hello World - lang = en') + const responseRoot = await fetch(`${proxyAddress}/second-service?lang=en`) + const bodyRoot = await responseRoot.text() + t.assert.strictEqual(bodyRoot, 'Hello World - lang = en') - const resultFooRoute = await request( - `${proxyAddress}/second-service/foo?lang=en` - ) - t.assert.strictEqual(resultFooRoute.body, 'Hello World (foo) - lang = en') + const responseFoo = await fetch(`${proxyAddress}/second-service/foo?lang=en`) + const bodyFoo = await responseFoo.text() + t.assert.strictEqual(bodyFoo, 'Hello World (foo) - lang = en') }) test('keep the query params on proxy', async t => { @@ -938,11 +840,10 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await request( - `http://localhost:${proxyServer.server.address().port}/api/123/endpoint?foo=bar&foo=baz&abc=qux` - ) + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/api/123/endpoint?foo=bar&foo=baz&abc=qux`) + const body = await response.text() const queryParams = JSON.stringify(qs.parse('foo=bar&foo=baz&abc=qux')) - t.assert.strictEqual(firstProxyPrefix.body, `this is "variable-api" endpoint with id 123 and query params ${queryParams}`) + t.assert.strictEqual(body, `this is "variable-api" endpoint with id 123 and query params ${queryParams}`) }) test('manual from call via fromParameters', async t => { @@ -964,20 +865,16 @@ async function run () { t.after(() => server.close()) { - const { - statusCode, - body - } = await request(`http://localhost:${server.server.address().port}/`) - t.assert.strictEqual(statusCode, 200) + const response = await fetch(`http://localhost:${server.server.address().port}/`) + const body = await response.text() + t.assert.strictEqual(response.status, 200) t.assert.strictEqual(body, 'this is root') } { - const { - statusCode, - body - } = await request(`http://localhost:${server.server.address().port}/fake-a`) - t.assert.strictEqual(statusCode, 200) + const response = await fetch(`http://localhost:${server.server.address().port}/fake-a`) + const body = await response.text() + t.assert.strictEqual(response.status, 200) t.assert.strictEqual(body, 'this is a') } }) @@ -1004,10 +901,9 @@ async function run () { proxyServer.close() }) - const firstProxyPrefix = await request( - `http://localhost:${proxyServer.server.address().port}/api/abc` - ) - t.assert.strictEqual(firstProxyPrefix.body, 'this is /api2/a') + const response = await fetch(`http://localhost:${proxyServer.server.address().port}/api/abc`) + const body = await response.text() + t.assert.strictEqual(body, 'this is /api2/a') }) } diff --git a/test/ws-prefix-rewrite-core.js b/test/ws-prefix-rewrite-core.js index c5fb7cd..603ec13 100644 --- a/test/ws-prefix-rewrite-core.js +++ b/test/ws-prefix-rewrite-core.js @@ -2,13 +2,11 @@ const test = require('node:test') const { once } = require('node:events') - const Fastify = require('fastify') const fastifyWebSocket = require('@fastify/websocket') const proxy = require('..') const WebSocket = require('ws') const { convertUrlToWebSocket } = require('../utils') -const { request } = require('./helper/helper') const level = 'warn' @@ -53,8 +51,12 @@ async function processRequest (t, frontendURL, path, expected) { } try { - const result = await request(url) - gotResult = result.body + const response = await fetch(url) + if (!response.ok) { + gotResult = 'error' + } else { + gotResult = await response.text() + } } catch { gotResult = 'error' } diff --git a/test/ws-prefix-rewrite.js b/test/ws-prefix-rewrite.js index 53ee5fc..f65f2f1 100644 --- a/test/ws-prefix-rewrite.js +++ b/test/ws-prefix-rewrite.js @@ -7,7 +7,6 @@ const Fastify = require('fastify') const fastifyWebSocket = require('@fastify/websocket') const proxy = require('..') const WebSocket = require('ws') -const { request } = require('./helper/helper') const level = 'warn' @@ -51,8 +50,12 @@ async function processRequest (t, frontendURL, path, expected) { } try { - const result = await request(url) - gotResult = result.body + const response = await fetch(url) + if (!response.ok) { + gotResult = 'error' + } else { + gotResult = await response.text() + } } catch { gotResult = 'error' }