Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json unmarshaling asserts an error when using json Encoding #34

Closed
sdalezman opened this issue Mar 26, 2018 · 2 comments
Closed

json unmarshaling asserts an error when using json Encoding #34

sdalezman opened this issue Mar 26, 2018 · 2 comments

Comments

@sdalezman
Copy link

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)

@sjkaliski
Copy link
Collaborator

Thanks for pointing this out! Was able to reproduce. This seems to be the culprit -->

lines := strings.Split(data, "\n")

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.

sjkaliski added a commit that referenced this issue Mar 27, 2018
resolve error when using json encoder, fixes #34
@sdalezman
Copy link
Author

thanks for the quick turnaround. there's definitely some information lost, but I think for the purposes of the snapshot makes sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants