From c604dead9bf154547a6bd607d9d58f23009b9e32 Mon Sep 17 00:00:00 2001 From: Min RK Date: Fri, 5 Mar 2021 13:32:15 +0100 Subject: [PATCH] test last-activity behavior for errors and basic requests --- lib/testutil.js | 7 +++++- test/proxy_spec.js | 59 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 4 deletions(-) diff --git a/lib/testutil.js b/lib/testutil.js index c431a668..9717f52b 100644 --- a/lib/testutil.js +++ b/lib/testutil.js @@ -45,7 +45,11 @@ var addTarget = (exports.addTarget = function (proxy, path, port, websocket, tar server.listen(port); servers.push(server); - return proxy.addRoute(path, { target: target }); + return proxy.addRoute(path, { target: target }).then(() => { + // routes are created with an activity timestamp artificially shifted into the past + // so that activity can more easily be measured + return proxy._routes.update(path, { last_activity: proxy._setup_timestamp }); + }); }); var addTargetRedirecting = (exports.addTargetRedirecting = function ( @@ -90,6 +94,7 @@ exports.setupProxy = function (port, options, paths) { options.log = defaultLogger({ level: "error" }); var proxy = new configproxy.ConfigurableProxy(options); + proxy._setup_timestamp = new Date(new Date().getTime() - 60000); var ip = "127.0.0.1"; var countdown = 2; var resolvePromise; diff --git a/test/proxy_spec.js b/test/proxy_spec.js index ab1251b9..2dfbef66 100644 --- a/test/proxy_spec.js +++ b/test/proxy_spec.js @@ -43,7 +43,12 @@ describe("Proxy Tests", function () { path: "/", }) ); - done(); + + // check last_activity was updated + return proxy._routes.get("/").then((route) => { + expect(route.last_activity).toBeGreaterThan(proxy._setup_timestamp); + done(); + }); }); }); @@ -66,8 +71,12 @@ describe("Proxy Tests", function () { message: "hi", }) ); - ws.close(); - done(); + // check last_activity was updated + return proxy._routes.get("/").then((route) => { + expect(route.last_activity).toBeGreaterThan(proxy._setup_timestamp); + ws.close(); + done(); + }); } nmsgs++; }); @@ -269,6 +278,50 @@ describe("Proxy Tests", function () { .then(done); }); + it("last_activity not updated on errors", function (done) { + let now = new Date(); + // mock timestamp in the past + let firstActivity = new Date(now.getTime() - 60000); + + function expectNoActivity() { + return proxy._routes.get("/missing", (route) => { + expect(route.last_activity).toEqual(proxy._setup_timestamp); + }); + } + + proxy + .removeRoute("/") + // add a route to nowhere + .then(() => proxy.addRoute("/missing", { target: "https://127.0.0.1:54321" })) + .then(() => { + // set last_activity into the past + 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); + }) + // check that activity was not updated + .then(expectNoActivity) + // fail a websocket request + .then(() => { + var ws = new WebSocket("ws://127.0.0.1:" + port + "/missing/ws"); + ws.on("error", () => { + // expect this, since there is no websocket handler + // check last_activity was not updated + expectNoActivity().then((route) => { + ws.close(); + done(); + }); + }); + ws.on("open", () => { + done.fail("Expected websocket error"); + }); + }); + }); + it("custom error target", function (done) { var proxyPort = 55550; util