Check first path for package, sub-packages are implicitly okay

Signed-off-by: jolheiser <john.olheiser@gmail.com>
pull/11/head
jolheiser 2021-03-11 22:57:57 -06:00
parent b451e46e35
commit 2fc704f38c
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
2 changed files with 27 additions and 0 deletions

View File

@ -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 {

View File

@ -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()
}
}