Skip to content

Commit ec998cd

Browse files
authored
Merge pull request #293 from minrk/activity-on-success
Add tests for last-activity updates
2 parents 15ac3af + c604dea commit ec998cd

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

lib/testutil.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ var addTarget = (exports.addTarget = function (proxy, path, port, websocket, tar
4545

4646
server.listen(port);
4747
servers.push(server);
48-
return proxy.addRoute(path, { target: target });
48+
return proxy.addRoute(path, { target: target }).then(() => {
49+
// routes are created with an activity timestamp artificially shifted into the past
50+
// so that activity can more easily be measured
51+
return proxy._routes.update(path, { last_activity: proxy._setup_timestamp });
52+
});
4953
});
5054

5155
var addTargetRedirecting = (exports.addTargetRedirecting = function (
@@ -90,6 +94,7 @@ exports.setupProxy = function (port, options, paths) {
9094
options.log = defaultLogger({ level: "error" });
9195

9296
var proxy = new configproxy.ConfigurableProxy(options);
97+
proxy._setup_timestamp = new Date(new Date().getTime() - 60000);
9398
var ip = "127.0.0.1";
9499
var countdown = 2;
95100
var resolvePromise;

test/proxy_spec.js

+56-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ describe("Proxy Tests", function () {
4343
path: "/",
4444
})
4545
);
46-
done();
46+
47+
// check last_activity was updated
48+
return proxy._routes.get("/").then((route) => {
49+
expect(route.last_activity).toBeGreaterThan(proxy._setup_timestamp);
50+
done();
51+
});
4752
});
4853
});
4954

@@ -66,8 +71,12 @@ describe("Proxy Tests", function () {
6671
message: "hi",
6772
})
6873
);
69-
ws.close();
70-
done();
74+
// check last_activity was updated
75+
return proxy._routes.get("/").then((route) => {
76+
expect(route.last_activity).toBeGreaterThan(proxy._setup_timestamp);
77+
ws.close();
78+
done();
79+
});
7180
}
7281
nmsgs++;
7382
});
@@ -269,6 +278,50 @@ describe("Proxy Tests", function () {
269278
.then(done);
270279
});
271280

281+
it("last_activity not updated on errors", function (done) {
282+
let now = new Date();
283+
// mock timestamp in the past
284+
let firstActivity = new Date(now.getTime() - 60000);
285+
286+
function expectNoActivity() {
287+
return proxy._routes.get("/missing", (route) => {
288+
expect(route.last_activity).toEqual(proxy._setup_timestamp);
289+
});
290+
}
291+
292+
proxy
293+
.removeRoute("/")
294+
// add a route to nowhere
295+
.then(() => proxy.addRoute("/missing", { target: "https://127.0.0.1:54321" }))
296+
.then(() => {
297+
// set last_activity into the past
298+
proxy._routes.update("/missing", { last_activity: firstActivity });
299+
})
300+
// fail a web request
301+
.then(() => r(hostUrl + "/missing/prefix"))
302+
.then((body) => done.fail("Expected 503"))
303+
.catch((err) => {
304+
expect(err.statusCode).toEqual(503);
305+
})
306+
// check that activity was not updated
307+
.then(expectNoActivity)
308+
// fail a websocket request
309+
.then(() => {
310+
var ws = new WebSocket("ws://127.0.0.1:" + port + "/missing/ws");
311+
ws.on("error", () => {
312+
// expect this, since there is no websocket handler
313+
// check last_activity was not updated
314+
expectNoActivity().then((route) => {
315+
ws.close();
316+
done();
317+
});
318+
});
319+
ws.on("open", () => {
320+
done.fail("Expected websocket error");
321+
});
322+
});
323+
});
324+
272325
it("custom error target", function (done) {
273326
var proxyPort = 55550;
274327
util

0 commit comments

Comments
 (0)