-
Notifications
You must be signed in to change notification settings - Fork 35
/
translate_test.go
80 lines (59 loc) · 3.03 KB
/
translate_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
package main
import "testing"
func TestTranslate(t *testing.T) {
var redirect *Redirect
redirect = Translate("/", nil)
if redirect != nil {
t.Errorf("Expected %#v to be %#v", redirect, nil)
}
redirect = Translate("/", &Config{To: "https://example.com/"})
assertEqual(t, redirect.Location, "https://example.com/")
assertEqual(t, redirect.Status, 302)
redirect = Translate("/", &Config{To: "https://example.com/", RedirectState: "301"})
assertEqual(t, redirect.Location, "https://example.com/")
assertEqual(t, redirect.Status, 301)
redirect = Translate("/", &Config{From: "/twitter", To: "https://example.com/", RedirectState: "permanently"})
if redirect != nil {
t.Errorf("Expected %#v to be %#v", redirect, nil)
}
redirect = Translate("/", &Config{From: "/", To: "https://example.com/", RedirectState: "permanently"})
assertEqual(t, redirect.Location, "https://example.com/")
assertEqual(t, redirect.Status, 301)
redirect = Translate("/", &Config{From: "/", To: "https://example.com/", RedirectState: "temporarily"})
assertEqual(t, redirect.Location, "https://example.com/")
assertEqual(t, redirect.Status, 302)
// Test status codes
redirect = Translate("/", &Config{From: "/", To: "https://example.com/", RedirectState: "301"})
assertEqual(t, redirect.Status, 301)
redirect = Translate("/", &Config{From: "/", To: "https://example.com/", RedirectState: "302"})
assertEqual(t, redirect.Status, 302)
redirect = Translate("/", &Config{From: "/", To: "https://example.com/", RedirectState: "307"})
assertEqual(t, redirect.Status, 307)
redirect = Translate("/", &Config{From: "/", To: "https://example.com/", RedirectState: "308"})
assertEqual(t, redirect.Status, 308)
}
func TestTranslateWildcard(t *testing.T) {
var redirect *Redirect
redirect = Translate("/about-us", &Config{From: "/*", To: "http://example.com/"})
assertEqual(t, redirect.Location, "http://example.com/")
assertEqual(t, redirect.Status, 302)
redirect = Translate("/about-us", &Config{From: "/*", To: "http://example.com/*"})
assertEqual(t, redirect.Location, "http://example.com/about-us")
assertEqual(t, redirect.Status, 302)
redirect = Translate("/about-us", &Config{From: "/*", To: "http://example.com/*"})
assertEqual(t, redirect.Location, "http://example.com/about-us")
assertEqual(t, redirect.Status, 302)
redirect = Translate("/blog/1", &Config{From: "/*/1", To: "http://example.com/*", RedirectState: "temporarily"})
assertEqual(t, redirect.Location, "http://example.com/blog")
assertEqual(t, redirect.Status, 302)
redirect = Translate("/wildcard", &Config{From: "/*", To: "http://example.com/**"})
assertEqual(t, redirect.Location, "http://example.com/wildcard*")
assertEqual(t, redirect.Status, 302)
redirect = Translate("/wildcard", &Config{From: "/**", To: "http://example.com/*"})
if redirect != nil {
t.Errorf("Expected %#v to be %#v", redirect, nil)
}
redirect = Translate("/wildcard*", &Config{From: "/**", To: "http://example.com/*"})
assertEqual(t, redirect.Location, "http://example.com/wildcard")
assertEqual(t, redirect.Status, 302)
}