|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements.
|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
| 4 | +using System.Net; |
4 | 5 | using System.Net.Http;
|
5 | 6 | using System.Net.Http.Headers;
|
6 | 7 | using System.Text;
|
7 | 8 | using System.Text.Json;
|
8 | 9 | using Grpc.Core;
|
9 | 10 | using IntegrationTestsWebsite;
|
10 | 11 | using Microsoft.AspNetCore.Grpc.JsonTranscoding.IntegrationTests.Infrastructure;
|
11 |
| -using Microsoft.AspNetCore.Grpc.JsonTranscoding.Internal; |
12 |
| -using Microsoft.AspNetCore.Grpc.JsonTranscoding.Tests.Infrastructure; |
13 | 12 | using Microsoft.AspNetCore.Testing;
|
14 | 13 | using Xunit.Abstractions;
|
15 | 14 |
|
@@ -105,4 +104,163 @@ Task<HelloReply> UnaryMethod(ComplextHelloRequest request, ServerCallContext con
|
105 | 104 | // Assert
|
106 | 105 | Assert.Equal("Hello complex_greeter/test2/b last_name!", result.RootElement.GetProperty("message").GetString());
|
107 | 106 | }
|
| 107 | + |
| 108 | + [Fact] |
| 109 | + public async Task SimpleCatchAllParameter_PrefixSuffixSlashes_MatchUrl_SuccessResult() |
| 110 | + { |
| 111 | + // Arrange |
| 112 | + Task<HelloReply> UnaryMethod(HelloRequest request, ServerCallContext context) |
| 113 | + { |
| 114 | + return Task.FromResult(new HelloReply { Message = $"Hello {request.Name}!" }); |
| 115 | + } |
| 116 | + var method = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>( |
| 117 | + UnaryMethod, |
| 118 | + Greeter.Descriptor.FindMethodByName("SayHelloComplexCatchAll4")); |
| 119 | + |
| 120 | + var client = new HttpClient(Fixture.Handler) { BaseAddress = new Uri("http://localhost") }; |
| 121 | + |
| 122 | + // Act |
| 123 | + var response = await client.GetAsync("/v1/greeter//name/one/two//").DefaultTimeout(); |
| 124 | + var responseStream = await response.Content.ReadAsStreamAsync(); |
| 125 | + using var result = await JsonDocument.ParseAsync(responseStream); |
| 126 | + |
| 127 | + // Assert |
| 128 | + Assert.Equal("Hello /name/one/two//!", result.RootElement.GetProperty("message").GetString()); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public async Task ParameterVerb_MatchUrl_SuccessResult() |
| 133 | + { |
| 134 | + // Arrange |
| 135 | + Task<HelloReply> UnaryMethod1(HelloRequest request, ServerCallContext context) |
| 136 | + { |
| 137 | + return Task.FromResult(new HelloReply { Message = $"Hello {request.Name} one!" }); |
| 138 | + } |
| 139 | + Task<HelloReply> UnaryMethod2(HelloRequest request, ServerCallContext context) |
| 140 | + { |
| 141 | + return Task.FromResult(new HelloReply { Message = $"Hello {request.Name} two!" }); |
| 142 | + } |
| 143 | + var method1 = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>( |
| 144 | + UnaryMethod1, |
| 145 | + Greeter.Descriptor.FindMethodByName("SayHelloCustomVerbOne")); |
| 146 | + var method2 = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>( |
| 147 | + UnaryMethod2, |
| 148 | + Greeter.Descriptor.FindMethodByName("SayHelloCustomVerbTwo")); |
| 149 | + |
| 150 | + var client = new HttpClient(Fixture.Handler) { BaseAddress = new Uri("http://localhost") }; |
| 151 | + |
| 152 | + // Act 1 |
| 153 | + var response1 = await client.GetAsync("/v1/greeter_custom/test:one").DefaultTimeout(); |
| 154 | + var responseStream1 = await response1.Content.ReadAsStreamAsync(); |
| 155 | + using var result1 = await JsonDocument.ParseAsync(responseStream1); |
| 156 | + |
| 157 | + // Assert 2 |
| 158 | + Assert.Equal("Hello test one!", result1.RootElement.GetProperty("message").GetString()); |
| 159 | + |
| 160 | + // Act 2 |
| 161 | + var response2 = await client.GetAsync("/v1/greeter_custom/test:two").DefaultTimeout(); |
| 162 | + var responseStream2 = await response2.Content.ReadAsStreamAsync(); |
| 163 | + using var result2 = await JsonDocument.ParseAsync(responseStream2); |
| 164 | + |
| 165 | + // Assert 2 |
| 166 | + Assert.Equal("Hello test two!", result2.RootElement.GetProperty("message").GetString()); |
| 167 | + |
| 168 | + // Act 3 |
| 169 | + var response3 = await client.GetAsync("/v1/greeter_custom/test").DefaultTimeout(); |
| 170 | + |
| 171 | + // Assert 3 |
| 172 | + Assert.Equal(HttpStatusCode.NotFound, response3.StatusCode); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + public async Task CatchAllVerb_MatchUrl_SuccessResult() |
| 177 | + { |
| 178 | + // Arrange |
| 179 | + Task<HelloReply> UnaryMethod1(HelloRequest request, ServerCallContext context) |
| 180 | + { |
| 181 | + return Task.FromResult(new HelloReply { Message = $"Hello {request.Name} one!" }); |
| 182 | + } |
| 183 | + Task<HelloReply> UnaryMethod2(HelloRequest request, ServerCallContext context) |
| 184 | + { |
| 185 | + return Task.FromResult(new HelloReply { Message = $"Hello {request.Name} two!" }); |
| 186 | + } |
| 187 | + var method1 = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>( |
| 188 | + UnaryMethod1, |
| 189 | + Greeter.Descriptor.FindMethodByName("SayHelloCatchAllCustomVerbOne")); |
| 190 | + var method2 = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>( |
| 191 | + UnaryMethod2, |
| 192 | + Greeter.Descriptor.FindMethodByName("SayHelloCatchAllCustomVerbTwo")); |
| 193 | + |
| 194 | + var client = new HttpClient(Fixture.Handler) { BaseAddress = new Uri("http://localhost") }; |
| 195 | + |
| 196 | + // Act 1 |
| 197 | + var response1 = await client.GetAsync("/v1/greeter_customcatchall/test/name:one").DefaultTimeout(); |
| 198 | + var responseStream1 = await response1.Content.ReadAsStreamAsync(); |
| 199 | + using var result1 = await JsonDocument.ParseAsync(responseStream1); |
| 200 | + |
| 201 | + // Assert 2 |
| 202 | + Assert.Equal("Hello test/name one!", result1.RootElement.GetProperty("message").GetString()); |
| 203 | + |
| 204 | + // Act 2 |
| 205 | + var response2 = await client.GetAsync("/v1/greeter_customcatchall/test/name:two").DefaultTimeout(); |
| 206 | + var responseStream2 = await response2.Content.ReadAsStreamAsync(); |
| 207 | + using var result2 = await JsonDocument.ParseAsync(responseStream2); |
| 208 | + |
| 209 | + // Assert 2 |
| 210 | + Assert.Equal("Hello test/name two!", result2.RootElement.GetProperty("message").GetString()); |
| 211 | + |
| 212 | + // Act 3 |
| 213 | + var response3 = await client.GetAsync("/v1/greeter_customcatchall/test/name").DefaultTimeout(); |
| 214 | + |
| 215 | + // Assert 3 |
| 216 | + Assert.Equal(HttpStatusCode.NotFound, response3.StatusCode); |
| 217 | + } |
| 218 | + |
| 219 | + [Fact] |
| 220 | + public async Task PostVerb_MatchUrl_SuccessResult() |
| 221 | + { |
| 222 | + // Arrange |
| 223 | + Task<HelloReply> UnaryMethod1(HelloRequest request, ServerCallContext context) |
| 224 | + { |
| 225 | + return Task.FromResult(new HelloReply { Message = $"Hello {request.Name} one!" }); |
| 226 | + } |
| 227 | + Task<HelloReply> UnaryMethod2(HelloRequest request, ServerCallContext context) |
| 228 | + { |
| 229 | + return Task.FromResult(new HelloReply { Message = $"Hello {request.Name} two!" }); |
| 230 | + } |
| 231 | + var method1 = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>( |
| 232 | + UnaryMethod1, |
| 233 | + Greeter.Descriptor.FindMethodByName("SayHelloPostCustomVerbOne")); |
| 234 | + var method2 = Fixture.DynamicGrpc.AddUnaryMethod<HelloRequest, HelloReply>( |
| 235 | + UnaryMethod2, |
| 236 | + Greeter.Descriptor.FindMethodByName("SayHelloPostCustomVerbTwo")); |
| 237 | + |
| 238 | + var client = new HttpClient(Fixture.Handler) { BaseAddress = new Uri("http://localhost") }; |
| 239 | + |
| 240 | + var requestMessage = new HelloRequest { Name = "test" }; |
| 241 | + var content = new ByteArrayContent(Encoding.UTF8.GetBytes(requestMessage.ToString())); |
| 242 | + content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); |
| 243 | + |
| 244 | + // Act 1 |
| 245 | + var response1 = await client.PostAsync("/v1/greeter_custompost:one", content).DefaultTimeout(); |
| 246 | + var responseStream1 = await response1.Content.ReadAsStreamAsync(); |
| 247 | + using var result1 = await JsonDocument.ParseAsync(responseStream1); |
| 248 | + |
| 249 | + // Assert 2 |
| 250 | + Assert.Equal("Hello test one!", result1.RootElement.GetProperty("message").GetString()); |
| 251 | + |
| 252 | + // Act 2 |
| 253 | + var response2 = await client.PostAsync("/v1/greeter_custompost:two", content).DefaultTimeout(); |
| 254 | + var responseStream2 = await response2.Content.ReadAsStreamAsync(); |
| 255 | + using var result2 = await JsonDocument.ParseAsync(responseStream2); |
| 256 | + |
| 257 | + // Assert 2 |
| 258 | + Assert.Equal("Hello test two!", result2.RootElement.GetProperty("message").GetString()); |
| 259 | + |
| 260 | + // Act 3 |
| 261 | + var response3 = await client.PostAsync("/v1/greeter_custompost", content).DefaultTimeout(); |
| 262 | + |
| 263 | + // Assert 3 |
| 264 | + Assert.Equal(HttpStatusCode.NotFound, response3.StatusCode); |
| 265 | + } |
108 | 266 | }
|
0 commit comments