Skip to content
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

feat(mux): support http.Request.PathValue in Go 1.22 #901

Merged
merged 1 commit into from
Feb 17, 2024
Merged
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
4 changes: 4 additions & 0 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ func (mx *Mux) routeHTTP(w http.ResponseWriter, r *http.Request) {

// Find the route
if _, _, h := mx.tree.FindRoute(rctx, method, routePath); h != nil {
if supportsPathValue {
setPathValue(rctx, r)
}

h.ServeHTTP(w, r)
return
}
Expand Down
20 changes: 20 additions & 0 deletions path_value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build go1.22
// +build go1.22

package chi

import "net/http"

// supportsPathValue is true if the Go version is 1.22 and above.
//
// If this is true, `net/http.Request` has methods `SetPathValue` and `PathValue`.
const supportsPathValue = true

// setPathValue sets the path values in the Request value
// based on the provided request context.
func setPathValue(rctx *Context, r *http.Request) {
for i, key := range rctx.URLParams.Keys {
value := rctx.URLParams.Values[i]
r.SetPathValue(key, value)
}
}
19 changes: 19 additions & 0 deletions path_value_fallback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build !go1.22
// +build !go1.22

package chi

import "net/http"

// supportsPathValue is true if the Go version is 1.22 and above.
//
// If this is true, `net/http.Request` has methods `SetPathValue` and `PathValue`.
const supportsPathValue = false

// setPathValue sets the path values in the Request value
// based on the provided request context.
//
// setPathValue is only supported in Go 1.22 and above so
// this is just a blank function so that it compiles.
func setPathValue(rctx *Context, r *http.Request) {
}
69 changes: 69 additions & 0 deletions path_value_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//go:build go1.22
// +build go1.22

package chi

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

func TestPathValue(t *testing.T) {
testCases := []struct {
name string
pattern string
method string
pathKeys []string
requestPath string
expectedBody string
}{
{
name: "Basic path value",
pattern: "/hubs/{hubID}",
method: "GET",
pathKeys: []string{"hubID"},
requestPath: "/hubs/392",
expectedBody: "392",
},
{
name: "Two path values",
pattern: "/users/{userID}/conversations/{conversationID}",
method: "POST",
pathKeys: []string{"userID", "conversationID"},
requestPath: "/users/Gojo/conversations/2948",
expectedBody: "Gojo 2948",
},
}

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) {
pathValues := []string{}
for _, pathKey := range tc.pathKeys {
pathValue := r.PathValue(pathKey)
if pathValue == "" {
pathValue = "NOT_FOUND:" + pathKey
}

pathValues = append(pathValues, pathValue)
}

body := strings.Join(pathValues, " ")

w.Write([]byte(body))
}))

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

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