-
Notifications
You must be signed in to change notification settings - Fork 1
/
new_test.go
141 lines (115 loc) · 3.46 KB
/
new_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package nchi_test
import (
"testing"
"github.com/muir/nchi"
)
func TestWithRedirectTrailingSlash(t *testing.T) {
mux := nchi.NewRouter(nchi.WithRedirectTrailingSlash(true))
mux.Use("")
mux.Use(makeDown("a"))
mux.Get("/ts1", makeDown("b"), bottom)
doTest(t, mux, []testCase{
{path: "/ts1", want: "ab"},
{path: "/ts1/", want: "<a href=\"/ts1\">Moved Permanently</a>.\n\n"},
})
mux = nchi.NewRouter(nchi.WithRedirectTrailingSlash(false))
mux.Use("")
mux.Use(makeDown("a"))
mux.Get("/ts2", makeDown("b"), bottom)
doTest(t, mux, []testCase{
{path: "/ts2", want: "ab"},
{path: "/ts2/", want: "404 page not found\n"},
})
mux = nchi.NewRouter()
mux.Use("")
mux.Use(makeDown("a"))
mux.Get("/ts3", makeDown("b"), bottom)
doTest(t, mux, []testCase{
{path: "/ts3", want: "ab"},
{path: "/ts3/", want: "<a href=\"/ts3\">Moved Permanently</a>.\n\n"},
})
}
func TestWithRedirectFixedPath(t *testing.T) {
mux := nchi.NewRouter(nchi.WithRedirectFixedPath(true))
mux.Use("")
mux.Use(makeDown("a"))
mux.Get("/ts1", makeDown("b"), bottom)
doTest(t, mux, []testCase{
{path: "/ts1", want: "ab"},
{path: "/xx/..//ts1/", want: "<a href=\"/ts1\">Moved Permanently</a>.\n\n"},
})
mux = nchi.NewRouter(nchi.WithRedirectFixedPath(false))
mux.Use("")
mux.Use(makeDown("a"))
mux.Get("/ts2", makeDown("b"), bottom)
doTest(t, mux, []testCase{
{path: "/ts2", want: "ab"},
{path: "/xx/..//ts2/", want: "404 page not found\n"},
})
mux = nchi.NewRouter()
mux.Use("")
mux.Use(makeDown("a"))
mux.Get("/ts3", makeDown("b"), bottom)
doTest(t, mux, []testCase{
{path: "/ts3", want: "ab"},
{path: "/xx/..//ts3", want: "<a href=\"/ts3\">Moved Permanently</a>.\n\n"},
})
}
func TestWithHandleMethodNotAllowed(t *testing.T) {
mux := nchi.NewRouter(nchi.WithHandleMethodNotAllowed(true))
mux.Use("")
mux.Use(makeDown("a"))
mux.Post("/mna1", makeDown("b"), bottom)
doTest(t, mux, []testCase{
{path: "/mna1", want: "Method Not Allowed\n"},
})
mux = nchi.NewRouter(nchi.WithHandleMethodNotAllowed(false))
mux.Use("")
mux.Use(makeDown("a"))
mux.Post("/mna2", makeDown("b"), bottom)
doTest(t, mux, []testCase{
{path: "/mna2", want: "404 page not found\n"},
})
mux = nchi.NewRouter()
mux.Use("")
mux.Use(makeDown("a"))
mux.Post("/mna3", makeDown("b"), bottom)
doTest(t, mux, []testCase{
{path: "/mna3", want: "Method Not Allowed\n"},
})
}
func TestWithHandleOPTIONS(t *testing.T) {
mux := nchi.NewRouter(nchi.WithHandleOPTIONS(true))
mux.Use("")
mux.Use(makeDown("a"))
mux.Get("/ho1", makeDown("b"), bottom)
mux.Options("/ho1", makeDown("c"), bottom)
mux.Get("/ho2", makeDown("c"), bottom)
doTestMethod(t, mux, "OPTIONS", []testCase{
{path: "/ho1", want: "ac"},
{path: "/ho2", want: ""},
{path: "/ho3", want: "404 page not found\n"},
})
mux = nchi.NewRouter(nchi.WithHandleOPTIONS(false))
mux.Use("")
mux.Use(makeDown("a"))
mux.Get("/ho3", makeDown("b"), bottom)
mux.Options("/ho3", makeDown("c"), bottom)
mux.Get("/ho4", makeDown("c"), bottom)
doTestMethod(t, mux, "OPTIONS", []testCase{
{path: "/ho3", want: "ac"},
{path: "/ho4", want: "Method Not Allowed\n"},
{path: "/ho5", want: "404 page not found\n"},
})
mux = nchi.NewRouter()
mux.Use("")
mux.Use(makeDown("a"))
mux.Get("/ho6", makeDown("b"), bottom)
mux.Options("/ho6", makeDown("c"), bottom)
mux.Get("/ho7", makeDown("c"), bottom)
doTestMethod(t, mux, "OPTIONS", []testCase{
{path: "/ho6", want: "ac"},
{path: "/ho7", want: ""},
{path: "/ho8", want: "404 page not found\n"},
})
}