Repository navigation
middleware: add Deprecation (RFC 9745), make Sunset RFC 8594 only #1195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VojtechVitek
wants to merge
11
commits into
master
Choose a base branch
from
middleware-deprecation-header
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
698cc3f
middleware: add Deprecation (RFC 9745), make Sunset RFC 8594 only
VojtechVitek 2097683
middleware: cross-link Sunset and Deprecation in docs
VojtechVitek 6ee9bf3
middleware: address review on Sunset/Deprecation docs and Link examples
VojtechVitek 8715ef7
middleware: recommend deprecate-then-sunset lifecycle in docs
VojtechVitek 60a0c22
middleware: cite RFC 9651 for the Structured Field Date type
VojtechVitek 0d8c215
middleware: panic on zero time in Sunset and Deprecation
VojtechVitek 5b12ee1
middleware: document how to fix the zero-time panic
VojtechVitek 98c007a
middleware: fix placeholder in zero-time panic godoc
VojtechVitek 71aa2d2
middleware: shorten zero-time panic messages
VojtechVitek 3137440
middleware: guide users on choosing Deprecation and Sunset
VojtechVitek be8ab91
middleware: test multiple Link values for Sunset and Deprecation
VojtechVitek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strconv" | ||
| "time" | ||
| ) | ||
|
|
||
| // Deprecation marks a route as deprecated, per RFC 9745. | ||
| // https://www.rfc-editor.org/rfc/rfc9745.html | ||
| // | ||
| // Use it from the day you decide to retire a route. The route keeps working, | ||
| // and clients are told to migrate. When you know the removal date, add [Sunset]. | ||
| // Middleware order doesn't matter. | ||
| // | ||
| // deprecatedAt is when the route was, or will be, deprecated. A past date is fine. | ||
| // It panics if deprecatedAt is the zero time, which is usually an unset value. | ||
| // Pass a real date, or skip the middleware when no date is set. | ||
| // | ||
| // Each link is added as-is as a Link header, so it must be a full RFC 8288 | ||
| // value, e.g. `<https://example.com/migrate>; rel="deprecation"`. | ||
| func Deprecation(deprecatedAt time.Time, links ...string) func(http.Handler) http.Handler { | ||
| if deprecatedAt.IsZero() { | ||
| panic("middleware.Deprecation: deprecatedAt must not be zero") | ||
| } | ||
|
|
||
| // RFC 9745 uses a Structured Field Date (RFC 9651), not an HTTP-date. | ||
| value := "@" + strconv.FormatInt(deprecatedAt.Unix(), 10) | ||
|
|
||
| return func(next http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.Header().Set("Deprecation", value) | ||
|
|
||
| for _, link := range links { | ||
| w.Header().Add("Link", link) | ||
| } | ||
| next.ServeHTTP(w, r) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| 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(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(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(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(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(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(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) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.