diff --git a/lib/anubis_test.go b/lib/anubis_test.go index 133c56d0..1a0b2d97 100644 --- a/lib/anubis_test.go +++ b/lib/anubis_test.go @@ -457,7 +457,7 @@ func TestBasePrefix(t *testing.T) { }{ { name: "no prefix", - basePrefix: "/", + basePrefix: "", path: "/.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.Set("redir", tc.basePrefix) + redir := tc.basePrefix + if tc.basePrefix == "" { + redir = "/" + } + q.Set("redir", redir) req.URL.RawQuery = q.Encode() + t.Log(req.URL.String()) + // Test API endpoint with prefix resp, err := cli.Do(req) if err != nil { @@ -513,8 +519,15 @@ func TestBasePrefix(t *testing.T) { 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 - 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) } @@ -535,7 +548,7 @@ func TestBasePrefix(t *testing.T) { nonce++ } elapsedTime := 420 - redir := "/" + redir = "/" cli.CheckRedirect = func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse diff --git a/lib/config.go b/lib/config.go index 7ab312d1..3c220dc5 100644 --- a/lib/config.go +++ b/lib/config.go @@ -107,7 +107,7 @@ func New(opts Options) (*Server, error) { opts.ED25519PrivateKey = priv } - anubis.BasePrefix = opts.BasePrefix + anubis.BasePrefix = strings.TrimRight(opts.BasePrefix, "/") anubis.PublicUrl = opts.PublicUrl result := &Server{