ev/pkg/mux/httpmux_test.go

42 lines
716 B
Go
Raw Normal View History

package mux_test
2022-09-07 16:00:10 -06:00
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/matryer/is"
"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
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)
}