From facce22b898fedf09f4365606114aee3f1e4dcde Mon Sep 17 00:00:00 2001 From: Eric Bower <me@erock.io> Date: Thu, 9 Jan 2025 13:15:00 -0500 Subject: [PATCH] fix(pgs): handle empty filepaths in calc_route --- pgs/calc_route.go | 5 ++++- pgs/calc_route_test.go | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/pgs/calc_route.go b/pgs/calc_route.go index 506eedc1..c6ba18bd 100644 --- a/pgs/calc_route.go +++ b/pgs/calc_route.go @@ -186,8 +186,11 @@ func genRedirectRoute(actual string, fromStr string, to string) string { func calcRoutes(projectName, fp string, userRedirects []*RedirectRule) []*HttpReply { rts := []*HttpReply{} + if fp == "" { + fp = "/" + } // add route as-is without expansion - if fp != "" && !strings.HasSuffix(fp, "/") { + if !strings.HasSuffix(fp, "/") { defRoute := shared.GetAssetFileName(&utils.FileEntry{ Filepath: filepath.Join(projectName, fp), }) diff --git a/pgs/calc_route_test.go b/pgs/calc_route_test.go index 71df140f..8870f66d 100644 --- a/pgs/calc_route_test.go +++ b/pgs/calc_route_test.go @@ -212,6 +212,42 @@ func TestCalcRoutes(t *testing.T) { {Filepath: "test/404.html", Status: 404}, }, }, + { + Name: "redirect-root-full-url", + Actual: calcRoutes( + "test", + "/", + []*RedirectRule{ + { + From: "/*", + To: "https://pico.sh", + Status: 301, + }, + }, + ), + Expected: []*HttpReply{ + {Filepath: "test/index.html", Status: 200}, + {Filepath: "https://pico.sh", Status: 301}, + }, + }, + { + Name: "redirect-empty-route-full-url", + Actual: calcRoutes( + "test", + "", + []*RedirectRule{ + { + From: "/*", + To: "https://pico.sh", + Status: 301, + }, + }, + ), + Expected: []*HttpReply{ + {Filepath: "test/index.html", Status: 200}, + {Filepath: "https://pico.sh", Status: 301}, + }, + }, { Name: "redirect-full-url-directory", Actual: calcRoutes(