-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_skipper_test.go
More file actions
112 lines (105 loc) · 2.94 KB
/
http_skipper_test.go
File metadata and controls
112 lines (105 loc) · 2.94 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package pages
import (
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestPageSkipper(t *testing.T) {
t.Run("panics with nil decorator strategy", func(t *testing.T) {
assert.Panics(t, func() {
PageSkipper(nil)
})
})
tests := []struct {
name string
method string
path string
pattern string
isPatternDecorable bool
isURIDecorable bool
want bool
}{
{
name: "non-GET method always skips",
method: http.MethodPost,
path: "/page",
pattern: "/page",
isPatternDecorable: true,
isURIDecorable: true,
want: true,
},
{
name: "PUT method skips",
method: http.MethodPut,
path: "/page",
pattern: "/page",
isPatternDecorable: true,
isURIDecorable: true,
want: true,
},
{
name: "DELETE method skips",
method: http.MethodDelete,
path: "/page",
pattern: "/page",
isPatternDecorable: true,
isURIDecorable: true,
want: true,
},
{
name: "GET method with decorable pattern",
method: http.MethodGet,
path: "/page",
pattern: "/page",
isPatternDecorable: true,
isURIDecorable: true,
want: false,
},
{
name: "GET method with non-decorable pattern",
method: http.MethodGet,
path: "/page",
pattern: "/page",
isPatternDecorable: false,
isURIDecorable: true,
want: true,
},
{
name: "GET with PageCMSPattern calls IsURIDecorable",
method: http.MethodGet,
path: "/some/page",
pattern: PageCMSPattern,
isPatternDecorable: false,
isURIDecorable: false,
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockStrategy := NewMockPageDecoratorStrategy(tt.isPatternDecorable)
skipper := PageSkipper(mockStrategy)
req := createRequestWithMethodPattern(tt.method, tt.path, tt.pattern)
got := skipper(req)
assert.Equal(t, tt.want, got)
if tt.method == http.MethodGet {
if tt.pattern == PageCMSPattern {
mockStrategy.AssertCalled(t, "IsURIDecorable", mock.Anything, tt.path)
} else {
mockStrategy.AssertCalled(t, "IsPatternDecorable", mock.Anything, tt.pattern)
}
} else {
mockStrategy.AssertNotCalled(t, "IsPatternDecorable", mock.Anything, mock.Anything)
mockStrategy.AssertNotCalled(t, "IsURIDecorable", mock.Anything, mock.Anything)
}
})
}
}
func createRequestWithMethodPattern(method, path, pattern string) *http.Request {
return &http.Request{
Method: method,
URL: &url.URL{Path: path},
Pattern: pattern,
}
}