You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

31 lines
673 B

  1. package mux
  2. import (
  3. "context"
  4. "net/http"
  5. "testing"
  6. "time"
  7. )
  8. func TestNativeContextMiddleware(t *testing.T) {
  9. withTimeout := func(h http.Handler) http.Handler {
  10. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  11. ctx, cancel := context.WithTimeout(r.Context(), time.Minute)
  12. defer cancel()
  13. h.ServeHTTP(w, r.WithContext(ctx))
  14. })
  15. }
  16. r := NewRouter()
  17. r.Handle("/path/{foo}", withTimeout(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  18. vars := Vars(r)
  19. if vars["foo"] != "bar" {
  20. t.Fatal("Expected foo var to be set")
  21. }
  22. })))
  23. rec := NewRecorder()
  24. req := newRequest("GET", "/path/bar")
  25. r.ServeHTTP(rec, req)
  26. }