Add AcceptedAtActionResult tests
This commit is contained in:
parent
bfe50ddd8c
commit
1dc4b28bbe
|
|
@ -2,11 +2,9 @@
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net.Http;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Abstractions;
|
||||
using Microsoft.AspNetCore.Mvc.Formatters;
|
||||
|
|
@ -24,6 +22,60 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
{
|
||||
public class AcceptedAtActionResultTests
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData("actionName")]
|
||||
public void Constructor_InitializesActionName(string actionName)
|
||||
{
|
||||
// Act
|
||||
var result = new AcceptedAtActionResult(actionName: actionName, controllerName: null, routeValues: null, value: null);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(actionName, result.ActionName);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
[InlineData("controllerName")]
|
||||
public void Constructor_InitializesControllerName(string controllerName)
|
||||
{
|
||||
// Act
|
||||
var result = new AcceptedAtActionResult(actionName: null, controllerName: controllerName, routeValues: null, value: null);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(controllerName, result.ControllerName);
|
||||
}
|
||||
|
||||
public static TheoryData<object, int> RouteValuesData()
|
||||
{
|
||||
return new TheoryData<object, int>()
|
||||
{
|
||||
{ null, -1 },
|
||||
{ "value", 1 },
|
||||
{ new object(), 0 }
|
||||
};
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RouteValuesData))]
|
||||
public void Constructor_InitializesRouteValues(object routeValues, int expectedRouteValuesCount)
|
||||
{
|
||||
// Act
|
||||
var result = new AcceptedAtActionResult(actionName: null, controllerName: null, routeValues: routeValues, value: null);
|
||||
|
||||
// Assert
|
||||
if (expectedRouteValuesCount == -1)
|
||||
{
|
||||
Assert.Null(result.RouteValues);
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.Equal(expectedRouteValuesCount, result.RouteValues.Count);
|
||||
}
|
||||
}
|
||||
|
||||
public static TheoryData<object> ValuesData
|
||||
{
|
||||
get
|
||||
|
|
@ -56,6 +108,16 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
Assert.Same(value, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UrlHelper_Get_ReturnsNull()
|
||||
{
|
||||
// Act
|
||||
var result = new AcceptedAtActionResult(actionName: null, controllerName: null, routeValues: null, value: null);
|
||||
|
||||
// Assert
|
||||
Assert.Null(result.UrlHelper);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(ValuesData))]
|
||||
public async Task ExecuteResultAsync_SetsObjectValueOfFormatter(object value)
|
||||
|
|
@ -111,14 +173,16 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
Assert.Equal(expectedUrl, httpContext.Response.Headers["Location"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ExecuteResultAsync_ThrowsIfActionUrlIsNull()
|
||||
[Theory]
|
||||
[InlineData(null)]
|
||||
[InlineData("")]
|
||||
public async Task ExecuteResultAsync_ThrowsIfActionUrlIsNullOrEmpty(string returnValue)
|
||||
{
|
||||
// Arrange
|
||||
var formatter = CreateMockFormatter();
|
||||
var httpContext = GetHttpContext(formatter);
|
||||
var actionContext = GetActionContext(httpContext);
|
||||
var urlHelper = GetMockUrlHelper(returnValue: null);
|
||||
var urlHelper = GetMockUrlHelper(returnValue);
|
||||
|
||||
// Act
|
||||
var result = new AcceptedAtActionResult(
|
||||
|
|
@ -135,6 +199,46 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
"No route matches the supplied values.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OnFormatting_NullUrlHelperContextHasRequestServices_ReturnsRequestServicesAction()
|
||||
{
|
||||
// Arrange
|
||||
var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
|
||||
context.HttpContext.RequestServices = new ForwardingServiceProvider();
|
||||
|
||||
// Act
|
||||
var result = new AcceptedAtActionResult(actionName: null, controllerName: null, routeValues: null, value: null);
|
||||
result.OnFormatting(context);
|
||||
|
||||
// Assert
|
||||
var header = context.HttpContext.Response.Headers.Last();
|
||||
Assert.Equal("Location", header.Key);
|
||||
Assert.Equal("abc", header.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OnFormatting_NullUrlHelperContextNoRequestServices_ThrowsArgumentNullExeption()
|
||||
{
|
||||
// Arrange
|
||||
var context = new ActionContext(new DefaultHttpContext(), new RouteData(), new ActionDescriptor());
|
||||
|
||||
// Act
|
||||
var result = new AcceptedAtActionResult(actionName: null, controllerName: null, routeValues: null, value: null);
|
||||
|
||||
// Assert
|
||||
Assert.Throws<ArgumentNullException>("provider", () => result.OnFormatting(context));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OnFormatting_NullContext_ThrowsArgumentNullException()
|
||||
{
|
||||
// Act
|
||||
var result = new AcceptedAtActionResult("actionName", "controllerName", "routeValues", "value");
|
||||
|
||||
// Assert
|
||||
Assert.Throws<ArgumentNullException>("context", () => result.OnFormatting(null));
|
||||
}
|
||||
|
||||
private static ActionContext GetActionContext(HttpContext httpContext)
|
||||
{
|
||||
var routeData = new RouteData();
|
||||
|
|
@ -184,5 +288,32 @@ namespace Microsoft.AspNetCore.Mvc
|
|||
|
||||
return urlHelper.Object;
|
||||
}
|
||||
|
||||
private class ForwardingServiceProvider : IServiceProvider
|
||||
{
|
||||
public object GetService(Type serviceType) => new ForwardingUrlHelperFactory();
|
||||
}
|
||||
|
||||
private class ForwardingUrlHelperFactory : IUrlHelperFactory
|
||||
{
|
||||
public IUrlHelper GetUrlHelper(ActionContext context) => new ForwardingUrlHelper() { ActionValue = "abc" };
|
||||
}
|
||||
|
||||
private class ForwardingUrlHelper : IUrlHelper
|
||||
{
|
||||
public string ActionValue { get; set; }
|
||||
|
||||
public ActionContext ActionContext => null;
|
||||
|
||||
public string Action(UrlActionContext actionContext) => ActionValue;
|
||||
|
||||
public string Content(string contentPath) => null;
|
||||
|
||||
public bool IsLocalUrl(string url) => false;
|
||||
|
||||
public string Link(string routeName, object values) => null;
|
||||
|
||||
public string RouteUrl(UrlRouteContext routeContext) => null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue