json unmarshaling asserts an error when using json Encoding
sdalezman opened this issue · 2 comments
The hard coded implementation in assert.go for parsing json responses breaks when using json.NewEncoder([insert-my-struct]).Enocde(w)
. It asserts that
To reproduce:
type myStruct struct {
Message `json:"message"`
}
func myHandler(w http.ResponseWriter, r *http.Request) {
m := myStruct{
Message: "hello abide"
}
json.NewEncoder(w).Encode(&m)
}
if you call assert http response on the above in some test: abide.AssertHTTPResponse(t, name, w.Result())
, assert will return an error for unexpected end of JSON input
. This is because there's a new line that seems to be returned from httputil.DumpResponse
on the json response when using NewEncoder
vs using json.Marshal
and writing bytes.
assert.go seems to be manually parsing the lines to determine the body, instead it probably makes more sense to actually read the properties from the response rather than the dump from httputil (I think there are a few cases where the dump won't always be consistent - but can't remember off the top of my head)
Thanks for pointing this out! Was able to reproduce. This seems to be the culprit -->
Line 46 in d831b87
The below change should make it work:
lines := strings.Split(strings.TrimSpace(data), "\n")
I'm inclined to use the built in http dump unless there is a desire to include information which is not provided by that method.
thanks for the quick turnaround. there's definitely some information lost, but I think for the purposes of the snapshot makes sense!