Skip to content

Commit

Permalink
Merge pull request #44 from vinxi/test/increase_coverage
Browse files Browse the repository at this point in the history
[WIP] Increase test coverage
  • Loading branch information
h2non committed Jun 8, 2016
2 parents ab535f2 + e00ef08 commit de6a369
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 11 deletions.
37 changes: 37 additions & 0 deletions layer/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ func TestAdaptMiddlewareFunc(t *testing.T) {
st.Expect(t, w.Code, 502)
}

func TestAdaptMiddlewareHandlerFunc(t *testing.T) {
middlewareFunc := func(h http.Handler) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("foo", "bar")
h.ServeHTTP(w, r)
}
}

w := utils.NewWriterStub()
req := &http.Request{}

adaptedFunc := AdaptFunc(middlewareFunc)
adaptedFunc(FinalHandler).ServeHTTP(w, req)

st.Expect(t, w.Header().Get("foo"), "bar")
st.Expect(t, w.Code, 502)
}

func TestAdaptNegroniInterface(t *testing.T) {
middlewareFunc := func(w http.ResponseWriter, r *http.Request, h http.Handler) {
w.Header().Set("foo", "bar")
Expand Down Expand Up @@ -90,3 +108,22 @@ func TestVinciHandler(t *testing.T) {
st.Expect(t, w.Header().Get("foo"), "bar")
st.Expect(t, w.Code, 502)
}

type partialHandler struct{}

func (ph *partialHandler) HandleHTTP(w http.ResponseWriter, r *http.Request, h http.Handler) {
w.Header().Set("foo", "bar")
h.ServeHTTP(w, r)
}

func TestPartialHandler(t *testing.T) {

w := utils.NewWriterStub()
req := &http.Request{}

adaptedFunc := AdaptFunc(&partialHandler{})
adaptedFunc(FinalHandler).ServeHTTP(w, req)

st.Expect(t, w.Header().Get("foo"), "bar")
st.Expect(t, w.Code, 502)
}
55 changes: 47 additions & 8 deletions mux/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"testing"
)

func TestMuxComposeIfMatches(t *testing.T) {
type composer func(...*Mux) *Mux

func testMuxIfMatches(t *testing.T, c composer) {
mx := New()
mx.Use(If(Method("GET"), Host("foo.com")).Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
mx.Use(c(Method("GET"), Host("foo.com")).Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
w.Header().Set("foo", "bar")
h.ServeHTTP(w, r)
}))
Expand All @@ -23,9 +25,17 @@ func TestMuxComposeIfMatches(t *testing.T) {
st.Expect(t, wrt.Header().Get("foo"), "bar")
}

func TestMuxComposeIfUnmatch(t *testing.T) {
func TestMuxComposeIfMatches(t *testing.T) {
testMuxIfMatches(t, If)
}

func TestMuxComposeEveryMatches(t *testing.T) {
testMuxIfMatches(t, Every)
}

func testMuxComposeIfUnmatch(t *testing.T, c composer) {
mx := New()
mx.Use(If(Method("GET"), Host("bar.com")).Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
mx.Use(c(Method("GET"), Host("bar.com")).Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
w.Header().Set("foo", "bar")
h.ServeHTTP(w, r)
}))
Expand All @@ -38,9 +48,17 @@ func TestMuxComposeIfUnmatch(t *testing.T) {
st.Expect(t, wrt.Header().Get("foo"), "")
}

func TestMuxComposeOrMatch(t *testing.T) {
func TestMuxComposeIfUnmatch(t *testing.T) {
testMuxComposeIfUnmatch(t, If)
}

func TestMuxComposeEveryUnmatch(t *testing.T) {
testMuxComposeIfUnmatch(t, Every)
}

func testMuxComposeOrMatch(t *testing.T, c composer) {
mx := New()
mx.Use(Or(Method("GET"), Host("bar.com")).Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
mx.Use(c(Method("GET"), Host("bar.com")).Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
w.Header().Set("foo", "bar")
h.ServeHTTP(w, r)
}))
Expand All @@ -53,9 +71,17 @@ func TestMuxComposeOrMatch(t *testing.T) {
st.Expect(t, wrt.Header().Get("foo"), "bar")
}

func TestMuxComposeOrUnMatch(t *testing.T) {
func TestMuxComposeOrMatch(t *testing.T) {
testMuxComposeOrMatch(t, Or)
}

func TestMuxComposeSomeMatch(t *testing.T) {
testMuxComposeOrMatch(t, Some)
}

func testMuxComposeOrUnmatch(t *testing.T, c composer) {
mx := New()
mx.Use(Or(Method("GET"), Host("bar.com")).Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
mx.Use(c(Method("GET"), Host("bar.com")).Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
w.Header().Set("foo", "bar")
h.ServeHTTP(w, r)
}))
Expand All @@ -66,6 +92,19 @@ func TestMuxComposeOrUnMatch(t *testing.T) {

mx.Layer.Run("request", wrt, req, nil)
st.Expect(t, wrt.Header().Get("foo"), "bar")

req.Method = "POST"
wrt = utils.NewWriterStub()
mx.Layer.Run("request", wrt, req, nil)
st.Expect(t, wrt.Header().Get("foo"), "")
}

func TestMuxComposeOrUnmatch(t *testing.T) {
testMuxComposeOrUnmatch(t, Or)
}

func TestMuxComposeSomeUnmatch(t *testing.T) {
testMuxComposeOrUnmatch(t, Some)
}

func newRequest() *http.Request {
Expand Down
1 change: 1 addition & 0 deletions mux/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestMatchPath(t *testing.T) {
path string
matches bool
}{
{"/baz", "/baz", true},
{"baz", "/bar/foo/baz", true},
{"bar", "/bar/foo/baz", true},
{"^/bar", "/bar/foo/baz", true},
Expand Down
127 changes: 124 additions & 3 deletions mux/mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,137 @@ import (
"testing"
)

func TestMuxSimple(t *testing.T) {
mx := New()
mx.Use(func(w http.ResponseWriter, r *http.Request, h http.Handler) {
var (
fooMw = func(w http.ResponseWriter, r *http.Request, h http.Handler) {
w.Header().Set("foo", "bar")
h.ServeHTTP(w, r)
}
barMw = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("bar", "foo")
})
)

func TestMux_Match_matched(t *testing.T) {
mx := New()
mx.AddMatcher(MatchMethod("GET"))

req := newRequest()
req.Method = "GET"

st.Expect(t, mx.Match(req), true)
}

func TestMux_Match_unmatched(t *testing.T) {
mx := New()
mx.AddMatcher(MatchMethod("POST"))

req := newRequest()
req.Method = "GET"

st.Expect(t, mx.Match(req), false)
}

func testMuxAddMatcherAndIf(t *testing.T, f func(m *Mux) func(matchers ...Matcher) *Mux) {
mx := New()
f(mx)(MatchMethod("POST"), MatchHost("www.example.com"))

req := newRequest()
req.Method = "POST"
st.Expect(t, mx.Match(req), false)

req.Host = "www.example.com"
st.Expect(t, mx.Match(req), true)
}

func TestMux_Addmatcher(t *testing.T) {
testMuxAddMatcherAndIf(t, func(m *Mux) func(matchers ...Matcher) *Mux {
return m.AddMatcher
})
}

func TestMux_If(t *testing.T) {
testMuxAddMatcherAndIf(t, func(m *Mux) func(matchers ...Matcher) *Mux {
return m.If
})
}

func TestMux_Some(t *testing.T) {
mx := New()
mx.Some(MatchMethod("POST"), MatchHost("www.example.com"))

req := newRequest()

st.Expect(t, mx.Match(req), false)

req.Method = "POST"
st.Expect(t, mx.Match(req), true)

req.Method = "GET"
req.Host = "www.example.com"
st.Expect(t, mx.Match(req), true)

req.Method = "GET"
st.Expect(t, mx.Match(req), true)
}

func TestMux_Use(t *testing.T) {
mx := New()
mx.Use(fooMw)

wrt := httptest.NewRecorder()
req := newRequest()

mx.Layer.Run("request", wrt, req, nil)
st.Expect(t, wrt.Header().Get("foo"), "bar")
}

func TestMux_UsePhase(t *testing.T) {
mx := New()
mx.UsePhase("request", fooMw)

wrt := httptest.NewRecorder()
req := newRequest()

mx.Layer.Run("request", wrt, req, nil)
st.Expect(t, wrt.Header().Get("foo"), "bar")
}

func TestMux_UseFinalHandler(t *testing.T) {
mx := New()
mx.Use(fooMw)
mx.UseFinalHandler(barMw)

wrt := httptest.NewRecorder()
req := newRequest()

mx.Layer.Run("request", wrt, req, nil)
st.Expect(t, wrt.Header().Get("foo"), "bar")
st.Expect(t, wrt.Header().Get("bar"), "foo")
}

func TestMux_HandleHTTP_matched(t *testing.T) {
mx := New()
mx.If(MatchMethod("GET"))
mx.Use(fooMw)

wrt := httptest.NewRecorder()
req := newRequest()
req.Method = "GET"

mx.HandleHTTP(wrt, req, barMw)
st.Expect(t, wrt.Header().Get("foo"), "bar")
st.Expect(t, wrt.Header().Get("bar"), "foo")
}

func TestMux_HandleHTTP_unmatched(t *testing.T) {
mx := New()
mx.If(MatchMethod("GET"))
mx.Use(fooMw)

wrt := httptest.NewRecorder()
req := newRequest()
req.Method = "POST"

mx.HandleHTTP(wrt, req, barMw)
st.Expect(t, wrt.Header().Get("bar"), "foo")
}

0 comments on commit de6a369

Please sign in to comment.