Skip to content

Commit

Permalink
Add test for connection failure
Browse files Browse the repository at this point in the history
  • Loading branch information
ivov committed Nov 28, 2024
1 parent 69b86d3 commit 84e4798
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions internal/http/fetch_grant_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 84e4798

Please sign in to comment.