1
- import {
2
- Matchers ,
3
- Pact ,
4
- RequestOptions ,
5
- ResponseOptions ,
6
- } from '@pact-foundation/pact' ;
1
+ import { Matchers , ResponseOptions } from '@pact-foundation/pact' ;
7
2
import { Chance } from 'chance' ;
8
- import * as path from 'path' ;
9
- import { ApiVersion , AutosuggestClient } from '../../src' ;
10
3
import {
11
4
BadGatewayError ,
12
5
ForbiddenError ,
@@ -15,18 +8,16 @@ import {
15
8
NotFoundError ,
16
9
ServiceUnavailableError ,
17
10
} from '../../src/lib/transport/error' ;
11
+ import {
12
+ createAutosuggestPact ,
13
+ createPactMockRequest ,
14
+ createPactMockResponse ,
15
+ } from '../pact/utils' ;
18
16
19
17
const chance = new Chance ( ) ;
20
18
21
19
describe ( 'Autosuggest Session Pact' , ( ) => {
22
- const randomApiKey = chance . string ( {
23
- length : 8 ,
24
- alpha : true ,
25
- numeric : true ,
26
- symbols : false ,
27
- casing : 'upper' ,
28
- } ) ;
29
-
20
+ const correlationId = Matchers . uuid ( ) . getValue ( ) ;
30
21
const genRandomDigit = ( ) =>
31
22
chance . string ( {
32
23
length : 1 ,
@@ -35,11 +26,6 @@ describe('Autosuggest Session Pact', () => {
35
26
symbols : false ,
36
27
casing : 'lower' ,
37
28
} ) ;
38
- const apiKey = Matchers . term ( {
39
- matcher : '[a-zA-Z0-9]{8}' ,
40
- generate : randomApiKey ,
41
- } ) . getValue ( ) ;
42
- const correlationId = Matchers . uuid ( ) . getValue ( ) ;
43
29
const randomVersion = [
44
30
genRandomDigit ( ) ,
45
31
genRandomDigit ( ) ,
@@ -57,21 +43,7 @@ describe('Autosuggest Session Pact', () => {
57
43
chance . pickone ( [ 'default' , 'inherit' ] )
58
44
) . getValue ( ) as never ;
59
45
60
- const apiVersion = ApiVersion . Version3 ;
61
- const port = 9000 ;
62
- const host = `http://localhost:${ port } ` ;
63
- const client = new AutosuggestClient ( apiKey , { host, apiVersion, headers : { } } ) ;
64
-
65
- // Create the Pact object to represent your provider
66
- const provider = new Pact ( {
67
- consumer : 'w3w-node-wrapper' ,
68
- provider : 'api-server' ,
69
- port,
70
- log : path . resolve ( process . cwd ( ) , 'logs' , 'pact.log' ) ,
71
- dir : path . resolve ( process . cwd ( ) , 'pacts' ) ,
72
- logLevel : 'info' ,
73
- pactfileWriteMode : 'overwrite' ,
74
- } ) ;
46
+ const { apiKey, apiVersion, client, provider } = createAutosuggestPact ( ) ;
75
47
76
48
// Start the mock server
77
49
beforeAll ( ( ) => provider . setup ( ) ) ;
@@ -88,8 +60,8 @@ describe('Autosuggest Session Pact', () => {
88
60
afterAll ( ( ) => provider . finalize ( ) ) ;
89
61
90
62
describe ( 'When I do not have a current autosuggest session' , ( ) => {
91
- describe ( 'And there is a sucessful request to start a session' , ( ) => {
92
- const MOCK_REQUEST : RequestOptions = {
63
+ describe ( 'And there is a successful request to start a session' , ( ) => {
64
+ const MOCK_REQUEST = createPactMockRequest ( {
93
65
method : 'POST' as const ,
94
66
path : `/${ apiVersion } /autosuggest-session` ,
95
67
query : {
@@ -105,8 +77,9 @@ describe('Autosuggest Session Pact', () => {
105
77
variant,
106
78
version,
107
79
} ,
108
- } ;
109
- const MOCK_RESPONSE : ResponseOptions = {
80
+ } ) ;
81
+
82
+ const MOCK_RESPONSE = createPactMockResponse ( {
110
83
status : 200 ,
111
84
headers : {
112
85
'Content-Type' : 'application/json' ,
@@ -116,7 +89,7 @@ describe('Autosuggest Session Pact', () => {
116
89
status : 200 ,
117
90
message : 'ok' ,
118
91
} ,
119
- } ;
92
+ } ) ;
120
93
121
94
// Add interactions to the Mock Server, as many as required
122
95
beforeAll ( ( ) =>
@@ -148,7 +121,7 @@ describe('Autosuggest Session Pact', () => {
148
121
} ) ;
149
122
150
123
describe ( 'And there is a failed request to update the session' , ( ) => {
151
- const MOCK_REQUEST : RequestOptions = {
124
+ const MOCK_REQUEST = createPactMockRequest ( {
152
125
method : 'POST' as const ,
153
126
path : `/${ apiVersion } /autosuggest-session` ,
154
127
query : {
@@ -164,7 +137,7 @@ describe('Autosuggest Session Pact', () => {
164
137
variant,
165
138
version,
166
139
} ,
167
- } ;
140
+ } ) ;
168
141
169
142
const errors = [
170
143
new ForbiddenError ( ) ,
@@ -216,7 +189,7 @@ describe('Autosuggest Session Pact', () => {
216
189
217
190
describe ( 'And there is an unauthorized request' , ( ) => {
218
191
it ( 'should return a 401 error when apiKey is missing' , async ( ) => {
219
- const MOCK_REQUEST : RequestOptions = {
192
+ const MOCK_REQUEST = createPactMockRequest ( {
220
193
method : 'POST' as const ,
221
194
path : `/${ apiVersion } /autosuggest-session` ,
222
195
headers : {
@@ -229,8 +202,8 @@ describe('Autosuggest Session Pact', () => {
229
202
variant,
230
203
version,
231
204
} ,
232
- } ;
233
- const MOCK_RESPONSE : ResponseOptions = {
205
+ } ) ;
206
+ const MOCK_RESPONSE = createPactMockResponse ( {
234
207
status : 401 ,
235
208
headers : {
236
209
'Content-Type' : 'application/json' ,
@@ -240,7 +213,7 @@ describe('Autosuggest Session Pact', () => {
240
213
status : 401 ,
241
214
message : 'Unauthorized' ,
242
215
} ,
243
- } ;
216
+ } ) ;
244
217
// Add interactions to the Mock Server, as many as required
245
218
await provider . addInteraction ( {
246
219
// The 'state' field specifies a "Provider State"
@@ -267,7 +240,7 @@ describe('Autosuggest Session Pact', () => {
267
240
268
241
describe ( 'And there is bad request ' , ( ) => {
269
242
it ( 'should return a 400 error when returnCoordinates is missing' , async ( ) => {
270
- const MOCK_REQUEST : RequestOptions = {
243
+ const MOCK_REQUEST = createPactMockRequest ( {
271
244
method : 'POST' as const ,
272
245
path : `/${ apiVersion } /autosuggest-session` ,
273
246
headers : {
@@ -279,8 +252,8 @@ describe('Autosuggest Session Pact', () => {
279
252
variant,
280
253
version,
281
254
} ,
282
- } ;
283
- const MOCK_RESPONSE : ResponseOptions = {
255
+ } ) ;
256
+ const MOCK_RESPONSE = createPactMockResponse ( {
284
257
status : 400 ,
285
258
headers : {
286
259
'Content-Type' : 'application/json' ,
@@ -290,7 +263,7 @@ describe('Autosuggest Session Pact', () => {
290
263
status : 400 ,
291
264
message : 'Bad Request' ,
292
265
} ,
293
- } ;
266
+ } ) ;
294
267
// Add interactions to the Mock Server, as many as required
295
268
await provider . addInteraction ( {
296
269
// The 'state' field specifies a "Provider State"
@@ -315,7 +288,7 @@ describe('Autosuggest Session Pact', () => {
315
288
} ) ;
316
289
317
290
it ( 'should return a 400 error when typeheadDelay is missing' , async ( ) => {
318
- const MOCK_REQUEST : RequestOptions = {
291
+ const MOCK_REQUEST = createPactMockRequest ( {
319
292
method : 'POST' as const ,
320
293
path : `/${ apiVersion } /autosuggest-session` ,
321
294
headers : {
@@ -327,8 +300,8 @@ describe('Autosuggest Session Pact', () => {
327
300
variant,
328
301
version,
329
302
} ,
330
- } ;
331
- const MOCK_RESPONSE : ResponseOptions = {
303
+ } ) ;
304
+ const MOCK_RESPONSE = createPactMockResponse ( {
332
305
status : 400 ,
333
306
headers : {
334
307
'Content-Type' : 'application/json' ,
@@ -338,7 +311,7 @@ describe('Autosuggest Session Pact', () => {
338
311
status : 400 ,
339
312
message : 'Bad Request' ,
340
313
} ,
341
- } ;
314
+ } ) ;
342
315
// Add interactions to the Mock Server, as many as required
343
316
await provider . addInteraction ( {
344
317
// The 'state' field specifies a "Provider State"
@@ -366,7 +339,7 @@ describe('Autosuggest Session Pact', () => {
366
339
367
340
describe ( 'When I have a current autosuggest session' , ( ) => {
368
341
describe ( 'And there is a request to update the session' , ( ) => {
369
- const MOCK_REQUEST : RequestOptions = {
342
+ const MOCK_REQUEST = createPactMockRequest ( {
370
343
method : 'PUT' as const ,
371
344
path : `/${ apiVersion } /autosuggest-session` ,
372
345
query : {
@@ -382,8 +355,8 @@ describe('Autosuggest Session Pact', () => {
382
355
variant,
383
356
version,
384
357
} ,
385
- } ;
386
- const MOCK_RESPONSE : ResponseOptions = {
358
+ } ) ;
359
+ const MOCK_RESPONSE = createPactMockResponse ( {
387
360
status : 200 ,
388
361
headers : {
389
362
'Content-Type' : 'application/json' ,
@@ -392,7 +365,7 @@ describe('Autosuggest Session Pact', () => {
392
365
body : {
393
366
status : 200 ,
394
367
} ,
395
- } ;
368
+ } ) ;
396
369
397
370
beforeAll ( ( ) =>
398
371
provider . addInteraction ( {
@@ -422,7 +395,7 @@ describe('Autosuggest Session Pact', () => {
422
395
423
396
describe ( 'And there is an unauthorized request' , ( ) => {
424
397
it ( 'should return a 401 error when apiKey is missing' , async ( ) => {
425
- const MOCK_REQUEST : RequestOptions = {
398
+ const MOCK_REQUEST = createPactMockRequest ( {
426
399
method : 'PUT' as const ,
427
400
path : `/${ apiVersion } /autosuggest-session` ,
428
401
headers : {
@@ -435,8 +408,8 @@ describe('Autosuggest Session Pact', () => {
435
408
variant,
436
409
version,
437
410
} ,
438
- } ;
439
- const MOCK_RESPONSE : ResponseOptions = {
411
+ } ) ;
412
+ const MOCK_RESPONSE = createPactMockResponse ( {
440
413
status : 401 ,
441
414
headers : {
442
415
'Content-Type' : 'application/json' ,
@@ -446,7 +419,7 @@ describe('Autosuggest Session Pact', () => {
446
419
status : 401 ,
447
420
message : 'Unauthorized' ,
448
421
} ,
449
- } ;
422
+ } ) ;
450
423
// Add interactions to the Mock Server, as many as required
451
424
await provider . addInteraction ( {
452
425
// The 'state' field specifies a "Provider State"
@@ -473,7 +446,7 @@ describe('Autosuggest Session Pact', () => {
473
446
474
447
describe ( 'And there is bad request ' , ( ) => {
475
448
it ( 'should return a 400 error when returnCoordinates is missing' , async ( ) => {
476
- const MOCK_REQUEST : RequestOptions = {
449
+ const MOCK_REQUEST = createPactMockRequest ( {
477
450
method : 'PUT' as const ,
478
451
path : `/${ apiVersion } /autosuggest-session` ,
479
452
headers : {
@@ -485,8 +458,8 @@ describe('Autosuggest Session Pact', () => {
485
458
variant,
486
459
version,
487
460
} ,
488
- } ;
489
- const MOCK_RESPONSE : ResponseOptions = {
461
+ } ) ;
462
+ const MOCK_RESPONSE = createPactMockResponse ( {
490
463
status : 400 ,
491
464
headers : {
492
465
'Content-Type' : 'application/json' ,
@@ -496,7 +469,7 @@ describe('Autosuggest Session Pact', () => {
496
469
status : 400 ,
497
470
message : 'Bad Request' ,
498
471
} ,
499
- } ;
472
+ } ) ;
500
473
// Add interactions to the Mock Server, as many as required
501
474
await provider . addInteraction ( {
502
475
// The 'state' field specifies a "Provider State"
@@ -521,7 +494,7 @@ describe('Autosuggest Session Pact', () => {
521
494
} ) ;
522
495
523
496
it ( 'should return a 400 error when typeheadDelay is missing' , async ( ) => {
524
- const MOCK_REQUEST : RequestOptions = {
497
+ const MOCK_REQUEST = createPactMockRequest ( {
525
498
method : 'PUT' as const ,
526
499
path : `/${ apiVersion } /autosuggest-session` ,
527
500
headers : {
@@ -533,8 +506,8 @@ describe('Autosuggest Session Pact', () => {
533
506
variant,
534
507
version,
535
508
} ,
536
- } ;
537
- const MOCK_RESPONSE : ResponseOptions = {
509
+ } ) ;
510
+ const MOCK_RESPONSE = createPactMockResponse ( {
538
511
status : 400 ,
539
512
headers : {
540
513
'Content-Type' : 'application/json' ,
@@ -544,7 +517,7 @@ describe('Autosuggest Session Pact', () => {
544
517
status : 400 ,
545
518
message : 'Bad Request' ,
546
519
} ,
547
- } ;
520
+ } ) ;
548
521
// Add interactions to the Mock Server, as many as required
549
522
await provider . addInteraction ( {
550
523
// The 'state' field specifies a "Provider State"
@@ -570,7 +543,7 @@ describe('Autosuggest Session Pact', () => {
570
543
} ) ;
571
544
572
545
describe ( 'And there is a failed request to update the session' , ( ) => {
573
- const MOCK_REQUEST : RequestOptions = {
546
+ const MOCK_REQUEST = createPactMockRequest ( {
574
547
method : 'PUT' as const ,
575
548
path : `/${ apiVersion } /autosuggest-session` ,
576
549
query : {
@@ -586,7 +559,7 @@ describe('Autosuggest Session Pact', () => {
586
559
variant,
587
560
version,
588
561
} ,
589
- } ;
562
+ } ) ;
590
563
591
564
const errors = [
592
565
new ForbiddenError ( ) ,
@@ -600,7 +573,7 @@ describe('Autosuggest Session Pact', () => {
600
573
errors . forEach ( err => {
601
574
// Write your test(s)
602
575
it ( `should return a ${ err . status } error` , async ( ) => {
603
- const MOCK_RESPONSE : ResponseOptions = {
576
+ const MOCK_RESPONSE = createPactMockResponse ( {
604
577
status : err . status ,
605
578
headers : {
606
579
'Content-Type' : 'application/json' ,
@@ -610,7 +583,7 @@ describe('Autosuggest Session Pact', () => {
610
583
status : err . status ,
611
584
message : err . message ,
612
585
} ,
613
- } ;
586
+ } ) ;
614
587
// Add interactions to the Mock Server, as many as required
615
588
await provider . addInteraction ( {
616
589
// The 'state' field specifies a "Provider State"
0 commit comments