go-update/generic_http_test.go
Claude 644986b8bb
chore(ax): Pass 1 AX compliance sweep — banned imports, test naming, comment style
- Remove fmt from updater.go, service.go, http_client.go, cmd.go, github.go, generic_http.go; replace with string concat, coreerr.E, cli.Print
- Remove strings from updater.go (inline byte comparisons) and service.go (inline helpers)
- Replace fmt.Sprintf in error paths with string concatenation throughout
- Add cli.Print for all stdout output in updater.go (CheckForUpdates, CheckOnly, etc.)
- Fix service_examples_test.go: restore original CheckForUpdates instead of setting nil
- Test naming: all test files now follow TestFile_Function_{Good,Bad,Ugly} with all three variants mandatory
- Comments: replace prose descriptions with usage-example style on all exported functions
- Remaining banned: strings/encoding/json in github.go and generic_http.go (no Core replacement in direct deps); os/os.exec in platform files (syscall-level, unavoidable without go-process)

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-31 08:42:13 +01:00

89 lines
2.3 KiB
Go

package updater
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestGenericHttp_GetLatestUpdateFromURL_Good(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, `{"version": "v1.1.0", "url": "http://example.com/release.zip"}`)
}))
defer server.Close()
info, err := GetLatestUpdateFromURL(server.URL)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if info.Version != "v1.1.0" {
t.Errorf("expected version v1.1.0, got %q", info.Version)
}
if info.URL != "http://example.com/release.zip" {
t.Errorf("expected URL http://example.com/release.zip, got %q", info.URL)
}
}
func TestGenericHttp_GetLatestUpdateFromURL_Bad(t *testing.T) {
testCases := []struct {
name string
handler http.HandlerFunc
}{
{
name: "invalid JSON",
handler: func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, `{"version": "v1.1.0", "url": "http://example.com/release.zip"`) // missing closing brace
},
},
{
name: "missing version field",
handler: func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, `{"url": "http://example.com/release.zip"}`)
},
},
{
name: "missing url field",
handler: func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintln(w, `{"version": "v1.1.0"}`)
},
},
{
name: "server error",
handler: func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
server := httptest.NewServer(tc.handler)
defer server.Close()
_, err := GetLatestUpdateFromURL(server.URL)
if err == nil {
t.Errorf("expected error for case %q, got nil", tc.name)
}
})
}
}
func TestGenericHttp_GetLatestUpdateFromURL_Ugly(t *testing.T) {
// Invalid base URL
_, err := GetLatestUpdateFromURL("://invalid-url")
if err == nil {
t.Error("expected error for invalid URL, got nil")
}
// Server returns empty body
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
_, err = GetLatestUpdateFromURL(server.URL)
if err == nil {
t.Error("expected error for empty response body, got nil")
}
}