mcp: add syntax and scheme validation to AddResourceTemplate (#253)

Add template validation and scheme check in `AddResourceTemplate`.
This commit is contained in:
cryo 2025-08-20 05:43:26 +08:00 • committed by GitHub
commit 732b97fdaf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -373,6 +373,42 @@ func TestServerCapabilities(t *testing.T) {
}
}
func TestServerAddResourceTemplate(t *testing.T) {
tests := []struct {
name string
template string
expectPanic bool
}{
{"ValidFileTemplate", "file:///{a}/{b}", false},
{"ValidCustomScheme", "myproto:///{a}", false},
{"MissingScheme1", "://example.com/{path}", true},
{"MissingScheme2", "/api/v1/users/{id}", true},
{"EmptyVariable", "file:///{}/{b}", true},
{"UnclosedVariable", "file:///{a", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rt := ResourceTemplate{URITemplate: tt.template}
defer func() {
if r := recover(); r != nil {
if !tt.expectPanic {
t.Errorf("%s: unexpected panic: %v", tt.name, r)
}
} else {
if tt.expectPanic {
t.Errorf("%s: expected panic but did not panic", tt.name)
}
}
}()
s := NewServer(testImpl, nil)
s.AddResourceTemplate(&rt, nil)
})
}
}
// TestServerSessionkeepaliveCancelOverwritten is to verify that `ServerSession.keepaliveCancel` is assigned exactly once,
// ensuring that only a single goroutine is responsible for the session's keepalive ping mechanism.
func TestServerSessionkeepaliveCancelOverwritten(t *testing.T) {