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

Commit f6e39a1

Browse files
committed
Fixed plaintext subset and added tests
1 parent f4e66d9 commit f6e39a1

File tree

2 files changed

+252
-127
lines changed

2 files changed

+252
-127
lines changed

common/step/validate.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
http2 "net/http"
7+
"strings"
78

89
"github.com/tidwall/gjson"
910

@@ -106,7 +107,11 @@ func (httpValidator) validateBody(exp *ExpectBody, actual []byte) error {
106107
err := json.Unmarshal([]byte(exp.Is), &expected)
107108
if err != nil {
108109
expected = exp.Is
109-
// return fmt.Errorf("couldn't convert expected body into type: %w, body = %s", err, exp.Is)
110+
}
111+
112+
message := "expected value for body"
113+
if exp.Selector != nil {
114+
message = fmt.Sprintf("expected value for `%s` field", *exp.Selector)
110115
}
111116

112117
var received interface{}
@@ -119,16 +124,13 @@ func (httpValidator) validateBody(exp *ExpectBody, actual []byte) error {
119124
} else {
120125
err = json.Unmarshal(actual, &received)
121126
if err != nil {
122-
// treat it as plaintext
123-
received = string(actual)
127+
if !plainTextEqual(exp.Is, string(actual), *exp.Subset) {
128+
return fmt.Errorf("%s doesn't match actual:\nwant:\n%s\nreceived:\n%s", message, prettyPrint(exp.Is), prettyPrint(actual))
129+
}
130+
return nil
124131
}
125132
}
126133

127-
message := "expected value for body"
128-
if exp.Selector != nil {
129-
message = fmt.Sprintf("expected value for `%s` field", *exp.Selector)
130-
}
131-
132134
if !mapStructsEqual(expected, received, *exp.KeysOnly, *exp.Subset) {
133135
return fmt.Errorf("%s doesn't match actual:\nwant:\n%s\nreceived:\n%s", message, prettyPrint(expected), prettyPrint(received))
134136
}
@@ -140,6 +142,13 @@ func prettyPrint(i interface{}) string {
140142
return string(s)
141143
}
142144

145+
func plainTextEqual(exp, actual string, subset bool) bool {
146+
if !subset {
147+
return exp == actual
148+
}
149+
return strings.Index(actual, exp) != -1
150+
}
151+
143152
// mapStructsEqual checks if the actual map has all the fields and their corresponding values that exp has.
144153
// If the value for a field in exp is also a map, then mapStructsEqual will check recursively there as well
145154
// (returning false if the type in actual is not another map)

0 commit comments

Comments
 (0)