mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-20 03:02:34 +03:00
* chore(lint): ban panic in production code Add a forbidigo rule that rejects panic outside tests and test helpers (_test.go, tests/ and storagetest/). Its message says to return an error, and to use log.Fatal only for startup failures. The existing production panics are gone: - model.MustParseArtworkID was only used by core/artwork tests. It is now a Gomega helper in those test suites, and it also panicked with the parsed ID instead of the error. - isDBInitialized in db/migrations panicked on a query error and never closed its rows. It now uses QueryRowContext and logs the error. The two http.ErrAbortHandler panics in the stream and API v1 handlers stay, because net/http requires them to abort a response. They are marked with //nolint:forbidigo. cmd/wire_injectors.go is not linted, because of its wireinject build tag. * chore(lint): ban panic in tests too The panic ban excluded test files and test helpers, so agents could read it as permission to panic in tests. The rule now applies everywhere, and its message says what to use instead: return an error, or Expect(...)/Fail(...) in tests. Test panics are replaced with Gomega assertions, and the helpers that assert call GinkgoHelper() so failures point at the calling spec. The fake HTTP clients in the deezer tests and getDirEntry use Fail(...). storagetest.FakeFS.UpdateTags now asserts with Gomega instead of panicking. Seven test panics stay, marked //nolint:forbidigo: they exercise panic recovery or stand in for log.Fatal. * refactor(tests): share panicking test helpers Move mustParseArtworkID out of the two core/artwork suites into tests.MustParseArtworkID, and add conf.PanicOnLogFatal so the conf specs share one panicking logFatal stub and one //nolint. The MustPath spec now asserts the fatal message instead of capturing it. Also merge the back-to-back user and artist loops in the persistence suite setup.
76 lines
2.8 KiB
Go
76 lines
2.8 KiB
Go
package apiv1
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
|
|
"github.com/navidrome/navidrome/tests"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("Router", func() {
|
|
var router *Router
|
|
|
|
BeforeEach(func() {
|
|
router = New(&tests.MockDataStore{})
|
|
})
|
|
|
|
It("returns a 404 problem for unknown paths", func() {
|
|
w := serve(router, httptest.NewRequest(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.NewRequest(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.NewRequest(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.NewRequest(http.MethodHead, "/api/v1/openapi.json", nil)).Header().Get("ETag")
|
|
req := httptest.NewRequest(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.NewRequest(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.NewRequest(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("re-panics http.ErrAbortHandler so the server can drop the connection", func() {
|
|
Expect(func() {
|
|
panicking(http.ErrAbortHandler).ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/abort", nil))
|
|
}).To(PanicWith(http.ErrAbortHandler))
|
|
})
|
|
})
|