chore: fixes to http mux

This commit is contained in:
Jon Lundy
2023-01-09 12:32:45 -07:00
parent 4fc9c78dae
commit 0810ec73a0
9 changed files with 93 additions and 27 deletions

View File

@@ -1,6 +1,7 @@
package mux
import (
"log"
"net/http"
)
@@ -12,16 +13,16 @@ type mux struct {
func (mux *mux) Add(fns ...interface{ RegisterHTTP(*http.ServeMux) }) {
for _, fn := range fns {
// log.Printf("HTTP: %T", fn)
log.Printf("HTTP: %T", fn)
fn.RegisterHTTP(mux.ServeMux)
if fn, ok := fn.(interface{ RegisterAPIv1(*http.ServeMux) }); ok {
// log.Printf("APIv1: %T", fn)
log.Printf("APIv1: %T", fn)
fn.RegisterAPIv1(mux.api)
}
if fn, ok := fn.(interface{ RegisterWellKnown(*http.ServeMux) }); ok {
// log.Printf("APIv1: %T", fn)
log.Printf("WellKnown: %T", fn)
fn.RegisterWellKnown(mux.wellknown)
}
}
@@ -33,13 +34,11 @@ func New() *mux {
ServeMux: http.NewServeMux(),
}
mux.Handle("/api/v1/", http.StripPrefix("/api/v1", mux.api))
mux.Handle("/.well-known/", http.StripPrefix("/.well-known/", mux.api))
mux.Handle("/.well-known/", http.StripPrefix("/.well-known", mux.wellknown))
return mux
}
type RegisterHTTP func(*http.ServeMux)
func (fn RegisterHTTP) RegisterHTTP(mux *http.ServeMux) {
fn(mux)
}
func (fn RegisterHTTP) RegisterHTTP(mux *http.ServeMux) { fn(mux) }

View File

@@ -10,26 +10,62 @@ import (
)
type mockHTTP struct {
onServeHTTP func()
onServeHTTP func()
onServeAPIv1 func()
onServeWellKnown func()
}
func (m *mockHTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
m.onServeHTTP()
func (*mockHTTP) ServeFn(fn func()) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) { fn() }
}
func (h *mockHTTP) RegisterHTTP(mux *http.ServeMux) {
mux.Handle("/", h)
mux.HandleFunc("/", h.ServeFn(h.onServeHTTP))
}
func (h *mockHTTP) RegisterAPIv1(mux *http.ServeMux) {
mux.Handle("/ping", h)
mux.HandleFunc("/ping", h.ServeFn(h.onServeAPIv1))
}
func (h *mockHTTP) RegisterWellKnown(mux *http.ServeMux) {
mux.HandleFunc("/echo", h.ServeFn(h.onServeWellKnown))
}
func TestHttpMux(t *testing.T) {
func TestHttp(t *testing.T) {
is := is.New(t)
called := false
calledAPIv1 := false
calledWellKnown := false
mux := mux.New()
mux.Add(&mockHTTP{func() { called = true }})
mux.Add(&mockHTTP{
func() { called = true },
func() { calledAPIv1 = true },
func() { calledWellKnown = true },
})
is.True(mux != nil)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/", nil)
mux.ServeHTTP(w, r)
is.True(called)
is.True(!calledAPIv1)
is.True(!calledWellKnown)
}
func TestHttpAPIv1(t *testing.T) {
is := is.New(t)
called := false
calledAPIv1 := false
calledWellKnown := false
mux := mux.New()
mux.Add(&mockHTTP{
func() { called = true },
func() { calledAPIv1 = true },
func() { calledWellKnown = true },
})
is.True(mux != nil)
@@ -37,5 +73,32 @@ func TestHttpMux(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/api/v1/ping", nil)
mux.ServeHTTP(w, r)
is.True(called)
is.True(!called)
is.True(calledAPIv1)
is.True(!calledWellKnown)
}
func TestHttpWellKnown(t *testing.T) {
is := is.New(t)
called := false
calledAPIv1 := false
calledWellKnown := false
mux := mux.New()
mux.Add(&mockHTTP{
func() { called = true },
func() { calledAPIv1 = true },
func() { calledWellKnown = true },
})
is.True(mux != nil)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/.well-known/echo", nil)
mux.ServeHTTP(w, r)
is.True(!called)
is.True(!calledAPIv1)
is.True(calledWellKnown)
}