test(lib): fix tests for double slash fix

Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
Xe Iaso
2025-09-27 17:37:17 +00:00
parent bb8baef871
commit a62bcb5e61
2 changed files with 18 additions and 5 deletions
+17 -4
View File
@@ -457,7 +457,7 @@ func TestBasePrefix(t *testing.T) {
}{ }{
{ {
name: "no prefix", name: "no prefix",
basePrefix: "/", basePrefix: "",
path: "/.within.website/x/cmd/anubis/api/make-challenge", path: "/.within.website/x/cmd/anubis/api/make-challenge",
expected: "/.within.website/x/cmd/anubis/api/make-challenge", expected: "/.within.website/x/cmd/anubis/api/make-challenge",
}, },
@@ -499,9 +499,15 @@ func TestBasePrefix(t *testing.T) {
} }
q := req.URL.Query() q := req.URL.Query()
q.Set("redir", tc.basePrefix) redir := tc.basePrefix
if tc.basePrefix == "" {
redir = "/"
}
q.Set("redir", redir)
req.URL.RawQuery = q.Encode() req.URL.RawQuery = q.Encode()
t.Log(req.URL.String())
// Test API endpoint with prefix // Test API endpoint with prefix
resp, err := cli.Do(req) resp, err := cli.Do(req)
if err != nil { if err != nil {
@@ -513,8 +519,15 @@ func TestBasePrefix(t *testing.T) {
t.Errorf("expected status code %d, got: %d", http.StatusOK, resp.StatusCode) t.Errorf("expected status code %d, got: %d", http.StatusOK, resp.StatusCode)
} }
data, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("can't read body: %v", err)
}
t.Log(string(data))
var chall challengeResp var chall challengeResp
if err := json.NewDecoder(resp.Body).Decode(&chall); err != nil { if err := json.NewDecoder(bytes.NewBuffer(data)).Decode(&chall); err != nil {
t.Fatalf("can't read challenge response body: %v", err) t.Fatalf("can't read challenge response body: %v", err)
} }
@@ -535,7 +548,7 @@ func TestBasePrefix(t *testing.T) {
nonce++ nonce++
} }
elapsedTime := 420 elapsedTime := 420
redir := "/" redir = "/"
cli.CheckRedirect = func(req *http.Request, via []*http.Request) error { cli.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse return http.ErrUseLastResponse
+1 -1
View File
@@ -107,7 +107,7 @@ func New(opts Options) (*Server, error) {
opts.ED25519PrivateKey = priv opts.ED25519PrivateKey = priv
} }
anubis.BasePrefix = opts.BasePrefix anubis.BasePrefix = strings.TrimRight(opts.BasePrefix, "/")
anubis.PublicUrl = opts.PublicUrl anubis.PublicUrl = opts.PublicUrl
result := &Server{ result := &Server{