repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

pico / pgs
Eric Bower · 20 Aug 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}
14
15func TestParseRedirectText(t *testing.T) {
16	empty := map[string]string{}
17	spa := RedirectFixture{
18		name:  "spa",
19		input: "/*   /index.html   200",
20		expect: []*RedirectRule{
21			{
22				From:       "/*",
23				To:         "/index.html",
24				Status:     200,
25				Query:      empty,
26				Conditions: empty,
27			},
28		},
29	}
30
31	withStatus := RedirectFixture{
32		name:  "with-status",
33		input: "/wow     /index.html     301",
34		expect: []*RedirectRule{
35			{
36				From:       "/wow",
37				To:         "/index.html",
38				Status:     301,
39				Query:      empty,
40				Conditions: empty,
41			},
42		},
43	}
44
45	noStatus := RedirectFixture{
46		name:  "no-status",
47		input: "/wow     /index.html",
48		expect: []*RedirectRule{
49			{
50				From:       "/wow",
51				To:         "/index.html",
52				Status:     200,
53				Query:      empty,
54				Conditions: empty,
55			},
56		},
57	}
58
59	fixtures := []RedirectFixture{
60		spa,
61		withStatus,
62		noStatus,
63	}
64
65	for _, fixture := range fixtures {
66		t.Run(fixture.name, func(t *testing.T) {
67			results, err := parseRedirectText(fixture.input)
68			if err != nil {
69				t.Error(err)
70			}
71			if cmp.Equal(results, fixture.expect) == false {
72				//nolint
73				t.Fatalf(cmp.Diff(fixture.expect, results))
74			}
75		})
76	}
77}