Skip to content

Commit 883d377

Browse files
Unit tests add for Server and LoggedServer classes
1 parent 31f6403 commit 883d377

File tree

6 files changed

+610
-79
lines changed

6 files changed

+610
-79
lines changed

README.md

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ Your endpoints must implement `Endpoint` class interface (`route()` and `async h
99

1010
```javascript
1111
const http = require('node:http');
12-
const cluster = require('node:cluster');
1312

1413
const {
1514
Server,
16-
ClusteredServer,
1715
LoggedServer,
1816
InputRequest,
1917
JsonInputRequest,
@@ -26,26 +24,52 @@ const {
2624
Endpoints
2725
} = require('objective-http').server;
2826

29-
new ClusteredServer(
30-
new LoggedServer(
31-
new Server(
32-
new Endpoints([
33-
new MyFirstEndpoint(new LoggedEndpoint(new Endpoint(), console)),
34-
new MySecondEndpoint(new LoggedEndpoint(new Endpoint(), console)),
35-
new MyThirdEndpoint(new LoggedEndpoint(new Endpoint(), console))
36-
]),
37-
{port: server_port},
38-
new LoggedInputRequest(new JsonInputRequest(new InputRequest()), console),
39-
new LoggedOutputResponse(new JsonOutputResponse(new OutputResponse()), console),
40-
http
41-
),
42-
console
27+
new LoggedServer(
28+
new Server(
29+
new Endpoints([
30+
new MyFirstEndpoint(new LoggedEndpoint(new Endpoint(), console)),
31+
new MySecondEndpoint(new LoggedEndpoint(new Endpoint(), console)),
32+
new MyThirdEndpoint(new LoggedEndpoint(new Endpoint(), console))
33+
]),
34+
{port: server_port},
35+
new LoggedInputRequest(new JsonInputRequest(new InputRequest()), console),
36+
new LoggedOutputResponse(new JsonOutputResponse(new OutputResponse()), console),
37+
http
4338
),
44-
cluster,
45-
{workers: workers_count}
39+
console
4640
).start();
4741
```
4842

43+
`MyEndpoint` class example:
44+
45+
```javascript
46+
class MyEndpoint {
47+
route() {
48+
return {
49+
method: 'GET',
50+
path: '/test'
51+
};
52+
}
53+
54+
async handle(request) {
55+
try {
56+
const processResult = await someProcess();
57+
58+
return {
59+
statusCode: 200,
60+
body: processResult.toString()
61+
};
62+
63+
} catch (e) {
64+
return {
65+
statusCode: 404,
66+
body: 'process does not found anything'
67+
};
68+
}
69+
}
70+
}
71+
72+
```
4973

5074
## Client
5175

@@ -90,8 +114,7 @@ console.log(response.body().toString());
90114

91115
`server` and `client` packages support Bun by default.
92116
But there ara special `bun` package with native [Bun API](https://bun.sh/docs/runtime/bun-apis) implementation (like `Bun.serve()`).
93-
And you should replace `node:http` package with `objective-http.bun.bunttp` in your `Server` configuration.
94-
[Don't use](https://bun.sh/docs/runtime/nodejs-apis#node-cluster) `ClusteredServer` with `Bun`!!!
117+
And you should replace `node:http` package with `objective-http.bun.bunttp` in your `Server` configuration.
95118

96119

97120
### Server Bun usage

src/js/server/ClusteredServer.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/js/server/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module.exports = {
22
Server: require('./Server'),
33
LoggedServer: require('./LoggedServer'),
4-
ClusteredServer: require('./ClusteredServer'),
54
Endpoint: require('./endpoint/Endpoint'),
65
LoggedEndpoint: require('./endpoint/LoggedEndpoint'),
76
Endpoints: require('./endpoint/Endpoints'),

src/test/js/server/ClusteredServer.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/test/js/server/LoggedServer.js

Lines changed: 187 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
11
/* node:coverage disable */
22

3-
const {LoggedServer} = require('../../../js/index').server;
43
const {describe, it, mock, beforeEach, afterEach} = require('node:test');
54
const assert = require('node:assert');
65

6+
const {LoggedServer} = require('../../../js').server;
7+
8+
const diagnosticLogger = {
9+
debug() {
10+
}
11+
}
12+
13+
const diagnosticOrigin = {
14+
start() {
15+
},
16+
stop() {
17+
},
18+
options() {
19+
}
20+
}
721

822
function prepareDiagnostic() {
23+
diagnosticLogger.options = {};
24+
diagnosticLogger.debug = (message) => {
25+
diagnosticLogger.options.message = message;
26+
return diagnosticLogger;
27+
}
28+
29+
mock.method(diagnosticLogger, 'debug');
30+
31+
diagnosticOrigin.start = () => {
32+
return diagnosticOrigin;
33+
}
34+
diagnosticOrigin.stop = () => {
35+
return diagnosticOrigin;
36+
}
37+
diagnosticOrigin.options = () => {
38+
return {port: 80};
39+
}
40+
41+
mock.method(diagnosticOrigin, 'start');
42+
mock.method(diagnosticOrigin, 'stop');
43+
mock.method(diagnosticOrigin, 'options');
944
}
1045

1146
function resetDiagnostic() {
@@ -20,7 +55,158 @@ describe('LoggedServer', () => {
2055
it('should not call anything', () => {
2156
assert.doesNotThrow(() => {
2257
new LoggedServer();
58+
new LoggedServer(diagnosticOrigin, diagnosticLogger);
2359
});
2460
});
2561
});
62+
63+
describe('start', () => {
64+
it('should fall, cause origin is null', async () => {
65+
await assert.rejects(() => new LoggedServer().start(),
66+
{name: 'TypeError'});
67+
});
68+
69+
it('should fall, cause origin start throws error', async () => {
70+
diagnosticOrigin.start = () => {throw new Error('start error')};
71+
mock.method(diagnosticOrigin, 'start');
72+
73+
await assert.rejects(() => new LoggedServer(diagnosticOrigin).start(),
74+
{message: 'start error'});
75+
76+
assert.strictEqual(diagnosticOrigin.start.mock.calls.length , 1);
77+
assert.strictEqual(diagnosticOrigin.options.mock.calls.length , 0);
78+
});
79+
80+
it('should fall, cause origin options throws error', async () => {
81+
diagnosticOrigin.options = () => {throw new Error('options error')};
82+
mock.method(diagnosticOrigin, 'options');
83+
84+
await assert.rejects(() => new LoggedServer(diagnosticOrigin, diagnosticLogger).start(),
85+
{message: 'options error'});
86+
87+
assert.strictEqual(diagnosticOrigin.start.mock.calls.length , 1);
88+
assert.strictEqual(diagnosticOrigin.options.mock.calls.length , 1);
89+
});
90+
91+
it('should fall, cause logger is null', async () => {
92+
await assert.rejects(() => new LoggedServer(diagnosticOrigin).start(),
93+
{name: 'TypeError'});
94+
95+
assert.strictEqual(diagnosticOrigin.start.mock.calls.length , 1);
96+
assert.strictEqual(diagnosticOrigin.options.mock.calls.length , 0);
97+
});
98+
99+
it('should fall, cause logger debug throws error', async () => {
100+
diagnosticLogger.debug = () => {throw new Error('debug error')};
101+
mock.method(diagnosticLogger, 'debug');
102+
103+
await assert.rejects(() => new LoggedServer(diagnosticOrigin, diagnosticLogger).start(),
104+
{message: 'debug error'});
105+
106+
assert.strictEqual(diagnosticOrigin.start.mock.calls.length , 1);
107+
assert.strictEqual(diagnosticOrigin.options.mock.calls.length , 1);
108+
assert.strictEqual(diagnosticLogger.debug.mock.calls.length , 1);
109+
});
110+
111+
it('should not fall', async () => {
112+
await assert.doesNotReject(() => new LoggedServer(diagnosticOrigin, diagnosticLogger).start(),);
113+
114+
assert.strictEqual(diagnosticOrigin.start.mock.calls.length , 1);
115+
assert.strictEqual(diagnosticOrigin.options.mock.calls.length , 1);
116+
assert.strictEqual(diagnosticLogger.debug.mock.calls.length , 1);
117+
});
118+
119+
it('should logg message', async () => {
120+
await assert.doesNotReject(() => new LoggedServer(diagnosticOrigin, diagnosticLogger).start());
121+
122+
assert.strictEqual(diagnosticLogger.options.message, 'HttpServer is running at port: 80');
123+
});
124+
125+
it('should return new Server instance', async () => {
126+
const server = new LoggedServer(diagnosticOrigin, diagnosticLogger);
127+
const startedServer = await server.start();
128+
129+
assert.notEqual(server, startedServer);
130+
assert.strictEqual(typeof server, typeof startedServer)
131+
});
132+
});
133+
134+
describe('stop', () => {
135+
it('should fall, cause origin is null', async () => {
136+
await assert.rejects(() => new LoggedServer().stop(),
137+
{name: 'TypeError'});
138+
});
139+
140+
it('should fall, cause origin stop throws error', async () => {
141+
diagnosticOrigin.stop = () => {throw new Error('stop error')};
142+
mock.method(diagnosticOrigin, 'stop');
143+
144+
await assert.rejects(() => new LoggedServer(diagnosticOrigin).stop(),
145+
{message: 'stop error'});
146+
147+
assert.strictEqual(diagnosticOrigin.stop.mock.calls.length , 1);
148+
assert.strictEqual(diagnosticOrigin.options.mock.calls.length , 0);
149+
});
150+
151+
it('should fall, cause origin options throws error', async () => {
152+
diagnosticOrigin.options = () => {throw new Error('options error')};
153+
mock.method(diagnosticOrigin, 'options');
154+
155+
await assert.rejects(() => new LoggedServer(diagnosticOrigin, diagnosticLogger).stop(),
156+
{message: 'options error'});
157+
158+
assert.strictEqual(diagnosticOrigin.stop.mock.calls.length , 1);
159+
assert.strictEqual(diagnosticOrigin.options.mock.calls.length , 1);
160+
});
161+
162+
it('should fall, cause logger is null', async () => {
163+
await assert.rejects(() => new LoggedServer(diagnosticOrigin).stop(),
164+
{name: 'TypeError'});
165+
166+
assert.strictEqual(diagnosticOrigin.stop.mock.calls.length , 1);
167+
assert.strictEqual(diagnosticOrigin.options.mock.calls.length , 0);
168+
});
169+
170+
it('should fall, cause logger debug throws error', async () => {
171+
diagnosticLogger.debug = () => {throw new Error('debug error')};
172+
mock.method(diagnosticLogger, 'debug');
173+
174+
await assert.rejects(() => new LoggedServer(diagnosticOrigin, diagnosticLogger).stop(),
175+
{message: 'debug error'});
176+
177+
assert.strictEqual(diagnosticOrigin.stop.mock.calls.length , 1);
178+
assert.strictEqual(diagnosticOrigin.options.mock.calls.length , 1);
179+
assert.strictEqual(diagnosticLogger.debug.mock.calls.length , 1);
180+
});
181+
182+
it('should not fall', async () => {
183+
await assert.doesNotReject(() => new LoggedServer(diagnosticOrigin, diagnosticLogger).stop(),);
184+
185+
assert.strictEqual(diagnosticOrigin.stop.mock.calls.length , 1);
186+
assert.strictEqual(diagnosticOrigin.options.mock.calls.length , 1);
187+
assert.strictEqual(diagnosticLogger.debug.mock.calls.length , 1);
188+
});
189+
190+
it('should logg message', async () => {
191+
await assert.doesNotReject(() => new LoggedServer(diagnosticOrigin, diagnosticLogger).stop());
192+
193+
assert.strictEqual(diagnosticLogger.options.message, 'HttpServer at port: 80 is stopped');
194+
});
195+
196+
it('should return new Server instance', async () => {
197+
const server = new LoggedServer(diagnosticOrigin, diagnosticLogger);
198+
const stoppedServer = await server.stop();
199+
200+
assert.notEqual(server, stoppedServer);
201+
assert.strictEqual(typeof server, typeof stoppedServer)
202+
});
203+
});
204+
205+
describe('options', () => {
206+
it('should return same options', () => {
207+
const resultOptions = new LoggedServer(diagnosticOrigin, diagnosticLogger).options();
208+
209+
assert.deepStrictEqual(resultOptions, diagnosticOrigin.options());
210+
});
211+
});
26212
});

0 commit comments

Comments
 (0)