mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-30 09:09:03 +00:00
Pipeline: Test, Lint, Build / Get version info (push) Canceled after 0s
Pipeline: Test, Lint, Build / Lint Go code (push) Canceled after 0s
Pipeline: Test, Lint, Build / Validate DB migrations (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test Go code (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test Go plugins (push) Canceled after 0s
Pipeline: Test, Lint, Build / Report coverage (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test Go code (Windows) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test JS code (push) Canceled after 0s
Pipeline: Test, Lint, Build / Lint i18n files (push) Canceled after 0s
Pipeline: Test, Lint, Build / Check Docker configuration (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/386) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/riscv64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (windows/386) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Push to GHCR (push) Canceled after 0s
Pipeline: Test, Lint, Build / Push to Docker Hub () (push) Canceled after 0s
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build Windows installers (push) Canceled after 0s
Pipeline: Test, Lint, Build / Package/Release (push) Canceled after 0s
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Canceled after 0s
129 lines
4.7 KiB
Go
129 lines
4.7 KiB
Go
package apiv1
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
|
|
"github.com/getkin/kin-openapi/openapi3"
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/navidrome/navidrome/api"
|
|
"github.com/navidrome/navidrome/db"
|
|
"github.com/navidrome/navidrome/persistence"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("Router", func() {
|
|
var ctx context.Context
|
|
var router *Router
|
|
|
|
BeforeEach(func() {
|
|
ctx = GinkgoT().Context()
|
|
router = New(persistence.New(db.Db()))
|
|
})
|
|
|
|
It("routes every operation in the embedded spec", func() {
|
|
doc, err := openapi3.NewLoader().LoadFromData(api.SpecJSON())
|
|
Expect(err).ToNot(HaveOccurred())
|
|
mux := router.Handler.(chi.Routes)
|
|
for path, item := range doc.Paths.Map() {
|
|
for method := range item.Operations() {
|
|
Expect(mux.Find(chi.NewRouteContext(), method, path)).To(Equal(path), method+" "+path)
|
|
}
|
|
}
|
|
})
|
|
|
|
It("declares Cache-Control no-store on the success responses of every no-store operation", func() {
|
|
doc, err := openapi3.NewLoader().LoadFromData(api.SpecJSON())
|
|
Expect(err).ToNot(HaveOccurred())
|
|
checked := map[string]bool{}
|
|
for _, item := range doc.Paths.Map() {
|
|
for _, op := range item.Operations() {
|
|
if !gateRulesV1.noStore[op.OperationID] {
|
|
continue
|
|
}
|
|
for code, resp := range op.Responses.Map() {
|
|
if !strings.HasPrefix(code, "2") {
|
|
continue
|
|
}
|
|
h := resp.Value.Headers["Cache-Control"]
|
|
Expect(h).ToNot(BeNil(), op.OperationID+" "+code)
|
|
Expect(h.Value.Schema.Value.Enum).To(ConsistOf("no-store"), op.OperationID+" "+code)
|
|
checked[op.OperationID] = true
|
|
}
|
|
}
|
|
}
|
|
Expect(checked).To(HaveLen(len(gateRulesV1.noStore)))
|
|
})
|
|
|
|
It("returns a 404 problem for unknown paths", func() {
|
|
w := serve(router, httptest.NewRequestWithContext(ctx, http.MethodGet, "/api/v1/nope", nil))
|
|
Expect(w.Code).To(Equal(http.StatusNotFound))
|
|
Expect(w.Header().Get("Content-Type")).To(Equal(problemContentType))
|
|
Expect(decodeProblem(w).Code).To(Equal(ProblemCodeNotFound))
|
|
})
|
|
|
|
It("returns a 405 problem listing the allowed methods for a wrong method on a known path", func() {
|
|
w := serve(router, httptest.NewRequestWithContext(ctx, http.MethodPost, "/api/v1/server", nil))
|
|
Expect(w.Code).To(Equal(http.StatusMethodNotAllowed))
|
|
Expect(w.Header().Get("Allow")).To(Equal("GET, HEAD"))
|
|
Expect(decodeProblem(w).Code).To(Equal(ProblemCodeMethodNotAllowed))
|
|
})
|
|
|
|
DescribeTable("answers HEAD wherever GET is routed",
|
|
func(path, contentType string) {
|
|
w := serve(router, httptest.NewRequestWithContext(ctx, http.MethodHead, path, nil))
|
|
Expect(w.Code).To(Equal(http.StatusOK))
|
|
Expect(w.Header().Get("Content-Type")).To(Equal(contentType))
|
|
},
|
|
Entry("server info", "/api/v1/server", "application/json"),
|
|
Entry("JSON spec", "/api/v1/openapi.json", "application/json"),
|
|
Entry("YAML spec", "/api/v1/openapi.yaml", "application/yaml"),
|
|
)
|
|
|
|
It("revalidates HEAD requests with If-None-Match", func() {
|
|
etag := serve(router, httptest.NewRequestWithContext(ctx, http.MethodHead, "/api/v1/openapi.json", nil)).Header().Get("ETag")
|
|
req := httptest.NewRequestWithContext(ctx, http.MethodHead, "/api/v1/openapi.json", nil)
|
|
req.Header.Set("If-None-Match", etag)
|
|
Expect(serve(router, req).Code).To(Equal(http.StatusNotModified))
|
|
})
|
|
|
|
It("returns a 404 problem for HEAD on unknown paths", func() {
|
|
w := serve(router, httptest.NewRequestWithContext(ctx, http.MethodHead, "/api/v1/nope", nil))
|
|
Expect(w.Code).To(Equal(http.StatusNotFound))
|
|
Expect(w.Header().Get("Allow")).To(BeEmpty())
|
|
})
|
|
|
|
panicking := func(v any) http.Handler {
|
|
return problemRecoverer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { panic(v) })) //nolint:forbidigo
|
|
}
|
|
|
|
It("turns a handler panic into a 500 problem", func() {
|
|
w := httptest.NewRecorder()
|
|
panicking("kaboom").ServeHTTP(w, httptest.NewRequestWithContext(ctx, http.MethodGet, "/boom", nil))
|
|
Expect(w.Code).To(Equal(http.StatusInternalServerError))
|
|
p := decodeProblem(w)
|
|
Expect(p.Code).To(Equal(ProblemCodeInternal))
|
|
Expect(p.Detail).To(BeNil())
|
|
})
|
|
|
|
It("tags internal errors with a referenceId that is also on the request's log lines", func() {
|
|
logs := captureLogs()
|
|
w := httptest.NewRecorder()
|
|
h := referenceIDMiddleware(panicking("kaboom"))
|
|
h.ServeHTTP(w, httptest.NewRequestWithContext(GinkgoT().Context(), http.MethodGet, "/boom", nil))
|
|
p := decodeProblem(w)
|
|
Expect(p.ReferenceId).ToNot(BeNil())
|
|
Expect(*p.ReferenceId).To(MatchRegexp(`^[0-9A-Za-z]{22}$`))
|
|
Expect(logs.String()).To(ContainSubstring(*p.ReferenceId))
|
|
})
|
|
|
|
It("re-panics http.ErrAbortHandler so the server can drop the connection", func() {
|
|
Expect(func() {
|
|
panicking(http.ErrAbortHandler).ServeHTTP(httptest.NewRecorder(), httptest.NewRequestWithContext(ctx, http.MethodGet, "/abort", nil))
|
|
}).To(PanicWith(http.ErrAbortHandler))
|
|
})
|
|
})
|