Check first path for package, sub-packages are implicitly okay
Signed-off-by: jolheiser <john.olheiser@gmail.com>pull/11/head
parent
b451e46e35
commit
2fc704f38c
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.jolheiser.com/vanity/cmd/flags"
|
"go.jolheiser.com/vanity/cmd/flags"
|
||||||
|
@ -58,6 +59,7 @@ func indexGET(db *database.Database) http.HandlerFunc {
|
||||||
func vanityGET(db *database.Database) http.HandlerFunc {
|
func vanityGET(db *database.Database) http.HandlerFunc {
|
||||||
return func(res http.ResponseWriter, req *http.Request) {
|
return func(res http.ResponseWriter, req *http.Request) {
|
||||||
key := chi.URLParam(req, "*")
|
key := chi.URLParam(req, "*")
|
||||||
|
key = strings.Split(key, "/")[0]
|
||||||
|
|
||||||
pkg, err := db.Package(key)
|
pkg, err := db.Package(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -2,6 +2,8 @@ package router
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -81,6 +83,15 @@ func TestRouter(t *testing.T) {
|
||||||
// Info (after second package)
|
// Info (after second package)
|
||||||
checkInfo(t, client, 2)
|
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
|
// Update package
|
||||||
checkUpdate(t, client, pkg1)
|
checkUpdate(t, client, pkg1)
|
||||||
|
|
||||||
|
@ -148,3 +159,17 @@ func checkRemove(t *testing.T, client *vanity.Client, pkg vanity.Package) {
|
||||||
t.Fail()
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue