updates api example json matcher
Этот коммит содержится в:
родитель
f3b7331cea
коммит
865c0c18b6
1 изменённых файлов: 24 добавлений и 9 удалений
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -53,23 +52,39 @@ func (a *apiFeature) theResponseCodeShouldBe(code int) error {
|
||||||
|
|
||||||
func (a *apiFeature) theResponseShouldMatchJSON(body *gherkin.DocString) (err error) {
|
func (a *apiFeature) theResponseShouldMatchJSON(body *gherkin.DocString) (err error) {
|
||||||
var expected, actual []byte
|
var expected, actual []byte
|
||||||
var data interface{}
|
var exp, act interface{}
|
||||||
if err = json.Unmarshal([]byte(body.Content), &data); err != nil {
|
|
||||||
|
// re-encode expected response
|
||||||
|
if err = json.Unmarshal([]byte(body.Content), &exp); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if expected, err = json.Marshal(data); err != nil {
|
if expected, err = json.MarshalIndent(exp, "", " "); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
actual = bytes.TrimSpace(a.resp.Body.Bytes())
|
|
||||||
|
// re-encode actual response too
|
||||||
|
if err = json.Unmarshal(a.resp.Body.Bytes(), &act); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if actual, err = json.MarshalIndent(act, "", " "); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// the matching may be adapted per different requirements.
|
||||||
if len(actual) != len(expected) {
|
if len(actual) != len(expected) {
|
||||||
return fmt.Errorf("expected json length: %d does not match actual: %d", len(expected), len(actual))
|
return fmt.Errorf(
|
||||||
|
"expected json length: %d does not match actual: %d:\n%s",
|
||||||
|
len(expected),
|
||||||
|
len(actual),
|
||||||
|
string(actual),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, b := range actual {
|
for i, b := range actual {
|
||||||
if b != expected[i] {
|
if b != expected[i] {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf(
|
||||||
"expected json does not match actual at character: %s^%s",
|
"expected JSON does not match actual, showing up to last matched character:\n%s",
|
||||||
string(actual[:i]),
|
string(actual[:i+1]),
|
||||||
string(actual[i:i+1]),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче