aboutsummaryrefslogtreecommitdiffhomepage
path: root/caddyhttp/rewrite/setup_test.go
blob: 3f32a15e97d4614a46275d829846b93d024c3550 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package rewrite

import (
	"fmt"
	"regexp"
	"testing"

	"github.com/mholt/caddy"
	"github.com/mholt/caddy/caddyhttp/httpserver"
)

func TestSetup(t *testing.T) {
	c := caddy.NewTestController("http", `rewrite /from /to`)
	err := setup(c)
	if err != nil {
		t.Errorf("Expected no errors, but got: %v", err)
	}
	mids := httpserver.GetConfig(c).Middleware()
	if len(mids) == 0 {
		t.Fatal("Expected middleware, had 0 instead")
	}

	handler := mids[0](httpserver.EmptyNext)
	myHandler, ok := handler.(Rewrite)
	if !ok {
		t.Fatalf("Expected handler to be type Rewrite, got: %#v", handler)
	}

	if !httpserver.SameNext(myHandler.Next, httpserver.EmptyNext) {
		t.Error("'Next' field of handler was not set properly")
	}

	if len(myHandler.Rules) != 1 {
		t.Errorf("Expected handler to have %d rule, has %d instead", 1, len(myHandler.Rules))
	}
}

func TestRewriteParse(t *testing.T) {
	simpleTests := []struct {
		input     string
		shouldErr bool
		expected  []Rule
	}{
		{`rewrite /from /to`, false, []Rule{
			SimpleRule{From: "/from", To: "/to"},
		}},
		{`rewrite /from /to
		  rewrite a b`, false, []Rule{
			SimpleRule{From: "/from", To: "/to"},
			SimpleRule{From: "a", To: "b"},
		}},
		{`rewrite a`, true, []Rule{}},
		{`rewrite`, true, []Rule{}},
		{`rewrite a b c`, false, []Rule{
			SimpleRule{From: "a", To: "b c"},
		}},
	}

	for i, test := range simpleTests {
		actual, err := rewriteParse(caddy.NewTestController("http", test.input))

		if err == nil && test.shouldErr {
			t.Errorf("Test %d didn't error, but it should have", i)
		} else if err != nil && !test.shouldErr {
			t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
		} else if err != nil && test.shouldErr {
			continue
		}

		if len(actual) != len(test.expected) {
			t.Fatalf("Test %d expected %d rules, but got %d",
				i, len(test.expected), len(actual))
		}

		for j, e := range test.expected {
			actualRule := actual[j].(SimpleRule)
			expectedRule := e.(SimpleRule)

			if actualRule.From != expectedRule.From {
				t.Errorf("Test %d, rule %d: Expected From=%s, got %s",
					i, j, expectedRule.From, actualRule.From)
			}

			if actualRule.To != expectedRule.To {
				t.Errorf("Test %d, rule %d: Expected To=%s, got %s",
					i, j, expectedRule.To, actualRule.To)
			}
		}
	}

	regexpTests := []struct {
		input     string
		shouldErr bool
		expected  []Rule
	}{
		{`rewrite {
			r	.*
			to	/to /index.php?
		 }`, false, []Rule{
			&ComplexRule{Base: "/", To: "/to /index.php?", Regexp: regexp.MustCompile(".*")},
		}},
		{`rewrite {
			regexp	.*
			to		/to
			ext		/ html txt
		 }`, false, []Rule{
			&ComplexRule{Base: "/", To: "/to", Exts: []string{"/", "html", "txt"}, Regexp: regexp.MustCompile(".*")},
		}},
		{`rewrite /path {
			r	rr
			to	/dest
		 }
		 rewrite / {
		 	regexp	[a-z]+
		 	to 		/to /to2
		 }
		 `, false, []Rule{
			&ComplexRule{Base: "/path", To: "/dest", Regexp: regexp.MustCompile("rr")},
			&ComplexRule{Base: "/", To: "/to /to2", Regexp: regexp.MustCompile("[a-z]+")},
		}},
		{`rewrite {
			r	.*
		 }`, true, []Rule{
			&ComplexRule{},
		}},
		{`rewrite {

		 }`, true, []Rule{
			&ComplexRule{},
		}},
		{`rewrite /`, true, []Rule{
			&ComplexRule{},
		}},
		{`rewrite {
			to	/to
			if {path} is a
		 }`, false, []Rule{
			&ComplexRule{Base: "/", To: "/to", Ifs: []If{{A: "{path}", Operator: "is", B: "a"}}},
		}},
		{`rewrite {
			status 500
		 }`, true, []Rule{
			&ComplexRule{},
		}},
		{`rewrite {
			status 400
		 }`, false, []Rule{
			&ComplexRule{Base: "/", Status: 400},
		}},
		{`rewrite {
			to /to
			status 400
		 }`, false, []Rule{
			&ComplexRule{Base: "/", To: "/to", Status: 400},
		}},
		{`rewrite {
			status 399
		 }`, true, []Rule{
			&ComplexRule{},
		}},
		{`rewrite {
			status 200
		 }`, false, []Rule{
			&ComplexRule{Base: "/", Status: 200},
		}},
		{`rewrite {
			to /to
			status 200
		 }`, false, []Rule{
			&ComplexRule{Base: "/", To: "/to", Status: 200},
		}},
		{`rewrite {
			status 199
		 }`, true, []Rule{
			&ComplexRule{},
		}},
		{`rewrite {
			status 0
		 }`, true, []Rule{
			&ComplexRule{},
		}},
		{`rewrite {
			to /to
			status 0
		 }`, true, []Rule{
			&ComplexRule{},
		}},
	}

	for i, test := range regexpTests {
		actual, err := rewriteParse(caddy.NewTestController("http", test.input))

		if err == nil && test.shouldErr {
			t.Errorf("Test %d didn't error, but it should have", i)
		} else if err != nil && !test.shouldErr {
			t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
		} else if err != nil && test.shouldErr {
			continue
		}

		if len(actual) != len(test.expected) {
			t.Fatalf("Test %d expected %d rules, but got %d",
				i, len(test.expected), len(actual))
		}

		for j, e := range test.expected {
			actualRule := actual[j].(*ComplexRule)
			expectedRule := e.(*ComplexRule)

			if actualRule.Base != expectedRule.Base {
				t.Errorf("Test %d, rule %d: Expected Base=%s, got %s",
					i, j, expectedRule.Base, actualRule.Base)
			}

			if actualRule.To != expectedRule.To {
				t.Errorf("Test %d, rule %d: Expected To=%s, got %s",
					i, j, expectedRule.To, actualRule.To)
			}

			if fmt.Sprint(actualRule.Exts) != fmt.Sprint(expectedRule.Exts) {
				t.Errorf("Test %d, rule %d: Expected Ext=%v, got %v",
					i, j, expectedRule.To, actualRule.To)
			}

			if actualRule.Regexp != nil {
				if actualRule.String() != expectedRule.String() {
					t.Errorf("Test %d, rule %d: Expected Pattern=%s, got %s",
						i, j, expectedRule.String(), actualRule.String())
				}
			}

			if fmt.Sprint(actualRule.Ifs) != fmt.Sprint(expectedRule.Ifs) {
				t.Errorf("Test %d, rule %d: Expected Pattern=%s, got %s",
					i, j, fmt.Sprint(expectedRule.Ifs), fmt.Sprint(actualRule.Ifs))
			}

		}
	}

}