This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsetup_test.go
110 lines (106 loc) · 2.79 KB
/
setup_test.go
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
package search_test
import (
"testing"
"time"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/pedronasser/caddy-search"
. "github.com/smartystreets/goconvey/convey"
)
var (
defaultPath = search.ConvertToRegExp([]string{"^/"})
configCases = []struct {
config string
expectConf search.Config
expectMsg string
expectMatch func(search.Config, search.Config)
}{
{
`search`,
search.Config{
Endpoint: "/search",
IncludePaths: defaultPath,
},
"Should support `search` without any arguments",
func(expected, result search.Config) {
So(expected.Endpoint, ShouldEqual, result.Endpoint)
},
},
{
`search /path`,
search.Config{
IncludePaths: search.ConvertToRegExp([]string{"/path"}),
},
"Should support `search` with only one argument",
func(expected, result search.Config) {
So(expected.IncludePaths[0].String(), ShouldEqual, result.IncludePaths[0].String())
},
},
{
`search / /path`,
search.Config{
Endpoint: "/path",
},
"Should support `search` with two arguments",
func(expected, result search.Config) {
So(expected.Endpoint, ShouldEqual, result.Endpoint)
},
},
{
`search / /search {
endpoint /search2
}`,
search.Config{
Endpoint: "/search2",
},
"Should support `search` arguments and override configurations",
func(expected, result search.Config) {
So(expected.Endpoint, ShouldEqual, result.Endpoint)
},
},
{
`search {
+path /path
+path /otherPath
-path /forbidden
}`,
search.Config{
IncludePaths: search.ConvertToRegExp([]string{"/path", "/otherPath"}),
ExcludePaths: search.ConvertToRegExp([]string{"/forbidden"}),
},
"Should `search` support multiple include and excludes",
func(expected, result search.Config) {
So(expected.IncludePaths[0].String(), ShouldEqual, result.IncludePaths[0].String())
So(expected.IncludePaths[1].String(), ShouldEqual, result.IncludePaths[1].String())
So(expected.ExcludePaths[0].String(), ShouldEqual, result.ExcludePaths[0].String())
},
},
{
`search {
expire 1000
}`,
search.Config{
Expire: 1000 * time.Second,
},
"Should `search` support multiple include and excludes",
func(expected, result search.Config) {
So(expected.Expire, ShouldEqual, result.Expire)
},
},
}
)
func TestSearchSetup(t *testing.T) {
for _, kase := range configCases {
Convey("Given a Caddy controller with the search middleware", t, func() {
c := caddy.NewTestController(kase.config)
cnf := httpserver.GetConfig("")
result, err := search.ParseSearchConfig(c, cnf)
Convey("Should not receive an error when parsing", func() {
So(err, ShouldBeNil)
})
Convey(kase.expectMsg, func() {
kase.expectMatch(kase.expectConf, *result)
})
})
}
}