1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Moq;
using Xunit;
namespace Microsoft.AspNetCore.Http.Result;
public class RedirectToRouteResultTest
{
[Fact]
public async Task RedirectToRoute_Execute_ThrowsOnNullUrl()
{
// Arrange
var httpContext = new DefaultHttpContext();
httpContext.RequestServices = CreateServices(null).BuildServiceProvider();
var result = new RedirectToRouteResult(null, new Dictionary<string, object>());
// Act & Assert
await ExceptionAssert.ThrowsAsync<InvalidOperationException>(
async () =>
{
await result.ExecuteAsync(httpContext);
},
"No route matches the supplied values.");
}
[Fact]
public async Task ExecuteResultAsync_UsesRouteName_ToGenerateLocationHeader()
{
// Arrange
var routeName = "orders_api";
var locationUrl = "/api/orders/10";
var httpContext = GetHttpContext(locationUrl);
var result = new RedirectToRouteResult(routeName, new { id = 10 });
// Act
await result.ExecuteAsync(httpContext);
// Assert
Assert.True(httpContext.Response.Headers.ContainsKey("Location"), "Location header not found");
Assert.Equal(locationUrl, httpContext.Response.Headers["Location"]);
}
[Fact]
public async Task ExecuteResultAsync_WithFragment_PassesCorrectValuesToRedirect()
{
// Arrange
var expectedUrl = "/SampleAction#test";
var expectedStatusCode = StatusCodes.Status301MovedPermanently;
var httpContext = GetHttpContext(expectedUrl);
var result = new RedirectToRouteResult("Sample", null, true, "test");
// Act
await result.ExecuteAsync(httpContext);
// Assert
Assert.Equal(expectedStatusCode, httpContext.Response.StatusCode);
Assert.Equal(expectedUrl, httpContext.Response.Headers["Location"]);
}
[Fact]
public async Task ExecuteResultAsync_WithFragment_PassesCorrectValuesToRedirect_WithPreserveMethod()
{
// Arrange
var expectedUrl = "/SampleAction#test";
var expectedStatusCode = StatusCodes.Status308PermanentRedirect;
var httpContext = GetHttpContext(expectedUrl);
var result = new RedirectToRouteResult("Sample", null, true, true, "test");
// Act
await result.ExecuteAsync(httpContext);
// Assert
Assert.Equal(expectedStatusCode, httpContext.Response.StatusCode);
Assert.Equal(expectedUrl, httpContext.Response.Headers["Location"]);
}
private static HttpContext GetHttpContext(string path)
{
var services = CreateServices(path);
var httpContext = new DefaultHttpContext();
httpContext.RequestServices = services.BuildServiceProvider();
return httpContext;
}
private static IServiceCollection CreateServices(string path)
{
var services = new ServiceCollection();
services.AddSingleton<LinkGenerator>(new TestLinkGenerator { Url = path });
services.AddSingleton(typeof(ILogger<>), typeof(NullLogger<>));
return services;
}
}
|