diff --git a/router/router.go b/router/router.go index 1f17ca4..8720345 100644 --- a/router/router.go +++ b/router/router.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net/http" + "strings" "time" "go.jolheiser.com/vanity/cmd/flags" @@ -58,6 +59,7 @@ func indexGET(db *database.Database) http.HandlerFunc { func vanityGET(db *database.Database) http.HandlerFunc { return func(res http.ResponseWriter, req *http.Request) { key := chi.URLParam(req, "*") + key = strings.Split(key, "/")[0] pkg, err := db.Package(key) if err != nil { diff --git a/router/router_test.go b/router/router_test.go index 34f141c..dea64e6 100644 --- a/router/router_test.go +++ b/router/router_test.go @@ -2,6 +2,8 @@ package router import ( "context" + "fmt" + "net/http" "net/http/httptest" "os" "path/filepath" @@ -81,6 +83,15 @@ func TestRouter(t *testing.T) { // Info (after second package) checkInfo(t, client, 2) + // Check invalid package (404) + checkResp(t, "test3", http.StatusNotFound) + + // Check valid package (200) + checkResp(t, "test1", http.StatusOK) + + // Check valid sub-package (200) + checkResp(t, "test1/foo/bar", http.StatusOK) + // Update package checkUpdate(t, client, pkg1) @@ -148,3 +159,17 @@ func checkRemove(t *testing.T, client *vanity.Client, pkg vanity.Package) { t.Fail() } } + +func checkResp(t *testing.T, path string, status int) { + resp, err := http.Get(fmt.Sprintf("%s/%s", server.URL, path)) + if err != nil { + t.Logf("could not GET %s: %v", path, err) + t.Fail() + return + } + + if resp.StatusCode != status { + t.Logf("incorrect response from %s, expected %d but got %d", path, status, resp.StatusCode) + t.Fail() + } +}