Skip to content

Commit 87305d9

Browse files
mpywclaude
andcommitted
refactor: move delay from mock to query/mutation level
Move the `delay` field from mock configuration to the query/mutation top level, allowing delay to be used with both SQL and mock sources. Changes: - Remove Delay from config.Mock struct - Add Delay to config.Query and config.Mutation structs - Update handler to parse and apply delay before response - Remove internal/mock/delay.go (no longer needed) - Update schema.json, SCHEMA.md, and example config - Rename e2e/mock_delay_test.* to e2e/delay_test.* Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 663875b commit 87305d9

11 files changed

Lines changed: 626 additions & 251 deletions

File tree

SCHEMA.md

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ This document describes all configuration options for sql-http-proxy. For usage
2424
- [Object Sources](#object-sources-type-one-only)
2525
- [Array Sources](#array-sources-type-many-or-type-one-with-filter)
2626
- [Filter](#filter)
27-
- [Delay](#delay)
2827
- [Mock JS Variables](#mock-js-variables)
28+
- [Delay](#delay)
2929
- [Transform](#transform)
3030
- [Pre-Transform](#pre-transform)
3131
- [Post-Transform](#post-transform)
@@ -415,14 +415,16 @@ The `filter` option allows filtering array data using JavaScript.
415415
filter: return row.role === input.role
416416
```
417417

418-
### Delay
418+
## Delay
419419

420-
Add artificial latency to mock responses. Useful for testing loading states and timeouts.
420+
Add artificial latency to responses. Useful for testing loading states and timeouts.
421421

422422
```yaml
423-
mock:
424-
object: { id: 1, name: Alice }
425-
delay: 500ms
423+
queries:
424+
- type: one
425+
path: /slow-user
426+
delay: 500ms
427+
sql: SELECT * FROM users WHERE id = :id
426428
```
427429

428430
| Value | Description |
@@ -432,20 +434,23 @@ mock:
432434
**Supported units:** `ns`, `us`/`µs`, `ms`, `s`, `m`, `h`
433435

434436
```yaml
435-
# With array source
436-
mock:
437-
array:
438-
- { id: 1, name: Alice }
439-
- { id: 2, name: Bob }
440-
delay: 1s
441-
442-
# With filter
443-
mock:
444-
array:
445-
- { id: 1, name: Alice }
446-
- { id: 2, name: Bob }
447-
filter: return row.id == parseInt(input.id)
448-
delay: 200ms
437+
# With mock source
438+
queries:
439+
- type: many
440+
path: /slow-users
441+
delay: 1s
442+
mock:
443+
array:
444+
- { id: 1, name: Alice }
445+
- { id: 2, name: Bob }
446+
447+
# With mutations
448+
mutations:
449+
- type: one
450+
method: POST
451+
path: /slow-create
452+
delay: 200ms
453+
sql: INSERT INTO users (name) VALUES (:name) RETURNING *
449454
```
450455

451456
### Mock JS Variables
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/mpyw/sql-http-proxy/internal/server"
1313
)
1414

15-
func TestMockDelay(t *testing.T) {
16-
cfg, err := config.ParseFile("mock_delay_test.yaml")
15+
func TestDelay(t *testing.T) {
16+
cfg, err := config.ParseFile("delay_test.yaml")
1717
require.NoError(t, err)
1818

1919
mux, err := server.NewServeMux(nil, cfg, ".")
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,38 @@ queries:
22
# Object source with delay
33
- type: one
44
path: /slow-user
5+
delay: 50ms
56
mock:
67
object:
78
id: 1
89
name: Slow User
9-
delay: 50ms
1010

1111
# Array source with delay
1212
- type: many
1313
path: /slow-users
14+
delay: 50ms
1415
mock:
1516
array:
1617
- { id: 1, name: Alice }
1718
- { id: 2, name: Bob }
18-
delay: 50ms
1919

2020
# Array source with filter and delay
2121
- type: one
2222
path: /slow-filtered-user
23+
delay: 50ms
2324
mock:
2425
array:
2526
- { id: 1, name: Alice }
2627
- { id: 2, name: Bob }
2728
filter: return row.id == parseInt(input.id)
28-
delay: 50ms
2929

3030
# JS source with delay
3131
- type: one
3232
path: /slow-js-user
33+
delay: 50ms
3334
mock:
3435
object_js: |
3536
return { id: parseInt(input.id), name: "JS User" };
36-
delay: 50ms
3737
3838
# Zero delay (no actual delay)
3939
- type: one

0 commit comments

Comments
 (0)