|
| 1 | +// filepath: c:\Users\ddelimarsky\source\csharp-sdk\tests\ModelContextProtocol.Tests\Protocol\Auth\ProtectedResourceMetadataTests.cs |
| 2 | +using ModelContextProtocol.Protocol.Auth; |
| 3 | +using System.Text.Json; |
| 4 | + |
| 5 | +namespace ModelContextProtocol.Tests.Protocol.Auth; |
| 6 | + |
| 7 | +public class ProtectedResourceMetadataTests |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void ProtectedResourceMetadata_JsonSerialization_Works() |
| 11 | + { |
| 12 | + // Arrange |
| 13 | + var metadata = new ProtectedResourceMetadata |
| 14 | + { |
| 15 | + Resource = new Uri("http://localhost:7071"), |
| 16 | + AuthorizationServers = [new Uri("https://login.microsoftonline.com/tenant/v2.0")], |
| 17 | + BearerMethodsSupported = ["header"], |
| 18 | + ScopesSupported = ["mcp.tools", "mcp.prompts"], |
| 19 | + ResourceDocumentation = new Uri("https://example.com/docs") |
| 20 | + }; |
| 21 | + |
| 22 | + // Act |
| 23 | + var json = JsonSerializer.Serialize(metadata); |
| 24 | + var deserialized = JsonSerializer.Deserialize<ProtectedResourceMetadata>(json); |
| 25 | + |
| 26 | + // Assert |
| 27 | + Assert.NotNull(deserialized); |
| 28 | + Assert.Equal("http://localhost:7071", deserialized.Resource.ToString()); |
| 29 | + Assert.Equal("https://login.microsoftonline.com/tenant/v2.0", deserialized.AuthorizationServers[0].ToString()); |
| 30 | + Assert.Equal("header", deserialized.BearerMethodsSupported![0]); |
| 31 | + Assert.Equal(2, deserialized.ScopesSupported!.Length); |
| 32 | + Assert.Contains("mcp.tools", deserialized.ScopesSupported!); |
| 33 | + Assert.Contains("mcp.prompts", deserialized.ScopesSupported!); |
| 34 | + Assert.Equal("https://example.com/docs", deserialized.ResourceDocumentation!.ToString()); |
| 35 | + } |
| 36 | + |
| 37 | + [Fact] |
| 38 | + public void ProtectedResourceMetadata_JsonDeserialization_WorksWithStringProperties() |
| 39 | + { |
| 40 | + // Arrange |
| 41 | + var json = @"{ |
| 42 | + ""resource"": ""http://localhost:7071"", |
| 43 | + ""authorization_servers"": [""https://login.microsoftonline.com/tenant/v2.0""], |
| 44 | + ""bearer_methods_supported"": [""header""], |
| 45 | + ""scopes_supported"": [""mcp.tools"", ""mcp.prompts""], |
| 46 | + ""resource_documentation"": ""https://example.com/docs"" |
| 47 | + }"; |
| 48 | + |
| 49 | + // Act |
| 50 | + var deserialized = JsonSerializer.Deserialize<ProtectedResourceMetadata>(json); |
| 51 | + |
| 52 | + // Assert |
| 53 | + Assert.NotNull(deserialized); |
| 54 | + Assert.Equal("http://localhost:7071", deserialized.Resource.ToString()); |
| 55 | + Assert.Equal("https://login.microsoftonline.com/tenant/v2.0", deserialized.AuthorizationServers[0].ToString()); |
| 56 | + Assert.Equal("header", deserialized.BearerMethodsSupported![0]); |
| 57 | + Assert.Equal(2, deserialized.ScopesSupported!.Length); |
| 58 | + Assert.Contains("mcp.tools", deserialized.ScopesSupported!); |
| 59 | + Assert.Contains("mcp.prompts", deserialized.ScopesSupported!); |
| 60 | + Assert.Equal("https://example.com/docs", deserialized.ResourceDocumentation!.ToString()); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void ResourceMetadata_JsonSerialization_Works() |
| 65 | + { |
| 66 | + // Arrange |
| 67 | + var metadata = new ResourceMetadata |
| 68 | + { |
| 69 | + Resource = new Uri("http://localhost:7071"), |
| 70 | + AuthorizationServers = [new Uri("https://login.microsoftonline.com/tenant/v2.0")], |
| 71 | + BearerMethodsSupported = ["header"], |
| 72 | + ScopesSupported = ["mcp.tools", "mcp.prompts"], |
| 73 | + ResourceDocumentation = new Uri("https://example.com/docs") |
| 74 | + }; |
| 75 | + |
| 76 | + // Act |
| 77 | + var json = JsonSerializer.Serialize(metadata); |
| 78 | + var deserialized = JsonSerializer.Deserialize<ResourceMetadata>(json); |
| 79 | + |
| 80 | + // Assert |
| 81 | + Assert.NotNull(deserialized); |
| 82 | + Assert.Equal("http://localhost:7071", deserialized.Resource.ToString()); |
| 83 | + Assert.Equal("https://login.microsoftonline.com/tenant/v2.0", deserialized.AuthorizationServers[0].ToString()); |
| 84 | + Assert.Equal("header", deserialized.BearerMethodsSupported![0]); |
| 85 | + Assert.Equal(2, deserialized.ScopesSupported!.Length); |
| 86 | + Assert.Contains("mcp.tools", deserialized.ScopesSupported!); |
| 87 | + Assert.Contains("mcp.prompts", deserialized.ScopesSupported!); |
| 88 | + Assert.Equal("https://example.com/docs", deserialized.ResourceDocumentation!.ToString()); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void ToResourceMetadata_ConversionWorks() |
| 93 | + { |
| 94 | + // Arrange |
| 95 | + var prm = new ProtectedResourceMetadata |
| 96 | + { |
| 97 | + Resource = new Uri("http://localhost:7071"), |
| 98 | + AuthorizationServers = [new Uri("https://login.microsoftonline.com/tenant/v2.0")], |
| 99 | + BearerMethodsSupported = ["header"], |
| 100 | + ScopesSupported = ["mcp.tools", "mcp.prompts"], |
| 101 | + ResourceDocumentation = new Uri("https://example.com/docs") |
| 102 | + }; |
| 103 | + |
| 104 | + // Act |
| 105 | + var resourceMetadata = prm.ToResourceMetadata(); |
| 106 | + |
| 107 | + // Assert |
| 108 | + Assert.Equal(prm.Resource, resourceMetadata.Resource); |
| 109 | + Assert.Equal(prm.AuthorizationServers, resourceMetadata.AuthorizationServers); |
| 110 | + Assert.Equal(prm.BearerMethodsSupported, resourceMetadata.BearerMethodsSupported); |
| 111 | + Assert.Equal(prm.ScopesSupported, resourceMetadata.ScopesSupported); |
| 112 | + Assert.Equal(prm.ResourceDocumentation, resourceMetadata.ResourceDocumentation); |
| 113 | + } |
| 114 | +} |
0 commit comments