|
1 | | -// Copyright (c) .NET Foundation. All rights reserved. |
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
3 | 3 |
|
4 | 4 | using System; |
@@ -232,6 +232,65 @@ public async Task CheckNoRedirectToWww(string requestUri) |
232 | 232 | Assert.Null(response.Headers.Location); |
233 | 233 | } |
234 | 234 |
|
| 235 | + [Theory] |
| 236 | + [InlineData(StatusCodes.Status301MovedPermanently)] |
| 237 | + [InlineData(StatusCodes.Status302Found)] |
| 238 | + [InlineData(StatusCodes.Status307TemporaryRedirect)] |
| 239 | + [InlineData(StatusCodes.Status308PermanentRedirect)] |
| 240 | + public async Task CheckRedirectToNonWwwWithStatusCode(int statusCode) |
| 241 | + { |
| 242 | + var options = new RewriteOptions().AddRedirectToNonWww(statusCode: statusCode); |
| 243 | + var builder = new WebHostBuilder() |
| 244 | + .Configure(app => |
| 245 | + { |
| 246 | + app.UseRewriter(options); |
| 247 | + }); |
| 248 | + var server = new TestServer(builder); |
| 249 | + |
| 250 | + var response = await server.CreateClient().GetAsync(new Uri("https://www.example.com")); |
| 251 | + |
| 252 | + Assert.Equal("https://example.com/", response.Headers.Location.OriginalString); |
| 253 | + Assert.Equal(statusCode, (int)response.StatusCode); |
| 254 | + } |
| 255 | + |
| 256 | + [Theory] |
| 257 | + [InlineData("http://www.example.com", "http://example.com/")] |
| 258 | + [InlineData("https://www.example.com", "https://example.com/")] |
| 259 | + [InlineData("http://www.example.com:8081", "http://example.com:8081/")] |
| 260 | + [InlineData("http://www.example.com:8081/example?q=1", "http://example.com:8081/example?q=1")] |
| 261 | + public async Task CheckRedirectToNonWww(string requestUri, string redirectUri) |
| 262 | + { |
| 263 | + var options = new RewriteOptions().AddRedirectToNonWww(); |
| 264 | + var builder = new WebHostBuilder() |
| 265 | + .Configure(app => |
| 266 | + { |
| 267 | + app.UseRewriter(options); |
| 268 | + }); |
| 269 | + var server = new TestServer(builder); |
| 270 | + |
| 271 | + var response = await server.CreateClient().GetAsync(new Uri(requestUri)); |
| 272 | + |
| 273 | + Assert.Equal(redirectUri, response.Headers.Location.OriginalString); |
| 274 | + Assert.Equal(StatusCodes.Status307TemporaryRedirect, (int)response.StatusCode); |
| 275 | + } |
| 276 | + |
| 277 | + [Fact] |
| 278 | + public async Task CheckPermanentRedirectToNonWww() |
| 279 | + { |
| 280 | + var options = new RewriteOptions().AddRedirectToNonWwwPermanent(); |
| 281 | + var builder = new WebHostBuilder() |
| 282 | + .Configure(app => |
| 283 | + { |
| 284 | + app.UseRewriter(options); |
| 285 | + }); |
| 286 | + var server = new TestServer(builder); |
| 287 | + |
| 288 | + var response = await server.CreateClient().GetAsync(new Uri("https://www.example.com")); |
| 289 | + |
| 290 | + Assert.Equal("https://example.com/", response.Headers.Location.OriginalString); |
| 291 | + Assert.Equal(StatusCodes.Status308PermanentRedirect, (int)response.StatusCode); |
| 292 | + } |
| 293 | + |
235 | 294 | [Fact] |
236 | 295 | public async Task CheckIfEmptyStringRedirectCorrectly() |
237 | 296 | { |
|
0 commit comments