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/450: mux; detect route conflicts in more cases #453

Merged
merged 27 commits into from
Jun 13, 2024
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Most recent version is listed first.


# v0.0.99
- ong/errors: errors; retain stack traces when errors are wrapped with fmt: https://github.com/komuw/ong/pull/452
- ong/errors: retain stack traces when errors are wrapped with fmt: https://github.com/komuw/ong/pull/452
- ong/mux: detect route conflicts in more cases: https://github.com/komuw/ong/pull/453

# v0.0.98
- ong/errors: add equivalent functions from standard library: https://github.com/komuw/ong/pull/449
Expand Down
2 changes: 1 addition & 1 deletion internal/mx/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (r *router) detectConflict(method, pattern string, originalHandler http.Han
sameLen := len(incomingSegments) == len(existingSegments)
if !sameLen {
// no conflict
break
continue
}

errMsg := fmt.Errorf(`
Expand Down
14 changes: 14 additions & 0 deletions internal/mx/route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,20 @@ func TestConflicts(t *testing.T) {
errH := r.handle(http.MethodGet, "/index.php", secondRoute(msg2), secondRoute(msg2))
attest.Ok(t, errH)
})

t.Run("http MethodAll conflicts with all other methods", func(t *testing.T) {
t.Parallel()
r := newRouter(nil)

msg1 := "firstRoute"
msg2 := "secondRoute"
err := r.handle(http.MethodGet, "/post", firstRoute(msg1), firstRoute(msg1))
attest.Ok(t, err)

// This one returns with a conflict message.
errB := r.handle(MethodAll, "post/", secondRoute(msg2), secondRoute(msg2))
attest.Error(t, errB)
})
}

func TestNotFound(t *testing.T) {
Expand Down
48 changes: 48 additions & 0 deletions mux/mux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package mux

import (
"net/http"
"testing"

"github.com/komuw/ong/config"
"go.akshayshah.org/attest"
)

func TestNew(t *testing.T) {
routes := func() []Route {
return []Route{
NewRoute("/home", MethodGet, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})),
NewRoute("/home/", MethodAll, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})),
}
}

// There are other tests in internal/mx
t.Run("conflict detected", func(t *testing.T) {
rtz := []Route{}
rtz = append(rtz, tarpitRoutes()...)
rtz = append(rtz, routes()...)

attest.Panics(t, func() {
_ = New(config.DevOpts(nil, "secretKey12@34String"), nil, rtz...)
})
})

t.Run("okay", func(t *testing.T) {
rtz := []Route{
NewRoute("/home", MethodGet, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})),
NewRoute("/health/", MethodAll, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})),
}
// does not panic.
_ = New(config.DevOpts(nil, "secretKey12@34String"), nil, rtz...)
})
}

func tarpitRoutes() []Route {
return []Route{
NewRoute(
"/libraries/joomla/",
MethodAll,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}),
),
}
}
Loading