aboutsummaryrefslogtreecommitdiff
path: root/src/net/http/request_test.go
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2023-09-13 16:58:24 -0400
committerJonathan Amsterdam <jba@google.com>2023-09-14 00:00:28 +0000
commit495830acd6976c8a2b39dd4aa4fdc105ad72de52 (patch)
treea44f8bcbd8d75e67e2cd3ce5c607a6cbacde1616 /src/net/http/request_test.go
parentfccd0b9b70255099691deca5dc1d577efcfc889b (diff)
downloadgo-495830acd6976c8a2b39dd4aa4fdc105ad72de52.tar.xz
net/http: implement path value methods on Request
Add Request.PathValue and Request.SetPathValue, and the fields on Request required to support them. Populate those fields in ServeMux.ServeHTTP. Updates #61410. Change-Id: Ic88cb865b0d865a30d3b35ece8e0382c58ef67d1 Reviewed-on: https://go-review.googlesource.com/c/go/+/528355 Run-TryBot: Jonathan Amsterdam <jba@google.com> Reviewed-by: Damien Neil <dneil@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/net/http/request_test.go')
-rw-r--r--src/net/http/request_test.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/net/http/request_test.go b/src/net/http/request_test.go
index 5711164894..1aeb93fe14 100644
--- a/src/net/http/request_test.go
+++ b/src/net/http/request_test.go
@@ -16,6 +16,7 @@ import (
"math"
"mime/multipart"
. "net/http"
+ "net/http/httptest"
"net/url"
"os"
"reflect"
@@ -1414,3 +1415,92 @@ func TestErrNotSupported(t *testing.T) {
t.Error("errors.Is(ErrNotSupported, errors.ErrUnsupported) failed")
}
}
+
+func TestPathValueNoMatch(t *testing.T) {
+ // Check that PathValue and SetPathValue work on a Request that was never matched.
+ var r Request
+ if g, w := r.PathValue("x"), ""; g != w {
+ t.Errorf("got %q, want %q", g, w)
+ }
+ r.SetPathValue("x", "a")
+ if g, w := r.PathValue("x"), "a"; g != w {
+ t.Errorf("got %q, want %q", g, w)
+ }
+}
+
+func TestPathValue(t *testing.T) {
+ for _, test := range []struct {
+ pattern string
+ url string
+ want map[string]string
+ }{
+ {
+ "/{a}/is/{b}/{c...}",
+ "/now/is/the/time/for/all",
+ map[string]string{
+ "a": "now",
+ "b": "the",
+ "c": "time/for/all",
+ "d": "",
+ },
+ },
+ // TODO(jba): uncomment these tests when we implement path escaping (forthcoming).
+ // {
+ // "/names/{name}/{other...}",
+ // "/names/" + url.PathEscape("/john") + "/address",
+ // map[string]string{
+ // "name": "/john",
+ // "other": "address",
+ // },
+ // },
+ // {
+ // "/names/{name}/{other...}",
+ // "/names/" + url.PathEscape("john/doe") + "/address",
+ // map[string]string{
+ // "name": "john/doe",
+ // "other": "address",
+ // },
+ // },
+ } {
+ mux := NewServeMux()
+ mux.HandleFunc(test.pattern, func(w ResponseWriter, r *Request) {
+ for name, want := range test.want {
+ got := r.PathValue(name)
+ if got != want {
+ t.Errorf("%q, %q: got %q, want %q", test.pattern, name, got, want)
+ }
+ }
+ })
+ server := httptest.NewServer(mux)
+ defer server.Close()
+ _, err := Get(server.URL + test.url)
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+}
+
+func TestSetPathValue(t *testing.T) {
+ mux := NewServeMux()
+ mux.HandleFunc("/a/{b}/c/{d...}", func(_ ResponseWriter, r *Request) {
+ kvs := map[string]string{
+ "b": "X",
+ "d": "Y",
+ "a": "Z",
+ }
+ for k, v := range kvs {
+ r.SetPathValue(k, v)
+ }
+ for k, w := range kvs {
+ if g := r.PathValue(k); g != w {
+ t.Errorf("got %q, want %q", g, w)
+ }
+ }
+ })
+ server := httptest.NewServer(mux)
+ defer server.Close()
+ _, err := Get(server.URL + "/a/b/c/d/e")
+ if err != nil {
+ t.Fatal(err)
+ }
+}