You've already forked linux-packaging-mono
data
debian
docs
eglib
external
Lucene.Net.Light
Newtonsoft.Json
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft.Web.Helpers.Test
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
System.Web.Helpers.Test
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
Common
Controllers
Apis
ApiControllerActionInvokerTest.cs
ApiControllerActionSelectorTest.cs
ApiControllerTest.cs
HttpActionContextTest.cs
HttpActionDescriptorTest.cs
HttpConfigurationTest.cs
HttpControllerContextTest.cs
HttpControllerDescriptorTest.cs
HttpParameterDescriptorTest.cs
ReflectedHttpActionDescriptorTest.cs
ReflectedHttpParameterDescriptorTest.cs
ResponseMessageResultConverterTest.cs
ValueResultConverterTest.cs
VoidResultConverterTest.cs
Dispatcher
Filters
Hosting
Internal
Metadata
ModelBinding
Properties
Query
Routing
Services
Tracing
Util
Validation
ValueProviders
AuthorizeAttributeTest.cs
DictionaryExtensionsTest.cs
HttpRequestMessageExtensionsTest.cs
HttpResponseExceptionTest.cs
HttpResponseMessageExtensionsTest.cs
HttpRouteCollectionExtensionsTest.cs
HttpServerTest.cs
QueryableAttributeTest.cs
System.Web.Http.Test.csproj
packages.config
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
binary-reference-assemblies
cecil
debian-snapshot
ikdasm
ikvm
referencesource
rx
ikvm-native
libgc
m4
man
mcs
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
ChangeLog.REMOVED.git-id
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
118 lines
4.9 KiB
C#
118 lines
4.9 KiB
C#
![]() |
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
|
|||
|
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Net.Http;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Web.Http.Controllers;
|
|||
|
using Moq;
|
|||
|
using Xunit;
|
|||
|
using Assert = Microsoft.TestCommon.AssertEx;
|
|||
|
|
|||
|
namespace System.Web.Http
|
|||
|
{
|
|||
|
public class ApiControllerActionInvokerTest
|
|||
|
{
|
|||
|
private readonly HttpActionContext _actionContext;
|
|||
|
private readonly Mock<HttpActionDescriptor> _actionDescriptorMock = new Mock<HttpActionDescriptor>();
|
|||
|
private readonly ApiControllerActionInvoker _actionInvoker = new ApiControllerActionInvoker();
|
|||
|
private readonly HttpControllerContext _controllerContext;
|
|||
|
private readonly HttpRequestMessage _request = new HttpRequestMessage();
|
|||
|
private readonly Mock<IActionResultConverter> _converterMock = new Mock<IActionResultConverter>();
|
|||
|
|
|||
|
public ApiControllerActionInvokerTest()
|
|||
|
{
|
|||
|
_controllerContext = new HttpControllerContext()
|
|||
|
{
|
|||
|
Request = _request
|
|||
|
};
|
|||
|
_actionContext = new HttpActionContext(_controllerContext, _actionDescriptorMock.Object);
|
|||
|
_actionDescriptorMock.Setup(ad => ad.ResultConverter).Returns(_converterMock.Object);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void InvokeActionAsync_Cancels_IfCancellationTokenRequested()
|
|||
|
{
|
|||
|
CancellationTokenSource cancellationSource = new CancellationTokenSource();
|
|||
|
cancellationSource.Cancel();
|
|||
|
|
|||
|
var response = _actionInvoker.InvokeActionAsync(ContextUtil.CreateActionContext(), cancellationSource.Token);
|
|||
|
|
|||
|
Assert.Equal<TaskStatus>(TaskStatus.Canceled, response.Status);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void InvokeActionAsync_Throws_IfContextIsNull()
|
|||
|
{
|
|||
|
Assert.ThrowsArgumentNull(
|
|||
|
() => _actionInvoker.InvokeActionAsync(null, CancellationToken.None),
|
|||
|
"actionContext");
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void InvokeActionAsync_InvokesActionDescriptorExecuteAsync()
|
|||
|
{
|
|||
|
var result = _actionInvoker.InvokeActionAsync(_actionContext, CancellationToken.None);
|
|||
|
|
|||
|
result.WaitUntilCompleted();
|
|||
|
_actionDescriptorMock.Verify(ad => ad.ExecuteAsync(_actionContext.ControllerContext, _actionContext.ActionArguments), Times.Once());
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void InvokeActionAsync_PassesExecutionResultToConfiguredConverter()
|
|||
|
{
|
|||
|
var value = new object();
|
|||
|
_actionDescriptorMock.Setup(ad => ad.ExecuteAsync(_actionContext.ControllerContext, _actionContext.ActionArguments))
|
|||
|
.Returns(TaskHelpers.FromResult(value));
|
|||
|
|
|||
|
var result = _actionInvoker.InvokeActionAsync(_actionContext, CancellationToken.None);
|
|||
|
|
|||
|
result.WaitUntilCompleted();
|
|||
|
_converterMock.Verify(c => c.Convert(_actionContext.ControllerContext, value), Times.Once());
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void InvokeActionAsync_ReturnsResponseFromConverter()
|
|||
|
{
|
|||
|
var response = new HttpResponseMessage();
|
|||
|
_actionDescriptorMock.Setup(ad => ad.ExecuteAsync(_actionContext.ControllerContext, _actionContext.ActionArguments))
|
|||
|
.Returns(TaskHelpers.FromResult(new object()));
|
|||
|
_converterMock.Setup(c => c.Convert(_actionContext.ControllerContext, It.IsAny<object>()))
|
|||
|
.Returns(response);
|
|||
|
|
|||
|
var result = _actionInvoker.InvokeActionAsync(_actionContext, CancellationToken.None);
|
|||
|
|
|||
|
result.WaitUntilCompleted();
|
|||
|
Assert.Same(response, result.Result);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void InvokeActionAsync_WhenExecuteThrowsHttpResponseException_ReturnsResponse()
|
|||
|
{
|
|||
|
HttpResponseMessage response = new HttpResponseMessage();
|
|||
|
_actionDescriptorMock.Setup(ad => ad.ExecuteAsync(It.IsAny<HttpControllerContext>(), It.IsAny<IDictionary<string, object>>()))
|
|||
|
.Throws(new HttpResponseException(response));
|
|||
|
|
|||
|
var result = _actionInvoker.InvokeActionAsync(_actionContext, CancellationToken.None);
|
|||
|
|
|||
|
result.WaitUntilCompleted();
|
|||
|
Assert.Same(response, result.Result);
|
|||
|
Assert.Same(_request, result.Result.RequestMessage);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public void InvokeActionAsync_WhenExecuteThrows_ReturnsFaultedTask()
|
|||
|
{
|
|||
|
Exception exception = new Exception();
|
|||
|
_actionDescriptorMock.Setup(ad => ad.ExecuteAsync(It.IsAny<HttpControllerContext>(), It.IsAny<IDictionary<string, object>>()))
|
|||
|
.Throws(exception);
|
|||
|
|
|||
|
var result = _actionInvoker.InvokeActionAsync(_actionContext, CancellationToken.None);
|
|||
|
|
|||
|
result.WaitUntilCompleted();
|
|||
|
Assert.Equal(TaskStatus.Faulted, result.Status);
|
|||
|
Assert.Same(exception, result.Exception.GetBaseException());
|
|||
|
}
|
|||
|
}
|
|||
|
}
|