summaryrefslogtreecommitdiff
path: root/README.md
blob: 64e85c2706ab2ba36d28c6b3b063af3b5fdb5f71 (plain)
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
Hepi is a command-line tool for testing REST APIs using YAML-based configurations. It supports environment management, dynamic data generation, and response chaining, allowing you to build complex test scenarios where results/state from one request is used in subsequent ones.

## Installation

```bash
go install github.com/mitjafelicijan/hepi@latest
```

## CLI Usage

### Basic Usage

```bash
# Execute a single request
hepi -env staging -file test.yaml -req create_user

# Execute multiple requests
hepi -env staging -file test.yaml -req login,get_profile

# Use a specific environment
hepi -env staging -file test.yaml -req get_status
```

### Executing Groups

```bash
# Execute a group of requests defined in the YAML
hepi -env staging -file test.yaml -group auth_flow
```

### Overriding Variables

You can override variables defined in the `environments` section by passing them as system environment variables. This is useful for CI/CD or quick testing.

```bash
# Override the 'host' variable from the command line
host=https://httpbin.org go run main.go generators.go -env test -file test.yaml -group all
```

### Variable Precedence

When resolving `{{variable}}` placeholders, Hepi follows a strict lookup sequence. The first source to return a value wins.

1.  **System Environment**: Variables set in your shell or passed as command-line prefixes (e.g., `HOST=... go run ...`).
2.  **Local `.env` File**: Variables loaded from a `.env` file in the current directory. These provide defaults that can be overridden by the system environment.
3.  **YAML Environment**: Variables defined within the specific `environments` block selected via the `-env` flag.
4.  **Persistent State**: Key-value pairs stored in `.hepi.json` from previous request executions (accessed via `{{request_name.path.to.key}}`).

#### Rationale

This hierarchy (System > .env > YAML > State) is designed for **dynamic runtime overrides**:
*   **Non-destructive testing**: Override values from the CLI without modifying the static YAML configuration.
*   **Secret Management**: Keep sensitive credentials in the environment or `.env` files to avoid committing them to version control.
*   **CI/CD Integration**: Automated pipelines can inject configuration via environment variables which seamlessly take precedence.


### Options

*   `-env`: The environment to use.
*   `-file`: Path to the YAML configuration file.
*   `-req`: Comma-separated list of request names to execute.
*   `-group`: The name of a request group to execute.
*   `-headers`: Show response headers in the output.

## Core Concepts

### Environments

Environments allow you to define variables that change based on the target (e.g., local development vs. production). Each environment is a map of key-value pairs.

### Requests

Requests are the individual API calls you want to perform. Each request specifies its method, URL, headers, and body (`json`, `form`, or `files`).

### Groups

Groups are ordered lists of requests. Executing a group runs the requests in the specified sequence.

## Configuration Syntax

The configuration is defined in a YAML file (e.g., `test.yaml`).

### Substitution Syntax

Hepi uses two types of placeholders:

1.  **`{{variable}}`**: Used for substituting values from:
    *   **Environment Variables**: Values from a `.env` file (loaded automatically if present) or system environment variables.
    *   **Config Variables**: Variables defined in the `environments` section of the YAML.
    *   **Request State**: Values captured from previous request responses (e.g., `{{login_req.token}}`). For arrays, use index notation (e.g., `{{setup_project.members.0.name}}`).
2.  **`[[generator]]`**: Used for generating dynamic data (e.g., `[[email]]`, `[[name]]`).
3.  **`[[oneof: a, b, c]]`**: Randomly selects one of the provided values.

### State Chaining (Persistence)

