-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilter_test.go
More file actions
37 lines (28 loc) · 828 Bytes
/
Copy pathfilter_test.go
File metadata and controls
37 lines (28 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package gomw
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFilterMiddleware(t *testing.T) {
block := func(r *http.Request) bool { return false }
allow := func(r *http.Request) bool { return true }
r, _ := http.NewRequest("GET", "/some/url", nil)
t.Run("should not call next on false predicate", func(t *testing.T) {
next := newMockNextHandler()
h := Filter(block)(next)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
assert.False(t, next.called)
assert.Equal(t, http.StatusBadRequest, w.Code)
})
t.Run("should call next on true predicate", func(t *testing.T) {
next := newMockNextHandler()
h := Filter(allow)(next)
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
assert.True(t, next.called)
assert.Equal(t, http.StatusOK, w.Code)
})
}