MCPcopy
hub / github.com/gorilla/mux / TestSubrouterHeader

Function TestSubrouterHeader

mux_test.go:2004–2031  ·  view source on GitHub ↗

https://plus.google.com/101022900381697718949/posts/eWy6DjFJ6uW

(t *testing.T)

Source from the content-addressed store, hash-verified

2002
2003// https://plus.google.com/101022900381697718949/posts/eWy6DjFJ6uW
2004func TestSubrouterHeader(t *testing.T) {
2005 expected := "func1 response"
2006 func1 := func(w http.ResponseWriter, r *http.Request) {
2007 fmt.Fprint(w, expected)
2008 }
2009 func2 := func(http.ResponseWriter, *http.Request) {}
2010
2011 r := NewRouter()
2012 s := r.Headers("SomeSpecialHeader", "").Subrouter()
2013 s.HandleFunc("/", func1).Name("func1")
2014 r.HandleFunc("/", func2).Name("func2")
2015
2016 req, _ := http.NewRequest("GET", "http://localhost/", nil)
2017 req.Header.Add("SomeSpecialHeader", "foo")
2018 match := new(RouteMatch)
2019 matched := r.Match(req, match)
2020 if !matched {
2021 t.Errorf("Should match request")
2022 }
2023 if match.Route.GetName() != "func1" {
2024 t.Errorf("Expecting func1 handler, got %s", match.Route.GetName())
2025 }
2026 resp := NewRecorder()
2027 match.Handler.ServeHTTP(resp, req)
2028 if resp.Body.String() != expected {
2029 t.Errorf("Expecting %q", expected)
2030 }
2031}
2032
2033func TestNoMatchMethodErrorHandler(t *testing.T) {
2034 func1 := func(w http.ResponseWriter, r *http.Request) {}

Callers

nothing calls this directly

Calls 9

HeadersMethod · 0.95
HandleFuncMethod · 0.95
MatchMethod · 0.95
NewRouterFunction · 0.85
NewRecorderFunction · 0.85
SubrouterMethod · 0.80
GetNameMethod · 0.80
NameMethod · 0.45
ServeHTTPMethod · 0.45

Tested by

no test coverage detected