Closed as not planned
Description
Go version
go version go1.23.0 linux/amd64
Output of go env
in your module/workspace:
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/sdfg/.cache/go-build'
GOENV='/home/sdfg/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/sdfg/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/sdfg/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.0'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/sdfg/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build4261393948=/tmp/go-build -gno-record-gcc-switches'
What did you do?
I create a test with a main router and a subrouter and try to provide the path value from the main router into subrouter handler:
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
)
func Test_main(t *testing.T) {
rt1 := http.NewServeMux()
rt2 := http.NewServeMux()
rt1.HandleFunc("GET /{myid}/{tail...}", func(w http.ResponseWriter, r *http.Request) {
t.Logf("r1.GET /{myid}/{tail...}: myid=%q tail=%q", r.PathValue("myid"), r.PathValue("tail"))
p := stripToLastSlash(r.URL.Path, 2)
rp := stripToLastSlash(r.URL.RawPath, 2)
r2 := new(http.Request)
*r2 = *r
r2.URL = new(url.URL)
*r2.URL = *r.URL
r2.URL.Path = p
r2.URL.RawPath = rp
tail := r.PathValue("tail")
if tail == "" {
t.Error("r1: tail not found")
}
r2.SetPathValue("tail", tail)
rt2.ServeHTTP(w, r2)
})
rt2.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) {
t.Logf("r2.GET /: tail=%q", r.PathValue("tail"))
if r.PathValue("tail") == "" {
t.Error("r2: tail not found")
}
fmt.Fprintf(w, "%s", r.PathValue("tail"))
})
ts := httptest.NewServer(rt1)
defer ts.Close()
if _, body := testRequest(t, ts, "GET", "/123/456/789", nil); body != "456/789" {
t.Error("tail was not provided")
}
}
func stripToLastSlash(s string, cnt int) string {
pos := 0
for i, r := range s {
if r == '/' {
pos = i
cnt--
if cnt <= 0 {
break
}
}
}
return s[pos:]
}
func testRequest(t *testing.T, ts *httptest.Server, method, path string, body io.Reader) (*http.Response, string) {
req, err := http.NewRequest(method, ts.URL+path, body)
if err != nil {
t.Fatal(err)
return nil, ""
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
return nil, ""
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
return nil, ""
}
defer resp.Body.Close()
return resp, string(respBody)
}
What did you see happen?
tail
path value is not provided to subrouter:
main_test.go:17: r1.GET /{myid}/{tail...}: myid="123" tail="456/789"
main_test.go:39: r2.GET /: tail=""
main_test.go:41: r2: tail not found
main_test.go:50: tail was not provided
What did you expect to see?
r2.GET /
must returns body "456/789"
Metadata
Metadata
Assignees
Labels
No labels