5
5
6
6
import { Application } from '@loopback/core' ;
7
7
import {
8
- supertest ,
9
- expect ,
10
8
createClientForHandler ,
11
- itSkippedOnTravis ,
12
- httpsGetAsync ,
9
+ expect ,
13
10
givenHttpServerConfig ,
11
+ httpsGetAsync ,
12
+ itSkippedOnTravis ,
13
+ supertest ,
14
14
} from '@loopback/testlab' ;
15
+ import * as fs from 'fs' ;
16
+ import { IncomingMessage , ServerResponse } from 'http' ;
17
+ import * as yaml from 'js-yaml' ;
18
+ import * as path from 'path' ;
19
+ import { is } from 'type-is' ;
20
+ import * as util from 'util' ;
15
21
import {
16
- RestBindings ,
17
- RestServer ,
18
- RestComponent ,
22
+ BodyParser ,
19
23
get ,
24
+ post ,
20
25
Request ,
26
+ requestBody ,
27
+ RequestContext ,
28
+ RestBindings ,
29
+ RestComponent ,
30
+ RestServer ,
21
31
RestServerConfig ,
22
- BodyParser ,
23
32
} from '../..' ;
24
- import { IncomingMessage , ServerResponse } from 'http' ;
25
- import * as yaml from 'js-yaml' ;
26
- import * as path from 'path' ;
27
- import * as fs from 'fs' ;
28
- import * as util from 'util' ;
29
33
const readFileAsync = util . promisify ( fs . readFile ) ;
30
34
31
- import { is } from 'type-is' ;
32
- import { requestBody , post } from '../../src' ;
33
-
34
35
const FIXTURES = path . resolve ( __dirname , '../../../fixtures' ) ;
35
36
const ASSETS = path . resolve ( FIXTURES , 'assets' ) ;
36
37
37
38
describe ( 'RestServer (integration)' , ( ) => {
38
39
it ( 'exports url property' , async ( ) => {
39
- // Explicitly setting host to IPv4 address so test runs on Travis
40
- const server = await givenAServer ( { rest : { port : 0 , host : '127.0.0.1' } } ) ;
40
+ const server = await givenAServer ( ) ;
41
41
server . handler ( dummyRequestHandler ) ;
42
42
expect ( server . url ) . to . be . undefined ( ) ;
43
43
await server . start ( ) ;
@@ -52,8 +52,26 @@ describe('RestServer (integration)', () => {
52
52
expect ( server . url ) . to . be . undefined ( ) ;
53
53
} ) ;
54
54
55
+ it ( 'parses query without decorated rest query params' , async ( ) => {
56
+ // This handler responds with the query object (which is expected to
57
+ // be parsed by Express)
58
+ function requestWithQueryHandler ( { request, response} : RequestContext ) {
59
+ response . json ( request . query ) ;
60
+ response . end ( ) ;
61
+ }
62
+
63
+ // See https://github.com/strongloop/loopback-next/issues/2088
64
+ const server = await givenAServer ( ) ;
65
+ server . handler ( requestWithQueryHandler ) ;
66
+ await server . start ( ) ;
67
+ await supertest ( server . url )
68
+ . get ( '/?x=1&y[a]=2' )
69
+ . expect ( 200 , { x : '1' , y : { a : '2' } } ) ;
70
+ await server . stop ( ) ;
71
+ } ) ;
72
+
55
73
it ( 'updates rest.port binding when listening on ephemeral port' , async ( ) => {
56
- const server = await givenAServer ( { rest : { port : 0 } } ) ;
74
+ const server = await givenAServer ( ) ;
57
75
await server . start ( ) ;
58
76
expect ( server . getSync ( RestBindings . PORT ) ) . to . be . above ( 0 ) ;
59
77
await server . stop ( ) ;
@@ -73,7 +91,7 @@ describe('RestServer (integration)', () => {
73
91
} ) ;
74
92
75
93
it ( 'responds with 500 when Sequence fails with unhandled error' , async ( ) => {
76
- const server = await givenAServer ( { rest : { port : 0 } } ) ;
94
+ const server = await givenAServer ( ) ;
77
95
server . handler ( ( context , sequence ) => {
78
96
return Promise . reject ( new Error ( 'unhandled test error' ) ) ;
79
97
} ) ;
@@ -111,11 +129,7 @@ describe('RestServer (integration)', () => {
111
129
112
130
it ( 'allows static assets via api' , async ( ) => {
113
131
const root = ASSETS ;
114
- const server = await givenAServer ( {
115
- rest : {
116
- port : 0 ,
117
- } ,
118
- } ) ;
132
+ const server = await givenAServer ( ) ;
119
133
120
134
server . static ( '/html' , root ) ;
121
135
const content = fs
@@ -129,11 +143,7 @@ describe('RestServer (integration)', () => {
129
143
130
144
it ( 'allows static assets to be mounted on multiple paths' , async ( ) => {
131
145
const root = ASSETS ;
132
- const server = await givenAServer ( {
133
- rest : {
134
- port : 0 ,
135
- } ,
136
- } ) ;
146
+ const server = await givenAServer ( ) ;
137
147
138
148
server . static ( '/html-0' , root ) ;
139
149
server . static ( '/html-1' , root ) ;
@@ -154,11 +164,7 @@ describe('RestServer (integration)', () => {
154
164
it ( 'merges different static asset directories when mounted on the same path' , async ( ) => {
155
165
const root = ASSETS ;
156
166
const otherAssets = path . join ( FIXTURES , 'other-assets' ) ;
157
- const server = await givenAServer ( {
158
- rest : {
159
- port : 0 ,
160
- } ,
161
- } ) ;
167
+ const server = await givenAServer ( ) ;
162
168
163
169
server . static ( '/html' , root ) ;
164
170
server . static ( '/html' , otherAssets ) ;
@@ -178,11 +184,7 @@ describe('RestServer (integration)', () => {
178
184
179
185
it ( 'allows static assets via api after start' , async ( ) => {
180
186
const root = ASSETS ;
181
- const server = await givenAServer ( {
182
- rest : {
183
- port : 0 ,
184
- } ,
185
- } ) ;
187
+ const server = await givenAServer ( ) ;
186
188
await createClientForHandler ( server . requestHandler )
187
189
. get ( '/html/index.html' )
188
190
. expect ( 404 ) ;
@@ -196,11 +198,7 @@ describe('RestServer (integration)', () => {
196
198
197
199
it ( 'allows non-static routes after assets' , async ( ) => {
198
200
const root = ASSETS ;
199
- const server = await givenAServer ( {
200
- rest : {
201
- port : 0 ,
202
- } ,
203
- } ) ;
201
+ const server = await givenAServer ( ) ;
204
202
server . static ( '/html' , root ) ;
205
203
server . handler ( dummyRequestHandler ) ;
206
204
@@ -211,11 +209,7 @@ describe('RestServer (integration)', () => {
211
209
212
210
it ( 'gives precedence to API routes over static assets' , async ( ) => {
213
211
const root = ASSETS ;
214
- const server = await givenAServer ( {
215
- rest : {
216
- port : 0 ,
217
- } ,
218
- } ) ;
212
+ const server = await givenAServer ( ) ;
219
213
server . static ( '/html' , root ) ;
220
214
server . handler ( dummyRequestHandler ) ;
221
215
@@ -226,11 +220,7 @@ describe('RestServer (integration)', () => {
226
220
227
221
it ( 'registers controllers defined later than static assets' , async ( ) => {
228
222
const root = ASSETS ;
229
- const server = await givenAServer ( {
230
- rest : {
231
- port : 0 ,
232
- } ,
233
- } ) ;
223
+ const server = await givenAServer ( ) ;
234
224
server . static ( '/html' , root ) ;
235
225
server . controller ( DummyController ) ;
236
226
@@ -256,7 +246,7 @@ describe('RestServer (integration)', () => {
256
246
}
257
247
}
258
248
259
- const server = await givenAServer ( { rest : { port : 0 } } ) ;
249
+ const server = await givenAServer ( ) ;
260
250
// Register a request body parser for xml
261
251
server . bodyParser ( XmlBodyParser ) ;
262
252
server . controller ( DummyXmlController ) ;
@@ -269,7 +259,7 @@ describe('RestServer (integration)', () => {
269
259
} ) ;
270
260
271
261
it ( 'allows cors' , async ( ) => {
272
- const server = await givenAServer ( { rest : { port : 0 } } ) ;
262
+ const server = await givenAServer ( ) ;
273
263
server . handler ( dummyRequestHandler ) ;
274
264
275
265
await createClientForHandler ( server . requestHandler )
@@ -280,7 +270,7 @@ describe('RestServer (integration)', () => {
280
270
} ) ;
281
271
282
272
it ( 'allows cors preflight' , async ( ) => {
283
- const server = await givenAServer ( { rest : { port : 0 } } ) ;
273
+ const server = await givenAServer ( ) ;
284
274
server . handler ( dummyRequestHandler ) ;
285
275
286
276
await createClientForHandler ( server . requestHandler )
@@ -311,11 +301,7 @@ describe('RestServer (integration)', () => {
311
301
} ) ;
312
302
313
303
it ( 'exposes "GET /openapi.json" endpoint' , async ( ) => {
314
- const server = await givenAServer ( {
315
- rest : {
316
- port : 0 ,
317
- } ,
318
- } ) ;
304
+ const server = await givenAServer ( ) ;
319
305
const greetSpec = {
320
306
responses : {
321
307
200 : {
@@ -407,7 +393,7 @@ describe('RestServer (integration)', () => {
407
393
} ) ;
408
394
409
395
it ( 'exposes "GET /openapi.yaml" endpoint' , async ( ) => {
410
- const server = await givenAServer ( { rest : { port : 0 } } ) ;
396
+ const server = await givenAServer ( ) ;
411
397
const greetSpec = {
412
398
responses : {
413
399
200 : {
@@ -701,7 +687,10 @@ paths:
701
687
await server . stop ( ) ;
702
688
} ) ;
703
689
704
- async function givenAServer ( options ?: { rest : RestServerConfig } ) {
690
+ async function givenAServer (
691
+ options : { rest : RestServerConfig } = { rest : { port : 0 } } ,
692
+ ) {
693
+ options . rest = givenHttpServerConfig ( options . rest ) ;
705
694
const app = new Application ( options ) ;
706
695
app . component ( RestComponent ) ;
707
696
return await app . getServer ( RestServer ) ;
0 commit comments