| author | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-10-30 18:41:38 UTC |
| committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-10-30 18:41:38 UTC |
| parent | 9ba7c429387f646539ca77188042421b7c8914d3 |
| server/http.go | +3 | -1 |
| server/server_test.go | +61 | -0 |
diff --git a/server/http.go b/server/http.go index 038cd00..d0019fb 100644 --- a/server/http.go +++ b/server/http.go @@ -523,7 +523,9 @@ func WithLogging(parent http.Handler) http.Handler { tr, _ := trace.FromContext(r.Context()) // Wrap the writer so we can get output information. - sw := statusWriter{ResponseWriter: w} + // The status defaults to 200, which is what net/http sends if the + // handler doesn't call WriteHeader. + sw := statusWriter{ResponseWriter: w, status: http.StatusOK} // Save the URL, since some of the callers will change it (e.g. // makeDir). diff --git a/server/server_test.go b/server/server_test.go index dc1818b..1a5db59 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -2,17 +2,20 @@ package server import ( "fmt" + "io" "io/ioutil" "net" "net/http" "net/http/httptest" "os" + "path/filepath" "strings" "testing" "time" "blitiri.com.ar/go/gofer/config" "blitiri.com.ar/go/gofer/ratelimit" + "blitiri.com.ar/go/gofer/reqlog" "blitiri.com.ar/go/log" ) @@ -277,6 +280,64 @@ func TestRateLimitRequestErrors(t *testing.T) { } } +// TestLoggingStatus checks that the request log records the status the client +// gets, in particular when the handler does not call WriteHeader explicitly. +func TestLoggingStatus(t *testing.T) { + cases := []struct { + name string + handler http.HandlerFunc + status int + length int + }{ + {"empty", func(w http.ResponseWriter, r *http.Request) {}, + 200, 0}, + {"write", func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte("hello")) + }, 200, 5}, + {"readfrom", func(w http.ResponseWriter, r *http.Request) { + w.(io.ReaderFrom).ReadFrom(strings.NewReader("hello")) + }, 200, 5}, + {"explicit", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusCreated) + }, 201, 0}, + {"error", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte("hello")) + }, 404, 5}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "log") + rl, err := reqlog.New(path, 1, "{{.Status}} {{.Length}}\n") + if err != nil { + t.Fatal(err) + } + + h := WithTrace("test", WithReqLog(WithLogging(c.handler), rl)) + w := httptest.NewRecorder() + h.ServeHTTP(w, httptest.NewRequest("GET", "http://unused/", nil)) + if w.Code != c.status { + t.Errorf("client got status %d, want %d", w.Code, c.status) + } + + // The log is written asynchronously, so wait for the entry. + want := fmt.Sprintf("%d %d\n", c.status, c.length) + got := "" + deadline := time.Now().Add(5 * time.Second) + for got == "" && time.Now().Before(deadline) { + time.Sleep(time.Millisecond) + buf, _ := os.ReadFile(path) + if strings.HasSuffix(string(buf), "\n") { + got = string(buf) + } + } + if got != want { + t.Errorf("logged %q, want %q", got, want) + } + }) + } +} + func Benchmark(b *testing.B) { makeBench := func(url string) func(b *testing.B) { return func(b *testing.B) {