From 84e4798a0c9e37a9470aeecb3b451ec25f0e7b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Thu, 28 Nov 2024 13:47:02 +0100 Subject: [PATCH] Add test for connection failure --- internal/http/fetch_grant_token_test.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/internal/http/fetch_grant_token_test.go b/internal/http/fetch_grant_token_test.go index 688ab0f..5430fb4 100644 --- a/internal/http/fetch_grant_token_test.go +++ b/internal/http/fetch_grant_token_test.go @@ -94,10 +94,10 @@ func TestFetchGrantToken(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - server := httptest.NewServer(http.HandlerFunc(tt.serverFn)) - defer server.Close() + srv := httptest.NewServer(http.HandlerFunc(tt.serverFn)) + defer srv.Close() - token, err := FetchGrantToken(server.URL, tt.authToken) + token, err := FetchGrantToken(srv.URL, tt.authToken) hasErr := err != nil if hasErr != tt.wantErr { @@ -154,3 +154,21 @@ func TestFetchGrantTokenRetry(t *testing.T) { t.Errorf("Expected 2 attempts, got %d", tryCount) } } + +func TestFetchGrantTokenConnectionFailure(t *testing.T) { + invalidServerURL := "http://localhost:1" + + token, err := FetchGrantToken(invalidServerURL, "test-token") + + if err == nil { + t.Error("Expected error for connection failure, got nil") + } + + if !strings.Contains(err.Error(), "connection refused") { + t.Errorf("Expected error containing 'connection refused', got %v", err) + } + + if token != "" { + t.Errorf("Expected empty token for failed connection, got %q", token) + } +}