updates api example json matcher

Этот коммит содержится в:
gedi 2017-05-03 14:46:10 +03:00
родитель f3b7331cea
коммит 865c0c18b6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 56604CDCCC201556

Просмотреть файл

@ -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]),
) )
} }
} }