Skip to content

feat(mux): support http.Request.Pattern in Go 1.23 #986

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,9 @@ func (mx *Mux) routeHTTP(w http.ResponseWriter, r *http.Request) {
if supportsPathValue {
setPathValue(rctx, r)
}
if supportsPattern {
setPattern(rctx, r)
}

h.ServeHTTP(w, r)
return
Expand Down
16 changes: 16 additions & 0 deletions pattern.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build go1.23 && !tinygo
// +build go1.23,!tinygo

package chi

import "net/http"

// supportsPattern is true if the Go version is 1.23 and above.
//
// If this is true, `net/http.Request` has field `Pattern`.
const supportsPattern = true

// setPattern sets the mux matched pattern in the http Request.
func setPattern(rctx *Context, r *http.Request) {
r.Pattern = rctx.routePattern
}
17 changes: 17 additions & 0 deletions pattern_fallback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build !go1.23 || tinygo
// +build !go1.23 tinygo

package chi

import "net/http"

// supportsPattern is true if the Go version is 1.23 and above.
//
// If this is true, `net/http.Request` has field `Pattern`.
const supportsPattern = false

// setPattern sets the mux matched pattern in the http Request.
//
// setPattern is only supported in Go 1.23 and above so
// this is just a blank function so that it compiles.
func setPattern(rctx *Context, r *http.Request) {}
56 changes: 56 additions & 0 deletions pattern_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//go:build go1.23
// +build go1.23

package chi

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestPattern(t *testing.T) {
testCases := []struct {
name string
pattern string
method string
requestPath string
}{
{
name: "Basic path value",
pattern: "/hubs/{hubID}",
method: "GET",
requestPath: "/hubs/392",
},
{
name: "Two path values",
pattern: "/users/{userID}/conversations/{conversationID}",
method: "POST",
requestPath: "/users/Gojo/conversations/2948",
},
{
name: "Wildcard path",
pattern: "/users/{userID}/friends/*",
method: "POST",
requestPath: "/users/Gojo/friends/all-of-them/and/more",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
r := NewRouter()

r.Handle(tc.method+" "+tc.pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(r.Pattern))
}))

ts := httptest.NewServer(r)
defer ts.Close()

_, body := testRequest(t, ts, tc.method, tc.requestPath, nil)
if body != tc.pattern {
t.Fatalf("expecting %q, got %q", tc.pattern, body)
}
})
}
}