From 2327f93098b4278c93055a96224ae82cef60b083 Mon Sep 17 00:00:00 2001 From: Adam Mathes Date: Sun, 15 Feb 2026 13:48:34 -0800 Subject: Backend: Fix linting issues, improve error handling, and replace magic numbers --- .golangci.yml | 3 +- api/api.go | 14 +++---- api/api_test.go | 54 +++++++++++++------------- cmd/neko/main.go | 5 ++- internal/importer/importer.go | 2 +- models/feed/feed.go | 8 ++-- models/item/item.go | 2 +- util/util.go | 19 ---------- util/util_test.go | 8 ---- web/auth_test.go | 10 ++--- web/frontend.go | 2 +- web/web.go | 20 +++++----- web/web_test.go | 88 +++++++++++++++++++++---------------------- 13 files changed, 105 insertions(+), 130 deletions(-) delete mode 100644 util/util.go delete mode 100644 util/util_test.go diff --git a/.golangci.yml b/.golangci.yml index 0837e25..e8e8364 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -19,8 +19,7 @@ issues: max-issues-per-linter: 0 max-same-issues: 0 exclude-rules: - - path: _test\.go + - path: .*_test\.go linters: - errcheck - - staticcheck diff --git a/api/api.go b/api/api.go index 9ea35c8..6833ce4 100644 --- a/api/api.go +++ b/api/api.go @@ -45,12 +45,12 @@ func (s *Server) routes() { func jsonError(w http.ResponseWriter, msg string, code int) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(code) - json.NewEncoder(w).Encode(map[string]string{"error": msg}) + _ = json.NewEncoder(w).Encode(map[string]string{"error": msg}) } func jsonResponse(w http.ResponseWriter, data interface{}) { w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(data) + _ = json.NewEncoder(w).Encode(data) } func (s *Server) HandleStream(w http.ResponseWriter, r *http.Request) { @@ -65,7 +65,7 @@ func (s *Server) HandleStream(w http.ResponseWriter, r *http.Request) { // Backward compatibility with feed_url if feed_id is not provided if feedID == 0 && r.FormValue("feed_url") != "" { var f feed.Feed - f.ByUrl(r.FormValue("feed_url")) + _ = f.ByUrl(r.FormValue("feed_url")) feedID = f.Id } @@ -157,7 +157,7 @@ func (s *Server) HandleFeed(w http.ResponseWriter, r *http.Request) { jsonError(w, "failed to create feed", http.StatusInternalServerError) return } - f.ByUrl(f.Url) + _ = f.ByUrl(f.Url) ch := make(chan string) go func() { crawler.CrawlFeed(&f, ch) @@ -165,7 +165,7 @@ func (s *Server) HandleFeed(w http.ResponseWriter, r *http.Request) { }() w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) - json.NewEncoder(w).Encode(f) + _ = json.NewEncoder(w).Encode(f) case http.MethodPut: var f feed.Feed @@ -237,7 +237,7 @@ func (s *Server) HandleExport(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", contentType) w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=neko_export.%s", extension)) - w.Write([]byte(exporter.ExportFeeds(format))) + _, _ = w.Write([]byte(exporter.ExportFeeds(format))) } func (s *Server) HandleImport(w http.ResponseWriter, r *http.Request) { @@ -256,7 +256,7 @@ func (s *Server) HandleImport(w http.ResponseWriter, r *http.Request) { jsonError(w, "file required", http.StatusBadRequest) return } - defer file.Close() + defer func() { _ = file.Close() }() err = importer.ImportFeeds(format, file) if err != nil { diff --git a/api/api_test.go b/api/api_test.go index 15679f7..010908a 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -60,7 +60,7 @@ func TestStream(t *testing.T) { server.ServeHTTP(rr, req) if rr.Code != http.StatusOK { - t.Errorf("expected 200, got %d", rr.Code) + t.Errorf("expected %d, got %d", http.StatusOK, rr.Code) } var items []item.Item @@ -82,7 +82,7 @@ func TestFeedCRUD(t *testing.T) { server.ServeHTTP(rr, req) if rr.Code != http.StatusCreated { - t.Errorf("expected 201, got %d", rr.Code) + t.Errorf("expected %d, got %d", http.StatusCreated, rr.Code) } // List @@ -106,7 +106,7 @@ func TestFeedCRUD(t *testing.T) { server.ServeHTTP(rr, req) if rr.Code != http.StatusOK { - t.Errorf("expected 200, got %d", rr.Code) + t.Errorf("expected %d, got %d", http.StatusOK, rr.Code) } // Delete @@ -115,7 +115,7 @@ func TestFeedCRUD(t *testing.T) { server.ServeHTTP(rr, req) if rr.Code != http.StatusNoContent { - t.Errorf("expected 204, got %d", rr.Code) + t.Errorf("expected %d, got %d", http.StatusNoContent, rr.Code) } } @@ -138,7 +138,7 @@ func TestItemUpdate(t *testing.T) { server.ServeHTTP(rr, req) if rr.Code != http.StatusOK { - t.Errorf("expected 200, got %d", rr.Code) + t.Errorf("expected %d, got %d", http.StatusOK, rr.Code) } } @@ -152,7 +152,7 @@ func TestGetCategories(t *testing.T) { server.ServeHTTP(rr, req) if rr.Code != http.StatusOK { - t.Errorf("expected 200, got %d", rr.Code) + t.Errorf("expected %d, got %d", http.StatusOK, rr.Code) } var cats []feed.Category @@ -174,7 +174,7 @@ func TestHandleExport(t *testing.T) { server.HandleExport(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200 for format %s, got %d", fmt, rr.Code) + t.Errorf("Expected %d for format %s, got %d", http.StatusOK, fmt, rr.Code) } } @@ -182,7 +182,7 @@ func TestHandleExport(t *testing.T) { rr := httptest.NewRecorder() server.HandleExport(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200 for unknown format, got %d", rr.Code) + t.Errorf("Expected %d for unknown format, got %d", http.StatusOK, rr.Code) } } @@ -195,7 +195,7 @@ func TestHandleCrawl(t *testing.T) { server.HandleCrawl(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusOK, rr.Code) } if !strings.Contains(rr.Body.String(), "crawl started") { t.Error("Expected crawl started message in response") @@ -210,7 +210,7 @@ func TestJsonError(t *testing.T) { server.HandleItem(rr, req) if rr.Code != http.StatusBadRequest { - t.Errorf("Expected 400, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusBadRequest, rr.Code) } var resp map[string]string json.Unmarshal(rr.Body.Bytes(), &resp) @@ -257,21 +257,21 @@ func TestHandleFeedErrors(t *testing.T) { rr := httptest.NewRecorder() server.ServeHTTP(rr, req) if rr.Code != http.StatusBadRequest { - t.Errorf("Expected 400 for missing URL, got %d", rr.Code) + t.Errorf("Expected %d for missing URL, got %d", http.StatusBadRequest, rr.Code) } req = httptest.NewRequest("POST", "/feed", strings.NewReader("not json")) rr = httptest.NewRecorder() server.ServeHTTP(rr, req) if rr.Code != http.StatusBadRequest { - t.Errorf("Expected 400 for invalid JSON, got %d", rr.Code) + t.Errorf("Expected %d for invalid JSON, got %d", http.StatusBadRequest, rr.Code) } req = httptest.NewRequest("PATCH", "/feed", nil) rr = httptest.NewRecorder() server.ServeHTTP(rr, req) if rr.Code != http.StatusMethodNotAllowed { - t.Errorf("Expected 405, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusMethodNotAllowed, rr.Code) } } @@ -284,14 +284,14 @@ func TestHandleItemEdgeCases(t *testing.T) { rr := httptest.NewRecorder() server.ServeHTTP(rr, req) if rr.Code != http.StatusNotFound { - t.Errorf("Expected 404, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusNotFound, rr.Code) } req = httptest.NewRequest("DELETE", "/item/1", nil) rr = httptest.NewRecorder() server.ServeHTTP(rr, req) if rr.Code != http.StatusMethodNotAllowed { - t.Errorf("Expected 405, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusMethodNotAllowed, rr.Code) } var id int64 @@ -304,7 +304,7 @@ func TestHandleItemEdgeCases(t *testing.T) { rr = httptest.NewRecorder() server.ServeHTTP(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusOK, rr.Code) } } @@ -316,7 +316,7 @@ func TestHandleFeedDeleteNoId(t *testing.T) { rr := httptest.NewRecorder() server.ServeHTTP(rr, req) if rr.Code != http.StatusBadRequest { - t.Errorf("Expected 400, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusBadRequest, rr.Code) } } @@ -338,7 +338,7 @@ func TestMethodNotAllowed(t *testing.T) { rr := httptest.NewRecorder() server.ServeHTTP(rr, req) if rr.Code != http.StatusMethodNotAllowed { - t.Errorf("Expected 405 for %s %s, got %d", tc.method, tc.url, rr.Code) + t.Errorf("Expected %d for %s %s, got %d", http.StatusMethodNotAllowed, tc.method, tc.url, rr.Code) } } } @@ -350,7 +350,7 @@ func TestExportBadRequest(t *testing.T) { rr := httptest.NewRecorder() server.HandleExport(rr, req) if rr.Code != http.StatusBadRequest { - t.Errorf("Expected 400 for empty format, got %d", rr.Code) + t.Errorf("Expected %d for empty format, got %d", http.StatusBadRequest, rr.Code) } } @@ -361,7 +361,7 @@ func TestHandleFeedPutInvalidJson(t *testing.T) { rr := httptest.NewRecorder() server.HandleFeed(rr, req) if rr.Code != http.StatusBadRequest { - t.Errorf("Expected 400 for invalid JSON in PUT, got %d", rr.Code) + t.Errorf("Expected %d for invalid JSON in PUT, got %d", http.StatusBadRequest, rr.Code) } } @@ -373,7 +373,7 @@ func TestHandleFeedPutMissingId(t *testing.T) { rr := httptest.NewRecorder() server.HandleFeed(rr, req) if rr.Code != http.StatusBadRequest { - t.Errorf("Expected 400 for missing ID in PUT, got %d", rr.Code) + t.Errorf("Expected %d for missing ID in PUT, got %d", http.StatusBadRequest, rr.Code) } } @@ -386,7 +386,7 @@ func TestHandleItemIdMismatch(t *testing.T) { rr := httptest.NewRecorder() server.HandleItem(rr, req) if rr.Code != http.StatusBadRequest { - t.Errorf("Expected 400 for ID mismatch, got %d", rr.Code) + t.Errorf("Expected %d for ID mismatch, got %d", http.StatusBadRequest, rr.Code) } } @@ -400,7 +400,7 @@ func TestHandleCategoryError(t *testing.T) { server.HandleCategory(rr, req) if rr.Code != http.StatusInternalServerError { - t.Errorf("Expected 500, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusInternalServerError, rr.Code) } } @@ -418,7 +418,7 @@ func TestHandleItemAlreadyHasContent(t *testing.T) { server.HandleItem(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusOK, rr.Code) } } @@ -428,7 +428,7 @@ func TestHandleCrawlMethodNotAllowed(t *testing.T) { rr := httptest.NewRecorder() server.HandleCrawl(rr, req) if rr.Code != http.StatusMethodNotAllowed { - t.Errorf("Expected 405, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusMethodNotAllowed, rr.Code) } } @@ -441,7 +441,7 @@ func TestHandleStreamComplexFilters(t *testing.T) { rr := httptest.NewRecorder() server.HandleStream(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusOK, rr.Code) } } @@ -455,6 +455,6 @@ func TestHandleCategorySuccess(t *testing.T) { rr := httptest.NewRecorder() server.HandleCategory(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusOK, rr.Code) } } diff --git a/cmd/neko/main.go b/cmd/neko/main.go index 68bc971..b9ac6fd 100644 --- a/cmd/neko/main.go +++ b/cmd/neko/main.go @@ -148,7 +148,10 @@ func Run(args []string) error { } if newFeed != "" { vlog.Printf("creating new feed\n") - feed.NewFeed(newFeed) + if err := feed.NewFeed(newFeed); err != nil { + vlog.Printf("error creating feed: %v\n", err) + return err + } return nil } if export != "" { diff --git a/internal/importer/importer.go b/internal/importer/importer.go index f74ace1..de5e66c 100644 --- a/internal/importer/importer.go +++ b/internal/importer/importer.go @@ -150,7 +150,7 @@ func ImportJSON(filename string) error { if err != nil { return err } - defer f.Close() + defer func() { _ = f.Close() }() return ImportJSONReader(f) } diff --git a/models/feed/feed.go b/models/feed/feed.go index 504b220..4f39335 100644 --- a/models/feed/feed.go +++ b/models/feed/feed.go @@ -53,7 +53,7 @@ func filter(where string) ([]*Feed, error) { if err != nil { return nil, err } - defer rows.Close() + defer func() { _ = rows.Close() }() feeds := make([]*Feed, 0) for rows.Next() { @@ -134,7 +134,7 @@ func ResolveFeedURL(url string) string { if err != nil { return url } - defer resp.Body.Close() + defer func() { _ = resp.Body.Close() }() contentType := resp.Header.Get("Content-Type") if contentType == "" { @@ -157,7 +157,7 @@ func ResolveFeedURL(url string) string { if err != nil { return url } - defer resp.Body.Close() + defer func() { _ = resp.Body.Close() }() doc, err := goquery.NewDocumentFromReader(resp.Body) if err != nil { return url @@ -204,7 +204,7 @@ func Categories() ([]*Category, error) { if err != nil { return nil, err } - defer rows.Close() + defer func() { _ = rows.Close() }() categories := make([]*Category, 0) for rows.Next() { diff --git a/models/item/item.go b/models/item/item.go index f960c65..dc277a3 100644 --- a/models/item/item.go +++ b/models/item/item.go @@ -194,7 +194,7 @@ func Filter(max_id int64, feed_id int64, category string, unread_only bool, star vlog.Println(err) return nil, err } - defer rows.Close() + defer func() { _ = rows.Close() }() p := filterPolicy() diff --git a/util/util.go b/util/util.go deleted file mode 100644 index d6fda0b..0000000 --- a/util/util.go +++ /dev/null @@ -1,19 +0,0 @@ -package util - -import ( - "os" - - "adammathes.com/neko/config" - "adammathes.com/neko/models" -) - -var DEFAULT_CONFIG = "config.json" - -func init() { - var configFile = DEFAULT_CONFIG - if len(os.Args) > 1 { - configFile = os.Args[1] - } - config.Init(configFile) - models.InitDB() -} diff --git a/util/util_test.go b/util/util_test.go deleted file mode 100644 index fc234d6..0000000 --- a/util/util_test.go +++ /dev/null @@ -1,8 +0,0 @@ -package util - -import "testing" - -func TestInit(t *testing.T) { - // init() is called automatically because of the package import - // We just want to verify it doesn't panic and does something reasonable -} diff --git a/web/auth_test.go b/web/auth_test.go index 6f319b9..847d32d 100644 --- a/web/auth_test.go +++ b/web/auth_test.go @@ -36,7 +36,7 @@ func TestAuthenticationNoPassword(t *testing.T) { wrappedHandler.ServeHTTP(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200 OK when no password is set, got %d", rr.Code) + t.Errorf("Expected %d OK when no password is set, got %d", http.StatusOK, rr.Code) } body := rr.Body.String() @@ -72,7 +72,7 @@ func TestAuthenticationWithPassword(t *testing.T) { wrappedHandler.ServeHTTP(rr, req) if rr.Code != http.StatusTemporaryRedirect { - t.Errorf("Expected 307 redirect when not authenticated, got %d", rr.Code) + t.Errorf("Expected %d redirect when not authenticated, got %d", http.StatusTemporaryRedirect, rr.Code) } location := rr.Header().Get("Location") @@ -125,7 +125,7 @@ func TestAuthenticationWithValidCookie(t *testing.T) { wrappedHandler.ServeHTTP(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200 OK with valid auth cookie, got %d", rr.Code) + t.Errorf("Expected %d OK with valid auth cookie, got %d", http.StatusOK, rr.Code) } } @@ -145,7 +145,7 @@ func TestApiLoginNoPassword(t *testing.T) { // Should succeed with any password (or empty) when no password is configured if rr.Code != http.StatusOK { - t.Errorf("Expected 200 OK for API login with no password configured, got %d", rr.Code) + t.Errorf("Expected %d OK for API login with no password configured, got %d", http.StatusOK, rr.Code) } } @@ -164,7 +164,7 @@ func TestApiAuthStatusNoPassword(t *testing.T) { // Should return authenticated:true when no password is set if rr.Code != http.StatusOK { - t.Errorf("Expected 200 OK for auth status with no password, got %d", rr.Code) + t.Errorf("Expected %d OK for auth status with no password, got %d", http.StatusOK, rr.Code) } body := rr.Body.String() diff --git a/web/frontend.go b/web/frontend.go index 6961ec0..d77c974 100644 --- a/web/frontend.go +++ b/web/frontend.go @@ -41,7 +41,7 @@ func ServeFrontend(w http.ResponseWriter, r *http.Request) { return } } - defer f.Close() + defer func() { _ = f.Close() }() d, err := f.Stat() if err != nil { diff --git a/web/web.go b/web/web.go index b1a95d1..1f1bd62 100644 --- a/web/web.go +++ b/web/web.go @@ -98,7 +98,7 @@ func imageProxyHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("ETag", string(decodedURL)) w.Header().Set("Cache-Control", "public") w.Header().Set("Expires", time.Now().Add(48*time.Hour).Format(time.RFC1123)) - w.Write(bts) + _, _ = w.Write(bts) } var AuthCookie = "auth" @@ -126,7 +126,7 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { func logoutHandler(w http.ResponseWriter, r *http.Request) { c := http.Cookie{Name: AuthCookie, MaxAge: 0, Path: "/", HttpOnly: true} http.SetCookie(w, &c) - fmt.Fprintf(w, "you are logged out") + _, _ = fmt.Fprintf(w, "you are logged out") } func Authenticated(r *http.Request) bool { @@ -170,7 +170,7 @@ func serveBoxedFile(w http.ResponseWriter, r *http.Request, filename string) { http.Error(w, "file not found", http.StatusNotFound) return } - defer f.Close() + defer func() { _ = f.Close() }() fi, _ := f.Stat() http.ServeContent(w, r, filename, fi.ModTime(), f.(io.ReadSeeker)) @@ -188,7 +188,7 @@ func apiLoginHandler(w http.ResponseWriter, r *http.Request) { c := http.Cookie{Name: AuthCookie, Value: "noauth", Path: "/", MaxAge: SecondsInAYear, HttpOnly: true} http.SetCookie(w, &c) w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, `{"status":"ok"}`) + _, _ = fmt.Fprintf(w, `{"status":"ok"}`) return } @@ -200,7 +200,7 @@ func apiLoginHandler(w http.ResponseWriter, r *http.Request) { c := http.Cookie{Name: AuthCookie, Value: string(v), Path: "/", MaxAge: SecondsInAYear, HttpOnly: true} http.SetCookie(w, &c) w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, `{"status":"ok"}`) + _, _ = fmt.Fprintf(w, `{"status":"ok"}`) } else { http.Error(w, `{"status":"error", "message":"bad login"}`, http.StatusUnauthorized) } @@ -209,10 +209,10 @@ func apiLoginHandler(w http.ResponseWriter, r *http.Request) { func apiAuthStatusHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if Authenticated(r) { - fmt.Fprintf(w, `{"status":"ok", "authenticated":true}`) + _, _ = fmt.Fprintf(w, `{"status":"ok", "authenticated":true}`) } else { w.WriteHeader(http.StatusUnauthorized) - fmt.Fprintf(w, `{"status":"error", "authenticated":false}`) + _, _ = fmt.Fprintf(w, `{"status":"error", "authenticated":false}`) } } @@ -311,7 +311,7 @@ func (w *gzipWriter) WriteHeader(status int) { func (w *gzipWriter) Flush() { if w.gz != nil { - w.gz.Flush() + _ = w.gz.Flush() } if f, ok := w.ResponseWriter.(http.Flusher); ok { f.Flush() @@ -338,7 +338,7 @@ func GzipMiddleware(next http.Handler) http.Handler { gzw := &gzipWriter{ResponseWriter: w} next.ServeHTTP(gzw, r) if gzw.gz != nil { - gzw.gz.Close() + _ = gzw.gz.Close() gzPool.Put(gzw.gz) } }) @@ -348,7 +348,7 @@ func apiLogoutHandler(w http.ResponseWriter, r *http.Request) { c := http.Cookie{Name: AuthCookie, Value: "", Path: "/", MaxAge: -1, HttpOnly: true} http.SetCookie(w, &c) w.Header().Set("Content-Type", "application/json") - fmt.Fprintf(w, `{"status":"ok"}`) + _, _ = fmt.Fprintf(w, `{"status":"ok"}`) } func generateRandomToken(n int) string { diff --git a/web/web_test.go b/web/web_test.go index 3c5e3df..ad79581 100644 --- a/web/web_test.go +++ b/web/web_test.go @@ -101,7 +101,7 @@ func TestAuthWrapUnauthenticated(t *testing.T) { handler.ServeHTTP(rr, req) if rr.Code != http.StatusTemporaryRedirect { - t.Errorf("Expected 307, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusTemporaryRedirect, rr.Code) } } @@ -131,7 +131,7 @@ func TestLoginHandlerPostSuccess(t *testing.T) { rr := httptest.NewRecorder() loginHandler(rr, req) if rr.Code != http.StatusTemporaryRedirect { - t.Errorf("Expected 307, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusTemporaryRedirect, rr.Code) } } @@ -142,7 +142,7 @@ func TestLoginHandlerPostFail(t *testing.T) { rr := httptest.NewRecorder() loginHandler(rr, req) if rr.Code != http.StatusUnauthorized { - t.Errorf("Expected 401, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusUnauthorized, rr.Code) } } @@ -151,7 +151,7 @@ func TestLoginHandlerBadMethod(t *testing.T) { rr := httptest.NewRecorder() loginHandler(rr, req) if rr.Code != http.StatusInternalServerError { - t.Errorf("Expected 500, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusInternalServerError, rr.Code) } } @@ -161,7 +161,7 @@ func TestLogoutHandler(t *testing.T) { logoutHandler(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusOK, rr.Code) } cookies := rr.Result().Cookies() found := false @@ -189,7 +189,7 @@ func TestImageProxyHandlerIfNoneMatch(t *testing.T) { rr := httptest.NewRecorder() imageProxyHandler(rr, req) if rr.Code != http.StatusNotModified { - t.Errorf("Expected 304, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusNotModified, rr.Code) } } @@ -206,14 +206,14 @@ func TestImageProxyHandlerEtag(t *testing.T) { rr := httptest.NewRecorder() imageProxyHandler(rr, req) if rr.Code != http.StatusNotModified { - t.Errorf("Expected 304, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusNotModified, rr.Code) } } func TestImageProxyHandlerSuccess(t *testing.T) { imgServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "image/jpeg") - w.WriteHeader(200) + w.WriteHeader(http.StatusOK) w.Write([]byte("fake-image-data")) })) defer imgServer.Close() @@ -225,7 +225,7 @@ func TestImageProxyHandlerSuccess(t *testing.T) { imageProxyHandler(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusOK, rr.Code) } if rr.Body.String() != "fake-image-data" { t.Errorf("Expected image data, got %q", rr.Body.String()) @@ -239,7 +239,7 @@ func TestImageProxyHandlerBadRemote(t *testing.T) { rr := httptest.NewRecorder() imageProxyHandler(rr, req) if rr.Code != http.StatusNotFound { - t.Errorf("Expected 404, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusNotFound, rr.Code) } } @@ -248,7 +248,7 @@ func TestImageProxyHandlerEmptyId(t *testing.T) { rr := httptest.NewRecorder() imageProxyHandler(rr, req) if rr.Code != http.StatusNotFound { - t.Errorf("Expected 404, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusNotFound, rr.Code) } } @@ -257,7 +257,7 @@ func TestImageProxyHandlerBadBase64(t *testing.T) { rr := httptest.NewRecorder() imageProxyHandler(rr, req) if rr.Code != http.StatusNotFound { - t.Errorf("Expected 404, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusNotFound, rr.Code) } } @@ -281,7 +281,7 @@ func TestApiMounting(t *testing.T) { handler.ServeHTTP(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200 via API mount, got %d", rr.Code) + t.Errorf("Expected %d via API mount, got %d", http.StatusOK, rr.Code) } } @@ -293,7 +293,7 @@ func TestIndexHandler(t *testing.T) { indexHandler(rr, req) if rr.Code != http.StatusOK && rr.Code != http.StatusNotFound { - t.Errorf("Expected 200 or 404, got %d", rr.Code) + t.Errorf("Expected %d or %d, got %d", http.StatusOK, http.StatusNotFound, rr.Code) } } @@ -305,7 +305,7 @@ func TestServeBoxedFile(t *testing.T) { serveBoxedFile(rr, req, "style.css") if rr.Code != http.StatusOK && rr.Code != http.StatusNotFound { - t.Errorf("Expected 200 or 404, got %d", rr.Code) + t.Errorf("Expected %d or %d, got %d", http.StatusOK, http.StatusNotFound, rr.Code) } } @@ -319,7 +319,7 @@ func TestAuthWrapHandlerUnauthenticated(t *testing.T) { handler.ServeHTTP(rr, req) if rr.Code != http.StatusTemporaryRedirect { - t.Errorf("Expected 307 redirect, got %d", rr.Code) + t.Errorf("Expected %d redirect, got %d", http.StatusTemporaryRedirect, rr.Code) } } @@ -330,7 +330,7 @@ func TestApiLoginHandlerSuccess(t *testing.T) { rr := httptest.NewRecorder() apiLoginHandler(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusOK, rr.Code) } body := rr.Body.String() if body != `{"status":"ok"}` { @@ -345,7 +345,7 @@ func TestApiLoginHandlerFail(t *testing.T) { rr := httptest.NewRecorder() apiLoginHandler(rr, req) if rr.Code != http.StatusUnauthorized { - t.Errorf("Expected 401, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusUnauthorized, rr.Code) } } @@ -356,7 +356,7 @@ func TestApiAuthStatusHandlerAuthenticated(t *testing.T) { rr := httptest.NewRecorder() apiAuthStatusHandler(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusOK, rr.Code) } body := rr.Body.String() if body != `{"status":"ok", "authenticated":true}` { @@ -396,7 +396,7 @@ func TestApiAuthStatusHandlerUnauthenticated(t *testing.T) { rr := httptest.NewRecorder() apiAuthStatusHandler(rr, req) if rr.Code != http.StatusUnauthorized { - t.Errorf("Expected 401, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusUnauthorized, rr.Code) } body := rr.Body.String() if body != `{"status":"error", "authenticated":false}` { @@ -411,7 +411,7 @@ func TestLoginHandlerGet(t *testing.T) { // should return login.html from rice box if rr.Code != http.StatusOK && rr.Code != http.StatusNotFound { - t.Errorf("Expected 200 or 404, got %d", rr.Code) + t.Errorf("Expected %d or %d, got %d", http.StatusOK, http.StatusNotFound, rr.Code) } } @@ -474,7 +474,7 @@ func TestGzipCompression(t *testing.T) { handler304.ServeHTTP(rr, req) if rr.Code != http.StatusNotModified { - t.Errorf("Expected 304, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusNotModified, rr.Code) } if rr.Header().Get("Content-Encoding") == "gzip" { t.Error("Expected no Content-Encoding for 304 response") @@ -494,7 +494,7 @@ func TestNewRouter(t *testing.T) { // Should be unauthorized but the route should be found if rr.Code != http.StatusUnauthorized { - t.Errorf("Expected 401 for unauthorized /api/auth, got %d", rr.Code) + t.Errorf("Expected %d for unauthorized /api/auth, got %d", http.StatusUnauthorized, rr.Code) } } @@ -509,7 +509,7 @@ func TestIndexHandlerRedirect(t *testing.T) { // Should redirect to login since not authenticated if rr.Code != http.StatusTemporaryRedirect { - t.Errorf("Expected 307 redirect for unauthenticated root, got %d", rr.Code) + t.Errorf("Expected %d redirect for unauthenticated root, got %d", http.StatusTemporaryRedirect, rr.Code) } } @@ -520,7 +520,7 @@ func TestServeFrontendEdgeCases(t *testing.T) { handler := http.StripPrefix("/v2/", http.HandlerFunc(ServeFrontend)) handler.ServeHTTP(rr, req) if rr.Code != http.StatusNotFound { - t.Errorf("Expected 404 for missing asset, got %d", rr.Code) + t.Errorf("Expected %d for missing asset, got %d", http.StatusNotFound, rr.Code) } // 2. Missing file without extension should serve index.html (or 404 if index.html missing) @@ -529,7 +529,7 @@ func TestServeFrontendEdgeCases(t *testing.T) { handler.ServeHTTP(rr, req) // We check for 200 or 404 depending on if index.html is in the box if rr.Code != http.StatusOK && rr.Code != http.StatusNotFound { - t.Errorf("Expected 200 or 404 for client route, got %d", rr.Code) + t.Errorf("Expected %d or %d, got %d", http.StatusOK, http.StatusNotFound, rr.Code) } } @@ -545,10 +545,10 @@ func TestGzipMiddlewareStatusCodes(t *testing.T) { handler.ServeHTTP(rr, req) if rr.Code != http.StatusCreated { - t.Errorf("Expected 201, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusCreated, rr.Code) } if rr.Header().Get("Content-Encoding") != "gzip" { - t.Error("Expected gzip encoding even for 201 Created") + t.Error("Expected gzip encoding even for http.StatusCreated") } } @@ -564,11 +564,11 @@ func TestGzipMiddlewareErrorStatus(t *testing.T) { handler.ServeHTTP(rr, req) if rr.Code != http.StatusNotFound { - t.Errorf("Expected 404, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusNotFound, rr.Code) } // Currently we gzip anything compressible regardless of status if rr.Header().Get("Content-Encoding") != "gzip" { - t.Error("Expected gzip encoding even for 404 (current behavior)") + t.Error("Expected gzip encoding even for http.StatusNotFound (current behavior)") } } @@ -616,7 +616,7 @@ func TestImageProxyHandlerMissingURL(t *testing.T) { rr := httptest.NewRecorder() imageProxyHandler(rr, req) if rr.Code != http.StatusNotFound { - t.Errorf("Expected 404, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusNotFound, rr.Code) } } @@ -625,7 +625,7 @@ func TestImageProxyHandlerInvalidBase64(t *testing.T) { rr := httptest.NewRecorder() imageProxyHandler(rr, req) if rr.Code != http.StatusNotFound { - t.Errorf("Expected 404, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusNotFound, rr.Code) } } @@ -635,7 +635,7 @@ func TestServeFrontendNotFound(t *testing.T) { ServeFrontend(rr, req) // Should fallback to index.html if it's not a dot-extension file if rr.Code != http.StatusOK { - t.Errorf("Expected 200 (fallback to index.html), got %d", rr.Code) + t.Errorf("Expected %d (fallback to index.html), got %d", http.StatusOK, rr.Code) } } @@ -649,7 +649,7 @@ func TestImageProxyHeaders(t *testing.T) { rr := httptest.NewRecorder() imageProxyHandler(rr, req) if rr.Code != http.StatusNotModified { - t.Errorf("Expected 304 for If-None-Match, got %d", rr.Code) + t.Errorf("Expected %d for If-None-Match, got %d", http.StatusNotModified, rr.Code) } // Test Etag @@ -658,7 +658,7 @@ func TestImageProxyHeaders(t *testing.T) { rr = httptest.NewRecorder() imageProxyHandler(rr, req) if rr.Code != http.StatusNotModified { - t.Errorf("Expected 304 for Etag, got %d", rr.Code) + t.Errorf("Expected %d for Etag, got %d", http.StatusNotModified, rr.Code) } } @@ -667,7 +667,7 @@ func TestServeFrontendAssetNotFound(t *testing.T) { rr := httptest.NewRecorder() ServeFrontend(rr, req) if rr.Code != http.StatusNotFound { - t.Errorf("Expected 404 for missing asset, got %d", rr.Code) + t.Errorf("Expected %d for missing asset, got %d", http.StatusNotFound, rr.Code) } } @@ -676,7 +676,7 @@ func TestServeBoxedFileNotFound(t *testing.T) { rr := httptest.NewRecorder() serveBoxedFile(rr, req, "nonexistent") if rr.Code != http.StatusNotFound { - t.Errorf("Expected 404 for nonexistent file, got %d", rr.Code) + t.Errorf("Expected %d for nonexistent file, got %d", http.StatusNotFound, rr.Code) } } @@ -689,7 +689,7 @@ func TestImageProxyHandlerHeaders(t *testing.T) { rr := httptest.NewRecorder() imageProxyHandler(rr, req) if rr.Code != http.StatusNotModified { - t.Errorf("Expected 304 for matching Etag, got %d", rr.Code) + t.Errorf("Expected %d for matching Etag, got %d", http.StatusNotModified, rr.Code) } } @@ -707,8 +707,8 @@ func TestImageProxyHandlerRemoteError(t *testing.T) { req := httptest.NewRequest("GET", "/"+id, nil) rr := httptest.NewRecorder() imageProxyHandler(rr, req) - if rr.Code != 404 { - t.Errorf("Expected 404 for remote error, got %d", rr.Code) + if rr.Code != http.StatusNotFound { + t.Errorf("Expected %d for remote error, got %d", http.StatusNotFound, rr.Code) } } @@ -717,7 +717,7 @@ func TestApiLoginHandlerBadMethod(t *testing.T) { rr := httptest.NewRecorder() apiLoginHandler(rr, req) if rr.Code != http.StatusMethodNotAllowed { - t.Errorf("Expected 405, got %d", rr.Code) + t.Errorf("Expected %d, got %d", http.StatusMethodNotAllowed, rr.Code) } } @@ -748,7 +748,7 @@ func TestCSRFMiddleware(t *testing.T) { rr := httptest.NewRecorder() handler.ServeHTTP(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200 for GET, got %d", rr.Code) + t.Errorf("Expected %d for GET, got %d", http.StatusOK, rr.Code) } cookies := rr.Result().Cookies() var csrfCookie *http.Cookie @@ -768,7 +768,7 @@ func TestCSRFMiddleware(t *testing.T) { rr = httptest.NewRecorder() handler.ServeHTTP(rr, req) if rr.Code != http.StatusForbidden { - t.Errorf("Expected 403 for POST without token, got %d", rr.Code) + t.Errorf("Expected %d for POST without token, got %d", http.StatusForbidden, rr.Code) } // Case 3: POST with valid token should succeed @@ -778,7 +778,7 @@ func TestCSRFMiddleware(t *testing.T) { rr = httptest.NewRecorder() handler.ServeHTTP(rr, req) if rr.Code != http.StatusOK { - t.Errorf("Expected 200 for POST with valid token, got %d", rr.Code) + t.Errorf("Expected %d for POST with valid token, got %d", http.StatusOK, rr.Code) } } -- cgit v1.2.3