@@ -2,7 +2,6 @@ package github
2
2
3
3
import (
4
4
"context"
5
- "encoding/base64"
6
5
"net/http"
7
6
"testing"
8
7
@@ -13,28 +12,35 @@ import (
13
12
"github.com/stretchr/testify/require"
14
13
)
15
14
15
+ var GetRawReposContentsByOwnerByRepoByPath mock.EndpointPattern = mock.EndpointPattern {
16
+ Pattern : "/{owner}/{repo}/main/{path:.+}" ,
17
+ Method : "GET" ,
18
+ }
19
+
16
20
func Test_repositoryResourceContentsHandler (t * testing.T ) {
17
21
mockDirContent := []* github.RepositoryContent {
18
22
{
19
- Type : github .Ptr ("file" ),
20
- Name : github .Ptr ("README.md" ),
21
- Path : github .Ptr ("README.md" ),
22
- SHA : github .Ptr ("abc123" ),
23
- Size : github .Ptr (42 ),
24
- HTMLURL : github .Ptr ("https://github.com/owner/repo/blob/main/README.md" ),
23
+ Type : github .Ptr ("file" ),
24
+ Name : github .Ptr ("README.md" ),
25
+ Path : github .Ptr ("README.md" ),
26
+ SHA : github .Ptr ("abc123" ),
27
+ Size : github .Ptr (42 ),
28
+ HTMLURL : github .Ptr ("https://github.com/owner/repo/blob/main/README.md" ),
29
+ DownloadURL : github .Ptr ("https://raw.githubusercontent.com/owner/repo/main/README.md" ),
25
30
},
26
31
{
27
- Type : github .Ptr ("dir" ),
28
- Name : github .Ptr ("src" ),
29
- Path : github .Ptr ("src" ),
30
- SHA : github .Ptr ("def456" ),
31
- HTMLURL : github .Ptr ("https://github.com/owner/repo/tree/main/src" ),
32
+ Type : github .Ptr ("dir" ),
33
+ Name : github .Ptr ("src" ),
34
+ Path : github .Ptr ("src" ),
35
+ SHA : github .Ptr ("def456" ),
36
+ HTMLURL : github .Ptr ("https://github.com/owner/repo/tree/main/src" ),
37
+ DownloadURL : github .Ptr ("https://raw.githubusercontent.com/owner/repo/main/src" ),
32
38
},
33
39
}
34
40
expectedDirContent := []mcp.TextResourceContents {
35
41
{
36
42
URI : "https://github.com/owner/repo/blob/main/README.md" ,
37
- MIMEType : "" ,
43
+ MIMEType : "text/markdown " ,
38
44
Text : "README.md" ,
39
45
},
40
46
{
@@ -44,20 +50,41 @@ func Test_repositoryResourceContentsHandler(t *testing.T) {
44
50
},
45
51
}
46
52
47
- mockFileContent := & github.RepositoryContent {
53
+ mockTextContent := & github.RepositoryContent {
48
54
Type : github .Ptr ("file" ),
49
55
Name : github .Ptr ("README.md" ),
50
56
Path : github .Ptr ("README.md" ),
51
- Content : github .Ptr ("IyBUZXN0IFJlcG9zaXRvcnkKClRoaXMgaXMgYSB0ZXN0IHJlcG9zaXRvcnku" ), // Base64 encoded " # Test Repository\n\nThis is a test repository."
57
+ Content : github .Ptr ("# Test Repository\n \n This is a test repository." ),
52
58
SHA : github .Ptr ("abc123" ),
53
59
Size : github .Ptr (42 ),
54
60
HTMLURL : github .Ptr ("https://github.com/owner/repo/blob/main/README.md" ),
55
61
DownloadURL : github .Ptr ("https://raw.githubusercontent.com/owner/repo/main/README.md" ),
56
62
}
57
63
64
+ mockFileContent := & github.RepositoryContent {
65
+ Type : github .Ptr ("file" ),
66
+ Name : github .Ptr ("data.png" ),
67
+ Path : github .Ptr ("data.png" ),
68
+ Content : github .Ptr ("IyBUZXN0IFJlcG9zaXRvcnkKClRoaXMgaXMgYSB0ZXN0IHJlcG9zaXRvcnku" ), // Base64 encoded "# Test Repository\n\nThis is a test repository."
69
+ SHA : github .Ptr ("abc123" ),
70
+ Size : github .Ptr (42 ),
71
+ HTMLURL : github .Ptr ("https://github.com/owner/repo/blob/main/data.png" ),
72
+ DownloadURL : github .Ptr ("https://raw.githubusercontent.com/owner/repo/main/data.png" ),
73
+ }
74
+
58
75
expectedFileContent := []mcp.BlobResourceContents {
59
76
{
60
- Blob : base64 .StdEncoding .EncodeToString ([]byte ("IyBUZXN0IFJlcG9zaXRvcnkKClRoaXMgaXMgYSB0ZXN0IHJlcG9zaXRvcnku" )),
77
+ Blob : "IyBUZXN0IFJlcG9zaXRvcnkKClRoaXMgaXMgYSB0ZXN0IHJlcG9zaXRvcnku" ,
78
+ MIMEType : "image/png" ,
79
+ URI : "" ,
80
+ },
81
+ }
82
+
83
+ expectedTextContent := []mcp.TextResourceContents {
84
+ {
85
+ Text : "# Test Repository\n \n This is a test repository." ,
86
+ MIMEType : "text/markdown" ,
87
+ URI : "" ,
61
88
},
62
89
}
63
90
@@ -94,21 +121,50 @@ func Test_repositoryResourceContentsHandler(t *testing.T) {
94
121
expectError : "repo is required" ,
95
122
},
96
123
{
97
- name : "successful file content fetch" ,
124
+ name : "successful blob content fetch" ,
98
125
mockedClient : mock .NewMockedHTTPClient (
99
126
mock .WithRequestMatch (
100
127
mock .GetReposContentsByOwnerByRepoByPath ,
101
128
mockFileContent ,
102
129
),
130
+ mock .WithRequestMatchHandler (
131
+ GetRawReposContentsByOwnerByRepoByPath ,
132
+ http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
133
+ w .Header ().Set ("Content-Type" , "image/png" )
134
+ // as this is given as a png, it will return the content as a blob
135
+ _ , err := w .Write ([]byte ("# Test Repository\n \n This is a test repository." ))
136
+ require .NoError (t , err )
137
+ }),
138
+ ),
103
139
),
104
140
requestArgs : map [string ]any {
105
141
"owner" : []string {"owner" },
106
142
"repo" : []string {"repo" },
107
- "path" : []string {"README.md " },
143
+ "path" : []string {"data.png " },
108
144
"branch" : []string {"main" },
109
145
},
110
146
expectedResult : expectedFileContent ,
111
147
},
148
+ {
149
+ name : "successful text content fetch" ,
150
+ mockedClient : mock .NewMockedHTTPClient (
151
+ mock .WithRequestMatch (
152
+ mock .GetReposContentsByOwnerByRepoByPath ,
153
+ mockTextContent ,
154
+ ),
155
+ mock .WithRequestMatch (
156
+ GetRawReposContentsByOwnerByRepoByPath ,
157
+ []byte ("# Test Repository\n \n This is a test repository." ),
158
+ ),
159
+ ),
160
+ requestArgs : map [string ]any {
161
+ "owner" : []string {"owner" },
162
+ "repo" : []string {"repo" },
163
+ "path" : []string {"README.md" },
164
+ "branch" : []string {"main" },
165
+ },
166
+ expectedResult : expectedTextContent ,
167
+ },
112
168
{
113
169
name : "successful directory content fetch" ,
114
170
mockedClient : mock .NewMockedHTTPClient (
0 commit comments