Skip to content

feat: capture mock request fields into test variables (v1 backport)#326

Merged
gaydin merged 1 commit into
lamoda:v1from
keyclaim:feat/mock-request-variable-capture-v1
Jul 10, 2026
Merged

feat: capture mock request fields into test variables (v1 backport)#326
gaydin merged 1 commit into
lamoda:v1from
keyclaim:feat/mock-request-variable-capture-v1

Conversation

@keyclaim

Copy link
Copy Markdown
Contributor

Backport of #325 to the v1 line.

What

Adds variablesToSet to mock definitions: values of the incoming request (JSON body, query, headers) are extracted with the template syntax of the template reply strategy ({{ .request.* }}) and become available to subsequent tests via {{ $varName }}.

mocks:
  backend:
    strategy: constant
    body: '{"ok": true}'
    variablesToSet:
      capturedUserId: '{{ .request.Json.user.id }}'
      capturedToken:  '{{ .request.Header "X-Auth" }}'
      capturedSource:
        template: '{{ .request.Query "source" }}'
        fromCall: first        # first | last (default)

Details

  • works on nested definitions of composite strategies (uriVary, methodVary, sequence, basedOnRequest)
  • fromCall: first|last selects the call to capture from when a definition is called multiple times (default: last)
  • capture happens only for requests that passed requestConstraints; a missing JSON field is a test error
  • on a name collision, the test response's variables_to_set takes precedence over a mock-captured value
  • adds {{ .request.Header "name" }} to the mock template context

Backport note

Same change as #325, adapted to the pre-goccy/go-yaml loader on v1 (map[interface{}]interface{} mock definitions). Tests and docs identical.

See #325 for the full description.

Add `variablesToSet` to mock definitions: values of the incoming
request (JSON body, query, headers) are extracted with the template
syntax of the template reply strategy ({{ .request.* }}) and become
available to subsequent tests via {{ $varName }}.

- works on nested definitions of composite strategies
  (uriVary, methodVary, sequence, basedOnRequest)
- `fromCall: first|last` selects the call to capture from when the
  definition is called multiple times (default: last)
- variables from the test response take precedence on name collision
- add {{ .request.Header "name" }} to the mock template context
@gaydin gaydin merged commit e0c01b1 into lamoda:v1 Jul 10, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants