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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Dispatcher;
using Microsoft.TestCommon;
using Moq;
using Moq.Protected;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Web.Http
{
public class HttpServerTest
{
[Fact]
public void IsCorrectType()
{
Assert.Type.HasProperties<HttpServer, DelegatingHandler>(TypeAssert.TypeProperties.IsPublicVisibleClass | TypeAssert.TypeProperties.IsDisposable);
}
[Fact]
public void DefaultConstructor()
{
Assert.NotNull(new HttpServer());
}
[Fact]
public void ConstructorConfigThrowsOnNull()
{
Assert.ThrowsArgumentNull(() => new HttpServer((HttpConfiguration)null), "configuration");
}
[Fact]
public void ConstructorConfigSetsUpProperties()
{
// Arrange
HttpConfiguration config = new HttpConfiguration();
// Act
HttpServer server = new HttpServer(config);
// Assert
Assert.Same(config, server.Configuration);
}
[Fact]
public void ConstructorDispatcherThrowsOnNull()
{
Assert.ThrowsArgumentNull(() => new HttpServer((HttpControllerDispatcher)null), "dispatcher");
}
[Fact]
public void ConstructorDispatcherSetsUpProperties()
{
// Arrange
Mock<HttpControllerDispatcher> controllerDispatcherMock = new Mock<HttpControllerDispatcher>();
// Act
HttpServer server = new HttpServer(controllerDispatcherMock.Object);
// Assert
Assert.Same(controllerDispatcherMock.Object, server.Dispatcher);
}
[Fact]
public void ConstructorThrowsOnNull()
{
Mock<HttpControllerDispatcher> controllerDispatcherMock = new Mock<HttpControllerDispatcher>();
Assert.ThrowsArgumentNull(() => new HttpServer((HttpConfiguration)null, controllerDispatcherMock.Object), "configuration");
Assert.ThrowsArgumentNull(() => new HttpServer(new HttpConfiguration(), null), "dispatcher");
}
[Fact]
public void ConstructorSetsUpProperties()
{
// Arrange
HttpConfiguration config = new HttpConfiguration();
Mock<HttpControllerDispatcher> controllerDispatcherMock = new Mock<HttpControllerDispatcher>();
// Act
HttpServer server = new HttpServer(config, controllerDispatcherMock.Object);
// Assert
Assert.Same(config, server.Configuration);
Assert.Same(controllerDispatcherMock.Object, server.Dispatcher);
}
[Fact]
public Task<HttpResponseMessage> DisposedReturnsServiceUnavailable()
{
// Arrange
Mock<HttpControllerDispatcher> dispatcherMock = new Mock<HttpControllerDispatcher>();
HttpServer server = new HttpServer(dispatcherMock.Object);
HttpMessageInvoker invoker = new HttpMessageInvoker(server);
server.Dispose();
HttpRequestMessage request = new HttpRequestMessage();
// Act
return invoker.SendAsync(request, CancellationToken.None).ContinueWith(
(reqTask) =>
{
// Assert
dispatcherMock.Protected().Verify<Task<HttpResponseMessage>>("SendAsync", Times.Never(), request, CancellationToken.None);
Assert.Equal(HttpStatusCode.ServiceUnavailable, reqTask.Result.StatusCode);
return reqTask.Result;
}
);
}
[Fact]
public Task<HttpResponseMessage> RequestGetsConfigurationAsParameter()
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage();
Mock<HttpControllerDispatcher> dispatcherMock = new Mock<HttpControllerDispatcher>();
dispatcherMock.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", request, CancellationToken.None).
Returns(TaskHelpers.FromResult<HttpResponseMessage>(request.CreateResponse()));
HttpConfiguration config = new HttpConfiguration();
HttpServer server = new HttpServer(config, dispatcherMock.Object);
HttpMessageInvoker invoker = new HttpMessageInvoker(server);
// Act
return invoker.SendAsync(request, CancellationToken.None).ContinueWith(
(reqTask) =>
{
// Assert
dispatcherMock.Protected().Verify<Task<HttpResponseMessage>>("SendAsync", Times.Once(), request, CancellationToken.None);
Assert.Same(config, request.GetConfiguration());
return reqTask.Result;
}
);
}
[Fact]
public Task<HttpResponseMessage> RequestGetsSyncContextAsParameter()
{
// Arrange
HttpRequestMessage request = new HttpRequestMessage();
Mock<HttpControllerDispatcher> dispatcherMock = new Mock<HttpControllerDispatcher>();
dispatcherMock.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", request, CancellationToken.None).
Returns(TaskHelpers.FromResult<HttpResponseMessage>(request.CreateResponse()));
HttpConfiguration config = new HttpConfiguration();
HttpServer server = new HttpServer(config, dispatcherMock.Object);
HttpMessageInvoker invoker = new HttpMessageInvoker(server);
SynchronizationContext syncContext = new SynchronizationContext();
SynchronizationContext.SetSynchronizationContext(syncContext);
// Act
return invoker.SendAsync(request, CancellationToken.None).ContinueWith(
(reqTask) =>
{
// Assert
dispatcherMock.Protected().Verify<Task<HttpResponseMessage>>("SendAsync", Times.Once(), request, CancellationToken.None);
Assert.Same(syncContext, request.GetSynchronizationContext());
return reqTask.Result;
}
);
}
[Fact, RestoreThreadPrincipal]
public Task SendAsync_SetsGenericPrincipalWhenThreadPrincipalIsNullAndCleansUpAfterward()
{
// Arrange
var config = new HttpConfiguration();
var request = new HttpRequestMessage();
var dispatcherMock = new Mock<HttpControllerDispatcher>();
var server = new HttpServer(config, dispatcherMock.Object);
var invoker = new HttpMessageInvoker(server);
IPrincipal callbackPrincipal = null;
Thread.CurrentPrincipal = null;
dispatcherMock.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", request, CancellationToken.None)
.Callback(() => callbackPrincipal = Thread.CurrentPrincipal)
.Returns(TaskHelpers.FromResult<HttpResponseMessage>(request.CreateResponse()));
// Act
return invoker.SendAsync(request, CancellationToken.None)
.ContinueWith(req =>
{
// Assert
Assert.NotNull(callbackPrincipal);
Assert.False(callbackPrincipal.Identity.IsAuthenticated);
Assert.Empty(callbackPrincipal.Identity.Name);
Assert.Null(Thread.CurrentPrincipal);
});
}
[Fact, RestoreThreadPrincipal]
public Task SendAsync_DoesNotChangeExistingThreadPrincipal()
{
// Arrange
var config = new HttpConfiguration();
var request = new HttpRequestMessage();
var dispatcherMock = new Mock<HttpControllerDispatcher>();
var server = new HttpServer(config, dispatcherMock.Object);
var invoker = new HttpMessageInvoker(server);
var principal = new GenericPrincipal(new GenericIdentity("joe"), new string[0]);
Thread.CurrentPrincipal = principal;
IPrincipal callbackPrincipal = null;
dispatcherMock.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", request, CancellationToken.None)
.Callback(() => callbackPrincipal = Thread.CurrentPrincipal)
.Returns(TaskHelpers.FromResult<HttpResponseMessage>(request.CreateResponse()));
// Act
return invoker.SendAsync(request, CancellationToken.None)
.ContinueWith(req =>
{
// Assert
Assert.Same(principal, callbackPrincipal);
Assert.Same(principal, Thread.CurrentPrincipal);
});
}
}
}
|