Skip to content

Commit acf0be5

Browse files
CopilotJoannaaKL
andcommitted
Migrate context_tools_test and repository_resource_test to testify
Co-authored-by: JoannaaKL <67866556+JoannaaKL@users.noreply.github.com>
1 parent 3beeef9 commit acf0be5

File tree

2 files changed

+60
-102
lines changed

2 files changed

+60
-102
lines changed

pkg/github/context_tools_test.go

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/github/github-mcp-server/internal/toolsnaps"
1212
"github.com/github/github-mcp-server/pkg/translations"
1313
"github.com/google/go-github/v79/github"
14-
"github.com/migueleliasweb/go-github-mock/src/mock"
1514
"github.com/shurcooL/githubv4"
1615
"github.com/stretchr/testify/assert"
1716
"github.com/stretchr/testify/require"
@@ -57,24 +56,18 @@ func Test_GetMe(t *testing.T) {
5756
}{
5857
{
5958
name: "successful get user",
60-
mockedClient: mock.NewMockedHTTPClient(
61-
mock.WithRequestMatch(
62-
mock.GetUser,
63-
mockUser,
64-
),
65-
),
59+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
60+
GetUser: mockResponse(t, http.StatusOK, mockUser),
61+
}),
6662
requestArgs: map[string]any{},
6763
expectToolError: false,
6864
expectedUser: mockUser,
6965
},
7066
{
7167
name: "successful get user with reason",
72-
mockedClient: mock.NewMockedHTTPClient(
73-
mock.WithRequestMatch(
74-
mock.GetUser,
75-
mockUser,
76-
),
77-
),
68+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
69+
GetUser: mockResponse(t, http.StatusOK, mockUser),
70+
}),
7871
requestArgs: map[string]any{
7972
"reason": "Testing API",
8073
},
@@ -90,12 +83,9 @@ func Test_GetMe(t *testing.T) {
9083
},
9184
{
9285
name: "get user fails",
93-
mockedClient: mock.NewMockedHTTPClient(
94-
mock.WithRequestMatchHandler(
95-
mock.GetUser,
96-
badRequestHandler("expected test failure"),
97-
),
98-
),
86+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
87+
GetUser: badRequestHandler("expected test failure"),
88+
}),
9989
requestArgs: map[string]any{},
10090
expectToolError: true,
10191
expectedToolErrMsg: "expected test failure",
@@ -255,21 +245,15 @@ func Test_GetTeams(t *testing.T) {
255245

256246
// Factory function for mock HTTP clients with user response
257247
httpClientWithUser := func() *http.Client {
258-
return mock.NewMockedHTTPClient(
259-
mock.WithRequestMatch(
260-
mock.GetUser,
261-
mockUser,
262-
),
263-
)
248+
return MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
249+
GetUser: mockResponse(t, http.StatusOK, mockUser),
250+
})
264251
}
265252

266253
httpClientUserFails := func() *http.Client {
267-
return mock.NewMockedHTTPClient(
268-
mock.WithRequestMatchHandler(
269-
mock.GetUser,
270-
badRequestHandler("expected test failure"),
271-
),
272-
)
254+
return MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
255+
GetUser: badRequestHandler("expected test failure"),
256+
})
273257
}
274258

