chore: fixes to http mux
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/matryer/is"
|
||||
"github.com/sour-is/ev/pkg/es"
|
||||
"github.com/sour-is/ev"
|
||||
"github.com/sour-is/ev/pkg/math"
|
||||
)
|
||||
|
||||
@@ -74,8 +74,8 @@ func TestPagerBox(t *testing.T) {
|
||||
{1, 10, -2, -10, 9, -9},
|
||||
{1, 10, 0, -10, 0, 0},
|
||||
{1, 10, 10, 10, 0, 0},
|
||||
{1, 10, 0, es.AllEvents, 1, 10},
|
||||
{1, 10, -1, -es.AllEvents, 10, -10},
|
||||
{1, 10, 0, ev.AllEvents, 1, 10},
|
||||
{1, 10, -1, -ev.AllEvents, 10, -10},
|
||||
|
||||
{5, 10, 0, 1, 5, 1},
|
||||
}
|
||||
|
||||
@@ -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) }
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user