upcake 0.1.2

Fast, easy and consistent testing for web APIs
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
<h1 align="center">
  🧁 Upcake
</h1>

<p align="center">
  <em>Cupcakes for your API</em>
</p>

<p align="center">
  <a href="https://github.com/johnnynotsolucky/upcake/actions/workflows/build.yaml"><img src="https://github.com/johnnynotsolucky/upcake/actions/workflows/build.yaml/badge.svg" /></a>
</p>

## Overview

Upcake enables assertions to be performed against HTTP requests. Requests and
their assertions are defined in a YAML file and can have dependencies on each
other, provided that dependency requests are named. Assertions can be against
request timing data and any response data including headers, content and
response code.

The request URL, headers and content support template rendering with
[Handlebars](https://docs.rs/handlebars/4.1.3/handlebars/index.html) syntax.

Requests are run in parallel except where they have a dependency to another
request in which case they will wait until their dependencies have completed
before starting.

The response headers and content of a request is made available as part of the
template context for dependent requests.

## Installation

### With cargo

```bash
cargo install upcake
```

### From source

```bash
git clone https://github.com/johnnynotsolucky/upcake.git
cd upcake
cargo install --path .
```

## Usage

### Command-line options

- `--env-var-prefix` - An optional prefix to filter environment variables
  injected into the template context
- `-k`, `--insecure` - Allow insecure server connections when using SSL
- `-L`, `--location` - Follow redirects
- `-v`, `--verbose` - Verbose output
- `-s`, `--max-response-size` - Maximum response size in bytes
- `-e`, `--extra-vars` Set additional variables as key=value or YAML. To use a
  file, prepend the value with "@". Available in the template context in the `user` property.
- `--connect-timeout` - Maximum time allowed for connection
- `-c`, `--config-file` - Path to the request config file. Defaults to
  "Upcakefile.yaml". Required if `--url` is not set.
- `-u`, `--url` - Run default assertions against a URL. Will use the default
  request config. Required if `--config-file` is not set.
- `-X`, `--request-method` - Specify request method to use. Used in conjunction
  with `--url`. Defaults to "GET".
- `-H`, `--header` - Pass custom headers to server. Used in conjunction with
  `--url`.
- `--status-code` - Verify the response code. Used in conjunction with `--url`.
  Defaults to 200.

**Note:** Configuration set from the command line will override configuration for that
property set in the Upcakefile.

### Configuration

- `location` - Follow redirects. Overridden by `--insecure`.
- `insecure` - Allow insecure server connections when using SSL. Overridden by
  `--insecure`.
- `connect_timeout` - Maximum time allowed for connection. Overridden by
  `--connect-timeout`.
- `extra_vars` - Set additional variables from YAML mapping. Merged with vars
  set with `--extra-vars`. Available in the template context in the `user` property.
- `verbose` - Verbose output. Overridden by `--verbose`.
- `max_response_size` - Maximum response size in bytes. Overridden by
  `--max-response-size`.
- `requests` - List of request configurations.

### Request configuration

- `name` _optional_ -  Name of the request.
- `requires` _optional_ - List of named requests this request depends upon.
- `request_method` _optional_ - The HTTP method to use. Defaults to "GET".
- `data` _optional_ - Data to send with the request. Send the contents of a file by prefixing the value with an "@", for example "@path/to/body/template.hbs". Relative paths are relative to the directory of the loaded configuration file.
- `headers`_optional_ - Mapping of headers to be sent.
- `url`- The URL to make the request to.
- `assertions` _optional_ - A list of assertions to perform on the response. Defaults to a HTTP 200 assertion.

### Assertion configuration

- `type` - The type of assertion to use. See [available assertions]#available-assertions.
- `path` - The [jql]https://crates.io/crates/jql path to the field the assertion should run against. Defaults to `.`. `path` is ignored on inner assertions, for example the [length]#length assertion.
- `skip` _optional_ - Whether to skip the assertion. If set, requires a string value for the reason.

### Available assertions

In addition to the top-level assertion configuartion, each assertion has its own properties which are required to be set.

####  Between

Type: `between`

Assert that a value is within a range.

- `min` - Start of range.
- `max` - End of range.
- `inclusive` _optional_ - Whether to include `min` and `max`in the assertion.

##### Example

```yaml
- type: between
  path: ."response_code"
  min: 200
  max: 399
  inclusive: true
```

#### Equal

Type: `equal`

Assert that a value equals the given value.

- `value` - Value to assert.

##### Example

```yaml
- type: equal
  path: ."response_code"
  value: 200
```

#### Not Equal

Type: `not-equal`

Assert that a value is not equal to the given value.

- `value` - Value to assert.

##### Example

```yaml
- type: not-equal
  path: ."response_code"
  value: 204
```

#### Length

Type: `length`

Assert that the length of a value passes the given assertion

- `assertion` - Any assertion with the same configuration defined in [assertion configuration](#assertion-configuration).

##### Example

```yaml
- type: length
  path: ."headers".[]
  assertion:
    - type: equal
      value: 5
```

#### Contains

Type: `contains`

Assert that a response value contains the given value.

- For strings, it asserts that the substring is present in the value;
- For arrays, it asserts that the value is present in the array;
- For mappings (dictionary/object types), asserts that the input map is present in the response map.

- `value` - The value to assert is contained in the given value.

##### Examples

**Assert mapping contains all key/value pairs**

```yaml
- type: contains
  path: ."content".{}
  value:
    key: Value
    another_property: Some other value
```

**Assert array contains a value**

```yaml
- type: contains
  path: ."content"."my_integer_array".[]
  value: 10
```

or

```yaml
- type: contains
  path: ."content"."my_object_array".[]
  value:
    id: item_10
    value: Item Value
```

**Assert substring appears in response value**

```yaml
- type: contains
  path: ."content"."my_string"
  value: "value"
```

#### Exists

Type: `exists`

Assert that the given value exists as a key in the response value.

- `value` - The value to assert exists in the given mapping.

##### Example

```yaml
- type: exists
  path: ."content"."my_object".{}
  value: id
```

#### Greater than

Type: `greater-than`

Assert that a value is greater than the given value.

- `value` - Value to assert.

##### Example

```yaml
- type: greater-than
  path: ."response_code"
  value: 200
```

#### Greater than equal

Type: `greater-than-equal`

Assert that a value is greater or equal to the given value.

- `value` - Value to assert.

##### Example

```yaml
- type: greater-than-equal
  path: ."response_code"
  value: 200
```

#### Less than

Type: `less-than`

Assert that a value is less than the given value

- `value` - Value to assert.

##### Example

```yaml
- type: less-than
  path: ."response_code"
  value: 400
```

#### Less than equal

Type: `less-than-equal`

Assert that a value is less than or equal to the given value

- `value` - Value to assert.

##### Example

```yaml
- type: less-than-equal
  path: ."timing"."starttransfer"
  value: 100
```

### Response data

#### Request result

- `http_version` - The HTTP version used for the request.
- `response_code` - The HTTP response code returned.
- `response_message` - A HTTP response message returned from the host, if any.
- `headers` - A list of response headers.
- `timing` - Response timing data. See [timing results](#timing-results).
- `content` - Response content, either formatted as JSON, or raw content if it
couldn't be parsed as JSON.

#### Timing results

- `namelookup` - Duration in milliseconds from the start of the request until
name lookup resolved.
- `connect` - Duration in milliseconds from the start of the request until a
connection to the remote host is established.
- `pretransfer` - Duration in milliseconds from the start of the request until
file transfer was about to begin.
- `starttransfer` - Duration in milliseconds from the start of the request until
the first byte was received. AKA TTFB.
- `total` - Duration in milliseconds from the start of the request until the
request ended.
- `dns_resolution` - Alias for `namelookup`.
- `tcp_connection` - Difference of `connect` and `namelookup`.
- `tls_connection` - Difference of `pretransfer` and `connect`.
- `server_processing` - Difference of `starttransfer` and `pretransfer`.
- `content_transfer` - Difference of `total` and `starttransfer`.

## Examples

Examples are in [examples](examples/).

They are configured to run against a local [httpbin](https://httpbin.org/) server.

### httpbin Server

#### Start with docker

```bash
docker run -p 8888:80 kennethreitz/httpbin
```

#### Start with docker-compose

```bash
docker-compose --file examples/docker-compose.yaml up
```

### Run the examples

```bash
upcake --config-file examples/basic.yaml
```

### Command-line examples

#### Inline POST request

```bash
upcake --url http://localhost:8888/post -X POST
```

#### Inline request with custom header

```bash
upcake --url http://localhost:8888/get -H "X-My-Token: token"
```

#### Inline request with environment variable

```bash
MY_TOKEN=token upcake --url http://localhost:8888/get -H "X-My-Token: {{env.MY_TOKEN}}"
```

#### Validate inline request status code

```bash
upcake --url http://localhost:8888/post -X PATCH --status-code 405
```

#### Dependencies example with AUTH_TOKEN

```bash
AUTH_TOKEN=my_token upcake --config-file ./examples/pipeline.yaml
```

#### Validation failure

```bash
upcake --url http://localhost:8888/post -X PATCH
echo $?
```

#### Verbose output (libcurl)

```bash
upcake --url http://localhost:8888/get --verbose
```