When a request is executed, its response (if it's JSON) is stored in a local `.hepi.json` file. This allows subsequent requests to reference any field from the response using the `{{request_name.path.to.field}}` syntax.

## Data Generators (Fakers)

Hepi includes a wide range of generators for dynamic data. You can use these by wrapping the tag in double brackets, e.g., `[[email]]`.

| Tag | Description |
| :--- | :--- |
| `name` | Random full name |
| `first_name` | Random first name |
| `last_name` | Random last name |
| `email` | Random email address |
| `username` | Random username |
| `password` | Random password |
| `url` | Random URL |
| `phone` | Random phone number |
| `int` | Random integer (0 - 1,000,000) |
| `datetime` | Random time string |
| `date` | Random date string |
| `timestamp` | Random timestamp |
| `uuid_hyphenated`| Random hyphenated UUID |
| `jwt` | Random JWT token |
| `ipv4` | Random IPv4 address |
| `amount` | Random currency amount |
| `word` | Random word |
| `sentence` | Random sentence |
| `real_address` | Random real-world address |
| `cc_number` | Random credit card number |
| `cc_type` | Random credit card type |
| `domain_name` | Random domain name |
| `ipv6` | Random IPv6 address |
| `mac_address` | Random MAC address |
| `unix_time` | Random Unix timestamp |
| `currency` | Random currency code |

*Refer to `generators.go` for the latest implementation of these functions.*

## State File

Hepi stores response data in `.hepi.json` in the current directory. This file is updated after every successful request that returns a JSON response. You can inspect this file or delete it to clear the "memory" of previous requests.

## Examples

### 1. Basic Request with Environments

This example shows how to define multiple environments and a simple GET request that uses the `host` variable.

```yaml
environments:
  local:
    host: http://localhost:8080
    api_key: "dev-key-123"
  staging:
    host: https://api.staging.example.com
    api_key: "staging-key-456"

requests:
  get_status:
    method: GET
    url: "{{host}}/v1/status"
    description: "Check API health status"
    headers:
      X-API-Key: "{{api_key}}"
      Accept: "application/json"
```

To execute this request:
```bash
hepi -env local -file test.yaml -req get_status
```

### 2. Request with Dynamic Data

Demonstrating the use of various generators to create a new resource with randomized data.

```yaml
environments:
  local:
    host: http://localhost:8080

requests:
  create_user:
    method: POST
    url: "{{host}}/v1/users"
    description: "Create a new user with random profile data"
    headers:
      Content-Type: "application/json"
    json:
      name: "[[name]]"
      email: "[[email]]"
      username: "[[username]]"
      password: "[[password]]"
      profile:
        bio: "[[sentence]]"
        age: "[[int]]"
        website: "[[url]]"
        phone: "[[phone]]"
```

To execute this request:
```bash
hepi -env local -file test.yaml -req create_user
```

### 3. State Chaining (Persistence)

This scenario shows a full authentication flow where the token from the login response is reused in a subsequent request.

```yaml
environments:
  local:
    host: http://localhost:8080

requests:
  login:
    method: POST
    url: "{{host}}/v1/auth/login"
    description: "Authenticate and get a token"
    headers:
      Content-Type: "application/json"
    json:
      username: "admin"
      password: "secret-password"

  get_profile:
    method: GET
    url: "{{host}}/v1/profile"
    description: "Fetch user profile using the token from login"
    headers:
      Authorization: "Bearer {{login.token}}"
      Accept: "application/json"

groups:
  auth_flow:
    - login
    - get_profile
```

To execute this group:
```bash
hepi -env local -file test.yaml -group auth_flow
```

### 4. Form Data and Query Parameters

Example of a complex search request using both query parameters and URL-encoded form data.

```yaml
environments:
  local:
    host: http://localhost:8080

requests:
  search_items:
    method: POST
    url: "{{host}}/v1/search"
    description: "Search items with filters and pagination"
    params:
      q: "[[word]]"
      page: "1"
      limit: "20"
      sort: "[[oneof: asc, desc]]"
    form:
      category: "[[oneof: electronics, books, clothing]]"
      include_out_of_stock: "true"
      min_price: "[[int]]"
```

To execute this request:
```bash
hepi -env local -file test.yaml -req search_items
```

### 5. Nested JSON, Arrays, and Header Subscriptions

Showing how to handle complex data structures and reuse specific nested fields from previous state.

```yaml
environments:
  local:
    host: http://localhost:8080

requests:
  setup_project:
    method: POST
    url: "{{host}}/v1/projects"
    description: "Create a complex project structure"
    headers:
      Content-Type: "application/json"
    json:
      title: "Project [[word]]"
      settings:
        visibility: "[[oneof: public, private]]"
        notifications:
          email: true
          push: false
      tags: ["active", "[[word]]", "[[word]]"]
      members:
        - name: "[[name]]"
          role: "owner"
        - name: "[[name]]"
          role: "editor"

  verify_project:
    method: GET
    url: "{{host}}/v1/projects/{{setup_project.id}}"
    description: "Verify the project creation using the ID from the previous request"
    headers:
      X-Project-Owner: "{{setup_project.members.0.name}}"
      Accept: "application/json"
```

To execute these requests:
```bash
hepi -env local -file test.yaml -req setup_project,verify_project
```

### 6. File Uploads (Multipart)

Hepi supports uploading files using `multipart/form-data`. You can combine `form` fields and `files` in the same request.

```yaml
environments:
  local:
    host: http://localhost:8080

requests:
  upload_document:
    method: POST
    url: "{{host}}/v1/upload"
    description: "Upload a document with metadata"
    form:
      category: "financial"
      priority: "high"
    files:
      document: "path/to/report.pdf"
      thumbnail: "path/to/image.png"
```

To execute this request:
```bash
hepi -env local -file test.yaml -req upload_document
```

### 7. CRUD Operations (PUT, PATCH, DELETE)

Hepi supports all standard HTTP methods. This example shows how to update and delete resources.

```yaml
requests:
  update_user:
    method: PUT
    url: "{{host}}/v1/users/{{create_user.id}}"
    json:
      name: "[[name]]"
      active: true

  patch_settings:
    method: PATCH
    url: "{{host}}/v1/users/{{create_user.id}}/settings"
    form:
      theme: "dark"
      notifications: "enabled"

  delete_user:
    method: DELETE
    url: "{{host}}/v1/users/{{create_user.id}}"
    params:
      force: "true"
```

To execute these requests:
```bash
hepi -env local -file test.yaml -req update_user,patch_settings,delete_user
```