From b6eecabab631ecc0ce518f5687a4749745804790 Mon Sep 17 00:00:00 2001 From: Matthew Prestifilippo Date: Mon, 30 Sep 2024 20:52:24 -0400 Subject: [PATCH] Convert remaining to fetch --- package.json | 2 +- test/api_spec.js | 62 ++++++++--------- test/cli_spec.js | 16 +++-- test/proxy_spec.js | 167 +++++++++++++++++++++++---------------------- 4 files changed, 125 insertions(+), 122 deletions(-) diff --git a/package.json b/package.json index c1f52fa..edcc41f 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "scripts": { "lint": "jshint bin/ lib/ test/", "fmt": "pre-commit run --all-files", - "test": "FORCE_COLOR=3 nyc node test/jasmine.js", + "test": "NODE_TLS_REJECT_UNAUTHORIZED=0 FORCE_COLOR=3 nyc node test/jasmine.js", "coverage-html": "nyc report --reporter=html", "codecov": "nyc report --reporter=lcov && codecov" } diff --git a/test/api_spec.js b/test/api_spec.js index ee2958d..c29b953 100644 --- a/test/api_spec.js +++ b/test/api_spec.js @@ -22,22 +22,19 @@ describe("API Tests", function () { proxy = newProxy; }) .then(function () { - r = (options = {}) => { - const path = options.path || ''; - delete options.path; - return fetch({ + r = (path, options) => { + options = options || {}; + path = path || ''; + const url = `${options.url || apiUrl}${path}`; + delete options.url; + const fetchOptions = { method: "GET", headers: { Authorization: `token ${proxy.authToken}`, }, - url: `${apiUrl}${path}`, ...options, - }).then((res) => { - if (!res.ok) { - throw res; - } - return res.text(); // return body - }); + }; + return fetch(url, fetchOptions); }; }) .then(function () { @@ -84,9 +81,7 @@ describe("API Tests", function () { }); it("GET /api/routes fetches the routing table", function (done) { - r() - .then(function (body) { - var reply = JSON.parse(body); + r().then(res => res.json()).then(function (reply) { var keys = Object.keys(reply); expect(keys.length).toEqual(1); expect(keys).toContain("/"); @@ -100,24 +95,22 @@ describe("API Tests", function () { proxy .addRoute(path, { target: url }) .then(function () { - return r({ path }); + return r(path); }) - .then(function (body) { - var reply = JSON.parse(body); + .then(res => res.json()) + .then(function (reply) { var keys = Object.keys(reply); expect(keys).toContain("target"); expect(reply.target).toEqual(url); }) + .catch(done.fail) .then(done); }); it("GET /api/routes[/path] fetches a single route (404 if missing)", function (done) { - r({ path: "/path" }) - .then((body) => { - done.fail("Expected a 404"); - }) - .catch((error) => { - expect(error.statusCode).toEqual(404); + r("/path") + .then((res) => { + expect(res.status).toEqual(404); }) .then(done); }); @@ -126,11 +119,11 @@ describe("API Tests", function () { var port = 8998; var target = "http://127.0.0.1:" + port; - r({ + r("/user/foo", { method: 'POST', - path: "/user/foo", body: JSON.stringify({ target: target }), }) + .then(res => res.text()) .then((body) => { expect(body).toEqual(""); }) @@ -139,17 +132,18 @@ describe("API Tests", function () { expect(route.target).toEqual(target); expect(typeof route.last_activity).toEqual("object"); }) + .catch(done.fail) .then(done); }); it("POST /api/routes[/foo%20bar] handles URI escapes", function (done) { var port = 8998; var target = "http://127.0.0.1:" + port; - r({ + r("/user/foo%40bar", { method: "POST", - path: "/user/foo%40bar", body: JSON.stringify({ target: target }), }) + .then(res => res.text()) .then((body) => { expect(body).toEqual(""); }) @@ -168,10 +162,11 @@ describe("API Tests", function () { it("POST /api/routes creates a new root route", function (done) { var port = 8998; var target = "http://127.0.0.1:" + port; - r({ + r('', { method: "POST", body: JSON.stringify({ target: target }), }) + .then(res => res.text()) .then((body) => { expect(body).toEqual(""); return proxy._routes.get("/"); @@ -192,7 +187,8 @@ describe("API Tests", function () { .addTarget(proxy, path, port, null, null) .then(() => proxy._routes.get(path)) .then((route) => expect(route.target).toEqual(target)) - .then(() => r.del(apiUrl + path)) + .then(() => r(path, { url: apiUrl, method: "DELETE" })) + .then(res => res.text()) .then((body) => expect(body).toEqual("")) .then(() => proxy._routes.get(path)) .then((deletedRoute) => expect(deletedRoute).toBe(undefined)) @@ -200,9 +196,8 @@ describe("API Tests", function () { }); it("GET /api/routes?inactiveSince= with bad value returns a 400", function (done) { - r({ path: "?inactiveSince=endoftheuniverse" }) - .then(() => done.fail("Expected 400")) - .catch((err) => expect(err.statusCode).toEqual(400)) + r("?inactiveSince=endoftheuniverse") + .then((res) => expect(res.status).toEqual(400)) .then(done); }); @@ -240,8 +235,7 @@ describe("API Tests", function () { var seen = 0; var doReq = function (i) { var t = tests[i]; - return r({ path: "?inactiveSince=" + t.since.toISOString() }).then(function (body) { - var routes = JSON.parse(body); + return r("?inactiveSince=" + t.since.toISOString()).then(res => res.json()).then(function (routes) { var routeKeys = Object.keys(routes); var expectedKeys = Object.keys(t.expected); diff --git a/test/cli_spec.js b/test/cli_spec.js index 90e19e6..d4eb0a8 100644 --- a/test/cli_spec.js +++ b/test/cli_spec.js @@ -151,6 +151,8 @@ describe("CLI Tests", function () { }) ); done(); + }).catch(err => { + done.fail(err); }); }); }); @@ -173,12 +175,12 @@ describe("CLI Tests", function () { ]; executeCLI(execCmd, args).then((cliProcess) => { childProcess = cliProcess; - fetch(redirectUrl) + fetch(redirectUrl, { redirect: 'manual'}) .then((res) => { - expect(res.statusCode).toEqual(301); - expect(res.response.headers.location).toContain(SSLproxyUrl); + expect(res.status).toEqual(301); + expect(res.headers.get('location')).toContain(SSLproxyUrl); }); - fetch({ url: redirectUrl, redirect: 'follow' }).then(res => res.json()).then((body) => { + fetch(redirectUrl, { redirect: 'follow' }).then(res => res.json()).then((body) => { expect(body).toEqual( jasmine.objectContaining({ name: "default", @@ -209,10 +211,10 @@ describe("CLI Tests", function () { ]; executeCLI(execCmd, args).then((cliProcess) => { childProcess = cliProcess; - fetch(redirectUrl) + fetch(redirectUrl, { redirect: 'manual'}) .then((res) => { - expect(res.statusCode).toEqual(301); - expect(res.response.headers.location).toContain(redirectToUrl); + expect(res.status).toEqual(301); + expect(res.headers.get('location')).toContain(redirectToUrl); done(); }); }); diff --git a/test/proxy_spec.js b/test/proxy_spec.js index 5bcb391..9d5092d 100644 --- a/test/proxy_spec.js +++ b/test/proxy_spec.js @@ -81,16 +81,15 @@ describe("Proxy Tests", function () { }); it("keep-alive proxy request", function (done) { - var agent = new http.Agent({ keepAlive: true }); - r(proxyUrl, { agent: agent, resolveWithFullResponse: true }).then((res) => { - agent.destroy(); - var body = JSON.parse(res.body); + fetch(proxyUrl).then(res => { + expect(res.headers.get("connection")).toEqual("keep-alive"); + return res.json(); + }).then((body) => { expect(body).toEqual( jasmine.objectContaining({ path: "/", }) ); - expect(res.headers["connection"]).toEqual("keep-alive"); done(); }); }); @@ -102,18 +101,21 @@ describe("Proxy Tests", function () { called.proxyRequest = true; }); - r(proxyUrl) + fetch(proxyUrl) + .then(res => { + expect(res.status).toBe(200); + return res.json(); + }) .then(function (body) { - body = JSON.parse(body); - expect(called.proxyRequest).toBe(true); - expect(body).toEqual( + expect(body.headers).toEqual( jasmine.objectContaining({ - path: "/", + testing: "Test Passed", }) ); - expect(body.headers).toEqual( + expect(called.proxyRequest).toBe(true); + expect(body).toEqual( jasmine.objectContaining({ - testing: "Test Passed", + path: "/", }) ); }) @@ -123,9 +125,9 @@ describe("Proxy Tests", function () { it("target path is prepended by default", function (done) { util .addTarget(proxy, "/bar", testPort, false, "/foo") - .then(() => r(proxyUrl + "/bar/rest/of/it")) + .then(() => fetch(proxyUrl + "/bar/rest/of/it")) + .then(res => res.json()) .then((body) => { - body = JSON.parse(body); expect(body).toEqual( jasmine.objectContaining({ path: "/bar", @@ -139,9 +141,9 @@ describe("Proxy Tests", function () { it("/prefix?query is proxied correctly", function (done) { util .addTarget(proxy, "/bar", testPort, null, "/foo") - .then(() => r(proxyUrl + "/bar?query=foo")) + .then(() => fetch(proxyUrl + "/bar?query=foo")) + .then(res => res.json()) .then((body) => { - body = JSON.parse(body); expect(body).toEqual( jasmine.objectContaining({ target: "http://127.0.0.1:" + testPort + "/foo", @@ -156,9 +158,9 @@ describe("Proxy Tests", function () { it("handle URI encoding", function (done) { util .addTarget(proxy, "/b@r/b r", testPort, false, "/foo") - .then(() => r(proxyUrl + "/b%40r/b%20r/rest/of/it")) + .then(() => fetch(proxyUrl + "/b%40r/b%20r/rest/of/it")) + .then(res => res.json()) .then((body) => { - body = JSON.parse(body); expect(body).toEqual( jasmine.objectContaining({ path: "/b@r/b r", @@ -172,9 +174,9 @@ describe("Proxy Tests", function () { it("handle @ in URI same as %40", function (done) { util .addTarget(proxy, "/b@r/b r", testPort, false, "/foo") - .then(() => r(proxyUrl + "/b@r/b%20r/rest/of/it")) + .then(() => fetch(proxyUrl + "/b@r/b%20r/rest/of/it")) + .then(res => res.json()) .then((body) => { - body = JSON.parse(body); expect(body).toEqual( jasmine.objectContaining({ path: "/b@r/b r", @@ -189,9 +191,9 @@ describe("Proxy Tests", function () { proxy.proxy.options.prependPath = false; util .addTarget(proxy, "/bar", testPort, false, "/foo") - .then(() => r(proxyUrl + "/bar/rest/of/it")) + .then(() => fetch(proxyUrl + "/bar/rest/of/it")) + .then(res => res.json()) .then((body) => { - body = JSON.parse(body); expect(body).toEqual( jasmine.objectContaining({ path: "/bar", @@ -206,9 +208,9 @@ describe("Proxy Tests", function () { proxy.includePrefix = false; util .addTarget(proxy, "/bar", testPort, false, "/foo") - .then(() => r(proxyUrl + "/bar/rest/of/it")) + .then(() => fetch(proxyUrl + "/bar/rest/of/it")) + .then(res => res.json()) .then((body) => { - body = JSON.parse(body); expect(body).toEqual( jasmine.objectContaining({ path: "/bar", @@ -258,9 +260,9 @@ describe("Proxy Tests", function () { proxy.proxy.options.prependPath = false; util .addTarget(proxy, "/bar", testPort, false, "/foo") - .then(() => r(proxyUrl + "/bar/rest/of/it")) + .then(() => fetch(proxyUrl + "/bar/rest/of/it")) + .then(res => res.json()) .then((body) => { - body = JSON.parse(body); expect(body).toEqual( jasmine.objectContaining({ path: "/bar", @@ -275,9 +277,9 @@ describe("Proxy Tests", function () { proxy.hostRouting = true; util .addTarget(proxy, "/" + hostTest, testPort, false, null) - .then(() => r(hostUrl + "/some/path")) + .then(() => fetch(hostUrl + "/some/path")) + .then(res => res.json()) .then((body) => { - body = JSON.parse(body); expect(body).toEqual( jasmine.objectContaining({ target: "http://127.0.0.1:" + testPort, @@ -308,10 +310,9 @@ describe("Proxy Tests", function () { proxy._routes.update("/missing", { last_activity: firstActivity }); }) // fail a web request - .then(() => r(hostUrl + "/missing/prefix")) - .then((body) => done.fail("Expected 503")) - .catch((err) => { - expect(err.statusCode).toEqual(503); + .then(() => fetch(hostUrl + "/missing/prefix")) + .then((res) => { + expect(res.status).toEqual(503); }) // check that activity was not updated .then(expectNoActivity) @@ -336,12 +337,13 @@ describe("Proxy Tests", function () { var proxyPort = 55550; util .setupProxy(proxyPort, { errorTarget: "http://127.0.0.1:55565" }, []) - .then(() => r("http://127.0.0.1:" + proxyPort + "/foo/bar")) - .then((body) => done.fail("Expected 404")) - .catch((err) => { - expect(err.statusCode).toEqual(404); - expect(err.response.headers["content-type"]).toEqual("text/plain"); - expect(err.response.body).toEqual("/foo/bar"); + .then(() => fetch("http://127.0.0.1:" + proxyPort + "/foo/bar")) + .then((res) => { + expect(res.status).toEqual(404); + expect(res.headers.get("content-type")).toEqual("text/plain"); + return res.text(); + }).then(body => { + expect(body).toEqual("/foo/bar"); }) .then(done); }); @@ -351,19 +353,21 @@ describe("Proxy Tests", function () { proxy .removeRoute("/") .then(() => proxy.addRoute("/missing", { target: "https://127.0.0.1:54321" })) - .then(() => r(hostUrl + "/nope")) - .then((body) => done.fail("Expected 404")) - .catch((err) => { - expect(err.statusCode).toEqual(404); - expect(err.response.headers["content-type"]).toEqual("text/html"); - expect(err.response.body).toMatch(/404'D/); + .then(() => fetch(hostUrl + "/nope")) + .then((res) => { + expect(res.status).toEqual(404); + expect(res.headers.get("content-type")).toEqual("text/html"); + return res.text(); + }).then(body => { + expect(body).toMatch(/404'D/); }) - .then(() => r(hostUrl + "/missing/prefix")) - .then((body) => done.fail("Expected 503")) - .catch((err) => { - expect(err.statusCode).toEqual(503); - expect(err.response.headers["content-type"]).toEqual("text/html"); - expect(err.response.body).toMatch(/UNKNOWN/); + .then(() => fetch(hostUrl + "/missing/prefix")) + .then((res) => { + expect(res.status).toEqual(503); + expect(res.headers.get("content-type")).toEqual("text/html"); + return res.text(); + }).then(body => { + expect(body).toMatch(/UNKNOWN/); }) .then(done); }); @@ -372,19 +376,21 @@ describe("Proxy Tests", function () { proxy.removeRoute("/"); proxy .addRoute("/missing", { target: "https://127.0.0.1:54321" }) - .then(() => r(hostUrl + "/nope")) - .then((body) => done.fail("Expected 404")) - .catch((err) => { - expect(err.statusCode).toEqual(404); - expect(err.response.headers["content-type"]).toEqual("text/html"); - expect(err.response.body).toMatch(/404:/); + .then(() => fetch(hostUrl + "/nope")) + .then((res) => { + expect(res.status).toEqual(404); + expect(res.headers.get("content-type")).toEqual("text/html"); + return res.text(); + }).then(body => { + expect(body).toMatch(/404:/); }) - .then(() => r(hostUrl + "/missing/prefix")) - .then((body) => done.fail("Expected 503")) - .catch((err) => { - expect(err.statusCode).toEqual(503); - expect(err.response.headers["content-type"]).toEqual("text/html"); - expect(err.response.body).toMatch(/503:/); + .then(() => fetch(hostUrl + "/missing/prefix")) + .then((res) => { + expect(res.status).toEqual(503); + expect(res.headers.get("content-type")).toEqual("text/html"); + return res.text(); + }).then(body => { + expect(body).toMatch(/503:/); }) .then(done); }); @@ -393,12 +399,13 @@ describe("Proxy Tests", function () { var proxyPort = 55550; util .setupProxy(proxyPort, { errorTarget: "http://127.0.0.1:55565" }, []) - .then(() => r("http://127.0.0.1:" + proxyPort + "/%")) - .then((body) => done.fail("Expected 500")) - .catch((err) => { - expect(err.statusCode).toEqual(500); - expect(err.response.headers["content-type"]).toEqual("text/plain"); - expect(err.response.body).toEqual("/%"); + .then(() => fetch("http://127.0.0.1:" + proxyPort + "/%")) + .then((res) => { + expect(res.status).toEqual(500); + expect(res.headers.get("content-type")).toEqual("text/plain"); + return res.text(); + }).then(body => { + expect(body).toEqual("/%"); }) .then(done); }); @@ -407,11 +414,10 @@ describe("Proxy Tests", function () { var redirectTo = "http://foo.com:12345/whatever"; util .addTargetRedirecting(proxy, "/external/urlpath/", testPort, "/internal/urlpath/", redirectTo) - .then(() => r(proxyUrl + "/external/urlpath/rest/of/it")) - .then((body) => done.fail("Expected 301")) - .catch((err) => { - expect(err.statusCode).toEqual(301); - expect(err.response.headers.location).toEqual(redirectTo); + .then(() => fetch(proxyUrl + "/external/urlpath/rest/of/it", { redirect: 'manual'})) + .then((res) => { + expect(res.status).toEqual(301); + expect(res.headers.get('location')).toEqual(redirectTo); }) .then(done); }); @@ -439,18 +445,19 @@ describe("Proxy Tests", function () { redirectTo ) ) - .then(() => r("http://127.0.0.1:" + proxyPort + "/external/urlpath/")) - .then((body) => done.fail("Expected 301")) - .catch((err) => { - expect(err.statusCode).toEqual(301); - expect(err.response.headers.location).toEqual(expectedRedirect); + .then(() => fetch("http://127.0.0.1:" + proxyPort + "/external/urlpath/", { redirect: 'manual'})) + .then((res) => { + expect(res.status).toEqual(301); + expect(res.headers.get('location')).toEqual(expectedRedirect); + }) + .catch(err => { + done.fail(err); }) .then(done); }); it("health check request", function (done) { - r(proxyUrl + "/_chp_healthz").then((body) => { - body = JSON.parse(body); + fetch(proxyUrl + "/_chp_healthz").then(res => res.json()).then((body) => { expect(body).toEqual({ status: "OK" }); done(); });