pact_mock_server_cli 2.0.0

Standalone pact mock server for consumer pact tests
Documentation
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# Standalone Pact Mock Server

This project provides both a restful web api and command line interface to run pact mock servers. It is a single
executable binary and is able to manage multiple mock servers. The lifecycle of each mock server can be controlled by the
restful web api or through the command line interface. It implements the [V4 Pact specification](https://github.com/pact-foundation/pact-specification/tree/version-4).

[Online rust docs](https://docs.rs/pact_mock_server_cli/)

## Command line interface

The mock server is bundles as a single binary executable `pact-mock-server`. Running this with out any options displays
the standard help.

```console,ignore
$ ./pact-mock-server --help
./pact-mock-server v1.0.0
Standalone Pact mock server

USAGE:
    pact-mock-server [FLAGS] [OPTIONS] <SUBCOMMAND>

FLAGS:
        --help           Prints help information
        --no-file-log    Do not log to an output file
        --no-term-log    Use a simple logger instead of the term based one
    -v, --version        Prints version information

OPTIONS:
    -h, --host <host>            hostname the master mock server runs on (defaults to localhost)
    -l, --loglevel <loglevel>    Log level for mock servers to write to the log file (defaults to info) [possible
                                 values: error, warn, info, debug, trace, none]
    -p, --port <port>            port the master mock server runs on (defaults to 8080)

SUBCOMMANDS:
    create             Creates a new mock server from a pact file
    help               Prints this message or the help of the given subcommand(s)
    list               Lists all the running mock servers
    shutdown           Shutdown the mock server by id or port number, releasing all its resources
    shutdown-master    Performs a graceful shutdown of the master server (displayed when it started)
    start              Starts the master mock server
    verify             Verify the mock server by id or port number, and generate a pact file if all ok


```

### Options

The following options are available for all subcommands:

#### Host: -h, --host <host>

This sets the host the master mock server runs on. By default this will be localhost.

#### Port: -p, --port <port>

This sets the port that the master mock server runs on. By default this will be 8080. The start command will start the
master server using this port.

#### Log level: -l, --loglevel <loglevel>

This sets the log level that the CLI and mock servers log at. It defaults to info. Valid values are: error, warn,
info, debug, trace, none.

### Sub-commands

#### help

This prints either the main help or the help for a sub-command.

#### start

This starts the master mock server. This server needs to be running for the other sub-commands to work.

```console
$ ./pact-mock-server help start
Starts the master mock server

Usage: pact-mock-server start [OPTIONS]

Options:
      --help                     Print help and exit
  -o, --output <output>          the directory where to write files to (defaults to current directory)
      --base-port <base-port>    the base port number that mock server ports will be allocated from. If not specified, ports will be randomly assigned by the OS.
  -v, --version                  Print version information and exit
  -p, --port <port>              port the master mock server runs on (defaults to 8080)
      --server-key <server-key>  the server key to use to authenticate shutdown requests (defaults to a random generated one)
  -h, --host <host>              hostname the master mock server runs on (defaults to localhost)
  -l, --loglevel <loglevel>      Log level for mock servers to write to the log file (defaults to info) [possible values: error, warn, info, debug, trace, none]
      --no-term-log              Turns off using terminal ANSI escape codes
      --no-file-log              Do not log to an output file


```

##### Options

###### Output directory: -o, --output <output>

This sets the output directory that log files and pact files are written to. It defaults to the current working directory.

##### Example

```console,ignore
$ ./pact-mock-server start -l debug -o logs/
15:40:08 [DEBUG] hyper::server: threads = 10
15:40:08 [INFO] pact_mock_server_cli::server: Server started on port 8080
```

#### create

This creates a new pact mock server managed by the master server from a pact file. The ID and port of the mock server
will be displayed.

Note, the tls option will only be available if the tls create feature is enabled. 

```console
$ ./pact-mock-server help create
Creates a new mock server from a pact file

Usage: pact-mock-server create [OPTIONS] --file <file>

Options:
  -f, --file <file>                    the pact file to define the mock server
      --help                           Print help and exit
  -c, --cors-preflight                 Handle CORS pre-flight requests
  -v, --version                        Print version information and exit
  -p, --port <port>                    port the master mock server runs on (defaults to 8080)
      --specification <specification>  The Pact specification version to use (defaults to V4)
  -h, --host <host>                    hostname the master mock server runs on (defaults to localhost)
      --tls                            Enable TLS with the mock server (will use a self-signed certificate)
  -l, --loglevel <loglevel>            Log level for mock servers to write to the log file (defaults to info) [possible values: error, warn, info, debug, trace, none]
      --no-term-log                    Turns off using terminal ANSI escape codes
      --no-file-log                    Do not log to an output file


```

##### Options

###### Pact File: -f, --file <file>

This option specifies the pact file to base the mock server on. It is a mandatory option.

##### Example

```console,ignore
$ ./pact-mock-server create -f ../../../libpact_matching/tests/pact.json
15:43:47 [INFO] pact_mock_server_cli::create_mock: Creating mock server from file ../../../libpact_matching/tests/pact.json
Mock server "7d1bf906d0ff42528f2d7d794dd19c5b" started on port 52943
```

#### list

Lists out all running mock servers with their ID, port, provider name and status.

```console
$ ./pact-mock-server list --help
Lists all the running mock servers

Usage: pact-mock-server list [OPTIONS]

Options:
      --help                 Print help and exit
  -v, --version              Print version information and exit
  -p, --port <port>          port the master mock server runs on (defaults to 8080)
  -h, --host <host>          hostname the master mock server runs on (defaults to localhost)
  -l, --loglevel <loglevel>  Log level for mock servers to write to the log file (defaults to info) [possible values: error, warn, info, debug, trace, none]
      --no-term-log          Turns off using terminal ANSI escape codes
      --no-file-log          Do not log to an output file


```

##### Example

```console,ignore
$ ./pact-mock-server list
Mock Server Id                    Port   Provider       Status
7d1bf906d0ff42528f2d7d794dd19c5b  52943  Alice Service  error
```

#### verify

This checks that the mock server, specified by ID or port number, has met all the expectations of the pact file. If all
expectations have been met, the pact file will be written out to the output directory that was specified with the start
sub-command. If there is any errors, no pact file will be written and the errors displayed to the console.

```console
$ ./pact-mock-server verify --help
Verify the mock server by id or port number, and generate a pact file if all ok

Usage: pact-mock-server verify [OPTIONS]

Options:
      --help
          Print help and exit
  -i, --mock-server-id <mock-server-id>
          the ID of the mock server
  -m, --mock-server-port <mock-server-port>
          the port number of the mock server
  -v, --version
          Print version information and exit
  -p, --port <port>
          port the master mock server runs on (defaults to 8080)
  -h, --host <host>
          hostname the master mock server runs on (defaults to localhost)
  -l, --loglevel <loglevel>
          Log level for mock servers to write to the log file (defaults to info) [possible values: error, warn, info, debug, trace, none]
      --no-term-log
          Turns off using terminal ANSI escape codes
      --no-file-log
          Do not log to an output file


```

##### Options

###### Mock server ID: -i, --mock-server-id <mock-server-id>

The ID of the mock server to verify. Either this option or the mock server port option must be provided.

###### Mock server Port: -m, --mock-server-port <mock-server-port>

The port number of the mock server to verify. Either this option or the mock server ID option must be provided.

##### Example

In the case of a mock server that has issues:

```console,ignore
$ ./pact-mock-server verify -m 52943
Mock server 7d1bf906d0ff42528f2d7d794dd19c5b/52943 failed verification with 1 errors

0 - Expected request was not received - {"method":"GET","path":"/mallory","query":"name=ron&status=good"}
```

and for a mock server that has matched all requests:

```console,ignore
$ ./pact-mock-server verify -m 52943
Mock server 7d1bf906d0ff42528f2d7d794dd19c5b/52943 verified ok
```

#### shutdown

Shutdown the mock server by id or port number, releasing all its resources.

```console
$ ./pact-mock-server help shutdown
Shutdown the mock server by id or port number, releasing all its resources

Usage: pact-mock-server shutdown [OPTIONS]

Options:
      --help
          Print help and exit
  -i, --mock-server-id <mock-server-id>
          the ID of the mock server
  -m, --mock-server-port <mock-server-port>
          the port number of the mock server
  -v, --version
          Print version information and exit
  -p, --port <port>
          port the master mock server runs on (defaults to 8080)
  -h, --host <host>
          hostname the master mock server runs on (defaults to localhost)
  -l, --loglevel <loglevel>
          Log level for mock servers to write to the log file (defaults to info) [possible values: error, warn, info, debug, trace, none]
      --no-term-log
          Turns off using terminal ANSI escape codes
      --no-file-log
          Do not log to an output file


```

##### Options

###### Mock server ID: -i, --mock-server-id <mock-server-id>

The ID of the mock server to shutdown. Either this option or the mock server port option must be provided.

###### Mock server Port: -m, --mock-server-port <mock-server-port>

The port number of the mock server to shutdown. Either this option or the mock server ID option must be provided.

##### Example

```console,ignore
$ ./pact-mock-server shutdown -i 3a94a472d04849048b78109e288702d0
Mock server with id '3a94a472d04849048b78109e288702d0' shutdown ok
```

## Restful JSON API

The master mock server provides a restful JSON API, and this API is what the command line sub-commands use to
communicate and control the master server.

### End points

#### GET /

This returns a list of all running mock servers managed by this master server.

example request:

```ignore
GET http://localhost:8080/ HTTP/1.1
```

example response:

```json
{
  "mockServers": [
    {
      "id": "7d1bf906d0ff42528f2d7d794dd19c5b",
      "port": 52943,
      "provider": "Alice Service",
      "status": "ok"
    }
  ]
}
```

#### POST /

This creates a new mock server from a pact file that must be present as JSON in the body. Returns the details of the mock server
in the response.

example request:

```ignore
POST http://localhost:8080/ HTTP/1.1
Content-Type: application/json
```

payload:

```json
{
  "provider": {
    "name": "Alice Service"
  },
  "consumer": {
    "name": "Consumer"
  },
  "interactions": [
    {
      "description": "a retrieve Mallory request",
      "request": {
        "method": "GET",
        "path": "/mallory",
        "query": "name=ron&status=good"
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "text/html"
        },
        "body": "\"That is some good Mallory.\""
      }
    }
  ]
}
```

example response:

```json
{
  "mockServer": {
    "id": "81c3483901e647ba8f545f2842d09cba",
    "port": 58276
  }
}
```

#### Response codes

##### 200 OK

This is returned when if the mock server was created successfully.

##### 422 Unprocessable Entity

This is returned if the pact JSON could not be parsed or the mock server could not be started.

#### GET /mockserver/:id

Returns details of the mock server with `:id`, which can be either a mockserver ID or port number.

example request:

```ignore
GET http://localhost:8080/mockserver/33218 HTTP/1.1
```

example response:

```json
{
  "id": "3201b3e2f04f402c83b374a077f8f8dd",
  "port": 33218,
  "provider": "Alice Service",
  "status": "error"
}
```

#### Response codes

##### 200 OK

This is returned with a valid mockserver.

##### 404 Not Found

This is returned if no mock server was found with the given ID or port number.

#### POST /mockserver/:id/verify

This checks that the mock server, specified by ID or port number, has met all the expectations of the pact file. If all
expectations have been met, the pact file will be written out to the output directory that was specified with the start
sub-command. If any mismatched requests where received by the mock server, they will be returned.

example request:

```ignore
POST http://localhost:8080/mockserver/33218/verify HTTP/1.1
```

#### Response codes

##### 204 No Content

This is returned when all expectations have been successfully met and the pact file has been written out to the output directory.

##### 422 Unprocessable Entity

This is returned if any expectations have not been met, or any unrecognised requests where received. The details are
returned in the body.

example response:

```json
{
  "mismatches": [
    {
      "method": "GET",
      "path": "/mallory",
      "request": {
        "method": "GET",
        "path": "/mallory",
        "query": "name=ron&status=good"
      },
      "type": "missing-request"
    }
  ],
  "mockServer": {
    "id": "3201b3e2f04f402c83b374a077f8f8dd",
    "port": 33218,
    "provider": "Alice Service",
    "status": "error"
  }
}
```

##### 404 Not Found

This is returned if the ID or port number did not correspond to a running mock server or the pact file could not be
written.

#### DELETE /mockserver/:id

Shuts down the mock server with `:id`, which can be either a mockserver ID or port number.

example request:

```ignore
DELETE http://localhost:8080/mockserver/33218 HTTP/1.1
```

#### Response codes

##### 204 No Content

This is returned when the mock server has been shutdown.

##### 404 Not Found

This is returned if no mock server was found with the given ID or port number.