Skip to content

Commit e3afc4d

Browse files
committed
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.
1 parent bf27230 commit e3afc4d

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

pkg/cmd/gist/shared/shared_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,98 @@ func TestPromptGists(t *testing.T) {
219219
})
220220
}
221221
}
222+
223+
func TestGetRawGistFile(t *testing.T) {
224+
tests := []struct {
225+
name string
226+
response string
227+
statusCode int
228+
want string
229+
wantErr bool
230+
errContains string
231+
}{
232+
{
233+
name: "successful request",
234+
response: "Hello, World!",
235+
statusCode: http.StatusOK,
236+
want: "Hello, World!",
237+
wantErr: false,
238+
},
239+
{
240+
name: "empty response",
241+
response: "",
242+
statusCode: http.StatusOK,
243+
want: "",
244+
wantErr: false,
245+
},
246+
{
247+
name: "not found error",
248+
response: "Not Found",
249+
statusCode: http.StatusNotFound,
250+
want: "",
251+
wantErr: true,
252+
errContains: "HTTP 404",
253+
},
254+
{
255+
name: "server error",
256+
response: "Internal Server Error",
257+
statusCode: http.StatusInternalServerError,
258+
want: "",
259+
wantErr: true,
260+
errContains: "HTTP 500",
261+
},
262+
{
263+
name: "large content",
264+
response: "This is a very large file content with multiple lines\nLine 2\nLine 3\nAnd more content...",
265+
statusCode: http.StatusOK,
266+
want: "This is a very large file content with multiple lines\nLine 2\nLine 3\nAnd more content...",
267+
wantErr: false,
268+
},
269+
{
270+
name: "special characters",
271+
response: "Special chars: àáâãäåæçèéêë 中文 🎉 \"quotes\" 'single'",
272+
statusCode: http.StatusOK,
273+
want: "Special chars: àáâãäåæçèéêë 中文 🎉 \"quotes\" 'single'",
274+
wantErr: false,
275+
},
276+
{
277+
name: "JSON content",
278+
response: `{"name": "test", "version": "1.0.0", "dependencies": {"lodash": "^4.17.21"}}`,
279+
statusCode: http.StatusOK,
280+
want: `{"name": "test", "version": "1.0.0", "dependencies": {"lodash": "^4.17.21"}}`,
281+
wantErr: false,
282+
},
283+
{
284+
name: "HTML content",
285+
response: "<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Hello</h1></body></html>",
286+
statusCode: http.StatusOK,
287+
want: "<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Hello</h1></body></html>",
288+
wantErr: false,
289+
},
290+
}
291+
292+
for _, tt := range tests {
293+
t.Run(tt.name, func(t *testing.T) {
294+
reg := &httpmock.Registry{}
295+
reg.Register(
296+
httpmock.REST("GET", "raw-url"),
297+
httpmock.StatusStringResponse(tt.statusCode, tt.response),
298+
)
299+
300+
client := &http.Client{Transport: reg}
301+
result, err := GetRawGistFile(client, "https://gist.githubusercontent.com/raw-url")
302+
303+
if tt.wantErr {
304+
assert.Error(t, err)
305+
if tt.errContains != "" {
306+
assert.Contains(t, err.Error(), tt.errContains)
307+
}
308+
} else {
309+
assert.NoError(t, err)
310+
assert.Equal(t, tt.want, result)
311+
}
312+
313+
reg.Verify(t)
314+
})
315+
}
316+
}

0 commit comments

Comments
 (0)