Eric Bower
·
13 Oct 24
redirect_test.go
1package pgs
2
3import (
4 "testing"
5
6 "github.com/google/go-cmp/cmp"
7)
8
9type RedirectFixture struct {
10 name string
11 input string
12 expect []*RedirectRule
13 shouldError bool
14}
15
16func TestParseRedirectText(t *testing.T) {
17 empty := map[string]string{}
18 spa := RedirectFixture{
19 name: "spa",
20 input: "/* /index.html 200",
21 expect: []*RedirectRule{
22 {
23 From: "/*",
24 To: "/index.html",
25 Status: 200,
26 Query: empty,
27 Conditions: empty,
28 },
29 },
30 }
31
32 withStatus := RedirectFixture{
33 name: "with-status",
34 input: "/wow /index.html 301",
35 expect: []*RedirectRule{
36 {
37 From: "/wow",
38 To: "/index.html",
39 Status: 301,
40 Query: empty,
41 Conditions: empty,
42 },
43 },
44 }
45
46 noStatus := RedirectFixture{
47 name: "no-status",
48 input: "/wow /index.html",
49 expect: []*RedirectRule{
50 {
51 From: "/wow",
52 To: "/index.html",
53 Status: 200,
54 Query: empty,
55 Conditions: empty,
56 },
57 },
58 }
59
60 absoluteUriNoProto := RedirectFixture{
61 name: "absolute-uri-no-proto",
62 input: "/* www.example.com 301",
63 expect: []*RedirectRule{},
64 shouldError: true,
65 }
66
67 absoluteUriWithProto := RedirectFixture{
68 name: "absolute-uri-no-proto",
69 input: "/* https://www.example.com 301",
70 expect: []*RedirectRule{
71 {
72 From: "/*",
73 To: "https://www.example.com",
74 Status: 301,
75 Query: empty,
76 Conditions: empty,
77 },
78 },
79 }
80
81 fixtures := []RedirectFixture{
82 spa,
83 withStatus,
84 noStatus,
85 absoluteUriNoProto,
86 absoluteUriWithProto,
87 }
88
89 for _, fixture := range fixtures {
90 t.Run(fixture.name, func(t *testing.T) {
91 results, err := parseRedirectText(fixture.input)
92 if err != nil && !fixture.shouldError {
93 t.Error(err)
94 }
95 if cmp.Equal(results, fixture.expect) == false {
96 //nolint
97 t.Fatalf(cmp.Diff(fixture.expect, results))
98 }
99 })
100 }
101}