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.
 
 
 

41 lines
868 B

  1. // +build !go1.7
  2. package mux
  3. import (
  4. "net/http"
  5. "testing"
  6. "github.com/gorilla/context"
  7. )
  8. // Tests that the context is cleared or not cleared properly depending on
  9. // the configuration of the router
  10. func TestKeepContext(t *testing.T) {
  11. func1 := func(w http.ResponseWriter, r *http.Request) {}
  12. r := NewRouter()
  13. r.HandleFunc("/", func1).Name("func1")
  14. req, _ := http.NewRequest("GET", "http://localhost/", nil)
  15. context.Set(req, "t", 1)
  16. res := new(http.ResponseWriter)
  17. r.ServeHTTP(*res, req)
  18. if _, ok := context.GetOk(req, "t"); ok {
  19. t.Error("Context should have been cleared at end of request")
  20. }
  21. r.KeepContext = true
  22. req, _ = http.NewRequest("GET", "http://localhost/", nil)
  23. context.Set(req, "t", 1)
  24. r.ServeHTTP(*res, req)
  25. if _, ok := context.GetOk(req, "t"); !ok {
  26. t.Error("Context should NOT have been cleared at end of request")
  27. }
  28. }