-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathrepository_resource_test.go
More file actions
314 lines (295 loc) · 11.3 KB
/
repository_resource_test.go
File metadata and controls
314 lines (295 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package github
import (
"context"
"errors"
"net/http"
"net/url"
"testing"
"github.com/github/github-mcp-server/pkg/raw"
"github.com/google/go-github/v82/github"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/stretchr/testify/require"
)
// errorTransport is a http.RoundTripper that always returns an error.
type errorTransport struct {
err error
}
func (t *errorTransport) RoundTrip(*http.Request) (*http.Response, error) {
return nil, t.err
}
type resourceResponseType int
const (
resourceResponseTypeUnknown resourceResponseType = iota
resourceResponseTypeBlob
resourceResponseTypeText
)
func Test_repositoryResourceContents(t *testing.T) {
base, _ := url.Parse("https://raw.example.com/")
tests := []struct {
name string
mockedClient *http.Client
uri string
handlerFn func() mcp.ResourceHandler
expectedResponseType resourceResponseType
expectError string
expectedResult *mcp.ReadResourceResult
}{
{
name: "missing owner",
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
GetRawReposContentsByOwnerByRepoByPath: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/markdown")
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
require.NoError(t, err)
}),
}),
uri: "repo:///repo/contents/README.md",
handlerFn: func() mcp.ResourceHandler {
return RepositoryResourceContentsHandler(repositoryResourceContentURITemplate)
},
expectedResponseType: resourceResponseTypeText, // Ignored as error is expected
expectError: "owner is required",
},
{
name: "missing repo",
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
GetRawReposContentsByOwnerByRepoByBranchByPath: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/markdown")
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
require.NoError(t, err)
}),
}),
uri: "repo://owner//refs/heads/main/contents/README.md",
handlerFn: func() mcp.ResourceHandler {
return RepositoryResourceContentsHandler(repositoryResourceBranchContentURITemplate)
},
expectedResponseType: resourceResponseTypeText, // Ignored as error is expected
expectError: "repo is required",
},
{
name: "successful blob content fetch",
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
GetRawReposContentsByOwnerByRepoByPath: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "image/png")
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
require.NoError(t, err)
}),
}),
uri: "repo://owner/repo/contents/data.png",
handlerFn: func() mcp.ResourceHandler {
return RepositoryResourceContentsHandler(repositoryResourceContentURITemplate)
},
expectedResponseType: resourceResponseTypeBlob,
expectedResult: &mcp.ReadResourceResult{
Contents: []*mcp.ResourceContents{{
Blob: []byte("IyBUZXN0IFJlcG9zaXRvcnkKClRoaXMgaXMgYSB0ZXN0IHJlcG9zaXRvcnku"),
MIMEType: "image/png",
URI: "",
}}},
},
{
name: "successful text content fetch (HEAD)",
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
GetRawReposContentsByOwnerByRepoByPath: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/markdown")
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
require.NoError(t, err)
}),
}),
uri: "repo://owner/repo/contents/README.md",
handlerFn: func() mcp.ResourceHandler {
return RepositoryResourceContentsHandler(repositoryResourceContentURITemplate)
},
expectedResponseType: resourceResponseTypeText,
expectedResult: &mcp.ReadResourceResult{
Contents: []*mcp.ResourceContents{{
Text: "# Test Repository\n\nThis is a test repository.",
MIMEType: "text/markdown",
URI: "",
}}},
},
{
name: "successful text content fetch (HEAD)",
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
GetRawReposContentsByOwnerByRepoByPath: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
require.Contains(t, r.URL.Path, "pkg/github/actions.go")
_, err := w.Write([]byte("package actions\n\nfunc main() {\n // Sample Go file content\n}\n"))
require.NoError(t, err)
}),
}),
uri: "repo://owner/repo/contents/pkg/github/actions.go",
handlerFn: func() mcp.ResourceHandler {
return RepositoryResourceContentsHandler(repositoryResourceContentURITemplate)
},
expectedResponseType: resourceResponseTypeText,
expectedResult: &mcp.ReadResourceResult{
Contents: []*mcp.ResourceContents{{
Text: "package actions\n\nfunc main() {\n // Sample Go file content\n}\n",
MIMEType: "text/plain",
URI: "",
}}},
},
{
name: "successful text content fetch (branch)",
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
GetRawReposContentsByOwnerByRepoByBranchByPath: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/markdown")
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
require.NoError(t, err)
}),
}),
uri: "repo://owner/repo/refs/heads/main/contents/README.md",
handlerFn: func() mcp.ResourceHandler {
return RepositoryResourceContentsHandler(repositoryResourceBranchContentURITemplate)
},
expectedResponseType: resourceResponseTypeText,
expectedResult: &mcp.ReadResourceResult{
Contents: []*mcp.ResourceContents{{
Text: "# Test Repository\n\nThis is a test repository.",
MIMEType: "text/markdown",
URI: "",
}}},
},
{
name: "successful text content fetch (tag)",
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
GetRawReposContentsByOwnerByRepoByTagByPath: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/markdown")
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
require.NoError(t, err)
}),
}),
uri: "repo://owner/repo/refs/tags/v1.0.0/contents/README.md",
handlerFn: func() mcp.ResourceHandler {
return RepositoryResourceContentsHandler(repositoryResourceTagContentURITemplate)
},
expectedResponseType: resourceResponseTypeText,
expectedResult: &mcp.ReadResourceResult{
Contents: []*mcp.ResourceContents{{
Text: "# Test Repository\n\nThis is a test repository.",
MIMEType: "text/markdown",
URI: "",
}}},
},
{
name: "successful text content fetch (sha)",
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
GetRawReposContentsByOwnerByRepoBySHAByPath: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/markdown")
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
require.NoError(t, err)
}),
}),
uri: "repo://owner/repo/sha/abc123/contents/README.md",
handlerFn: func() mcp.ResourceHandler {
return RepositoryResourceContentsHandler(repositoryResourceCommitContentURITemplate)
},
expectedResponseType: resourceResponseTypeText,
expectedResult: &mcp.ReadResourceResult{
Contents: []*mcp.ResourceContents{{
Text: "# Test Repository\n\nThis is a test repository.",
MIMEType: "text/markdown",
URI: "",
}}},
},
{
name: "successful text content fetch (pr)",
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
GetReposPullsByOwnerByRepoByPullNumber: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, err := w.Write([]byte(`{"head": {"sha": "abc123"}}`))
require.NoError(t, err)
}),
GetRawReposContentsByOwnerByRepoBySHAByPath: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/markdown")
_, err := w.Write([]byte("# Test Repository\n\nThis is a test repository."))
require.NoError(t, err)
}),
}),
uri: "repo://owner/repo/refs/pull/42/head/contents/README.md",
handlerFn: func() mcp.ResourceHandler {
return RepositoryResourceContentsHandler(repositoryResourcePrContentURITemplate)
},
expectedResponseType: resourceResponseTypeText,
expectedResult: &mcp.ReadResourceResult{
Contents: []*mcp.ResourceContents{{
Text: "# Test Repository\n\nThis is a test repository.",
MIMEType: "text/markdown",
URI: "",
}}},
},
{
name: "content fetch fails",
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
GetReposContentsByOwnerByRepoByPath: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"message": "Not Found"}`))
}),
}),
uri: "repo://owner/repo/contents/nonexistent.md",
handlerFn: func() mcp.ResourceHandler {
return RepositoryResourceContentsHandler(repositoryResourceContentURITemplate)
},
expectedResponseType: resourceResponseTypeText, // Ignored as error is expected
expectError: "404 Not Found",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
client := github.NewClient(tc.mockedClient)
mockRawClient := raw.NewClient(client, base)
deps := BaseDeps{
Client: client,
RawClient: mockRawClient,
}
ctx := ContextWithDeps(context.Background(), deps)
handler := tc.handlerFn()
request := &mcp.ReadResourceRequest{
Params: &mcp.ReadResourceParams{
URI: tc.uri,
},
}
resp, err := handler(ctx, request)
if tc.expectError != "" {
require.ErrorContains(t, err, tc.expectError)
return
}
require.NoError(t, err)
content := resp.Contents[0]
switch tc.expectedResponseType {
case resourceResponseTypeBlob:
require.Equal(t, tc.expectedResult.Contents[0].Blob, content.Blob)
case resourceResponseTypeText:
require.Equal(t, tc.expectedResult.Contents[0].Text, content.Text)
default:
t.Fatalf("unknown expectedResponseType %v", tc.expectedResponseType)
}
})
}
}
// Test_repositoryResourceContentsHandler_NetworkError tests that a network error
// during raw content fetch does not cause a panic (nil response body dereference).
func Test_repositoryResourceContentsHandler_NetworkError(t *testing.T) {
base, _ := url.Parse("https://raw.example.com/")
networkErr := errors.New("network error: connection refused")
httpClient := &http.Client{Transport: &errorTransport{err: networkErr}}
client := github.NewClient(httpClient)
mockRawClient := raw.NewClient(client, base)
deps := BaseDeps{
Client: client,
RawClient: mockRawClient,
}
ctx := ContextWithDeps(context.Background(), deps)
handler := RepositoryResourceContentsHandler(repositoryResourceContentURITemplate)
request := &mcp.ReadResourceRequest{
Params: &mcp.ReadResourceParams{
URI: "repo://owner/repo/contents/README.md",
},
}
// This should not panic, even though the HTTP client returns an error
resp, err := handler(ctx, request)
require.Error(t, err)
require.Nil(t, resp)
require.ErrorContains(t, err, "failed to get raw content")
}