/
sequence.acceptance.ts
144 lines (122 loc) · 3.98 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
// Copyright IBM Corp. 2013,2017. All Rights Reserved.
// Node module: loopback
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
Application,
api,
OpenApiSpec,
ParameterObject,
ServerRequest,
ServerResponse,
parseOperationArgs,
ParsedRequest,
OperationArgs,
FindRoute,
InvokeMethod,
Send,
Reject,
SequenceHandler,
} from '../../..';
import {expect, Client, createClientForApp} from '@loopback/testlab';
import {anOpenApiSpec} from '@loopback/openapi-spec-builder';
import {inject, Constructor, Context} from '@loopback/context';
import {ControllerClass} from '../../../src/router/routing-table';
/* # 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;
beforeEach(givenAppWithController);
it('provides a default sequence', async () => {
whenIMakeRequestTo(app).get('/name').expect('SequenceApp');
});
it('allows users to define a custom sequence as a function', () => {
app.handler((sequence, request, response) => {
sequence.send(response, 'hello world');
});
return whenIMakeRequestTo(app).get('/').expect('hello world');
});
it('allows users to define a custom sequence as a class', () => {
class MySequence implements SequenceHandler {
constructor(@inject('sequence.actions.send') private send: Send) {}
async handle(req: ParsedRequest, res: ServerResponse) {
this.send(res, 'hello world');
}
}
// bind user defined sequence
app.sequence(MySequence);
whenIMakeRequestTo(app).get('/').expect('hello world');
});
it('allows users to bind a custom sequence class', () => {
class MySequence {
constructor(
@inject('sequence.actions.findRoute') protected findRoute: FindRoute,
@inject('sequence.actions.invokeMethod') protected invoke: InvokeMethod,
@inject('sequence.actions.send') protected send: Send,
) {}
async handle(req: ParsedRequest, res: ServerResponse) {
const route = this.findRoute(req);
const args = await parseOperationArgs(req, route);
const result = await this.invoke(route, args);
this.send(res, `MySequence ${result}`);
}
}
app.sequence(MySequence);
return whenIMakeRequestTo(app)
.get('/name')
.expect('MySequence SequenceApp');
});
it('user-defined Send', () => {
const send: Send = (response, result) => {
response.setHeader('content-type', 'text/plain');
response.end(`CUSTOM FORMAT: ${result}`);
};
app.bind('sequence.actions.send').to(send);
return whenIMakeRequestTo(app)
.get('/name')
.expect('CUSTOM FORMAT: SequenceApp');
});
it('user-defined Reject', () => {
const reject: Reject = (response, request, error) => {
response.statusCode = 418; // I'm a teapot
response.end();
};
app.bind('sequence.actions.reject').to(reject);
return whenIMakeRequestTo(app).get('/unknown-url').expect(418);
});
function givenAppWithController() {
givenAnApplication();
app.bind('application.name').to('SequenceApp');
const apispec = anOpenApiSpec()
.withOperation('get', '/name', {
'x-operation-name': 'getName',
responses: {
'200': {
type: 'string',
},
},
})
.build();
@api(apispec)
class InfoController {
constructor(@inject('application.name') public appName: string) {}
async getName(): Promise<string> {
return this.appName;
}
}
givenControllerInApp(InfoController);
}
/* ===== HELPERS ===== */
function givenAnApplication() {
app = new Application();
}
function givenControllerInApp(controller: ControllerClass) {
app.controller(controller);
}
function whenIMakeRequestTo(application: Application): Client {
return createClientForApp(app);
}
});