|
| 1 | +--- |
| 2 | +title: Variable examples |
| 3 | +--- |
| 4 | + |
| 5 | +:::note |
| 6 | +- This page should be use as reference for specification files. |
| 7 | +- This page is subject to change. It is requested to check this page frequently. |
| 8 | +::: |
| 9 | + |
| 10 | +:::note |
| 11 | +Case-wise more example can be found in [https://github.com/chkware/cli](https://github.com/chkware/cli/tree/main/tests/resources/storage/sample_config/pass_cases/variables) repository |
| 12 | +::: |
| 13 | + |
| 14 | +[Variable specification document reference](/references/variable-reference) |
| 15 | + |
| 16 | +We can also use variables inside a http and testcase specification file. See examples below. |
| 17 | + |
| 18 | +** * This is highly experimental. Report any bug you can find.** |
| 19 | + |
| 20 | +### Request with query string using variables |
| 21 | + |
| 22 | +```yaml |
| 23 | +--- |
| 24 | +version: default:http:0.7.2 |
| 25 | + |
| 26 | +# define local variables |
| 27 | +variables: |
| 28 | + Foo: 'bar' |
| 29 | + Two: 2 |
| 30 | + Server: https://example.org/api |
| 31 | + |
| 32 | +request: |
| 33 | + # put variables on the path for query string |
| 34 | + url: {$Server}/path?foo={$Foo}&two={$Two} |
| 35 | + method: GET |
| 36 | + |
| 37 | +``` |
| 38 | + |
| 39 | +### Request with basic authentication header using variables |
| 40 | + |
| 41 | +```yaml |
| 42 | +--- |
| 43 | +version: default:http:0.7.2 |
| 44 | + |
| 45 | +# define local variables |
| 46 | +variables: |
| 47 | + userName: Some_USER |
| 48 | + password: Some-P@$$W03D |
| 49 | + content_type: application/json |
| 50 | + Server: https://example.org/api |
| 51 | + |
| 52 | +request: |
| 53 | + url: {$Server}/resource/id |
| 54 | + method: GET |
| 55 | + |
| 56 | + headers: |
| 57 | + Accept-Encoding: gzip, deflate |
| 58 | + Content-Type: {$content_type} |
| 59 | + |
| 60 | + auth[basic]: |
| 61 | + username: {$userName} |
| 62 | + password: {$password} |
| 63 | + |
| 64 | +``` |
| 65 | + |
| 66 | +### Testcase using variables |
| 67 | + |
| 68 | +```yaml |
| 69 | +--- |
| 70 | +version: 'default:testcase:0.7.2' |
| 71 | + |
| 72 | +variables: |
| 73 | + Name: 'Morpheus' |
| 74 | + Job: 'leader' |
| 75 | + Response: ~ |
| 76 | + Server: https://reqres.in/api/v1 |
| 77 | + |
| 78 | +request: |
| 79 | + url: "{$Server}/users" |
| 80 | + method: POST |
| 81 | + body[json]: { |
| 82 | + 'name': $Name, |
| 83 | + 'job': $Job, |
| 84 | + } |
| 85 | + return: ~ |
| 86 | + |
| 87 | +spec: |
| 88 | + execute: |
| 89 | + file: ~ |
| 90 | + result: $Response |
| 91 | + |
| 92 | + asserts: |
| 93 | + - {type: AssertEqual, actual: $Response.code, expected: 201} |
| 94 | + - {type: AssertIsMap, actual: $Response.body} |
| 95 | + |
| 96 | +``` |
0 commit comments