2023-01-09 11:30:02 -07:00
|
|
|
package mux_test
|
2022-09-07 16:00:10 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/matryer/is"
|
2023-01-09 11:30:02 -07:00
|
|
|
"github.com/sour-is/ev/pkg/mux"
|
2022-09-07 16:00:10 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type mockHTTP struct {
|
|
|
|
onServeHTTP func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockHTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
m.onServeHTTP()
|
|
|
|
}
|
|
|
|
func (h *mockHTTP) RegisterHTTP(mux *http.ServeMux) {
|
|
|
|
mux.Handle("/", h)
|
|
|
|
}
|
|
|
|
func (h *mockHTTP) RegisterAPIv1(mux *http.ServeMux) {
|
|
|
|
mux.Handle("/ping", h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHttpMux(t *testing.T) {
|
|
|
|
is := is.New(t)
|
|
|
|
|
|
|
|
called := false
|
|
|
|
|
2023-01-09 11:30:02 -07:00
|
|
|
mux := mux.New()
|
|
|
|
mux.Add(&mockHTTP{func() { called = true }})
|
2022-09-07 16:00:10 -06:00
|
|
|
|
|
|
|
is.True(mux != nil)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := httptest.NewRequest(http.MethodGet, "/api/v1/ping", nil)
|
|
|
|
mux.ServeHTTP(w, r)
|
|
|
|
|
|
|
|
is.True(called)
|
|
|
|
}
|