-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
sequence.acceptance.ts
220 lines (188 loc) · 6.1 KB
/
sequence.acceptance.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {inject} from '@loopback/context';
import {Application} from '@loopback/core';
import {anOpenApiSpec} from '@loopback/openapi-spec-builder';
import {api} from '@loopback/openapi-v3';
import {Client, createClientForHandler} from '@loopback/testlab';
import {
ControllerClass,
ControllerInstance,
DefaultSequence,
FindRoute,
HttpServerLike,
InvokeMethod,
ParseParams,
Reject,
RequestContext,
RestApplication,
RestBindings,
RestComponent,
RestServer,
Send,
SequenceHandler,
} from '../../..';
const SequenceActions = RestBindings.SequenceActions;
/* # Feature: Sequence
* - In order to build REST APIs
* - As a framework developer
* - I want the framework to handle default sequence and user defined sequence
*/
describe('Sequence', () => {
let app: Application;
let server: RestServer;
beforeEach(givenAppWithController);
it('provides a default sequence', async () => {
await whenIRequest()
.get('/name')
.expect('SequenceApp');
});
it('allows users to define a custom sequence as a function', () => {
server.handler(({response}, sequence) => {
sequence.send(response, 'hello world');
});
return whenIRequest()
.get('/')
.expect('hello world');
});
it('allows users to define a custom sequence as a class', async () => {
class MySequence implements SequenceHandler {
constructor(@inject(SequenceActions.SEND) private send: Send) {}
async handle({response}: RequestContext) {
this.send(response, 'hello world');
}
}
// bind user defined sequence
server.sequence(MySequence);
await whenIRequest()
.get('/')
.expect('hello world');
});
it('allows users to bind a custom sequence class', () => {
class MySequence implements SequenceHandler {
constructor(
@inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
@inject(SequenceActions.PARSE_PARAMS)
protected parseParams: ParseParams,
@inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
@inject(SequenceActions.SEND) protected send: Send,
) {}
async handle(context: RequestContext) {
const {request, response} = context;
const route = this.findRoute(request);
const args = await this.parseParams(request, route);
const result = await this.invoke(route, args);
this.send(response, `MySequence ${result}`);
}
}
server.sequence(MySequence);
return whenIRequest()
.get('/name')
.expect('MySequence SequenceApp');
});
it('allows users to bind a custom sequence class via app.sequence()', async () => {
class MySequence implements SequenceHandler {
constructor(@inject(SequenceActions.SEND) protected send: Send) {}
async handle({response}: RequestContext) {
this.send(response, 'MySequence was invoked.');
}
}
const restApp = new RestApplication();
restApp.sequence(MySequence);
await whenIRequest(restApp)
.get('/name')
.expect('MySequence was invoked.');
});
it('user-defined Send', () => {
const send: Send = (response, result) => {
response.setHeader('content-type', 'text/plain');
response.end(`CUSTOM FORMAT: ${result}`);
};
server.bind(SequenceActions.SEND).to(send);
return whenIRequest()
.get('/name')
.expect('CUSTOM FORMAT: SequenceApp');
});
it('user-defined Reject', () => {
const reject: Reject = ({response}, error) => {
response.statusCode = 418; // I'm a teapot
response.end();
};
server.bind(SequenceActions.REJECT).to(reject);
return whenIRequest()
.get('/unknown-url')
.expect(418);
});
it('makes ctx available in a custom sequence handler function', () => {
app.bind('test').to('hello world');
server.handler((context, sequence) => {
sequence.send(context.response, context.getSync('test'));
});
return whenIRequest()
.get('/')
.expect('hello world');
});
it('makes ctx available in a custom sequence class', () => {
class MySequence extends DefaultSequence {
constructor(
@inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
@inject(SequenceActions.PARSE_PARAMS)
protected parseParams: ParseParams,
@inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
@inject(SequenceActions.SEND) public send: Send,
@inject(SequenceActions.REJECT) public reject: Reject,
) {
super(findRoute, parseParams, invoke, send, reject);
}
async handle(context: RequestContext) {
this.send(context.response, context.getSync('test'));
}
}
server.sequence(MySequence);
app.bind('test').to('hello world');
return whenIRequest()
.get('/')
.expect('hello world');
});
async function givenAppWithController() {
await givenAnApplication();
app.bind('application.name').to('SequenceApp');
const apispec = anOpenApiSpec()
.withOperation('get', '/name', {
'x-operation-name': 'getName',
responses: {
'200': {
schema: {
type: 'string',
},
description: '',
},
},
})
.build();
@api(apispec)
class InfoController {
constructor(@inject('application.name') public appName: string) {}
async getName(): Promise<string> {
return this.appName;
}
}
givenControllerInServer(InfoController);
}
/* ===== HELPERS ===== */
async function givenAnApplication() {
app = new Application();
app.component(RestComponent);
server = await app.getServer(RestServer);
}
function givenControllerInServer(
controller: ControllerClass<ControllerInstance>,
) {
app.controller(controller);
}
function whenIRequest(restServerOrApp: HttpServerLike = server): Client {
return createClientForHandler(restServerOrApp.requestHandler);
}
});