This repository has been archived by the owner on May 8, 2020. It is now read-only.
/
express.server.spec.ts
180 lines (139 loc) · 5.57 KB
/
express.server.spec.ts
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { Server, RouteConfig } from './abstract.server';
import { RemoteCli } from '../services/remoteCli.service';
import { Logger } from '../../common/services/logger.service';
import { addProviders, inject, async } from '@angular/core/testing';
import { LoggerMock } from '../../common/services/logger.service.mock';
import * as proxyquire from 'proxyquire';
import { Response } from '../controllers/response';
import { Request } from '../controllers/request';
import { RemoteCliMock } from '../services/remoteCli.service.mock';
import { AuthServiceMock } from '../services/authService.service.mock';
import { AuthService } from '../services/authService.service';
describe('Express Server', () => {
const expressConstructorSpy = jasmine.createSpy('expressConstructor');
const expressSpy = jasmine.createSpyObj('express', ['get']);
expressConstructorSpy.and.returnValue(expressSpy);
const httpCreateServerSpy = jasmine.createSpy('httpCreateServer');
const httpServerSpy = jasmine.createSpyObj('httpServer', ['listen']);
httpServerSpy.listen = jasmine.createSpy('httpServerListener');
httpCreateServerSpy.and.returnValue(httpServerSpy);
const mockedModule = proxyquire('./express.server', {
express: expressConstructorSpy,
http: {
createServer: httpCreateServerSpy
}
});
const providers = [
{
provide: Server,
deps: [Logger, RemoteCli],
useFactory: (logger: Logger, cli: RemoteCli) => {
return new mockedModule.ExpressServer(logger, cli);
}
},
{provide: Logger, useClass: LoggerMock},
{provide: RemoteCli, useClass: RemoteCliMock},
{provide: AuthService, useClass: AuthServiceMock},
];
beforeEach(() => {
addProviders(providers);
});
it('initialized http server with new express instance', inject([Server], (server: Server) => {
expect(expressConstructorSpy)
.toHaveBeenCalled();
expect(httpCreateServerSpy)
.toHaveBeenCalled();
expect(server.getEngine())
.toEqual(expressSpy);
}));
it('kicks off an http server when started', async(inject([Server], (server: Server) => {
const startPromise = server.start()
.then((res) => {
expect(res)
.toEqual(server);
});
expect(httpServerSpy.listen)
.toHaveBeenCalledWith((server as any).port, (server as any).host, jasmine.any(Function));
const startedCallback = httpServerSpy.listen.calls.mostRecent().args[2];
startedCallback(); //resolve the promise
return startPromise;
})));
it('registers routes with the engine, and dispatches calls to the express engine', async(inject([Server], (server: Server) => {
const callStackHandlerSpy = jasmine.createSpy('callStackHandler');
const responseFixture = new Response().data('Hello World')
.header('Content-Length', '11');
callStackHandlerSpy.and.returnValue(Promise.resolve(responseFixture));
const routeConfig: RouteConfig = {
path: '/test',
methodName: 'test',
method: 'GET',
callStack: [],
callStackHandler: callStackHandlerSpy
};
server.register(routeConfig);
expect(expressSpy.get)
.toHaveBeenCalledWith(routeConfig.path, jasmine.any(Function));
const registrationCallback = expressSpy.get.calls.mostRecent().args[1];
const reqSpy = jasmine.createSpy('req')
.and
.returnValue({
params: {
id: 1,
},
header: {
Authorization: 'Bearer: Banana'
}
});
const resSpy = jasmine.createSpyObj('res', ['status', 'send', 'header']);
resSpy.status = jasmine.createSpy('res_status');
resSpy.send = jasmine.createSpy('res_send');
resSpy.header = jasmine.createSpy('res_send');
return registrationCallback(reqSpy, resSpy)
.then(() => {
expect(callStackHandlerSpy)
.toHaveBeenCalledWith(jasmine.any(Request), jasmine.any(Response));
expect(resSpy.status)
.toHaveBeenCalledWith(200);
expect(resSpy.send)
.toHaveBeenCalledWith(responseFixture.body);
expect(resSpy.header)
.toHaveBeenCalledWith('Content-Length', '11');
});
})));
it('registers routes with the engine, and dispatches errors to the express engine', async(inject([Server], (server: Server) => {
const callStackHandlerSpy = jasmine.createSpy('callStackHandler');
const responseFixture = new Error('Internal error');
callStackHandlerSpy.and.returnValue(Promise.reject(responseFixture));
const routeConfig: RouteConfig = {
path: '/test',
methodName: 'test',
method: 'GET',
callStack: [],
callStackHandler: callStackHandlerSpy
};
server.register(routeConfig);
expect(expressSpy.get)
.toHaveBeenCalledWith(routeConfig.path, jasmine.any(Function));
const registrationCallback = expressSpy.get.calls.mostRecent().args[1];
const reqSpy = jasmine.createSpy('req')
.and
.returnValue({
params: null,
header: null
});
const resSpy = jasmine.createSpyObj('res', ['status', 'send', 'header']);
resSpy.status = jasmine.createSpy('res_status');
resSpy.send = jasmine.createSpy('res_send');
resSpy.header = jasmine.createSpy('res_send');
resSpy.statusCode = 200;
return registrationCallback(reqSpy, resSpy)
.then(() => {
expect(callStackHandlerSpy)
.toHaveBeenCalledWith(jasmine.any(Request), jasmine.any(Response));
expect(resSpy.status)
.toHaveBeenCalledWith(500);
expect(resSpy.send)
.toHaveBeenCalledWith(responseFixture);
});
})));
});