Skip to content

Commit 4075d15

Browse files
committed
feat: allow handlers to override the default status code
this defers setting the status code till right before the write
1 parent 60c213d commit 4075d15

4 files changed

Lines changed: 357 additions & 92 deletions

File tree

cmd/muxt/testdata/argument_call_takes_response.txt

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ type T struct{}
3636

3737
func (T) F(context.Context, any) any { return nil }
3838

39-
func (T) Headers(response http.ResponseWriter) any { return nil }
39+
func (T) Headers(response http.ResponseWriter) any {
40+
response.Header().Set("x-some-data", "123")
41+
response.WriteHeader(http.StatusNotFound)
42+
return nil
43+
}
4044
-- template_test.go --
4145
package main
4246

@@ -80,10 +84,19 @@ func Test(t *testing.T) {
8084

8185
mux.ServeHTTP(res, req)
8286

83-
if res.WriteHeaderCallCount != 0 {
84-
t.Error("unexpected WriteHeader call")
87+
if res.WriteHeaderCallCount != 1 {
88+
t.Errorf("unexpected WriteHeader count: want %d got %d", 1, res.WriteHeaderCallCount)
89+
}
90+
if res.HeaderCallCount != 3 {
91+
t.Errorf("unexpected Header call count: want %d got %d", 3, res.HeaderCallCount)
92+
}
93+
if val := rec.Result().Header.Get("x-some-data"); val != "123" {
94+
t.Errorf("unexpected value for x-some-data: want %q got %q", "123", val)
95+
}
96+
if rec.Result().Header.Get("content-length") == "" {
97+
t.Error("unexpected content-length to be set")
8598
}
86-
if res.HeaderCallCount != 0 {
87-
t.Error("unexpected HeaderCall call")
99+
if rec.Result().Header.Get("content-type") == "" {
100+
t.Error("unexpected content-type to be set")
88101
}
89102
}

example/hypertext/template_routes.go

Lines changed: 48 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)