Skip to content

Commit

Permalink
fix(exp): configure headers before sending them
Browse files Browse the repository at this point in the history
Ensure the headers are configured before we set the status code and send the headers.
  • Loading branch information
jooola committed Jun 25, 2024
1 parent baf8806 commit baa13bf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 2 additions & 1 deletion hcloud/exp/mockutils/mockutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ func Handler(t *testing.T, requests []Request) http.HandlerFunc {
expected.Want(t, r)
}

w.WriteHeader(expected.Status)
switch {
case expected.JSON != nil:
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(expected.Status)
if err := json.NewEncoder(w).Encode(expected.JSON); err != nil {
t.Fatal(err)
}
case expected.JSONRaw != "":
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(expected.Status)
_, err := w.Write([]byte(expected.JSONRaw))
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 2 additions & 0 deletions hcloud/exp/mockutils/mockutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ func TestHandler(t *testing.T) {
resp, err := http.Get(server.URL)
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
assert.Equal(t, `{"data":"Hello"}`, readBody(t, resp))

// Request 2
resp, err = http.Get(server.URL)
require.NoError(t, err)
assert.Equal(t, 400, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
assert.Equal(t, `{"error": "failed"}`, readBody(t, resp))
}

Expand Down

0 comments on commit baa13bf

Please sign in to comment.