chi/middleware/deprecation_test.go
Vojtech Vitek (golang.cz) fff8bb28cb
middleware: add Deprecation (RFC 9745), make Sunset RFC 8594 only (#1195)
* middleware: add Deprecation (RFC 9745), make Sunset RFC 8594 only

Sunset used to set Deprecation to an HTTP-date, following the old draft.
RFC 9745 defines Deprecation as a Structured Field Date (e.g. @1766571600),
so that value was unparseable for compliant clients. It also conflated the
deprecation date with the sunset date.

- Add Deprecation(deprecatedAt, links...) per RFC 9745.
- Sunset now sets only the Sunset header per RFC 8594.
- Sunset formats the date in UTC so non-UTC times emit a valid GMT date.

Breaking for anyone relying on Sunset to also set Deprecation: add
Deprecation to the chain explicitly.

* middleware: cross-link Sunset and Deprecation in docs

* middleware: address review on Sunset/Deprecation docs and Link examples

* middleware: recommend deprecate-then-sunset lifecycle in docs

* middleware: cite RFC 9651 for the Structured Field Date type

* middleware: panic on zero time in Sunset and Deprecation

A zero time is almost always an unset value (ignored parse error, missing
config field). Failing at router construction surfaces it at startup
instead of silently dropping the header.

* middleware: document how to fix the zero-time panic

* middleware: fix placeholder in zero-time panic godoc

* middleware: shorten zero-time panic messages

* middleware: guide users on choosing Deprecation and Sunset

* middleware: test multiple Link values for Sunset and Deprecation

* middleware: pass subtest t into Deprecation test helper

serve closed over the parent t, so a failure inside a subtest reported
"subtest may have called FailNow on a parent test" instead of naming it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-13 22:39:07 +02:00

105 lines
3 KiB
Go

package middleware
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/go-chi/chi/v5"
)
func TestDeprecation(t *testing.T) {
deprecatedAt := time.Date(2025, 12, 24, 10, 20, 0, 0, time.UTC)
serve := func(t *testing.T, mw func(http.Handler) http.Handler) http.Header {
r := chi.NewRouter()
r.Use(mw)
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
})
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
r.ServeHTTP(w, req)
if w.Code != 200 {
t.Fatal("Response Code should be 200")
}
return w.Header()
}
t.Run("Deprecation without link", func(t *testing.T) {
h := serve(t, Deprecation(deprecatedAt))
if got, want := h.Get("Deprecation"), "@1766571600"; got != want {
t.Fatalf("Deprecation = %q, want %q", got, want)
}
if h.Get("Sunset") != "" {
t.Fatal("Deprecation should not set Sunset.")
}
if h.Get("Link") != "" {
t.Fatal("Link should be empty.")
}
})
t.Run("Deprecation with link", func(t *testing.T) {
link := `<https://example.com/v1/deprecation-details>; rel="deprecation"`
h := serve(t, Deprecation(deprecatedAt, link))
if got, want := h.Get("Deprecation"), "@1766571600"; got != want {
t.Fatalf("Deprecation = %q, want %q", got, want)
}
if got := h.Get("Link"); got != link {
t.Fatalf("Link = %q, want %q", got, link)
}
})
t.Run("Deprecation with multiple links", func(t *testing.T) {
docs := `<https://example.com/v1/deprecation-details>; rel="deprecation"`
next := `<https://example.com/v2/users>; rel="successor-version"`
h := serve(t, Deprecation(deprecatedAt, docs, next))
got := h.Values("Link")
if len(got) != 2 || got[0] != docs || got[1] != next {
t.Fatalf("Link = %q, want [%q %q]", got, docs, next)
}
})
t.Run("Zero time panics", func(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("Deprecation should panic for zero time.")
}
}()
Deprecation(time.Time{})
})
t.Run("Combined with Sunset", func(t *testing.T) {
sunsetAt := time.Date(2026, 6, 1, 0, 0, 0, 0, time.UTC)
h := serve(t, func(next http.Handler) http.Handler {
return Deprecation(deprecatedAt)(Sunset(sunsetAt)(next))
})
if h.Get("Deprecation") != "@1766571600" {
t.Fatal("Deprecation header missing.")
}
if h.Get("Link") != "" {
t.Fatal("Link should be empty.")
}
if got, want := h.Get("Sunset"), "Mon, 01 Jun 2026 00:00:00 GMT"; got != want {
t.Fatalf("Sunset = %q, want %q", got, want)
}
})
t.Run("Combined with Sunset keeps links from both", func(t *testing.T) {
sunsetAt := time.Date(2026, 6, 1, 0, 0, 0, 0, time.UTC)
dep := `<https://example.com/deprecation>; rel="deprecation"`
sun := `<https://example.com/sunset>; rel="sunset"`
h := serve(t, func(next http.Handler) http.Handler {
return Deprecation(deprecatedAt, dep)(Sunset(sunsetAt, sun)(next))
})
got := h.Values("Link")
if len(got) != 2 || got[0] != dep || got[1] != sun {
t.Fatalf("Link = %q, want [%q %q]", got, dep, sun)
}
})
}