From e3afc4dae496ea312fff8ccba110e7ff9faa2240 Mon Sep 17 00:00:00 2001 From: Lucas Date: Fri, 19 Sep 2025 05:01:16 +0200 Subject: [PATCH] test(gist): add tests for `GetRawGistFile` function - Implemented unit tests for various scenarios in `GetRawGistFile`. - Covered cases including successful requests, error handling, and different content types. - Ensured proper verification of HTTP responses and error messages. --- pkg/cmd/gist/shared/shared_test.go | 95 ++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/pkg/cmd/gist/shared/shared_test.go b/pkg/cmd/gist/shared/shared_test.go index 0bc1e1f11..d75ebc2b7 100644 --- a/pkg/cmd/gist/shared/shared_test.go +++ b/pkg/cmd/gist/shared/shared_test.go @@ -219,3 +219,98 @@ func TestPromptGists(t *testing.T) { }) } } + +func TestGetRawGistFile(t *testing.T) { + tests := []struct { + name string + response string + statusCode int + want string + wantErr bool + errContains string + }{ + { + name: "successful request", + response: "Hello, World!", + statusCode: http.StatusOK, + want: "Hello, World!", + wantErr: false, + }, + { + name: "empty response", + response: "", + statusCode: http.StatusOK, + want: "", + wantErr: false, + }, + { + name: "not found error", + response: "Not Found", + statusCode: http.StatusNotFound, + want: "", + wantErr: true, + errContains: "HTTP 404", + }, + { + name: "server error", + response: "Internal Server Error", + statusCode: http.StatusInternalServerError, + want: "", + wantErr: true, + errContains: "HTTP 500", + }, + { + name: "large content", + response: "This is a very large file content with multiple lines\nLine 2\nLine 3\nAnd more content...", + statusCode: http.StatusOK, + want: "This is a very large file content with multiple lines\nLine 2\nLine 3\nAnd more content...", + wantErr: false, + }, + { + name: "special characters", + response: "Special chars: àáâãäåæçèéêë 中文 🎉 \"quotes\" 'single'", + statusCode: http.StatusOK, + want: "Special chars: àáâãäåæçèéêë 中文 🎉 \"quotes\" 'single'", + wantErr: false, + }, + { + name: "JSON content", + response: `{"name": "test", "version": "1.0.0", "dependencies": {"lodash": "^4.17.21"}}`, + statusCode: http.StatusOK, + want: `{"name": "test", "version": "1.0.0", "dependencies": {"lodash": "^4.17.21"}}`, + wantErr: false, + }, + { + name: "HTML content", + response: "Test

Hello

", + statusCode: http.StatusOK, + want: "Test

Hello

", + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + reg := &httpmock.Registry{} + reg.Register( + httpmock.REST("GET", "raw-url"), + httpmock.StatusStringResponse(tt.statusCode, tt.response), + ) + + client := &http.Client{Transport: reg} + result, err := GetRawGistFile(client, "https://gist.githubusercontent.com/raw-url") + + if tt.wantErr { + assert.Error(t, err) + if tt.errContains != "" { + assert.Contains(t, err.Error(), tt.errContains) + } + } else { + assert.NoError(t, err) + assert.Equal(t, tt.want, result) + } + + reg.Verify(t) + }) + } +}