Skip to content

Commit

Permalink
issues/480: mux; add flexible pattern (#481)
Browse files Browse the repository at this point in the history
- Fixes: #480
  • Loading branch information
komuw authored Oct 21, 2024
1 parent 424f53c commit 7969424
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
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

0 comments on commit 7969424

Please sign in to comment.