Skip to content

fix: preserve auto HEAD status for route middleware - #3103

Open
jaeseon510 wants to merge 1 commit into
labstack:masterfrom
jaeseon510:fix/auto-head-logger-status
Open

jaeseon510 wants to merge 1 commit into
labstack:masterfrom
jaeseon510:fix/auto-head-logger-status

Conversation

@jaeseon510

Copy link
Copy Markdown
Contributor

Summary

With RouterConfig.AutoHandleHEAD enabled, a GET handler returning c.String(http.StatusNotFound, "missing") sends a 404 for HEAD requests, but a route- or group-level RequestLogger records 200. The global logger records 404.

headResponseWriter.WriteHeader marks the underlying response committed before route middleware returns, but leaves Response.Status unchanged until the later commit() call. This makes ResolveResponseStatus read the stale status.

Changes

  • Set the underlying Response.Status alongside Committed when the HEAD handler writes its first status.
  • Add regression coverage for GET and HEAD with global, group, and route loggers; verify repeated WriteHeader calls preserve the first status and headers remain delayed.
  • Update RouterConfig.AutoHandleHEAD GoDoc to describe status visibility to route and group middleware.

Test

  • The new regression tests fail before the fix: group/route HEAD logging reports 200 instead of 404.
  • go test -race ./...
  • go vet ./...
  • go run honnef.co/go/tools/cmd/staticcheck@latest ./...
  • go run golang.org/x/lint/golint@latest -set_exit_status ./...
  • An actual HTTP test server confirms route-level HEAD logging changes from 200 to 404 while the client response remains 404 with an empty body. GET and global logger results remain 404.

Docs impact: updated the AutoHandleHEAD API reference in GoDoc.

verification

@codecov

codecov Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.73%. Comparing base (dcb05f0) to head (1abc640).
⚠️ Report is 15 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3103      +/-   ##
==========================================
+ Coverage   93.34%   95.73%   +2.38%     
==========================================
  Files          43       44       +1     
  Lines        4735     4035     -700     
==========================================
- Hits         4420     3863     -557     
+ Misses        192      172      -20     
+ Partials      123        0     -123     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Successfully merging this pull request may close these issues.

1 participant