Skip to content

Commit 0e32603

Browse files
CopilotJoannaaKL
andcommitted
Migrate gists_test.go to testify mocks
Co-authored-by: JoannaaKL <67866556+JoannaaKL@users.noreply.github.com>
1 parent 6d01033 commit 0e32603

File tree

1 file changed

+43
-77
lines changed

1 file changed

+43
-77
lines changed

pkg/github/gists_test.go

Lines changed: 43 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/github/github-mcp-server/pkg/translations"
1212
"github.com/google/go-github/v79/github"
1313
"github.com/google/jsonschema-go/jsonschema"
14-
"github.com/migueleliasweb/go-github-mock/src/mock"
1514
"github.com/stretchr/testify/assert"
1615
"github.com/stretchr/testify/require"
1716
)
@@ -77,24 +76,18 @@ func Test_ListGists(t *testing.T) {
7776
}{
7877
{
7978
name: "list authenticated user's gists",
80-
mockedClient: mock.NewMockedHTTPClient(
81-
mock.WithRequestMatch(
82-
mock.GetGists,
83-
mockGists,
84-
),
85-
),
79+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
80+
GetGists: mockResponse(t, http.StatusOK, mockGists),
81+
}),
8682
requestArgs: map[string]interface{}{},
8783
expectError: false,
8884
expectedGists: mockGists,
8985
},
9086
{
9187
name: "list specific user's gists",
92-
mockedClient: mock.NewMockedHTTPClient(
93-
mock.WithRequestMatchHandler(
94-
mock.GetUsersGistsByUsername,
95-
mockResponse(t, http.StatusOK, mockGists),
96-
),
97-
),
88+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
89+
GetUsersGistsByUsername: mockResponse(t, http.StatusOK, mockGists),
90+
}),
9891
requestArgs: map[string]interface{}{
9992
"username": "testuser",
10093
},
@@ -103,18 +96,15 @@ func Test_ListGists(t *testing.T) {
10396
},
10497
{
10598
name: "list gists with pagination and since parameter",
106-
mockedClient: mock.NewMockedHTTPClient(
107-
mock.WithRequestMatchHandler(
108-
mock.GetGists,
109-
expectQueryParams(t, map[string]string{
110-
"since": "2023-01-01T00:00:00Z",
111-
"page": "2",
112-
"per_page": "5",
113-
}).andThen(
114-
mockResponse(t, http.StatusOK, mockGists),
115-
),
99+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
100+
GetGists: expectQueryParams(t, map[string]string{
101+
"since": "2023-01-01T00:00:00Z",
102+
"page": "2",
103+
"per_page": "5",
104+
}).andThen(
105+
mockResponse(t, http.StatusOK, mockGists),
116106
),
117-
),
107+
}),
118108
requestArgs: map[string]interface{}{
119109
"since": "2023-01-01T00:00:00Z",
120110
"page": float64(2),
@@ -125,12 +115,9 @@ func Test_ListGists(t *testing.T) {
125115
},
126116
{
127117
name: "invalid since parameter",
128-
mockedClient: mock.NewMockedHTTPClient(
129-
mock.WithRequestMatch(
130-
mock.GetGists,
131-
mockGists,
132-
),
133-
),
118+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
119+
GetGists: mockResponse(t, http.StatusOK, mockGists),
120+
}),
134121
requestArgs: map[string]interface{}{
135122
"since": "invalid-date",
136123
},
@@ -139,15 +126,12 @@ func Test_ListGists(t *testing.T) {
139126
},
140127
{
141128
name: "list gists fails with error",
142-
mockedClient: mock.NewMockedHTTPClient(
143-
mock.WithRequestMatchHandler(
144-
mock.GetGists,
145-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
129+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
130+
GetGists: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
146131
w.WriteHeader(http.StatusUnauthorized)
147132
_, _ = w.Write([]byte(`{"message": "Requires authentication"}`))
148133
}),
149-
),
150-
),
134+
}),
151135
requestArgs: map[string]interface{}{},
152136
expectError: true,
153137
expectedErrMsg: "failed to list gists",
@@ -242,12 +226,9 @@ func Test_GetGist(t *testing.T) {
242226
}{
243227
{
244228
name: "Successful fetching different gist",
245-
mockedClient: mock.NewMockedHTTPClient(
246-
mock.WithRequestMatchHandler(
247-
mock.GetGistsByGistId,
248-
mockResponse(t, http.StatusOK, mockGist),
249-
),
250-
),
229+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
230+
GetGistsByGistID: mockResponse(t, http.StatusOK, mockGist),
231+
}),
251232
requestArgs: map[string]interface{}{
252233
"gist_id": "gist1",
253234
},
@@ -256,15 +237,12 @@ func Test_GetGist(t *testing.T) {
256237
},
257238
{
258239
name: "gist_id parameter missing",
259-
mockedClient: mock.NewMockedHTTPClient(
260-
mock.WithRequestMatchHandler(
261-
mock.GetGistsByGistId,
262-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
240+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
241+
GetGistsByGistID: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
263242
w.WriteHeader(http.StatusUnprocessableEntity)
264243
_, _ = w.Write([]byte(`{"message": "Invalid Request"}`))
265244
}),
266-
),
267-
),
245+
}),
268246
requestArgs: map[string]interface{}{},
269247
expectError: true,
270248
expectedErrMsg: "missing required parameter: gist_id",
@@ -361,12 +339,9 @@ func Test_CreateGist(t *testing.T) {
361339
}{
362340
{
363341
name: "create gist successfully",
364-
mockedClient: mock.NewMockedHTTPClient(
365-
mock.WithRequestMatchHandler(
366-
mock.PostGists,
367-
mockResponse(t, http.StatusCreated, createdGist),
368-
),
369-
),
342+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
343+
PostGists: mockResponse(t, http.StatusCreated, createdGist),
344+
}),
370345
requestArgs: map[string]interface{}{
371346
"filename": "test.go",
372347
"content": "package main\n\nfunc main() {\n\tfmt.Println(\"Hello, Gist!\")\n}",
@@ -378,7 +353,7 @@ func Test_CreateGist(t *testing.T) {
378353
},
379354
{
380355
name: "missing required filename",
381-
mockedClient: mock.NewMockedHTTPClient(),
356+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{}),
382357
requestArgs: map[string]interface{}{
383358
"content": "test content",
384359
"description": "Test Gist",
@@ -388,7 +363,7 @@ func Test_CreateGist(t *testing.T) {
388363
},
389364
{
390365
name: "missing required content",
391-
mockedClient: mock.NewMockedHTTPClient(),
366+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{}),
392367
requestArgs: map[string]interface{}{
393368
"filename": "test.go",
394369
"description": "Test Gist",
@@ -398,15 +373,12 @@ func Test_CreateGist(t *testing.T) {
398373
},
399374
{
400375
name: "api returns error",
401-
mockedClient: mock.NewMockedHTTPClient(
402-
mock.WithRequestMatchHandler(
403-
mock.PostGists,
404-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
376+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
377+
PostGists: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
405378
w.WriteHeader(http.StatusUnauthorized)
406379
_, _ = w.Write([]byte(`{"message": "Requires authentication"}`))
407380
}),
408-
),
409-
),
381+
}),
410382
requestArgs: map[string]interface{}{
411383
"filename": "test.go",
412384
"content": "package main",
@@ -506,12 +478,9 @@ func Test_UpdateGist(t *testing.T) {
506478
}{
507479
{
508480
name: "update gist successfully",
509-
mockedClient: mock.NewMockedHTTPClient(
510-
mock.WithRequestMatchHandler(
511-
mock.PatchGistsByGistId,
512-
mockResponse(t, http.StatusOK, updatedGist),
513-
),
514-
),
481+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
482+
PatchGistsByGistID: mockResponse(t, http.StatusOK, updatedGist),
483+
}),
515484
requestArgs: map[string]interface{}{
516485
"gist_id": "existing-gist-id",
517486
"filename": "updated.go",
@@ -523,7 +492,7 @@ func Test_UpdateGist(t *testing.T) {
523492
},
524493
{
525494
name: "missing required gist_id",
526-
mockedClient: mock.NewMockedHTTPClient(),
495+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{}),
527496
requestArgs: map[string]interface{}{
528497
"filename": "updated.go",
529498
"content": "updated content",
@@ -534,7 +503,7 @@ func Test_UpdateGist(t *testing.T) {
534503
},
535504
{
536505
name: "missing required filename",
537-
mockedClient: mock.NewMockedHTTPClient(),
506+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{}),
538507
requestArgs: map[string]interface{}{
539508
"gist_id": "existing-gist-id",
540509
"content": "updated content",
@@ -545,7 +514,7 @@ func Test_UpdateGist(t *testing.T) {
545514
},
546515
{
547516
name: "missing required content",
548-
mockedClient: mock.NewMockedHTTPClient(),
517+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{}),
549518
requestArgs: map[string]interface{}{
550519
"gist_id": "existing-gist-id",
551520
"filename": "updated.go",
@@ -556,15 +525,12 @@ func Test_UpdateGist(t *testing.T) {
556525
},
557526
{
558527
name: "api returns error",
559-
mockedClient: mock.NewMockedHTTPClient(
560-
mock.WithRequestMatchHandler(
561-
mock.PatchGistsByGistId,
562-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
528+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
529+
PatchGistsByGistID: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
563530
w.WriteHeader(http.StatusNotFound)
564531
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
565532
}),
566-
),
567-
),
533+
}),
568534
requestArgs: map[string]interface{}{
569535
"gist_id": "nonexistent-gist-id",
570536
"filename": "updated.go",

0 commit comments

Comments
 (0)