23 lines
702 B
Go
23 lines
702 B
Go
package webutils_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
webutils "git.mstar.dev/mstar/goutils/http"
|
|
)
|
|
|
|
func TestContextValsMiddleware(t *testing.T) {
|
|
builder := webutils.ContextValsMiddleware(map[any]any{"foo": "bar"})
|
|
baseHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if val, _ := r.Context().Value("foo").(string); val != "bar" {
|
|
t.Fatalf(`Expected context value "foo" to be "bar", got %v`, val)
|
|
}
|
|
})
|
|
wrapped := builder(baseHandler)
|
|
recorder := httptest.NewRecorder()
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
wrapped.ServeHTTP(recorder, req)
|
|
// At this point baseHandler should have been called and checked for the context value
|
|
}
|