repos / pico

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

commit
5093a4e
parent
a526333
author
Eric Bower
date
2024-04-04 02:26:33 +0000 UTC
feat(pgs): append trailing slash to redirect matchers

This change will make it so a user can provide -- or not provide -- a
trailing slash for matching a route, that way the user doesn't need to
think about it.
2 files changed,  +51, -7
M pgs/cal_route.go
+6, -1
 1@@ -95,7 +95,12 @@ func calcRoutes(projectName, fp string, userRedirects []*RedirectRule) []*HttpRe
 2 		if redirect.From == redirect.To {
 3 			continue
 4 		}
 5-		rr := regexp.MustCompile(redirect.From)
 6+
 7+		from := redirect.From
 8+		if !strings.HasSuffix(redirect.From, "*") {
 9+			from = strings.TrimSuffix(redirect.From, "/") + "/?"
10+		}
11+		rr := regexp.MustCompile(from)
12 		match := rr.FindStringSubmatch(fp)
13 		if len(match) > 0 {
14 			isRedirect := checkIsRedirect(redirect.Status)
M pgs/calc_route_test.go
+45, -6
 1@@ -228,19 +228,19 @@ func TestCalcRoutes(t *testing.T) {
 2 			Name: "redirectDirectory",
 3 			Actual: calcRoutes(
 4 				"public",
 5-				"/stata2",
 6+				"/xyz",
 7 				[]*RedirectRule{
 8 					{
 9-						From:   "/stata2",
10-						To:     "/workshop-stata2",
11+						From:   "/xyz",
12+						To:     "/wrk-xyz",
13 						Status: 301,
14 					},
15 				},
16 			),
17 			Expected: []*HttpReply{
18-				{Filepath: "public/stata2.html", Status: 200},
19-				{Filepath: "/workshop-stata2", Status: 301},
20-				{Filepath: "/stata2/", Status: 301},
21+				{Filepath: "public/xyz.html", Status: 200},
22+				{Filepath: "/wrk-xyz", Status: 301},
23+				{Filepath: "/xyz/", Status: 301},
24 				{Filepath: "public/404.html", Status: 404},
25 			},
26 		},
27@@ -283,6 +283,45 @@ func TestCalcRoutes(t *testing.T) {
28 				{Filepath: "public/404.html", Status: 404},
29 			},
30 		},
31+		{
32+			Name: "redirectNoTrailingSlash",
33+			Actual: calcRoutes(
34+				"public",
35+				"/space/",
36+				[]*RedirectRule{
37+					{
38+						From:   "/space",
39+						To:     "/frontier/",
40+						Status: 301,
41+					},
42+				},
43+			),
44+			Expected: []*HttpReply{
45+				{Filepath: "public/space/index.html", Status: 200},
46+				{Filepath: "/frontier/", Status: 301},
47+				{Filepath: "public/404.html", Status: 404},
48+			},
49+		},
50+		{
51+			Name: "redirectWithTrailingSlash",
52+			Actual: calcRoutes(
53+				"public",
54+				"/space",
55+				[]*RedirectRule{
56+					{
57+						From:   "/space/",
58+						To:     "/frontier/",
59+						Status: 301,
60+					},
61+				},
62+			),
63+			Expected: []*HttpReply{
64+				{Filepath: "public/space.html", Status: 200},
65+				{Filepath: "/frontier/", Status: 301},
66+				{Filepath: "/space/", Status: 301},
67+				{Filepath: "public/404.html", Status: 404},
68+			},
69+		},
70 	}
71 
72 	for _, fixture := range fixtures {