Skip to content

Commit 6c6a54b

Browse files
committed
fix: wire env var collapse into test handler for cross-env matching
The test handler now collapses incoming request URLs before hashing, using the same EnvExpander as the record handler. This means: 1. Record against prod: URL stored as {{BASE_URL}}/users 2. Test against staging: incoming https://api.staging.com/users gets collapsed to {{BASE_URL}}/users before hash computation 3. Hashes match → snapshot found → comparison works Both record and test handlers now use Collapse() symmetrically, making env var support fully bidirectional.
1 parent b551abf commit 6c6a54b

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

cmd/etch/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ func runTest(args []string) int {
321321
}
322322

323323
handler := proxy.NewTestHandler(hc, ss, de, am)
324+
325+
// wire env var support for cross-environment matching
326+
if len(cfg.Env) > 0 {
327+
handler.EnvExpander = envvar.NewExpander(cfg.Env)
328+
}
329+
324330
addr := fmt.Sprintf(":%d", cfg.Port)
325331
srv := proxy.NewProxyServer(addr, proxy.ModeTest, cam, handler)
326332

internal/proxy/test_handler.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/ojuschugh1/etch/internal/approval"
1111
"github.com/ojuschugh1/etch/internal/diff"
12+
"github.com/ojuschugh1/etch/internal/envvar"
1213
"github.com/ojuschugh1/etch/internal/hash"
1314
"github.com/ojuschugh1/etch/internal/snapshot"
1415
)
@@ -20,6 +21,7 @@ type TestHandler struct {
2021
SnapshotStore *snapshot.SnapshotStore
2122
DiffEngine *diff.DiffEngine
2223
ApprovalManager *approval.ApprovalManager
24+
EnvExpander *envvar.Expander // optional, collapses URLs before hashing
2325
Summary *snapshot.TestSummary
2426
mu sync.Mutex
2527
}
@@ -39,8 +41,16 @@ func NewTestHandler(hc *hash.HashComputer, ss *snapshot.SnapshotStore, de *diff.
3941
// and compares the live response against it. Counters are updated
4042
// thread-safely.
4143
func (h *TestHandler) HandleRequest(req *http.Request, resp *http.Response) error {
42-
// Compute request hash.
43-
reqHash, err := h.HashComputer.ComputeHash(req.Method, req.URL.String(), req.Header)
44+
// Collapse the URL to match how it was stored during recording.
45+
// If env vars are configured, https://api.staging.com/users becomes
46+
// {{BASE_URL}}/users — matching the snapshot's collapsed URL.
47+
requestURL := req.URL.String()
48+
if h.EnvExpander != nil {
49+
requestURL = h.EnvExpander.Collapse(requestURL)
50+
}
51+
52+
// Compute request hash from the (possibly collapsed) URL.
53+
reqHash, err := h.HashComputer.ComputeHash(req.Method, requestURL, req.Header)
4454
if err != nil {
4555
return fmt.Errorf("test: compute hash: %w", err)
4656
}

0 commit comments

Comments
 (0)