-
Notifications
You must be signed in to change notification settings - Fork 103
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
Update error handling in DoJSON #722
Conversation
@sebasslash I added some unit tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks good. Some stylistic non-blocking items ⬇️
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
fakeBody := map[string]any{ | ||
"id": "example", | ||
"name": "fixture", | ||
"method": r.Method, | ||
} | ||
fakeBodyRaw, err := json.Marshal(fakeBody) | ||
require.NoError(t, err) | ||
|
||
if strings.HasSuffix(r.URL.String(), "/ok_request") { | ||
w.Header().Set("content-type", "application/json") | ||
w.Header().Set("content-length", strconv.FormatInt(int64(len(fakeBodyRaw)), 10)) | ||
w.WriteHeader(http.StatusOK) | ||
_, err = w.Write(fakeBodyRaw) | ||
require.NoError(t, err) | ||
} else if strings.HasSuffix(r.URL.String(), "/bad_request") { | ||
w.WriteHeader(http.StatusBadRequest) | ||
} else if strings.HasSuffix(r.URL.String(), "/created_request") { | ||
w.WriteHeader(http.StatusCreated) | ||
} else if strings.HasSuffix(r.URL.String(), "/not_modified_request") { | ||
w.WriteHeader(http.StatusNotModified) | ||
} else { | ||
w.WriteHeader(http.StatusNotFound) | ||
} | ||
})) | ||
t.Cleanup(func() { | ||
testServer.Close() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💅 Worth defining this as a helper in helper_test.go
?
if strings.HasSuffix(r.URL.String(), "/ok_request") { | ||
w.Header().Set("content-type", "application/json") | ||
w.Header().Set("content-length", strconv.FormatInt(int64(len(fakeBodyRaw)), 10)) | ||
w.WriteHeader(http.StatusOK) | ||
_, err = w.Write(fakeBodyRaw) | ||
require.NoError(t, err) | ||
} else if strings.HasSuffix(r.URL.String(), "/bad_request") { | ||
w.WriteHeader(http.StatusBadRequest) | ||
} else if strings.HasSuffix(r.URL.String(), "/created_request") { | ||
w.WriteHeader(http.StatusCreated) | ||
} else if strings.HasSuffix(r.URL.String(), "/not_modified_request") { | ||
w.WriteHeader(http.StatusNotModified) | ||
} else { | ||
w.WriteHeader(http.StatusNotFound) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💅 A switch/case may be cleaner given the longer if/else chain:
paths := strings.Split(r.URL.Path, "/")
switch paths[len(paths) - 1] {
case "ok_request":
...
}
Reminder to the contributor that merged this PR: if your changes have added important functionality or fixed a relevant bug, open a follow-up PR to update CHANGELOG.md with a note on your changes. |
This was part of a new method in #717 -- when an error occurs, the resp.Body is nil so we need to wait until after checking
err != nil