-
Notifications
You must be signed in to change notification settings - Fork 35
/
parse_test.go
88 lines (69 loc) · 2.85 KB
/
parse_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
package main
import "testing"
func assertEqual(t *testing.T, value interface{}, expectation interface{}) {
if value != expectation {
t.Errorf("Expected %#v to be %#v", value, expectation)
}
}
func TestParse(t *testing.T) {
var config *Config
config = Parse("This is not a valid statement")
if config != nil {
t.Errorf("Expected %#v to be %#v", config, nil)
}
config = Parse("Redirect to http://github.com/holic")
assertEqual(t, config.From, "")
assertEqual(t, config.To, "http://github.com/holic")
assertEqual(t, config.RedirectState, "")
config = Parse("Redirect to ftp://github.com")
assertEqual(t, config.From, "")
assertEqual(t, config.To, "ftp://github.com")
assertEqual(t, config.RedirectState, "")
config = Parse("Redirect to mailto:[email protected]")
assertEqual(t, config.From, "")
assertEqual(t, config.To, "mailto:[email protected]")
assertEqual(t, config.RedirectState, "")
config = Parse("Redirect from / to http://github.com/holic")
assertEqual(t, config.From, "/")
assertEqual(t, config.To, "http://github.com/holic")
assertEqual(t, config.RedirectState, "")
config = Parse("Redirects to /new from /old")
assertEqual(t, config.From, "/old")
assertEqual(t, config.To, "/new")
assertEqual(t, config.RedirectState, "")
config = Parse("Redirects from /old to /new permanently")
assertEqual(t, config.From, "/old")
assertEqual(t, config.To, "/new")
assertEqual(t, config.RedirectState, "permanently")
config = Parse("Redirects temporarily to /new")
assertEqual(t, config.From, "")
assertEqual(t, config.To, "/new")
assertEqual(t, config.RedirectState, "temporarily")
// Test status codes
config = Parse("Redirect to http://github.com/holic with 301")
assertEqual(t, config.From, "")
assertEqual(t, config.To, "http://github.com/holic")
assertEqual(t, config.RedirectState, "301")
config = Parse("Redirect with 302 from / to http://github.com/holic")
assertEqual(t, config.From, "/")
assertEqual(t, config.To, "http://github.com/holic")
assertEqual(t, config.RedirectState, "302")
config = Parse("Redirects to /new from /old with 307")
assertEqual(t, config.From, "/old")
assertEqual(t, config.To, "/new")
assertEqual(t, config.RedirectState, "307")
config = Parse("Redirects with 308 from /old to /new")
assertEqual(t, config.From, "/old")
assertEqual(t, config.To, "/new")
assertEqual(t, config.RedirectState, "308")
// Test that we get the first parsed value when multiple values
// of the same type are specified (e.g. `permanently` and `with 308`)
config = Parse("Redirects permanently from /old to /new with 302")
assertEqual(t, config.From, "/old")
assertEqual(t, config.To, "/new")
assertEqual(t, config.RedirectState, "permanently")
config = Parse("Redirects with 307 from /old to /new permanently")
assertEqual(t, config.From, "/old")
assertEqual(t, config.To, "/new")
assertEqual(t, config.RedirectState, "307")
}