Debian Codegraph @

debian/go/src/github.com/modelcontextprotocol/go-sdk/mcp/resource_test.go

raw symbols SCIP
1 // Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2 // Use of this source code is governed by an MIT-style
3 // license that can be found in the LICENSE file.
4
5 package mcp
6
7 import (
8 "path/filepath"
9 "runtime"
10 "strings"
11 "testing"
12 )
13
14 func TestFileRoot(t *testing.T) {
15 if runtime.GOOS == "windows" {
16 t.Skip("TODO: fix for Windows")
17 }
18
19 for _, tt := range []struct {
20 uri string
21 want string
22 wantErr string // error must contain this string
23 }{
24 {uri: "file:///foo", want: "/foo"},
25 {uri: "file:///foo/bar", want: "/foo/bar"},
26 {uri: "file:///foo/../bar", want: "/bar"},
27 {uri: "file:/foo", want: "/foo"},
28 {uri: "http:///foo", wantErr: "not a file"},
29 {uri: "file://foo", wantErr: "empty path"},
30 {uri: ":", wantErr: "missing protocol scheme"},
31 } {
32 got, err := fileRoot(&Root{URI: tt.uri})
33 if err != nil {
34 if tt.wantErr == "" {
35 t.Errorf("%s: got %v, want success", tt.uri, err)
36 continue
37 }
38 if !strings.Contains(err.Error(), tt.wantErr) {
39 t.Errorf("%s: got %v, does not contain %q", tt.uri, err, tt.wantErr)
40 continue
41 }
42 } else if tt.wantErr != "" {
43 t.Errorf("%s: succeeded, but wanted error with %q", tt.uri, tt.wantErr)
44 } else if got != tt.want {
45 t.Errorf("%s: got %q, want %q", tt.uri, got, tt.want)
46 }
47 }
48 }
49
50 func TestComputeURIFilepath(t *testing.T) {
51 if runtime.GOOS == "windows" {
52 t.Skip("TODO: fix for Windows")
53 }
54 // TODO(jba): test with Windows \\host paths and C: paths
55 dirFilepath := filepath.FromSlash("/files")
56 rootFilepaths := []string{
57 filepath.FromSlash("/files/public"),
58 filepath.FromSlash("/files/shared"),
59 }
60 for _, tt := range []struct {
61 uri string
62 want string
63 wantErr string // error must contain this
64 }{
65 {"file:///public", "public", ""},
66 {"file:///public/file", "public/file", ""},
67 {"file:///shared/file", "shared/file", ""},
68 {"http:///foo", "", "not a file"},
69 {"file://foo", "", "empty"},
70 {"file://foo/../bar", "", "localized"},
71 {"file:///secret", "", "root"},
72 {"file:///secret/file", "", "root"},
73 {"file:///private/file", "", "root"},
74 } {
75 t.Run(tt.uri, func(t *testing.T) {
76 tt.want = filepath.FromSlash(tt.want) // handle Windows
77 got, gotErr := computeURIFilepath(tt.uri, dirFilepath, rootFilepaths)
78 if gotErr != nil {
79 if tt.wantErr == "" {
80 t.Fatalf("got %v, wanted success", gotErr)
81 }
82 if !strings.Contains(gotErr.Error(), tt.wantErr) {
83 t.Fatalf("got error %v, does not contain %q", gotErr, tt.wantErr)
84 }
85 return
86 }
87 if tt.wantErr != "" {
88 t.Fatal("succeeded unexpectedly")
89 }
90 if got != tt.want {
91 t.Errorf("got %q, want %q", got, tt.want)
92 }
93 })
94 }
95 }
96
97 func TestReadFileResource(t *testing.T) {
98 if runtime.GOOS == "windows" {
99 t.Skip("TODO: fix for Windows")
100 }
101 abs, err := filepath.Abs("testdata")
102 if err != nil {
103 t.Fatal(err)
104 }
105 dirFilepath := filepath.Join(abs, "files")
106 got, err := readFileResource("file:///info.txt", dirFilepath, nil)
107 if err != nil {
108 t.Fatal(err)
109 }
110 want := "Contents\n"
111 if g := string(got); g != want {
112 t.Errorf("got %q, want %q", g, want)
113 }
114 }
115
116 func TestTemplateMatch(t *testing.T) {
117 uri := "file:///path/to/file"
118 for _, tt := range []struct {
119 template string
120 want bool
121 }{
122 {"file:///{}/{a}/{b}", false}, // invalid: empty variable expression "{}" is not allowed in RFC 6570
123 {"file:///{a}/{b}", false},
124 {"file:///{+path}", true},
125 {"file:///{a}/{+path}", true},
126 } {
127 resourceTmpl := serverResourceTemplate{resourceTemplate: &ResourceTemplate{URITemplate: tt.template}}
128 if matched := resourceTmpl.Matches(uri); matched != tt.want {
129 t.Errorf("%s: got %t, want %t", tt.template, matched, tt.want)
130 }
131 }
132 }