test: Add JSON decode error test for Client.GetNodes

Tests the error path when server returns invalid JSON (87.5% to 100%).
This commit is contained in:
rcourtman 2025-12-02 01:03:30 +00:00
parent 7d3de1bbcc
commit fac03d3a52

View file

@ -1,10 +1,14 @@
package proxmox
import (
"context"
"encoding/json"
"fmt"
"math"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestDiskUnmarshalWearout(t *testing.T) {
@ -1252,3 +1256,32 @@ func TestGetMHzString(t *testing.T) {
})
}
}
func TestGetNodes_InvalidJSON(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
// Return invalid JSON that will cause decode error
fmt.Fprint(w, `{"data": [{"node": "test" invalid json}`)
}))
defer server.Close()
cfg := ClientConfig{
Host: server.URL,
TokenName: "test@pve!token",
TokenValue: "secret",
VerifySSL: false,
Timeout: 2 * time.Second,
}
client, err := NewClient(cfg)
if err != nil {
t.Fatalf("NewClient failed: %v", err)
}
ctx := context.Background()
_, err = client.GetNodes(ctx)
if err == nil {
t.Fatal("expected JSON decode error, got nil")
}
}