|
| 1 | +# httpsuite |
| 2 | + |
| 3 | +Run `.http` files as API tests, for local development and CI. A single binary |
| 4 | +with no runtime dependencies beyond the Go standard library and two uRadical |
| 5 | +modules ([httpparser](../httpparser) for parsing, [webbrowser](../webbrowser) |
| 6 | +for the forthcoming `--ui`). |
| 7 | + |
| 8 | +httpsuite executes the requests in your `.http` files, checks any `# @expect` |
| 9 | +assertions you attach, and reports pass/fail with the right output for where |
| 10 | +it's running — coloured and aligned on a terminal, plain and diffable in CI. |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +```sh |
| 15 | +go install github.com/uradical/httpsuite@latest |
| 16 | +``` |
| 17 | + |
| 18 | +Requires Go 1.21 or later. |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +``` |
| 23 | +httpsuite [--var key=value]... [--ui] [path] |
| 24 | +``` |
| 25 | + |
| 26 | +- `path` is optional and defaults to the current directory. |
| 27 | +- `--var key=value` overrides a `{{placeholder}}`. Repeatable. |
| 28 | +- `--ui` is reserved for a results UI and currently prints a notice and exits. |
| 29 | + |
| 30 | +```sh |
| 31 | +httpsuite # discover and run in the current directory |
| 32 | +httpsuite ./api # run a directory |
| 33 | +httpsuite ./api/users.http # run a single file |
| 34 | +httpsuite --var token=abc ./api # override {{token}} |
| 35 | +``` |
| 36 | + |
| 37 | +Exit code is **0** when everything passes, **1** when any request or assertion |
| 38 | +fails, and **2** on a usage or setup error. |
| 39 | + |
| 40 | +## Discovery |
| 41 | + |
| 42 | +When `path` is a directory (or omitted), httpsuite decides what to run: |
| 43 | + |
| 44 | +1. If a **`httpsuite.yaml`** exists in the directory, it is used as the suite |
| 45 | + definition. |
| 46 | +2. Otherwise every **`*.http`** file in the directory is run serially, in |
| 47 | + sorted order. |
| 48 | + |
| 49 | +When `path` is a single file, only that file is run. |
| 50 | + |
| 51 | +## Suite definition (`httpsuite.yaml`) |
| 52 | + |
| 53 | +```yaml |
| 54 | +parallel: true # run groups concurrently (default false) |
| 55 | +timeout: 30s # global per-request timeout (default 10s) |
| 56 | +groups: |
| 57 | + - name: auth |
| 58 | + serial: true # opt this group out of parallel execution |
| 59 | + files: |
| 60 | + - auth/login.http |
| 61 | + - auth/refresh.http |
| 62 | + - name: users |
| 63 | + timeout: 5s # override the timeout for this group |
| 64 | + files: |
| 65 | + - users/list.http |
| 66 | + - users/create.http |
| 67 | +``` |
| 68 | +
|
| 69 | +- Files within a group always run serially, in order. |
| 70 | +- `serial: true` keeps a group off the parallel path even when `parallel` is on. |
| 71 | +- `timeout` may be set globally and overridden per group. |
| 72 | + |
| 73 | +## Variables |
| 74 | + |
| 75 | +`{{placeholder}}` tokens in a request's URL, headers, and body are resolved at |
| 76 | +execution time. Resolution order, from lowest to highest precedence: |
| 77 | + |
| 78 | +1. File-level `@key = value` declarations inside the `.http` file |
| 79 | +2. OS environment variables |
| 80 | +3. `--var key=value` flags |
| 81 | + |
| 82 | +A placeholder that is still unresolved at execution time fails that request. |
| 83 | + |
| 84 | +## Assertions (`# @expect`) |
| 85 | + |
| 86 | +Attach checks to a request with `# @expect` comments. All assertions on a |
| 87 | +request are evaluated — evaluation never stops at the first failure — and a |
| 88 | +request fails if **any** assertion fails. A request with no assertions passes as |
| 89 | +long as it returns any HTTP response. |
| 90 | + |
| 91 | +```http |
| 92 | +# @name createUser |
| 93 | +POST {{baseUrl}}/users |
| 94 | +Content-Type: application/json |
| 95 | +# @expect status == 201 |
| 96 | +# @expect header Location exists |
| 97 | +# @expect body.id number |
| 98 | +# @expect body.name == "Ada Lovelace" |
| 99 | +# @expect body.roles length > 0 |
| 100 | +# @expect duration < 500 |
| 101 | +
|
| 102 | +{ "name": "Ada Lovelace" } |
| 103 | +``` |
| 104 | + |
| 105 | +### Status |
| 106 | + |
| 107 | +```http |
| 108 | +# @expect status == 201 # == != < > <= >= |
| 109 | +# @expect status 2xx # 2xx 3xx 4xx 5xx range match |
| 110 | +``` |
| 111 | + |
| 112 | +### Headers |
| 113 | + |
| 114 | +```http |
| 115 | +# @expect header X-Request-Id exists |
| 116 | +# @expect header Content-Type == application/json # case-insensitive contains |
| 117 | +``` |
| 118 | + |
| 119 | +### Body |
| 120 | + |
| 121 | +The response body is parsed as JSON. Paths use dot notation with array indexing, |
| 122 | +e.g. `body.items[0].id`. |
| 123 | + |
| 124 | +```http |
| 125 | +# @expect body.id exists |
| 126 | +# @expect body.id == 10 # numeric compare: == != < > <= >= |
| 127 | +# @expect body.name == "Alan Bradley" # quoted string |
| 128 | +# @expect body.active == true # boolean |
| 129 | +# @expect body.items[0].name == "Alan" # array indexing |
| 130 | +
|
| 131 | +# type checks |
| 132 | +# @expect body.count number # integer, no decimal point |
| 133 | +# @expect body.price double # number with a decimal point |
| 134 | +# @expect body.name string |
| 135 | +# @expect body.active boolean |
| 136 | +# @expect body.address object |
| 137 | +# @expect body.items array |
| 138 | +
|
| 139 | +# length of an array or string |
| 140 | +# @expect body.items length 3 |
| 141 | +# @expect body.items length > 0 |
| 142 | +
|
| 143 | +# dates |
| 144 | +# @expect body.created date # ISO 8601 by default |
| 145 | +# @expect body.created date "YYYY-MM-DD" |
| 146 | +# @expect body.timestamp date "YYYY-MM-DD HH:mm" |
| 147 | +``` |
| 148 | + |
| 149 | +Date format tokens: `YYYY MM DD` (date) and `HH mm ss` (time). |
| 150 | + |
| 151 | +### Duration |
| 152 | + |
| 153 | +```http |
| 154 | +# @expect duration < 500 # milliseconds elapsed for the request |
| 155 | +``` |
| 156 | + |
| 157 | +> JetBrains `> {% ... %}` response-handler scripts are not executed. When one is |
| 158 | +> found, httpsuite prints a warning and continues — use `# @expect` instead. |
| 159 | + |
| 160 | +## Output |
| 161 | + |
| 162 | +On a terminal, results are coloured (green pass, red fail, bold summary) with |
| 163 | +`✓`/`✗` marks on assertion lines. With no TTY (CI), the same layout is printed |
| 164 | +without ANSI codes and with `PASS`/`FAIL` words instead of glyphs: |
| 165 | + |
| 166 | +``` |
| 167 | +FAIL POST https://api.example.com/users 422 89ms |
| 168 | + PASS status 2xx |
| 169 | + FAIL status == 201 expected 201, got 422 |
| 170 | + FAIL body.id exists field not present in response |
| 171 | + PASS body.name == "Alan" |
| 172 | +
|
| 173 | +1 requests 0 passed 1 failed 89ms |
| 174 | +``` |
| 175 | + |
| 176 | +## Development |
| 177 | + |
| 178 | +```sh |
| 179 | +go test ./... # unit tests |
| 180 | +go vet ./... |
| 181 | +``` |
0 commit comments