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

issues/480: mux; add flexible pattern #481

Merged
merged 6 commits into from
Oct 21, 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
99 changes: 99 additions & 0 deletions internal/mx/mx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,105 @@ func TestMux(t *testing.T) {
})
}

func TestMuxFlexiblePattern(t *testing.T) {
t.Parallel()

tr := &http.Transport{
// since we are using self-signed certificates, we need to skip verification.
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}

httpsPort := tst.GetPort()
domain := "localhost"

l := log.New(context.Background(), &bytes.Buffer{}, 500)

t.Run("flexible pattern accepts all uris", func(t *testing.T) {
t.Parallel()

msg := "hello world"
rt, err := NewRoute(
"/*",
MethodGet,
someMuxHandler(msg),
)
attest.Ok(t, err)
mux, err := New(
config.WithOpts(domain, httpsPort, tst.SecretKey(), config.DirectIpStrategy, l),
nil,
rt,
)
attest.Ok(t, err)

ts, err := tst.TlsServer(mux, domain, httpsPort)
attest.Ok(t, err)
defer ts.Close()

{
res, errA := client.Get(ts.URL + "/UnknownUri")
attest.Ok(t, errA)

rb, errB := io.ReadAll(res.Body)
attest.Ok(t, errB)

attest.Equal(t, res.StatusCode, http.StatusOK)
attest.Equal(t, string(rb), msg)
}

{
res, errC := client.Get(ts.URL + "/")
attest.Ok(t, errC)

rb, errD := io.ReadAll(res.Body)
attest.Ok(t, errD)

attest.Equal(t, res.StatusCode, http.StatusOK)
attest.Equal(t, string(rb), msg)
}

{
res, errE := client.Get(ts.URL + "/hey/a/b/cool")
attest.Ok(t, errE)

rb, errF := io.ReadAll(res.Body)
attest.Ok(t, errF)

attest.Equal(t, res.StatusCode, http.StatusOK)
attest.Equal(t, string(rb), msg)
}
})

t.Run("conflict", func(t *testing.T) {
t.Parallel()

msg := "hello world"

rt1, err := NewRoute(
"/*",
MethodGet,
someMuxHandler(msg),
)
attest.Ok(t, err)

rt2, err := NewRoute(
"/hi",
MethodGet,
thisIsAnotherMuxHandler(),
)
attest.Ok(t, err)

_, err = New(
config.WithOpts(domain, httpsPort, tst.SecretKey(), config.DirectIpStrategy, l),
nil,
rt1,
rt2,
)
attest.Error(t, err)
attest.Subsequence(t, err.Error(), "would conflict")
})
}

func getManyRoutes(b *testing.B) []Route {
b.Helper()

Expand Down
11 changes: 11 additions & 0 deletions internal/mx/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ func NewRoute(
}

func (r Route) match(ctx context.Context, segs []string) (context.Context, bool) {
if len(r.segments) == 1 && r.segments[0] == "*" {
// The router is allowed to handle all request paths
return ctx, true
}

if len(segs) > len(r.segments) {
return nil, false
}

for i, seg := range r.segments {
if i > len(segs)-1 {
return nil, false
Expand All @@ -91,6 +97,7 @@ func (r Route) match(ctx context.Context, segs []string) (context.Context, bool)
ctx = context.WithValue(ctx, muxContextKey(seg), segs[i])
}
}

return ctx, true
}

Expand Down Expand Up @@ -222,6 +229,10 @@ already exists and would conflict`,
getfunc(rt.originalHandler),
)

if len(existingSegments) == 1 && existingSegments[0] == "*" && len(incomingSegments) > 0 {
return errMsg
}

if pattern == rt.pattern {
return errMsg
}
Expand Down
Loading