Skip to content

wftest YAML runner: missing JSON path and header response assertions #366

@intel352

Description

@intel352

Summary

The YAML test runner's ResponseAssert struct only supports status (int) and body (substring match). The BDD framework supports richer response assertions that have no YAML equivalent:

  1. JSON path equality: the response JSON "key.path" should be "value" — dot-path traversal into JSON body
  2. JSON path non-empty: the response JSON "key" should not be empty
  3. Response header: the response header "X-Custom" should be "value"

Current behavior

assertions:
  - response:
      status: 200
      body: "some substring"    # only option for body checking

No way to assert that response.body.message == "Form created" or that a specific header is set.

Expected behavior

Extend ResponseAssert to support JSON path assertions and header checks:

assertions:
  - response:
      status: 200
      json:
        message: "Form created"        # exact match at JSON path
        data.id: "abc123"              # dot-path traversal
      json_not_empty:
        - data                          # assert path is non-empty
        - meta
      headers:
        Content-Type: "application/json"

Impact

We have ~20 BDD scenarios that use the response JSON "key" should be "value" assertions. These can only be approximated in YAML tests using body substring matching, which is less precise — e.g. body: "Form created" matches anywhere in the body, not specifically at the message key.

Suggested implementation

Type changes in yaml_types.go

type ResponseAssert struct {
    Status       int               `yaml:"status"`
    Body         string            `yaml:"body"`
    JSON         map[string]any    `yaml:"json"`           // dot-path → expected value
    JSONNotEmpty []string          `yaml:"json_not_empty"` // dot-paths that must be non-empty
    Headers      map[string]string `yaml:"headers"`        // header → expected value
}

Assertion logic in yaml_runner.go

The jsonPath() helper already exists in wftest/bdd/steps_assert.go and could be extracted to a shared location, or reimplemented in yaml_runner.go:

// JSON path assertions
for path, expected := range a.Response.JSON {
    val, err := jsonPath(result.RawBody, path)
    if err != nil {
        t.Errorf("assertion %s: %v", label, err)
        continue
    }
    if fmt.Sprintf("%v", val) != fmt.Sprintf("%v", expected) {
        t.Errorf("assertion %s: JSON %q: want %v, got %v", label, path, expected, val)
    }
}

// Header assertions
for header, expected := range a.Response.Headers {
    actual := result.Header(header)
    if actual != expected {
        t.Errorf("assertion %s: header %q: want %q, got %q", label, header, expected, actual)
    }
}

References

  • BDD JSON path assertions: wftest/bdd/steps_assert.go lines 161-189 (theResponseJSONShouldBe, theResponseJSONShouldNotBeEmpty)
  • BDD header assertion: wftest/bdd/steps_assert.go lines 192-201 (theResponseHeaderShouldBe)
  • YAML ResponseAssert: wftest/yaml_types.go lines 107-113

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions