Skip to content

Commit fbb855a

Browse files
committed
test(server): added mock server implementation tests
1 parent 479724c commit fbb855a

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed

test/server/__snapshots__/serverMode-option.test.js.snap

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,77 @@ Array [
77
"close",
88
]
99
`;
10+
11+
exports[`serverMode option server should close client with bad headers 1`] = `
12+
Array [
13+
Array [
14+
[Function],
15+
],
16+
]
17+
`;
18+
19+
exports[`serverMode option server should close client with bad headers 2`] = `
20+
Array [
21+
Array [
22+
Object {
23+
"foo": "bar",
24+
},
25+
"{\\"type\\":\\"error\\",\\"data\\":\\"Invalid Host/Origin header\\"}",
26+
],
27+
]
28+
`;
29+
30+
exports[`serverMode option server should close client with bad headers 3`] = `
31+
Array [
32+
Array [
33+
Object {
34+
"foo": "bar",
35+
},
36+
],
37+
]
38+
`;
39+
40+
exports[`serverMode option server should use server implementation correctly 1`] = `
41+
Array [
42+
Object {
43+
"foo": "bar",
44+
},
45+
]
46+
`;
47+
48+
exports[`serverMode option server should use server implementation correctly 2`] = `
49+
Array [
50+
Array [
51+
[Function],
52+
],
53+
]
54+
`;
55+
56+
exports[`serverMode option server should use server implementation correctly 3`] = `
57+
Array [
58+
Object {
59+
"foo": "bar",
60+
},
61+
"{\\"type\\":\\"liveReload\\"}",
62+
]
63+
`;
64+
65+
exports[`serverMode option server should use server implementation correctly 4`] = `
66+
Array [
67+
Object {
68+
"foo": "bar",
69+
},
70+
"{\\"type\\":\\"ok\\"}",
71+
]
72+
`;
73+
74+
exports[`serverMode option server should use server implementation correctly 5`] = `
75+
Array [
76+
Array [
77+
Object {
78+
"foo": "bar",
79+
},
80+
[Function],
81+
],
82+
]
83+
`;

test/server/serverMode-option.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,91 @@ describe('serverMode option', () => {
305305
});
306306
});
307307
});
308+
309+
describe('server', () => {
310+
let MockSockJSServer;
311+
beforeEach((done) => {
312+
jest.mock('../../lib/servers/SockJSServer');
313+
// eslint-disable-next-line global-require
314+
mockedTestServer = require('../helpers/test-server');
315+
// eslint-disable-next-line global-require
316+
MockSockJSServer = require('../../lib/servers/SockJSServer');
317+
318+
server = mockedTestServer.start(
319+
config,
320+
{
321+
port,
322+
},
323+
done
324+
);
325+
});
326+
327+
afterEach((done) => {
328+
mockedTestServer.close(done);
329+
jest.resetAllMocks();
330+
jest.resetModules();
331+
332+
server = null;
333+
});
334+
335+
it('should use server implementation correctly', () => {
336+
const mockServerInstance = MockSockJSServer.mock.instances[0];
337+
338+
const connectionObj = {
339+
foo: 'bar',
340+
};
341+
// this simulates a client connecting to the server
342+
mockServerInstance.onConnection.mock.calls[0][0](connectionObj, {
343+
host: `localhost:${port}`,
344+
origin: `http://localhost:${port}`,
345+
});
346+
347+
expect(server.sockets.length).toEqual(1);
348+
expect(server.sockets).toMatchSnapshot();
349+
350+
// this simulates a client leaving the server
351+
mockServerInstance.onConnectionClose.mock.calls[0][1](connectionObj);
352+
353+
expect(server.sockets.length).toEqual(0);
354+
355+
// check that the dev server was passed to the socket server implementation constructor
356+
expect(MockSockJSServer.mock.calls[0].length).toEqual(1);
357+
expect(MockSockJSServer.mock.calls[0][0].options.port).toEqual(port);
358+
359+
expect(mockServerInstance.onConnection.mock.calls).toMatchSnapshot();
360+
expect(mockServerInstance.send.mock.calls.length).toEqual(3);
361+
// call 0 to the send() method is liveReload
362+
expect(mockServerInstance.send.mock.calls[0]).toMatchSnapshot();
363+
// call 1 to the send() method is hash data, so we skip it
364+
// call 2 to the send() method is the "ok" message
365+
expect(mockServerInstance.send.mock.calls[2]).toMatchSnapshot();
366+
// close should not be called because the server never forcefully closes
367+
// a successful client connection
368+
expect(mockServerInstance.close.mock.calls.length).toEqual(0);
369+
expect(mockServerInstance.onConnectionClose.mock.calls).toMatchSnapshot();
370+
});
371+
372+
it('should close client with bad headers', () => {
373+
const mockServerInstance = MockSockJSServer.mock.instances[0];
374+
375+
// this simulates a client connecting to the server
376+
mockServerInstance.onConnection.mock.calls[0][0](
377+
{
378+
foo: 'bar',
379+
},
380+
{
381+
host: null,
382+
}
383+
);
384+
expect(server.sockets.length).toEqual(0);
385+
expect(MockSockJSServer.mock.calls[0].length).toEqual(1);
386+
expect(MockSockJSServer.mock.calls[0][0].options.port).toEqual(port);
387+
expect(mockServerInstance.onConnection.mock.calls).toMatchSnapshot();
388+
// the only call to send() here should be an invalid header message
389+
expect(mockServerInstance.send.mock.calls).toMatchSnapshot();
390+
expect(mockServerInstance.close.mock.calls).toMatchSnapshot();
391+
// onConnectionClose should never get called since the client should be closed first
392+
expect(mockServerInstance.onConnectionClose.mock.calls.length).toEqual(0);
393+
});
394+
});
308395
});

0 commit comments

Comments
 (0)