-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path_all_servers_test.js
More file actions
129 lines (94 loc) · 3.91 KB
/
_all_servers_test.js
File metadata and controls
129 lines (94 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright Titanium I.T. LLC.
import assert from "util/assert.js";
import * as ensure from "util/ensure.js";
import { AllServers } from "./all_servers.js";
import { CommandLine } from "infrastructure/command_line.js";
import { HttpServer } from "http/http_server.js";
import { HttpServerRequest } from "http/http_server_request.js";
import { Log } from "infrastructure/log.js";
import { WwwRouter } from "./www/www_router.js";
import { Rot13Router } from "./rot13_service/rot13_router.js";
const VALID_ARGS = [ "1000", "2000" ];
describe("All servers", () => {
describe("happy path", () => {
it("starts servers", async () => {
const { wwwServer, rot13Server } = await startAsync();
assert.equal(wwwServer.isStarted, true, "WWW server should be started");
assert.equal(rot13Server.isStarted, true, "ROT-13 service should be started");
});
it("routes WWW requests", async () => {
const { wwwServer } = await startAsync();
const request = HttpServerRequest.createNull({ url: "/", method: "GET" });
const expectedResponse = await WwwRouter.createNull().routeAsync(request);
const actualResponse = await wwwServer.simulateRequestAsync(request);
assert.deepEqual(actualResponse, expectedResponse);
});
it("routes ROT-13 service requests", async () => {
const { rot13Server } = await startAsync();
const request = HttpServerRequest.createNull({ url: "/rot13/transform", method: "POST" });
const expectedResponse = await Rot13Router.createNull().routeAsync(request);
const actualResponse = await rot13Server.simulateRequestAsync(request);
assert.deepEqual(actualResponse, expectedResponse);
});
it("uses ports provided on command line", async () => {
const { wwwServer, rot13Server } = await startAsync({ args: [ "5001", "5002" ] });
assert.equal(wwwServer.port, 5001, "www port");
assert.equal(rot13Server.port, 5002, "ROT-13 port");
});
it("provides WWW router with ROT-13 service port", async () => {
const { wwwRouter } = await startAsync({ args: [ "5001", "5002" ]});
assert.equal(wwwRouter.rot13ServicePort, 5002, "port");
});
it("binds node name to router logs", async () => {
const { wwwRouter, rot13Router } = await startAsync();
assert.deepEqual(wwwRouter.log.defaults, { node: "www" });
assert.deepEqual(rot13Router.log.defaults, { node: "rot13" });
});
});
describe("error handling", () => {
it("logs error if wrong number of arguments provided", async () => {
const { logOutput } = await startAsync({ args: [ "one", "two", "three" ] });
assert.deepEqual(logOutput.data, [{
alert: "emergency",
message: "startup error",
error: `Error: invalid command-line arguments (${AllServers.USAGE})`,
commandLineArguments: [ "one", "two", "three" ],
}]);
});
it("logs error if ports aren't numbers", async () => {
const { logOutput: wwwLog } = await startAsync({ args: [ "xxx", "1000" ] });
const { logOutput: rot13Log } = await startAsync({ args: [ "1000", "xxx" ] });
assertLogError(wwwLog, "www", [ "xxx", "1000" ]);
assertLogError(rot13Log, "ROT-13", [ "1000", "xxx" ]);
function assertLogError(logOutput, serverName, args) {
assert.deepEqual(logOutput.data, [{
alert: "emergency",
message: "startup error",
commandLineArguments: args,
error: `Error: ${serverName} server port is not a number`,
}]);
}
});
});
});
async function startAsync({
args = VALID_ARGS,
} = {}) {
ensure.signature(arguments, [[ undefined, {
args: [ undefined, Array ],
}]]);
const log = Log.createNull();
const logOutput = log.trackOutput();
const commandLine = CommandLine.createNull({ args });
const wwwServer = HttpServer.createNull();
const rot13Server = HttpServer.createNull();
const servers = new AllServers(log, commandLine, wwwServer, rot13Server);
await servers.startAsync();
return {
wwwServer,
rot13Server,
wwwRouter: servers._wwwRouter,
rot13Router: servers._rot13Router,
logOutput,
};
}