Skip to content
This repository was archived by the owner on Sep 17, 2022. It is now read-only.

Commit 13dc532

Browse files
committed
Renamed validator to matcher
1 parent 19cc484 commit 13dc532

File tree

12 files changed

+88
-88
lines changed

12 files changed

+88
-88
lines changed

cmd/check.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ func check(cmd *cobra.Command, args []string) error {
5252
httpClient := http.NewClient()
5353
stepHttpClient := step.NewHTTPClient(httpClient)
5454
stepInterpolator := step.NewInterpolator()
55-
stepValidator := step.NewValidator()
55+
stepMatcher := step.NewMatcher()
5656
stepExporter := step.NewExporter()
57-
stepRunner := step.NewRunner(stepHttpClient, *stepInterpolator, stepValidator, stepExporter)
57+
stepRunner := step.NewRunner(stepHttpClient, *stepInterpolator, stepMatcher, stepExporter)
5858
specRunner := runner.NewParallelSpecRunner(parallelism, stepRunner, w)
5959

6060
w.Prelude()
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package validator
1+
package matcher
22

33
import (
44
"fmt"
55
"strings"
66
)
77

8-
type BodyTextValidator string
8+
type BodyTextMatcher string
99

10-
func (v *BodyTextValidator) Set(str string) {
11-
val := BodyTextValidator(str)
10+
func (v *BodyTextMatcher) Set(str string) {
11+
val := BodyTextMatcher(str)
1212
v = &val
1313
}
1414

15-
func (v BodyTextValidator) Validate(str string) (pass []string, fail []string) {
15+
func (v BodyTextMatcher) Validate(str string) (pass []string, fail []string) {
1616
if len(v) == 0 {
1717
fail = append(fail, "expected body is empty")
1818
return

spec/matcher/headers.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package matcher
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/getapid/apid/log"
7+
)
8+
9+
type HeaderMatcher map[StringMatcher]StringMatcher
10+
11+
func (v *HeaderMatcher) Set(headers map[string]string) {
12+
m := make(map[StringMatcher]StringMatcher, len(headers))
13+
for name, value := range headers {
14+
m[StringMatcher(name)] = StringMatcher(value)
15+
}
16+
val := HeaderMatcher(m)
17+
v = &val
18+
}
19+
20+
func (v HeaderMatcher) Validate(headers map[string][]string) (pass []string, fail []string) {
21+
log.L.Infof("validating headers %v", headers)
22+
for nameMatcher, valueMatcher := range v {
23+
found := false
24+
errStr := fmt.Sprintf("expected header %s, but none found", nameMatcher)
25+
for name, values := range headers {
26+
if !nameMatcher.Validate(name) {
27+
continue
28+
}
29+
errStr = fmt.Sprintf("expected header %s to match %s, got %v", nameMatcher, valueMatcher, values)
30+
31+
for _, value := range values {
32+
if !valueMatcher.Validate(value) {
33+
continue
34+
}
35+
pass = append(pass, fmt.Sprintf("has header %s = %s", nameMatcher, valueMatcher))
36+
found = true
37+
break
38+
}
39+
if found {
40+
break
41+
}
42+
}
43+
if !found {
44+
fail = append(fail, errStr)
45+
}
46+
}
47+
return pass, fail
48+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package validator
1+
package matcher
22

33
import (
44
"encoding/json"
@@ -8,13 +8,13 @@ import (
88
"github.com/tidwall/gjson"
99
)
1010

11-
type JSONValidator struct {
11+
type JSONMatcher struct {
1212
Selector *string `json:"selector"`
1313
Subset *bool `json:"is_subset"`
1414
Is interface{} `json:"is"`
1515
}
1616

17-
func (v *JSONValidator) Validate(body []byte) (pass []string, fail []string) {
17+
func (v *JSONMatcher) Validate(body []byte) (pass []string, fail []string) {
1818
message := "body"
1919
var received interface{}
2020
if v.Selector != nil {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package validator
1+
package matcher
22

33
import (
44
"regexp"
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package validator
1+
package matcher
22

33
import (
44
"fmt"
55
)
66

7-
type StatusCodeValidator int64
7+
type StatusCodeMatcher int64
88

9-
func (v *StatusCodeValidator) Set(i int64) {
10-
val := StatusCodeValidator(i)
9+
func (v *StatusCodeMatcher) Set(i int64) {
10+
val := StatusCodeMatcher(i)
1111
v = &val
1212
}
1313

14-
func (v StatusCodeValidator) Validate(value int64) ([]string, []string) {
14+
func (v StatusCodeMatcher) Validate(value int64) ([]string, []string) {
1515
if int64(v) != value {
1616
return nil, []string{fmt.Sprintf("expected status code to be %d, got %d", v, value)}
1717
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
package validator
1+
package matcher
22

33
import (
44
"strings"
55
)
66

7-
type StringValidator string
7+
type StringMatcher string
88

9-
func (v *StringValidator) Set(str string) {
10-
val := StringValidator(str)
9+
func (v *StringMatcher) Set(str string) {
10+
val := StringMatcher(str)
1111
v = &val
1212
}
1313

14-
func (v StringValidator) Validate(str string) bool {
14+
func (v StringMatcher) Validate(str string) bool {
1515
if len(v) == 0 {
1616
return false
1717
}

spec/spec.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package spec
22

33
import (
4-
"github.com/getapid/apid/spec/validator"
4+
"github.com/getapid/apid/spec/matcher"
55
)
66

77
type Spec struct {
@@ -26,8 +26,8 @@ type Request struct {
2626
}
2727

2828
type Expect struct {
29-
Code *validator.StatusCodeValidator `json:"code"`
30-
Headers *validator.HeaderValidator `json:"headers"`
31-
JSON []validator.JSONValidator `json:"json"`
32-
Text *validator.BodyTextValidator `json:"text"`
29+
Code *matcher.StatusCodeMatcher `json:"code"`
30+
Headers *matcher.HeaderMatcher `json:"headers"`
31+
JSON []matcher.JSONMatcher `json:"json"`
32+
Text *matcher.BodyTextMatcher `json:"text"`
3333
}

spec/validator/headers.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

step/interpolator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (i *Interpolator) interpolate(step spec.Step, vars variables.Variables) (sp
7070
// }
7171

7272
if result.Expect.Headers != nil {
73-
headerValidators := make(map[string]string, len(*result.Expect.Headers))
73+
headerMatchers := make(map[string]string, len(*result.Expect.Headers))
7474
for header, value := range *result.Expect.Headers {
7575
header, err := template.Render(string(header), vars)
7676
if err != nil {
@@ -84,9 +84,9 @@ func (i *Interpolator) interpolate(step spec.Step, vars variables.Variables) (sp
8484
return result, ErrInterpolationError
8585
}
8686

87-
headerValidators[header] = value
87+
headerMatchers[header] = value
8888
}
89-
result.Expect.Headers.Set(headerValidators)
89+
result.Expect.Headers.Set(headerMatchers)
9090
}
9191

9292
return result, nil

0 commit comments

Comments
 (0)