|
|
|
|
@ -8,6 +8,8 @@ import (
@@ -8,6 +8,8 @@ import (
|
|
|
|
|
"fmt" |
|
|
|
|
"net/http" |
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
"github.com/gorilla/context" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type routeTest struct { |
|
|
|
|
@ -656,6 +658,36 @@ func testRoute(t *testing.T, test routeTest) {
@@ -656,6 +658,36 @@ func testRoute(t *testing.T, test routeTest) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Tests that the context is cleared or not cleared properly depending on
|
|
|
|
|
// the configuration of the router
|
|
|
|
|
func TestKeepContext(t *testing.T) { |
|
|
|
|
func1 := func(w http.ResponseWriter, r *http.Request) {} |
|
|
|
|
|
|
|
|
|
r := NewRouter() |
|
|
|
|
r.HandleFunc("/", func1).Name("func1") |
|
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("GET", "http://localhost/", nil) |
|
|
|
|
context.Set(req, "t", 1) |
|
|
|
|
|
|
|
|
|
res := new(http.ResponseWriter) |
|
|
|
|
r.ServeHTTP(*res, req) |
|
|
|
|
|
|
|
|
|
if _, ok := context.GetOk(req, "t"); ok { |
|
|
|
|
t.Error("Context should have been cleared at end of request") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
r.KeepContext = true |
|
|
|
|
|
|
|
|
|
req, _ = http.NewRequest("GET", "http://localhost/", nil) |
|
|
|
|
context.Set(req, "t", 1) |
|
|
|
|
|
|
|
|
|
r.ServeHTTP(*res, req) |
|
|
|
|
if _, ok := context.GetOk(req, "t"); !ok { |
|
|
|
|
t.Error("Context should NOT have been cleared at end of request") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// https://plus.google.com/101022900381697718949/posts/eWy6DjFJ6uW
|
|
|
|
|
func TestSubrouterHeader(t *testing.T) { |
|
|
|
|
expected := "func1 response" |
|
|
|
|
|