275259
tests := []struct {

pkg/github/repository_resource_test.go

Lines changed: 45 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/github/github-mcp-server/pkg/raw"
1010
"github.com/google/go-github/v79/github"
11-
"github.com/migueleliasweb/go-github-mock/src/mock"
1211
"github.com/modelcontextprotocol/go-sdk/mcp"
1312
"github.com/stretchr/testify/require"
1413
)
@@ -34,16 +33,14 @@ func Test_repositoryResourceContents(t *testing.T) {
3433
}{
3534
{
3635
name: "missing owner",
37-
mockedClient: mock.NewMockedHTTPClient(
38-
mock.WithRequestMatchHandler(
39-
raw.GetRawReposContentsByOwnerByRepoByPath,
36+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
37+
GetRawReposContentsByOwnerByRepoByPath:
4038
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
4139
w.Header().Set("Content-Type", "text/markdown")
4240
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
4341
require.NoError(t, err)
4442
}),
45-
),
46-
),
43+
}),
4744
uri: "repo:///repo/contents/README.md",
4845
handlerFn: func(deps ToolDependencies) mcp.ResourceHandler {
4946
return RepositoryResourceContentsHandler(deps, repositoryResourceContentURITemplate)
@@ -53,16 +50,14 @@ func Test_repositoryResourceContents(t *testing.T) {
5350
},
5451
{
5552
name: "missing repo",
56-
mockedClient: mock.NewMockedHTTPClient(
57-
mock.WithRequestMatchHandler(
58-
raw.GetRawReposContentsByOwnerByRepoByBranchByPath,
53+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
54+
GetRawReposContentsByOwnerByRepoByBranchByPath:
5955
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
6056
w.Header().Set("Content-Type", "text/markdown")
6157
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
6258
require.NoError(t, err)
6359
}),
64-
),
65-
),
60+
}),
6661
uri: "repo://owner//refs/heads/main/contents/README.md",
6762
handlerFn: func(deps ToolDependencies) mcp.ResourceHandler {
6863
return RepositoryResourceContentsHandler(deps, repositoryResourceBranchContentURITemplate)
@@ -72,16 +67,14 @@ func Test_repositoryResourceContents(t *testing.T) {
7267
},
7368
{
7469
name: "successful blob content fetch",
75-
mockedClient: mock.NewMockedHTTPClient(
76-
mock.WithRequestMatchHandler(
77-
raw.GetRawReposContentsByOwnerByRepoByPath,
70+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
71+
GetRawReposContentsByOwnerByRepoByPath:
7872
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
7973
w.Header().Set("Content-Type", "image/png")
8074
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
8175
require.NoError(t, err)
8276
}),
83-
),
84-
),
77+
}),
8578
uri: "repo://owner/repo/contents/data.png",
8679
handlerFn: func(deps ToolDependencies) mcp.ResourceHandler {
8780
return RepositoryResourceContentsHandler(deps, repositoryResourceContentURITemplate)
@@ -96,16 +89,14 @@ func Test_repositoryResourceContents(t *testing.T) {
9689
},
9790
{
9891
name: "successful text content fetch (HEAD)",
99-
mockedClient: mock.NewMockedHTTPClient(
100-
mock.WithRequestMatchHandler(
101-
raw.GetRawReposContentsByOwnerByRepoByPath,
92+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
93+
GetRawReposContentsByOwnerByRepoByPath:
10294
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
10395
w.Header().Set("Content-Type", "text/markdown")
10496
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
10597
require.NoError(t, err)
10698
}),
107-
),
108-
),
99+
}),
109100
uri: "repo://owner/repo/contents/README.md",
110101
handlerFn: func(deps ToolDependencies) mcp.ResourceHandler {
111102
return RepositoryResourceContentsHandler(deps, repositoryResourceContentURITemplate)
@@ -120,18 +111,16 @@ func Test_repositoryResourceContents(t *testing.T) {
120111
},
121112
{
122113
name: "successful text content fetch (HEAD)",
123-
mockedClient: mock.NewMockedHTTPClient(
124-
mock.WithRequestMatchHandler(
125-
raw.GetRawReposContentsByOwnerByRepoByPath,
114+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
115+
GetRawReposContentsByOwnerByRepoByPath:
126116
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
127117
w.Header().Set("Content-Type", "text/plain")
128118

129119
require.Contains(t, r.URL.Path, "pkg/github/actions.go")
130120
_, err := w.Write([]byte("package actions\n\nfunc main() {\n // Sample Go file content\n}\n"))
131121
require.NoError(t, err)
132122
}),
133-
),
134-
),
123+
}),
135124
uri: "repo://owner/repo/contents/pkg/github/actions.go",
136125
handlerFn: func(deps ToolDependencies) mcp.ResourceHandler {
137126
return RepositoryResourceContentsHandler(deps, repositoryResourceContentURITemplate)
@@ -146,16 +135,14 @@ func Test_repositoryResourceContents(t *testing.T) {
146135
},
147136
{
148137
name: "successful text content fetch (branch)",
149-
mockedClient: mock.NewMockedHTTPClient(
150-
mock.WithRequestMatchHandler(
151-
raw.GetRawReposContentsByOwnerByRepoByBranchByPath,
138+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
139+
GetRawReposContentsByOwnerByRepoByBranchByPath:
152140
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
153141
w.Header().Set("Content-Type", "text/markdown")
154142
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
155143
require.NoError(t, err)
156144
}),
157-
),
158-
),
145+
}),
159146
uri: "repo://owner/repo/refs/heads/main/contents/README.md",
160147
handlerFn: func(deps ToolDependencies) mcp.ResourceHandler {
161148
return RepositoryResourceContentsHandler(deps, repositoryResourceBranchContentURITemplate)
@@ -170,16 +157,14 @@ func Test_repositoryResourceContents(t *testing.T) {
170157
},
171158
{
172159
name: "successful text content fetch (tag)",
173-
mockedClient: mock.NewMockedHTTPClient(
174-
mock.WithRequestMatchHandler(
175-
raw.GetRawReposContentsByOwnerByRepoByTagByPath,
160+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
161+
GetRawReposContentsByOwnerByRepoByTagByPath:
176162
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
177163
w.Header().Set("Content-Type", "text/markdown")
178164
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
179165
require.NoError(t, err)
180166
}),
181-
),
182-
),
167+
}),
183168
uri: "repo://owner/repo/refs/tags/v1.0.0/contents/README.md",
184169
handlerFn: func(deps ToolDependencies) mcp.ResourceHandler {
185170
return RepositoryResourceContentsHandler(deps, repositoryResourceTagContentURITemplate)
@@ -194,16 +179,14 @@ func Test_repositoryResourceContents(t *testing.T) {
194179
},
195180
{
196181
name: "successful text content fetch (sha)",
197-
mockedClient: mock.NewMockedHTTPClient(
198-
mock.WithRequestMatchHandler(
199-
raw.GetRawReposContentsByOwnerByRepoBySHAByPath,
182+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
183+
GetRawReposContentsByOwnerByRepoBySHAByPath:
200184
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
201185
w.Header().Set("Content-Type", "text/markdown")
202186
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
203187
require.NoError(t, err)
204188
}),
205-
),
206-
),
189+
}),
207190
uri: "repo://owner/repo/sha/abc123/contents/README.md",
208191
handlerFn: func(deps ToolDependencies) mcp.ResourceHandler {
209192
return RepositoryResourceContentsHandler(deps, repositoryResourceCommitContentURITemplate)
@@ -218,24 +201,18 @@ func Test_repositoryResourceContents(t *testing.T) {
218201
},
219202
{
220203
name: "successful text content fetch (pr)",
221-
mockedClient: mock.NewMockedHTTPClient(
222-
mock.WithRequestMatchHandler(
223-
mock.GetReposPullsByOwnerByRepoByPullNumber,
224-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
225-
w.Header().Set("Content-Type", "application/json")
226-
_, err := w.Write([]byte(`{"head": {"sha": "abc123"}}`))
227-
require.NoError(t, err)
228-
}),
229-
),
230-
mock.WithRequestMatchHandler(
231-
raw.GetRawReposContentsByOwnerByRepoBySHAByPath,
232-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
233-
w.Header().Set("Content-Type", "text/markdown")
234-
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
235-
require.NoError(t, err)
236-
}),
237-
),
238-
),
204+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
205+
GetReposPullsByOwnerByRepoByPullNumber: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
206+
w.Header().Set("Content-Type", "application/json")
207+
_, err := w.Write([]byte(`{"head": {"sha": "abc123"}}`))
208+
require.NoError(t, err)
209+
}),
210+
GetRawReposContentsByOwnerByRepoBySHAByPath: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
211+
w.Header().Set("Content-Type", "text/markdown")
212+
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
213+
require.NoError(t, err)
214+
}),
215+
}),
239216
uri: "repo://owner/repo/refs/pull/42/head/contents/README.md",
240217
handlerFn: func(deps ToolDependencies) mcp.ResourceHandler {
241218
return RepositoryResourceContentsHandler(deps, repositoryResourcePrContentURITemplate)
@@ -250,15 +227,12 @@ func Test_repositoryResourceContents(t *testing.T) {
250227
},
251228
{
252229
name: "content fetch fails",
253-
mockedClient: mock.NewMockedHTTPClient(
254-
mock.WithRequestMatchHandler(
255-
mock.GetReposContentsByOwnerByRepoByPath,
256-
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
257-
w.WriteHeader(http.StatusNotFound)
258-
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
259-
}),
260-
),
261-
),
230+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
231+
GetReposContentsByOwnerByRepoByPath: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
232+
w.WriteHeader(http.StatusNotFound)
233+
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
234+
}),
235+
}),
262236
uri: "repo://owner/repo/contents/nonexistent.md",
263237
handlerFn: func(deps ToolDependencies) mcp.ResourceHandler {
264238
return RepositoryResourceContentsHandler(deps, repositoryResourceContentURITemplate)
@@ -295,11 +269,11 @@ func Test_repositoryResourceContents(t *testing.T) {
295269

296270
content := resp.Contents[0]
297271
switch tc.expectedResponseType {
298-
case resourceResponseTypeBlob:
272+
case resourceResponseTypeBlob:
299273
require.Equal(t, tc.expectedResult.Contents[0].Blob, content.Blob)
300-
case resourceResponseTypeText:
274+
case resourceResponseTypeText:
301275
require.Equal(t, tc.expectedResult.Contents[0].Text, content.Text)
302-
default:
276+
default:
303277
t.Fatalf("unknown expectedResponseType %v", tc.expectedResponseType)
304278
}
305279
})

0 commit comments

Comments
 (0)