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 \n This 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 \n This 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 \n This 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 \n This 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 \n func 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 \n This 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 \n This 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 \n This 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 \n This 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 \n This 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