Use reflect.DeepEqual to compare json strings
Signed-off-by: Joeky <jj16180339887@gmail.com>
Этот коммит содержится в:
родитель
338a78050c
коммит
7942151045
2 изменённых файлов: 20 добавлений и 35 удалений
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/DATA-DOG/godog"
|
"github.com/DATA-DOG/godog"
|
||||||
"github.com/DATA-DOG/godog/gherkin"
|
"github.com/DATA-DOG/godog/gherkin"
|
||||||
|
@ -51,44 +52,23 @@ 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 interface{}
|
||||||
var exp, act interface{}
|
|
||||||
|
|
||||||
// re-encode expected response
|
// re-encode expected response
|
||||||
if err = json.Unmarshal([]byte(body.Content), &exp); err != nil {
|
if err = json.Unmarshal([]byte(body.Content), &expected); err != nil {
|
||||||
return
|
|
||||||
}
|
|
||||||
if expected, err = json.MarshalIndent(exp, "", " "); err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// re-encode actual response too
|
// re-encode actual response too
|
||||||
if err = json.Unmarshal(a.resp.Body.Bytes(), &act); err != nil {
|
if err = json.Unmarshal(a.resp.Body.Bytes(), &actual); err != nil {
|
||||||
return
|
|
||||||
}
|
|
||||||
if actual, err = json.MarshalIndent(act, "", " "); err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// the matching may be adapted per different requirements.
|
// the matching may be adapted per different requirements.
|
||||||
if len(actual) != len(expected) {
|
if !reflect.DeepEqual(expected, actual) {
|
||||||
return fmt.Errorf(
|
return fmt.Errorf("expected JSON does not match actual, %v vs. %v", expected, actual)
|
||||||
"expected json length: %d does not match actual: %d:\n%s",
|
|
||||||
len(expected),
|
|
||||||
len(actual),
|
|
||||||
string(actual),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
for i, b := range actual {
|
|
||||||
if b != expected[i] {
|
|
||||||
return fmt.Errorf(
|
|
||||||
"expected JSON does not match actual, showing up to last matched character:\n%s",
|
|
||||||
string(actual[:i+1]),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func FeatureContext(s *godog.Suite) {
|
func FeatureContext(s *godog.Suite) {
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
txdb "github.com/DATA-DOG/go-txdb"
|
txdb "github.com/DATA-DOG/go-txdb"
|
||||||
|
@ -71,19 +72,23 @@ 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 interface{}
|
||||||
var data interface{}
|
|
||||||
if err = json.Unmarshal([]byte(body.Content), &data); err != nil {
|
// re-encode expected response
|
||||||
|
if err = json.Unmarshal([]byte(body.Content), &expected); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if expected, err = json.Marshal(data); err != nil {
|
|
||||||
|
// re-encode actual response too
|
||||||
|
if err = json.Unmarshal(a.resp.Body.Bytes(), &actual); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
actual = a.resp.Body.Bytes()
|
|
||||||
if string(actual) != string(expected) {
|
// the matching may be adapted per different requirements.
|
||||||
err = fmt.Errorf("expected json %s, does not match actual: %s", string(expected), string(actual))
|
if !reflect.DeepEqual(expected, actual) {
|
||||||
|
return fmt.Errorf("expected JSON does not match actual, %v vs. %v", expected, actual)
|
||||||
}
|
}
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *apiFeature) thereAreUsers(users *gherkin.DataTable) error {
|
func (a *apiFeature) thereAreUsers(users *gherkin.DataTable) error {
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче