11using ModelContextProtocol . Protocol ;
22using ModelContextProtocol . Server ;
3- using Moq ;
43using System . Reflection ;
54
65namespace ModelContextProtocol . Tests . Server ;
76
87public class DelegatingMcpServerResourceTests
98{
10- private static JsonRpcRequest CreateTestJsonRpcRequest ( ) => new ( )
11- {
12- Id = new RequestId ( "test-id" ) ,
13- Method = "test/method" ,
14- Params = null ,
15- } ;
16-
179 [ Fact ]
1810 public void Ctor_NullInnerResource_Throws ( )
1911 {
20- Assert . Throws < ArgumentNullException > ( "innerResource" , ( ) => new TestDelegatingMcpServerResource ( null ! ) ) ;
21- }
22-
23- [ Fact ]
24- public void ProtocolResourceTemplate_DelegatesToInnerResource ( )
25- {
26- ResourceTemplate expected = new ( ) { Name = "test-resource" , UriTemplate = "test://resource" } ;
27- Mock < McpServerResource > mock = new ( ) ;
28- mock . Setup ( r => r . ProtocolResourceTemplate ) . Returns ( expected ) ;
29-
30- TestDelegatingMcpServerResource delegating = new ( mock . Object ) ;
31-
32- Assert . Same ( expected , delegating . ProtocolResourceTemplate ) ;
33- mock . Verify ( r => r . ProtocolResourceTemplate , Times . Once ) ;
34- }
35-
36- [ Fact ]
37- public void ProtocolResource_DelegatesToInnerResource ( )
38- {
39- Resource expected = new ( ) { Name = "test-resource" , Uri = "test://resource" } ;
40- Mock < McpServerResource > mock = new ( ) ;
41- mock . Setup ( r => r . ProtocolResource ) . Returns ( expected ) ;
42-
43- TestDelegatingMcpServerResource delegating = new ( mock . Object ) ;
44-
45- Assert . Same ( expected , delegating . ProtocolResource ) ;
46- mock . Verify ( r => r . ProtocolResource , Times . Once ) ;
47- }
48-
49- [ Fact ]
50- public void Metadata_DelegatesToInnerResource ( )
51- {
52- IReadOnlyList < object > expected = new object [ ] { "attr1" , "attr2" } ;
53- Mock < McpServerResource > mock = new ( ) ;
54- mock . Setup ( r => r . Metadata ) . Returns ( expected ) ;
55-
56- TestDelegatingMcpServerResource delegating = new ( mock . Object ) ;
57-
58- Assert . Same ( expected , delegating . Metadata ) ;
59- mock . Verify ( r => r . Metadata , Times . Once ) ;
12+ Assert . Throws < ArgumentNullException > ( "innerResource" , ( ) => new TestDelegatingResource ( null ! ) ) ;
6013 }
6114
6215 [ Fact ]
63- public void IsMatch_DelegatesToInnerResource ( )
16+ public async Task AllMembers_DelegateToInnerResource ( )
6417 {
65- Mock < McpServerResource > mock = new ( ) ;
66- mock . Setup ( r => r . IsMatch ( "test://resource" ) ) . Returns ( true ) ;
18+ ResourceTemplate expectedTemplate = new ( ) { Name = "sentinel-resource" , UriTemplate = "test://resource" } ;
19+ Resource expectedResource = new ( ) { Name = "sentinel-resource" , Uri = "test://resource" } ;
20+ IReadOnlyList < object > expectedMetadata = new object [ ] { "m1" } ;
21+ ReadResourceResult expectedResult = new ( ) { Contents = [ ] } ;
22+ InnerResource inner = new ( expectedTemplate , expectedResource , expectedMetadata , expectedResult ) ;
6723
68- TestDelegatingMcpServerResource delegating = new ( mock . Object ) ;
24+ TestDelegatingResource delegating = new ( inner ) ;
6925
26+ Assert . Same ( expectedTemplate , delegating . ProtocolResourceTemplate ) ;
27+ Assert . Same ( expectedResource , delegating . ProtocolResource ) ;
28+ Assert . Same ( expectedMetadata , delegating . Metadata ) ;
7029 Assert . True ( delegating . IsMatch ( "test://resource" ) ) ;
71- mock . Verify ( r => r . IsMatch ( "test://resource" ) , Times . Once ) ;
72- }
73-
74- [ Fact ]
75- public async Task ReadAsync_DelegatesToInnerResource ( )
76- {
77- ReadResourceResult expected = new ( ) { Contents = [ ] } ;
78- RequestContext < ReadResourceRequestParams > request = new ( new Mock < McpServer > ( ) . Object , CreateTestJsonRpcRequest ( ) ) ;
79- using CancellationTokenSource cts = new ( ) ;
80- Mock < McpServerResource > mock = new ( ) ;
81- mock . Setup ( r => r . ReadAsync ( request , cts . Token ) ) . ReturnsAsync ( expected ) ;
82-
83- TestDelegatingMcpServerResource delegating = new ( mock . Object ) ;
84-
85- ReadResourceResult result = await delegating . ReadAsync ( request , cts . Token ) ;
86-
87- Assert . Same ( expected , result ) ;
88- mock . Verify ( r => r . ReadAsync ( request , cts . Token ) , Times . Once ) ;
89- }
90-
91- [ Fact ]
92- public void ToString_DelegatesToInnerResource ( )
93- {
94- Mock < McpServerResource > mock = new ( ) ;
95- mock . Setup ( r => r . ToString ( ) ) . Returns ( "inner-resource-string" ) ;
96-
97- TestDelegatingMcpServerResource delegating = new ( mock . Object ) ;
98-
99- Assert . Equal ( "inner-resource-string" , delegating . ToString ( ) ) ;
30+ Assert . Same ( expectedResult , await delegating . ReadAsync ( null ! , CancellationToken . None ) ) ;
31+ Assert . Equal ( inner . ToString ( ) , delegating . ToString ( ) ) ;
10032 }
10133
10234 [ Fact ]
@@ -112,7 +44,6 @@ public void OverridesAllVirtualAndAbstractMembers()
11244 {
11345 MethodInfo ? overriddenMethod = typeof ( DelegatingMcpServerResource ) . GetMethod (
11446 baseMethod . Name ,
115- BindingFlags . Public | BindingFlags . Instance ,
11647 baseMethod . GetParameters ( ) . Select ( p => p . ParameterType ) . ToArray ( ) ) ;
11748
11849 Assert . True (
@@ -121,5 +52,15 @@ public void OverridesAllVirtualAndAbstractMembers()
12152 }
12253 }
12354
124- private sealed class TestDelegatingMcpServerResource ( McpServerResource innerResource ) : DelegatingMcpServerResource ( innerResource ) ;
55+ private sealed class TestDelegatingResource ( McpServerResource innerResource ) : DelegatingMcpServerResource ( innerResource ) ;
56+
57+ private sealed class InnerResource( ResourceTemplate protocolResourceTemplate , Resource protocolResource , IReadOnlyList < object > metadata , ReadResourceResult result ) : McpServerResource
58+ {
59+ public override ResourceTemplate ProtocolResourceTemplate => protocolResourceTemplate ;
60+ public override Resource ? ProtocolResource => protocolResource ;
61+ public override IReadOnlyList < object > Metadata => metadata ;
62+ public override bool IsMatch ( string uri ) => true ;
63+ public override ValueTask < ReadResourceResult > ReadAsync ( RequestContext < ReadResourceRequestParams > request , CancellationToken cancellationToken = default ) => new ( result ) ;
64+ public override string ToString ( ) => "inner-resource" ;
65+ }
12566}
0 commit comments