Some checks failed
Test / test (1.24.x, ubuntu-latest) (push) Successful in 2m56s
Test / test (1.25.x, ubuntu-latest) (push) Successful in 2m15s
Test / test (1.26.x, ubuntu-latest) (push) Successful in 2m23s
Test / test (1.27.x, ubuntu-latest) (push) Failing after 3m56s
Test / test (1.24.x, windows-latest) (push) Has been cancelled
Test / test (1.25.x, windows-latest) (push) Has been cancelled
Test / test (1.26.x, windows-latest) (push) Has been cancelled
Test / test (1.27.x, windows-latest) (push) Has been cancelled
34 lines
678 B
Go
34 lines
678 B
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func TestWithValue(t *testing.T) {
|
|
ctxKey := "customKey"
|
|
ctxValue := "customValue"
|
|
request := func() *http.Request {
|
|
req, _ := http.NewRequest("GET", "/", nil)
|
|
return req
|
|
}
|
|
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
val := r.Context().Value(ctxKey).(string)
|
|
w.Write([]byte(val))
|
|
})
|
|
|
|
w := httptest.NewRecorder()
|
|
r := chi.NewRouter()
|
|
r.Use(WithValue(ctxKey, ctxValue))
|
|
r.Get("/", testHandler)
|
|
|
|
r.ServeHTTP(w, request())
|
|
|
|
if w.Body.String() != ctxValue {
|
|
t.Errorf("value: context did not contain expected value")
|
|
}
|
|
}
|