Add remaining unit tests for HttpRequestContext.

This commit is contained in:
davidmatson 2013-09-09 16:06:08 -07:00
Родитель d9684fbfaf
Коммит 96ecfae18e
25 изменённых файлов: 3747 добавлений и 663 удалений

Просмотреть файл

@ -5,14 +5,12 @@ using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Hosting;
using System.Web.Http.Owin.Properties;
using System.Web.Http.Routing;
using Microsoft.Owin;
namespace System.Web.Http.Owin
@ -155,7 +153,7 @@ namespace System.Web.Http.Owin
// Set the OWIN context on the request
request.SetOwinContext(context);
// Set the a request context on the request that lazily populates each property.
// Set a request context on the request that lazily populates each property.
HttpRequestContext requestContext = new OwinHttpRequestContext(context, request);
request.SetRequestContext(requestContext);
}
@ -290,177 +288,5 @@ namespace System.Web.Http.Owin
Dispose(true);
GC.SuppressFinalize(this);
}
private class OwinHttpRequestContext : HttpRequestContext
{
private readonly IOwinContext _context;
private readonly HttpRequestMessage _request;
private X509Certificate2 _clientCertificate;
private bool _clientCertificateSet;
private bool _includeErrorDetail;
private bool _includeErrorDetailSet;
private bool _isLocal;
private bool _isLocalSet;
private IPrincipal _principal;
private bool _principalSet;
private UrlHelper _url;
private bool _urlSet;
private string _virtualPathRoot;
private bool _virtualPathRootSet;
public OwinHttpRequestContext(IOwinContext context, HttpRequestMessage request)
{
Contract.Assert(context != null);
Contract.Assert(context.Request != null);
Contract.Assert(request != null);
_context = context;
_request = request;
}
// Configuration and RouteData are not overridden; these values are provided by later points in the
// pipeline (HttpServer and HttpRoutingDispatcher)
public override X509Certificate2 ClientCertificate
{
get
{
if (!_clientCertificateSet)
{
_clientCertificate = _context.Get<X509Certificate2>(OwinConstants.ClientCertifiateKey);
_clientCertificateSet = true;
}
return _clientCertificate;
}
set
{
_clientCertificate = value;
_clientCertificateSet = true;
}
}
public override bool IncludeErrorDetail
{
get
{
if (!_includeErrorDetailSet)
{
HttpConfiguration configuration = Configuration;
IncludeErrorDetailPolicy includeErrorDetailPolicy = IncludeErrorDetailPolicy.Default;
if (configuration != null)
{
includeErrorDetailPolicy = configuration.IncludeErrorDetailPolicy;
}
switch (includeErrorDetailPolicy)
{
case IncludeErrorDetailPolicy.Default:
case IncludeErrorDetailPolicy.LocalOnly:
_includeErrorDetail = IsLocal;
break;
case IncludeErrorDetailPolicy.Always:
_includeErrorDetail = true;
break;
case IncludeErrorDetailPolicy.Never:
default:
_includeErrorDetail = false;
break;
}
_includeErrorDetailSet = true;
}
return _includeErrorDetail;
}
set
{
_includeErrorDetail = value;
_includeErrorDetailSet = true;
}
}
public override bool IsLocal
{
get
{
if (!_isLocalSet)
{
_isLocal = _context.Get<bool>(OwinConstants.IsLocalKey);
_isLocalSet = true;
}
return _isLocal;
}
set
{
_isLocal = value;
_isLocalSet = true;
}
}
public override IPrincipal Principal
{
get
{
if (!_principalSet)
{
_principal = _context.Request.User;
_principalSet = true;
}
return _principal;
}
set
{
_principal = value;
_principalSet = true;
}
}
public override UrlHelper Url
{
get
{
if (!_urlSet)
{
_url = new UrlHelper(_request);
_urlSet = true;
}
return _url;
}
set
{
_url = value;
_urlSet = true;
}
}
public override string VirtualPathRoot
{
get
{
if (!_virtualPathRootSet)
{
// Set the virtual path root for link resolution and link generation to work
// OWIN spec requires request path base to be either the empty string or start with "/"
string requestPathBase = _context.Request.PathBase.ToString();
_virtualPathRoot = String.IsNullOrEmpty(requestPathBase) ? "/" : requestPathBase;
_virtualPathRootSet = true;
}
return _virtualPathRoot;
}
set
{
_virtualPathRoot = value;
_virtualPathRootSet = true;
}
}
}
}
}

Просмотреть файл

@ -0,0 +1,206 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Diagnostics.Contracts;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Web.Http.Controllers;
using System.Web.Http.Routing;
using Microsoft.Owin;
namespace System.Web.Http.Owin
{
internal class OwinHttpRequestContext : HttpRequestContext
{
private readonly IOwinContext _context;
private readonly HttpRequestMessage _request;
private X509Certificate2 _clientCertificate;
private bool _clientCertificateSet;
private bool _includeErrorDetail;
private bool _includeErrorDetailSet;
private bool _isLocal;
private bool _isLocalSet;
private IPrincipal _principal;
private bool _principalSet;
private UrlHelper _url;
private bool _urlSet;
private string _virtualPathRoot;
private bool _virtualPathRootSet;
public OwinHttpRequestContext(IOwinContext context, HttpRequestMessage request)
{
Contract.Assert(context != null);
Contract.Assert(context.Request != null);
Contract.Assert(request != null);
_context = context;
_request = request;
}
public IOwinContext Context
{
get { return _context; }
}
public HttpRequestMessage Request
{
get { return _request; }
}
//// Configuration and RouteData are not overridden; these values are provided by later points in the
//// pipeline (HttpServer and HttpRoutingDispatcher)
public override X509Certificate2 ClientCertificate
{
get
{
if (!_clientCertificateSet)
{
_clientCertificate = _context.Get<X509Certificate2>(OwinConstants.ClientCertifiateKey);
_clientCertificateSet = true;
}
return _clientCertificate;
}
set
{
_clientCertificate = value;
_clientCertificateSet = true;
}
}
public override bool IncludeErrorDetail
{
get
{
if (!_includeErrorDetailSet)
{
HttpConfiguration configuration = Configuration;
IncludeErrorDetailPolicy includeErrorDetailPolicy;
if (configuration != null)
{
includeErrorDetailPolicy = configuration.IncludeErrorDetailPolicy;
}
else
{
includeErrorDetailPolicy = IncludeErrorDetailPolicy.Default;
}
bool value;
switch (includeErrorDetailPolicy)
{
case IncludeErrorDetailPolicy.Default:
case IncludeErrorDetailPolicy.LocalOnly:
value = IsLocal;
break;
case IncludeErrorDetailPolicy.Always:
value = true;
break;
case IncludeErrorDetailPolicy.Never:
default:
value = false;
break;
}
_includeErrorDetail = value;
_includeErrorDetailSet = true;
}
return _includeErrorDetail;
}
set
{
_includeErrorDetail = value;
_includeErrorDetailSet = true;
}
}
public override bool IsLocal
{
get
{
if (!_isLocalSet)
{
_isLocal = _context.Get<bool>(OwinConstants.IsLocalKey);
_isLocalSet = true;
}
return _isLocal;
}
set
{
_isLocal = value;
_isLocalSet = true;
}
}
public override IPrincipal Principal
{
get
{
if (!_principalSet)
{
_principal = _context.Request.User;
_principalSet = true;
}
return _principal;
}
set
{
_principal = value;
_principalSet = true;
}
}
public override UrlHelper Url
{
get
{
if (!_urlSet)
{
_url = new UrlHelper(_request);
_urlSet = true;
}
return _url;
}
set
{
_url = value;
_urlSet = true;
}
}
public override string VirtualPathRoot
{
get
{
if (!_virtualPathRootSet)
{
// Set the virtual path root for link resolution and link generation to work
// OWIN spec requires request path base to be either the empty string or start with "/"
string requestPathBase = _context.Request.PathBase.ToString();
_virtualPathRoot = String.IsNullOrEmpty(requestPathBase) ? "/" : requestPathBase;
_virtualPathRootSet = true;
}
return _virtualPathRoot;
}
set
{
_virtualPathRoot = value;
_virtualPathRootSet = true;
}
}
}
}

Просмотреть файл

@ -53,6 +53,7 @@
<Compile Include="OwinConstants.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="HttpMessageHandlerAdapter.cs" />
<Compile Include="OwinHttpRequestContext.cs" />
<Compile Include="OwinHttpRequestMessageExtensions.cs" />
<Compile Include="PassiveAuthenticationMessageHandler.cs" />
<Compile Include="WebApiAppBuilderExtensions.cs" />

Просмотреть файл

@ -4,14 +4,13 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.ServiceModel.Security;
using System.Web.Http;
using System.Web.Http.SelfHost;
namespace System.Net.Http
{
[EditorBrowsable(EditorBrowsableState.Never)]
public static class HttpRequestMessageExtensions
{
private const string SecurityKey = "Security";
/// <summary>
/// Gets the current <see cref="T:System.ServiceModel.Security.SecurityMessageProperty"/>
/// stored in <see cref="M:HttpRequestMessage.Properties"/> for the given request.
@ -25,7 +24,7 @@ namespace System.Net.Http
throw Error.ArgumentNull("request");
}
return request.GetProperty<SecurityMessageProperty>(SecurityKey);
return request.GetProperty<SecurityMessageProperty>(HttpSelfHostServer.SecurityKey);
}
private static T GetProperty<T>(this HttpRequestMessage request, string key)

Просмотреть файл

@ -28,6 +28,8 @@ namespace System.Web.Http.SelfHost
/// </summary>
public sealed class HttpSelfHostServer : HttpServer
{
internal const string SecurityKey = "Security";
private static readonly AsyncCallback _onCloseListenerComplete = new AsyncCallback(OnCloseListenerComplete);
private static readonly AsyncCallback _onCloseChannelComplete = new AsyncCallback(OnCloseChannelComplete);
@ -236,7 +238,7 @@ namespace System.Web.Http.SelfHost
return request;
}
private static bool IsLocal(Message message)
internal static bool IsLocal(Message message)
{
RemoteEndpointMessageProperty remoteEndpointProperty;
if (message.Properties.TryGetValue(RemoteEndpointMessageProperty.Name, out remoteEndpointProperty))
@ -268,7 +270,7 @@ namespace System.Web.Http.SelfHost
}
}
private static X509Certificate2 RetrieveClientCertificate(HttpRequestMessage request)
internal static X509Certificate2 RetrieveClientCertificate(HttpRequestMessage request)
{
if (request == null)
{
@ -1084,169 +1086,5 @@ namespace System.Web.Http.SelfHost
}
}
}
private class SelfHostHttpRequestContext : HttpRequestContext
{
private readonly RequestContext _requestContext;
private readonly HttpRequestMessage _request;
private HttpConfiguration _configuration;
private X509Certificate2 _clientCertificate;
private bool _clientCertificateSet;
private bool _includeErrorDetail;
private bool _includeErrorDetailSet;
private bool _isLocal;
private bool _isLocalSet;
private UrlHelper _url;
private bool _urlSet;
private string _virtualPathRoot;
private bool _virtualPathRootSet;
public SelfHostHttpRequestContext(RequestContext requestContext, HttpConfiguration configuration,
HttpRequestMessage request)
{
Contract.Assert(requestContext != null);
Contract.Assert(configuration != null);
Contract.Assert(request != null);
_requestContext = requestContext;
_configuration = configuration;
_request = request;
}
// Principal and RouteData are not overridden; they are provided by later points in the pipeline
// (HttpServer and HttpRoutingDispatcher).
public override X509Certificate2 ClientCertificate
{
get
{
if (!_clientCertificateSet)
{
_clientCertificate = RetrieveClientCertificate(_request);
_clientCertificateSet = true;
}
return _clientCertificate;
}
set
{
_clientCertificate = value;
_clientCertificateSet = true;
}
}
public override HttpConfiguration Configuration
{
get
{
return _configuration;
}
set
{
_configuration = value;
}
}
public override bool IncludeErrorDetail
{
get
{
if (!_includeErrorDetailSet)
{
HttpConfiguration configuration = Configuration;
IncludeErrorDetailPolicy includeErrorDetailPolicy = IncludeErrorDetailPolicy.Default;
if (configuration != null)
{
includeErrorDetailPolicy = configuration.IncludeErrorDetailPolicy;
}
switch (includeErrorDetailPolicy)
{
case IncludeErrorDetailPolicy.Default:
case IncludeErrorDetailPolicy.LocalOnly:
_includeErrorDetail = IsLocal;
break;
case IncludeErrorDetailPolicy.Always:
_includeErrorDetail = true;
break;
case IncludeErrorDetailPolicy.Never:
default:
_includeErrorDetail = false;
break;
}
_includeErrorDetailSet = true;
}
return _includeErrorDetail;
}
set
{
_includeErrorDetail = value;
_includeErrorDetailSet = true;
}
}
public override bool IsLocal
{
get
{
if (!_isLocalSet)
{
_isLocal = IsLocal(_requestContext.RequestMessage);
_isLocalSet = true;
}
return _isLocal;
}
set
{
_isLocal = value;
_isLocalSet = true;
}
}
public override UrlHelper Url
{
get
{
if (!_urlSet)
{
_url = new UrlHelper(_request);
_urlSet = true;
}
return _url;
}
set
{
_url = value;
_urlSet = true;
}
}
public override string VirtualPathRoot
{
get
{
if (!_virtualPathRootSet)
{
_virtualPathRoot = _configuration.VirtualPathRoot;
_virtualPathRootSet = true;
}
return _virtualPathRoot;
}
set
{
_virtualPathRoot = value;
_virtualPathRootSet = true;
}
}
}
}
}

Просмотреть файл

@ -0,0 +1,220 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.IdentityModel.Claims;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Security;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Hosting;
using System.Web.Http.Routing;
using System.Web.Http.SelfHost.Channels;
using System.Web.Http.SelfHost.Properties;
using System.Web.Http.SelfHost.ServiceModel.Channels;
namespace System.Web.Http.SelfHost
{
internal class SelfHostHttpRequestContext : HttpRequestContext
{
private readonly RequestContext _requestContext;
private readonly HttpRequestMessage _request;
private HttpConfiguration _configuration;
private X509Certificate2 _clientCertificate;
private bool _clientCertificateSet;
private bool _includeErrorDetail;
private bool _includeErrorDetailSet;
private bool _isLocal;
private bool _isLocalSet;
private UrlHelper _url;
private bool _urlSet;
private string _virtualPathRoot;
private bool _virtualPathRootSet;
public SelfHostHttpRequestContext(RequestContext requestContext, HttpConfiguration configuration,
HttpRequestMessage request)
{
Contract.Assert(requestContext != null);
Contract.Assert(configuration != null);
Contract.Assert(request != null);
_requestContext = requestContext;
_configuration = configuration;
_request = request;
}
public RequestContext RequestContext
{
get { return _requestContext; }
}
public HttpRequestMessage Request
{
get { return _request; }
}
// Principal and RouteData are not overridden; they are provided by later points in the pipeline
// (HttpServer and HttpRoutingDispatcher).
public override X509Certificate2 ClientCertificate
{
get
{
if (!_clientCertificateSet)
{
_clientCertificate = HttpSelfHostServer.RetrieveClientCertificate(_request);
_clientCertificateSet = true;
}
return _clientCertificate;
}
set
{
_clientCertificate = value;
_clientCertificateSet = true;
}
}
public override HttpConfiguration Configuration
{
get
{
return _configuration;
}
set
{
_configuration = value;
}
}
public override bool IncludeErrorDetail
{
get
{
if (!_includeErrorDetailSet)
{
IncludeErrorDetailPolicy includeErrorDetailPolicy;
if (_configuration != null)
{
includeErrorDetailPolicy = _configuration.IncludeErrorDetailPolicy;
}
else
{
includeErrorDetailPolicy = IncludeErrorDetailPolicy.Default;
}
bool value;
switch (includeErrorDetailPolicy)
{
case IncludeErrorDetailPolicy.Default:
case IncludeErrorDetailPolicy.LocalOnly:
value = IsLocal;
break;
case IncludeErrorDetailPolicy.Always:
value = true;
break;
case IncludeErrorDetailPolicy.Never:
default:
value = false;
break;
}
_includeErrorDetail = value;
_includeErrorDetailSet = true;
}
return _includeErrorDetail;
}
set
{
_includeErrorDetail = value;
_includeErrorDetailSet = true;
}
}
public override bool IsLocal
{
get
{
if (!_isLocalSet)
{
_isLocal = HttpSelfHostServer.IsLocal(_requestContext.RequestMessage);
_isLocalSet = true;
}
return _isLocal;
}
set
{
_isLocal = value;
_isLocalSet = true;
}
}
public override UrlHelper Url
{
get
{
if (!_urlSet)
{
_url = new UrlHelper(_request);
_urlSet = true;
}
return _url;
}
set
{
_url = value;
_urlSet = true;
}
}
public override string VirtualPathRoot
{
get
{
if (!_virtualPathRootSet)
{
string value;
if (_configuration != null)
{
value = _configuration.VirtualPathRoot;
}
else
{
value = "/";
}
_virtualPathRoot = value;
_virtualPathRootSet = true;
}
return _virtualPathRoot;
}
set
{
_virtualPathRoot = value;
_virtualPathRootSet = true;
}
}
}
}

Просмотреть файл

@ -62,6 +62,7 @@
<Compile Include="Channels\HttpBindingSecurityModeHelper.cs" />
<Compile Include="HttpSelfHostConfiguration.cs" />
<Compile Include="HttpSelfHostServer.cs" />
<Compile Include="SelfHostHttpRequestContext.cs" />
<Compile Include="ServiceModel\Channels\AsyncResult.cs" />
<Compile Include="ServiceModel\Channels\BufferedOutputStream.cs" />
<Compile Include="ServiceModel\Channels\BufferManagerOutputStream.cs" />

Просмотреть файл

@ -1,16 +1,13 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Configuration;
@ -513,199 +510,6 @@ namespace System.Web.Http.WebHost
return result;
}
private class WebHostHttpRequestContext : HttpRequestContext
{
private readonly HttpContextBase _contextBase;
private readonly HttpRequestBase _requestBase;
private readonly HttpRequestMessage _request;
private X509Certificate2 _clientCertificate;
private bool _clientCertificateSet;
private HttpConfiguration _configuration;
private bool _configurationSet;
private bool _includeErrorDetail;
private bool _includeErrorDetailSet;
private bool _isLocal;
private bool _isLocalSet;
private IPrincipal _principal;
private bool _principalSet;
private UrlHelper _url;
private bool _urlSet;
private string _virtualPathRoot;
private bool _virtualPathRootSet;
public WebHostHttpRequestContext(HttpContextBase contextBase, HttpRequestBase requestBase,
HttpRequestMessage request)
{
Contract.Assert(contextBase != null);
Contract.Assert(requestBase != null);
Contract.Assert(request != null);
_contextBase = contextBase;
_requestBase = requestBase;
_request = request;
}
// RouteData is not overridden; it is provided by a later point in the pipeline
// (HttpControllerHandler.ProcessRequestAsyncCore).
public override X509Certificate2 ClientCertificate
{
get
{
if (!_clientCertificateSet)
{
if (_requestBase.ClientCertificate.Certificate != null
&& _requestBase.ClientCertificate.Certificate.Length > 0)
{
_clientCertificate = new X509Certificate2(_requestBase.ClientCertificate.Certificate);
}
_clientCertificateSet = true;
}
return _clientCertificate;
}
set
{
_clientCertificate = value;
_clientCertificateSet = true;
}
}
public override HttpConfiguration Configuration
{
get
{
if (!_configurationSet)
{
_configuration = GlobalConfiguration.Configuration;
_configurationSet = true;
}
return _configuration;
}
set
{
_configuration = value;
_configurationSet = true;
}
}
public override bool IncludeErrorDetail
{
get
{
if (!_includeErrorDetailSet)
{
switch (GlobalConfiguration.Configuration.IncludeErrorDetailPolicy)
{
case IncludeErrorDetailPolicy.Default:
_includeErrorDetail = !_contextBase.IsCustomErrorEnabled;
break;
case IncludeErrorDetailPolicy.LocalOnly:
_includeErrorDetail = IsLocal;
break;
case IncludeErrorDetailPolicy.Always:
_includeErrorDetail = true;
break;
case IncludeErrorDetailPolicy.Never:
default:
_includeErrorDetail = false;
break;
}
_includeErrorDetailSet = true;
}
return _includeErrorDetail;
}
set
{
_includeErrorDetail = value;
_includeErrorDetailSet = true;
}
}
public override bool IsLocal
{
get
{
if (!_isLocalSet)
{
_isLocal = _requestBase.IsLocal;
_isLocalSet = true;
}
return _isLocal;
}
set
{
_isLocal = value;
_isLocalSet = true;
}
}
public override IPrincipal Principal
{
get
{
if (!_principalSet)
{
_principal = _contextBase.User;
_principalSet = true;
}
return _principal;
}
set
{
_principal = value;
_principalSet = true;
}
}
public override UrlHelper Url
{
get
{
if (!_urlSet)
{
_url = new UrlHelper(_request);
_urlSet = true;
}
return _url;
}
set
{
_url = value;
_urlSet = true;
}
}
public override string VirtualPathRoot
{
get
{
if (!_virtualPathRootSet)
{
_virtualPathRoot = GlobalConfiguration.Configuration.VirtualPathRoot;
_virtualPathRootSet = true;
}
return _virtualPathRoot;
}
set
{
_virtualPathRoot = value;
_virtualPathRootSet = true;
}
}
}
private class DelegatingStreamContent : StreamContent
{
public DelegatingStreamContent(Stream stream)

Просмотреть файл

@ -78,6 +78,7 @@
<Compile Include="HttpControllerRouteHandler.cs" />
<Compile Include="RouteCollectionExtensions.cs" />
<Compile Include="TaskWrapperAsyncResult.cs" />
<Compile Include="WebHostHttpRequestContext.cs" />
<Compile Include="WebHostPrincipalService.cs" />
</ItemGroup>
<ItemGroup>

Просмотреть файл

@ -0,0 +1,257 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Diagnostics.Contracts;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Web.Http.Controllers;
using System.Web.Http.Routing;
namespace System.Web.Http.WebHost
{
internal class WebHostHttpRequestContext : HttpRequestContext
{
private readonly HttpContextBase _contextBase;
private readonly HttpRequestBase _requestBase;
private readonly HttpRequestMessage _request;
private X509Certificate2 _clientCertificate;
private bool _clientCertificateSet;
private HttpConfiguration _configuration;
private bool _configurationSet;
private bool _includeErrorDetail;
private bool _includeErrorDetailSet;
private bool _isLocal;
private bool _isLocalSet;
private IPrincipal _principal;
private bool _principalSet;
private UrlHelper _url;
private bool _urlSet;
private string _virtualPathRoot;
private bool _virtualPathRootSet;
public WebHostHttpRequestContext(HttpContextBase contextBase, HttpRequestBase requestBase,
HttpRequestMessage request)
{
Contract.Assert(contextBase != null);
Contract.Assert(requestBase != null);
Contract.Assert(request != null);
_contextBase = contextBase;
_requestBase = requestBase;
_request = request;
}
public HttpContextBase Context
{
get { return _contextBase; }
}
public HttpRequestBase WebRequest
{
get { return _requestBase; }
}
public HttpRequestMessage Request
{
get { return _request; }
}
// RouteData is not overridden; it is provided by a later point in the pipeline
// (HttpControllerHandler.ProcessRequestAsyncCore).
public override X509Certificate2 ClientCertificate
{
get
{
if (!_clientCertificateSet)
{
X509Certificate2 value;
if (_requestBase.ClientCertificate != null && _requestBase.ClientCertificate.Certificate != null
&& _requestBase.ClientCertificate.Certificate.Length > 0)
{
value = new X509Certificate2(_requestBase.ClientCertificate.Certificate);
}
else
{
value = null;
}
_clientCertificate = value;
_clientCertificateSet = true;
}
return _clientCertificate;
}
set
{
_clientCertificate = value;
_clientCertificateSet = true;
}
}
public override HttpConfiguration Configuration
{
get
{
if (!_configurationSet)
{
_configuration = GlobalConfiguration.Configuration;
_configurationSet = true;
}
return _configuration;
}
set
{
_configuration = value;
_configurationSet = true;
}
}
public override bool IncludeErrorDetail
{
get
{
if (!_includeErrorDetailSet)
{
IncludeErrorDetailPolicy includeErrorDetailPolicy;
if (_configuration != null)
{
includeErrorDetailPolicy = _configuration.IncludeErrorDetailPolicy;
}
else
{
includeErrorDetailPolicy = IncludeErrorDetailPolicy.Default;
}
bool value;
switch (includeErrorDetailPolicy)
{
case IncludeErrorDetailPolicy.Default:
value = !_contextBase.IsCustomErrorEnabled;
break;
case IncludeErrorDetailPolicy.LocalOnly:
value = IsLocal;
break;
case IncludeErrorDetailPolicy.Always:
value = true;
break;
case IncludeErrorDetailPolicy.Never:
default:
value = false;
break;
}
_includeErrorDetail = value;
_includeErrorDetailSet = true;
}
return _includeErrorDetail;
}
set
{
_includeErrorDetail = value;
_includeErrorDetailSet = true;
}
}
public override bool IsLocal
{
get
{
if (!_isLocalSet)
{
_isLocal = _requestBase.IsLocal;
_isLocalSet = true;
}
return _isLocal;
}
set
{
_isLocal = value;
_isLocalSet = true;
}
}
public override IPrincipal Principal
{
get
{
if (!_principalSet)
{
_principal = _contextBase.User;
_principalSet = true;
}
return _principal;
}
set
{
_principal = value;
_principalSet = true;
}
}
public override UrlHelper Url
{
get
{
if (!_urlSet)
{
_url = new UrlHelper(_request);
_urlSet = true;
}
return _url;
}
set
{
_url = value;
_urlSet = true;
}
}
public override string VirtualPathRoot
{
get
{
if (!_virtualPathRootSet)
{
string value;
if (_configuration != null)
{
value = _configuration.VirtualPathRoot;
}
else
{
value = null;
}
_virtualPathRoot = value;
_virtualPathRootSet = true;
}
return _virtualPathRoot;
}
set
{
_virtualPathRoot = value;
_virtualPathRootSet = true;
}
}
}
}

Просмотреть файл

@ -0,0 +1,131 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Web.Http.Controllers;
using System.Web.Http.Routing;
namespace System.Web.Http.Batch
{
internal class BatchHttpRequestContext : HttpRequestContext
{
private readonly HttpRequestContext _batchContext;
public BatchHttpRequestContext(HttpRequestContext batchContext)
{
if (batchContext == null)
{
throw new ArgumentNullException("batchContext");
}
_batchContext = batchContext;
}
public HttpRequestContext BatchContext
{
get { return _batchContext; }
}
public override X509Certificate2 ClientCertificate
{
get
{
return _batchContext.ClientCertificate;
}
set
{
_batchContext.ClientCertificate = value;
}
}
public override HttpConfiguration Configuration
{
get
{
// Use separate route data for configuration (base, not _batchContext).
return base.Configuration;
}
set
{
// Use separate route data for configuration (base, not _batchContext).
base.Configuration = value;
}
}
public override bool IncludeErrorDetail
{
get
{
return _batchContext.IncludeErrorDetail;
}
set
{
_batchContext.IncludeErrorDetail = value;
}
}
public override bool IsLocal
{
get
{
return _batchContext.IsLocal;
}
set
{
_batchContext.IsLocal = value;
}
}
public override IPrincipal Principal
{
get
{
return _batchContext.Principal;
}
set
{
_batchContext.Principal = value;
}
}
public override IHttpRouteData RouteData
{
get
{
// Use separate route data for batching (base, not _batchContext).
return base.RouteData;
}
set
{
// Use separate route data for batching (base, not _batchContext).
base.RouteData = value;
}
}
public override UrlHelper Url
{
get
{
// Use a separate URL factory for batching (base, not _batchContext).
return base.Url;
}
set
{
// Use a separate URL factory for batching (base, not _batchContext).
base.Url = value;
}
}
public override string VirtualPathRoot
{
get
{
return _batchContext.VirtualPathRoot;
}
set
{
_batchContext.VirtualPathRoot = value;
}
}
}
}

Просмотреть файл

@ -4,11 +4,8 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Web.Http.Controllers;
using System.Web.Http.Hosting;
using System.Web.Http.Routing;
namespace System.Web.Http.Batch
{
@ -60,122 +57,5 @@ namespace System.Web.Http.Batch
subRequest.SetRequestContext(new BatchHttpRequestContext(originalContext));
}
}
private class BatchHttpRequestContext : HttpRequestContext
{
private readonly HttpRequestContext _batchContext;
public BatchHttpRequestContext(HttpRequestContext batchContext)
{
if (batchContext == null)
{
throw new ArgumentNullException("batchContext");
}
_batchContext = batchContext;
}
public override X509Certificate2 ClientCertificate
{
get
{
return _batchContext.ClientCertificate;
}
set
{
_batchContext.ClientCertificate = value;
}
}
public override HttpConfiguration Configuration
{
get
{
// Use separate route data for configuration (base, not _batchContext).
return base.Configuration;
}
set
{
// Use separate route data for configuration (base, not _batchContext).
base.Configuration = value;
}
}
public override bool IncludeErrorDetail
{
get
{
return _batchContext.IncludeErrorDetail;
}
set
{
_batchContext.IncludeErrorDetail = value;
}
}
public override bool IsLocal
{
get
{
return _batchContext.IsLocal;
}
set
{
_batchContext.IsLocal = value;
}
}
public override IPrincipal Principal
{
get
{
return _batchContext.Principal;
}
set
{
_batchContext.Principal = value;
}
}
public override IHttpRouteData RouteData
{
get
{
// Use separate route data for batching (base, not _batchContext).
return base.RouteData;
}
set
{
// Use separate route data for batching (base, not _batchContext).
base.RouteData = value;
}
}
public override UrlHelper Url
{
get
{
// Use a separate URL factory for batching (base, not _batchContext).
return base.Url;
}
set
{
// Use a separate URL factory for batching (base, not _batchContext).
base.Url = value;
}
}
public override string VirtualPathRoot
{
get
{
return _batchContext.VirtualPathRoot;
}
set
{
_batchContext.VirtualPathRoot = value;
}
}
}
}
}

Просмотреть файл

@ -64,6 +64,7 @@
</Compile>
<Compile Include="AcceptVerbsAttribute.cs" />
<Compile Include="Batch\BatchExecutionOrder.cs" />
<Compile Include="Batch\BatchHttpRequestContext.cs" />
<Compile Include="Batch\HttpBatchHandler.cs" />
<Compile Include="Batch\DefaultHttpBatchHandler.cs" />
<Compile Include="Batch\BatchHttpRequestMessageExtensions.cs" />

Просмотреть файл

@ -11,6 +11,7 @@ using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Hosting;
using System.Web.Http.Routing;
using Microsoft.Owin;
@ -208,6 +209,61 @@ namespace System.Web.Http.Owin
Assert.Same(environment, request.GetOwinEnvironment());
}
[Fact]
public void Invoke_SetsOwinRequestContext()
{
// Arrange
IHostBufferPolicySelector bufferPolicySelector = CreateBufferPolicySelector(bufferInput: false,
bufferOutput: false);
using (HttpResponseMessage response = new HttpResponseMessage())
{
HttpRequestMessage request = null;
Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> sendAsync = (r, c) =>
{
request = r;
return Task.FromResult(response);
};
using (HttpMessageHandler messageHandler = new LambdaHttpMessageHandler(sendAsync))
using (HttpMessageHandlerAdapter adapter = new HttpMessageHandlerAdapter(next: null,
messageHandler: messageHandler, bufferPolicySelector: bufferPolicySelector))
{
Mock<IHeaderDictionary> requestHeadersMock = new Mock<IHeaderDictionary>(MockBehavior.Strict);
requestHeadersMock.Setup(h => h.GetEnumerator()).Returns(
new Mock<IEnumerator<KeyValuePair<string, string[]>>>().Object);
Mock<IOwinRequest> requestMock = new Mock<IOwinRequest>(MockBehavior.Strict);
requestMock.Setup(r => r.Method).Returns("GET");
requestMock.Setup(r => r.Uri).Returns(new Uri("http://ignore"));
requestMock.Setup(r => r.Body).Returns(Stream.Null);
requestMock.Setup(r => r.Headers).Returns(requestHeadersMock.Object);
requestMock.Setup(r => r.User).Returns((IPrincipal)null);
requestMock.Setup(r => r.CallCancelled).Returns(CancellationToken.None);
Mock<IHeaderDictionary> responseHeadersMock = new Mock<IHeaderDictionary>();
Mock<IOwinResponse> responseMock = new Mock<IOwinResponse>();
responseMock.Setup(r => r.Headers).Returns(responseHeadersMock.Object);
Mock<IOwinContext> contextMock = new Mock<IOwinContext>(MockBehavior.Strict);
contextMock.Setup(c => c.Request).Returns(requestMock.Object);
contextMock.Setup(c => c.Response).Returns(responseMock.Object);
IOwinContext expectedContext = contextMock.Object;
// Act
adapter.Invoke(expectedContext).Wait();
// Assert
HttpRequestContext requestContext = request.GetRequestContext();
Assert.IsType<OwinHttpRequestContext>(requestContext);
OwinHttpRequestContext typedContext = (OwinHttpRequestContext)requestContext;
Assert.Same(expectedContext, typedContext.Context);
Assert.Same(request, typedContext.Request);
}
}
}
[Theory]
[InlineData(null, false)]
[InlineData(false, false)]
@ -520,5 +576,22 @@ namespace System.Web.Http.Owin
return TaskHelpers.FromResult<HttpResponseMessage>(Response);
}
}
private class LambdaHttpMessageHandler : HttpMessageHandler
{
private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _sendAsync;
public LambdaHttpMessageHandler(
Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> sendAsync)
{
_sendAsync = sendAsync;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
return _sendAsync.Invoke(request, cancellationToken);
}
}
}
}

Просмотреть файл

@ -0,0 +1,744 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Web.Http.Controllers;
using System.Web.Http.Routing;
using Microsoft.Owin;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.Owin
{
public class OwinHttpRequestContextTests
{
[Fact]
public void ContextGet_ReturnsProvidedInstance()
{
// Arrange
IOwinContext expectedOwinContext = CreateStubOwinContext();
using (HttpRequestMessage request = CreateRequest())
{
OwinHttpRequestContext context = CreateProductUnderTest(expectedOwinContext, request);
// Act
IOwinContext owinContext = context.Context;
// Assert
Assert.Same(expectedOwinContext, owinContext);
}
}
[Fact]
public void RequestGet_ReturnsProvidedInstance()
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpRequestMessage expectedRequest = CreateRequest())
{
OwinHttpRequestContext context = CreateProductUnderTest(owinContext, expectedRequest);
// Act
HttpRequestMessage request = context.Request;
// Assert
Assert.Same(expectedRequest, request);
}
}
[Fact]
public void ClientCertificateGet_ReturnsContextClientCertificate()
{
// Arrange
X509Certificate2 expectedCertificate = CreateCertificate();
Mock<IOwinContext> owinContextMock = CreateOwinContextMock();
owinContextMock
.Setup(c => c.Get<X509Certificate2>(OwinConstants.ClientCertifiateKey))
.Returns(expectedCertificate);
IOwinContext owinContext = owinContextMock.Object;
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
X509Certificate2 certificate = context.ClientCertificate;
// Assert
Assert.Same(expectedCertificate, certificate);
}
}
[Fact]
public void ClientCertificateGet_ReturnsNull_ByDefault()
{
// Arrange
IOwinContext owinContext = new OwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
X509Certificate2 certificate = context.ClientCertificate;
// Assert
Assert.Null(certificate);
}
}
[Fact]
public void ClientCertificateSet_UpdatesClientCertificate()
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
X509Certificate2 expectedCertificate = CreateCertificate();
// Act
context.ClientCertificate = expectedCertificate;
// Assert
X509Certificate2 certificate = context.ClientCertificate;
Assert.Same(expectedCertificate, certificate);
}
}
[Fact]
public void ClientCertificateSet_UpdatesClientCertificate_WhenNull()
{
// Arrange
Mock<IOwinContext> owinContextMock = CreateOwinContextMock();
owinContextMock
.Setup(c => c.Get<X509Certificate2>(OwinConstants.ClientCertifiateKey))
.Returns(CreateCertificate());
IOwinContext owinContext = owinContextMock.Object;
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
context.ClientCertificate = null;
// Assert
X509Certificate2 certificate = context.ClientCertificate;
Assert.Null(certificate);
}
}
[Fact]
public void ClientCertificateGet_ReturnsFirstObservedContextClientCertificate()
{
// Arrange
X509Certificate2 expectedCertificate = CreateCertificate();
X509Certificate2 currentCertificate = expectedCertificate;
Mock<IOwinContext> owinContextMock = CreateOwinContextMock();
owinContextMock
.Setup(c => c.Get<X509Certificate2>(OwinConstants.ClientCertifiateKey))
.Returns(() => currentCertificate);
IOwinContext owinContext = owinContextMock.Object;
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
X509Certificate2 ignore = context.ClientCertificate;
currentCertificate = CreateCertificate();
// Act
X509Certificate2 certificate = context.ClientCertificate;
// Assert
Assert.Same(expectedCertificate, certificate);
}
}
[Fact]
public void IncludeErrorDetailGet_ReturnsFalse_ByDefault()
{
// Arrange
IOwinContext owinContext = new OwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(false, includeErrorDetail);
}
}
[Fact]
public void IncludeErrorDetailGet_ReturnsTrue_WhenUnconfiguredAndIsLocal()
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
context.IsLocal = true;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(true, includeErrorDetail);
}
}
[Theory]
[InlineData(true, IncludeErrorDetailPolicy.Always, true)]
[InlineData(true, IncludeErrorDetailPolicy.Always, false)]
[InlineData(false, IncludeErrorDetailPolicy.Never, true)]
[InlineData(false, IncludeErrorDetailPolicy.Never, false)]
[InlineData(true, IncludeErrorDetailPolicy.LocalOnly, true)]
[InlineData(false, IncludeErrorDetailPolicy.LocalOnly, false)]
[InlineData(true, IncludeErrorDetailPolicy.Default, true)]
[InlineData(false, IncludeErrorDetailPolicy.Default, false)]
public void IncludeErrorDetailGet_ForPolicy(bool expected, IncludeErrorDetailPolicy policy, bool isLocal)
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
context.Configuration = configuration;
context.IsLocal = isLocal;
configuration.IncludeErrorDetailPolicy = policy;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(expected, includeErrorDetail);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailSet_UpdatesIncludeErrorDetail(bool expectedIncludeErrorDetail)
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
context.IncludeErrorDetail = expectedIncludeErrorDetail;
// Assert
bool includeErrorDetail = context.IncludeErrorDetail;
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
}
[Theory]
[InlineData(true, IncludeErrorDetailPolicy.Never)]
[InlineData(false, IncludeErrorDetailPolicy.Always)]
public void IncludeErrorDetailSet_OverridesPolicy(bool expected, IncludeErrorDetailPolicy policy)
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
context.Configuration = configuration;
configuration.IncludeErrorDetailPolicy = policy;
// Act
context.IncludeErrorDetail = expected;
// Assert
bool includeErrorDetail = context.IncludeErrorDetail;
Assert.Equal(expected, includeErrorDetail);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailGet_ReturnsFirstObservedValue(bool expectedIncludeErrorDetail)
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
context.IsLocal = expectedIncludeErrorDetail;
bool ignore = context.IncludeErrorDetail;
context.IsLocal = !expectedIncludeErrorDetail;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
}
[Fact]
public void IsLocalGet_ReturnsFalse_ByDefault()
{
// Arrange
IOwinContext owinContext = new OwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
bool isLocal = context.IsLocal;
// Assert
Assert.Equal(false, isLocal);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void IsLocal_ReturnsContextIsLocalValue(bool expectedIsLocal)
{
// Arrange
Mock<IOwinContext> owinContextMock = CreateOwinContextMock();
owinContextMock.Setup(c => c.Get<bool>(OwinConstants.IsLocalKey)).Returns(expectedIsLocal);
IOwinContext owinContext = owinContextMock.Object;
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
bool isLocal = context.IsLocal;
// Assert
Assert.Equal(expectedIsLocal, isLocal);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsLocalSet_UpdatesIsLocal(bool expectedIsLocal)
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
context.IsLocal = expectedIsLocal;
// Assert
bool isLocal = context.IsLocal;
Assert.Equal(expectedIsLocal, isLocal);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsLocalGet_ReturnsFirstObservedValue(bool expectedIsLocal)
{
// Arrange
Mock<IOwinContext> owinContextMock = CreateOwinContextMock();
bool currentOwinContextIsLocal = expectedIsLocal;
owinContextMock.Setup(c => c.Get<bool>(OwinConstants.IsLocalKey)).Returns(() => currentOwinContextIsLocal);
IOwinContext owinContext = owinContextMock.Object;
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
bool ignore = context.IsLocal;
currentOwinContextIsLocal = !expectedIsLocal;
// Act
bool isLocal = context.IsLocal;
// Assert
Assert.Equal(expectedIsLocal, isLocal);
}
}
[Fact]
public void PrincipalGet_ReturnsNull_ByDefault()
{
// Arrange
IOwinContext owinContext = new OwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
IPrincipal principal = context.Principal;
// Assert
Assert.Null(principal);
}
}
[Fact]
public void PrincipalGet_ReturnsContextRequestUser()
{
// Arrange
IPrincipal expectedPrincipal = CreateDummyPrincipal();
Mock<IOwinRequest> owinRequestMock = new Mock<IOwinRequest>(MockBehavior.Strict);
owinRequestMock.Setup(r => r.User).Returns(expectedPrincipal);
IOwinContext owinContext = CreateStubOwinContext(owinRequestMock.Object);
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
IPrincipal principal = context.Principal;
// Assert
Assert.Same(expectedPrincipal, principal);
}
}
[Fact]
public void PrincipalSet_UpdatesPrincipal()
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
IPrincipal expectedPrincipal = CreateDummyPrincipal();
// Act
context.Principal = expectedPrincipal;
// Assert
IPrincipal principal = context.Principal;
Assert.Same(expectedPrincipal, principal);
}
}
[Fact]
public void PrincipalSet_UpdatesPrincipal_WhenNull()
{
// Arrange
Mock<IOwinRequest> owinRequestMock = new Mock<IOwinRequest>(MockBehavior.Strict);
owinRequestMock.Setup(r => r.User).Returns(CreateDummyPrincipal());
IOwinContext owinContext = CreateStubOwinContext(owinRequestMock.Object);
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
context.Principal = null;
// Assert
IPrincipal principal = context.Principal;
Assert.Null(principal);
}
}
[Fact]
public void PrincipalGet_ReturnsFirstObservedContextRequestUser()
{
// Arrange
IPrincipal expectedPrincipal = CreateDummyPrincipal();
IPrincipal currentPrincipal = expectedPrincipal;
Mock<IOwinRequest> owinRequestMock = new Mock<IOwinRequest>(MockBehavior.Strict);
owinRequestMock.Setup(r => r.User).Returns(() => currentPrincipal);
IOwinContext owinContext = CreateStubOwinContext(owinRequestMock.Object);
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
IPrincipal ignore = context.Principal;
currentPrincipal = CreateDummyPrincipal();
// Act
IPrincipal principal = context.Principal;
// Assert
Assert.Same(expectedPrincipal, principal);
}
}
[Fact]
public void UrlGet_ReturnsUrlHelperForRequest()
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpRequestMessage expectedRequest = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, expectedRequest);
// Act
UrlHelper url = context.Url;
// Assert
Assert.NotNull(url);
Assert.Same(expectedRequest, url.Request);
}
}
[Fact]
public void UrlGet_ReturnsSameInstance()
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
UrlHelper firstUrl = context.Url;
// Act
UrlHelper url = context.Url;
// Assert
Assert.Same(firstUrl, url);
}
}
[Fact]
public void UrlSet_UpdatesUrl()
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
UrlHelper expectedUrl = CreateDummyUrlHelper();
// Act
context.Url = expectedUrl;
// Assert
UrlHelper url = context.Url;
Assert.Same(expectedUrl, url);
}
}
[Fact]
public void UrlSet_UpdatesUrl_WhenNull()
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
context.Url = null;
// Assert
UrlHelper url = context.Url;
Assert.Null(url);
}
}
[Fact]
public void VirtualPathRootGet_ReturnsSlash_ByDefault()
{
// Arrange
IOwinContext owinContext = new OwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Equal("/", virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootGet_ReturnsContextRequestPathBase()
{
// Arrange
string expectedVirtualPathRoot = "/foo";
Mock<IOwinRequest> owinRequestMock = new Mock<IOwinRequest>(MockBehavior.Strict);
owinRequestMock.Setup(r => r.PathBase).Returns(new PathString(expectedVirtualPathRoot));
IOwinContext owinContext = CreateStubOwinContext(owinRequestMock.Object);
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Equal(expectedVirtualPathRoot, virtualPathRoot);
}
}
[Theory]
[InlineData(null)]
[InlineData("")]
public void VirtualPathRootGet_ReturnsSlash_WhenContextRequestPathBaseIsValue(string contextRequestPathBase)
{
// Arrange
Mock<IOwinRequest> owinRequestMock = new Mock<IOwinRequest>(MockBehavior.Strict);
owinRequestMock.Setup(r => r.PathBase).Returns(new PathString(contextRequestPathBase));
IOwinContext owinContext = CreateStubOwinContext(owinRequestMock.Object);
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Equal("/", virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootSet_UpdatesVirtualPathRoot()
{
// Arrange
IOwinContext owinContext = CreateStubOwinContext();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
string expectedVirtualPathRoot = "foo";
// Act
context.VirtualPathRoot = expectedVirtualPathRoot;
// Assert
string virtualPathRoot = context.VirtualPathRoot;
Assert.Same(expectedVirtualPathRoot, virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootSet_UpdatesVirtualPathRoot_WhenNull()
{
// Arrange
Mock<IOwinRequest> owinRequestMock = new Mock<IOwinRequest>(MockBehavior.Strict);
owinRequestMock.Setup(r => r.PathBase).Returns(new PathString("/other"));
IOwinContext owinContext = CreateStubOwinContext(owinRequestMock.Object);
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
// Act
context.VirtualPathRoot = null;
// Assert
string virtualPathRoot = context.VirtualPathRoot;
Assert.Null(virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootGet_ReturnsFirstObservedContextRequestPathBase()
{
// Arrange
string expectedVirtualPathRoot = "/expected";
string currentVirtualPathRoot = expectedVirtualPathRoot;
Mock<IOwinRequest> owinRequestMock = new Mock<IOwinRequest>(MockBehavior.Strict);
owinRequestMock.Setup(r => r.PathBase).Returns(() => new PathString(currentVirtualPathRoot));
IOwinContext owinContext = CreateStubOwinContext(owinRequestMock.Object);
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(owinContext, request);
string ignore = context.VirtualPathRoot;
currentVirtualPathRoot = "/other";
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Equal(expectedVirtualPathRoot, virtualPathRoot);
}
}
private static X509Certificate2 CreateCertificate()
{
return new X509Certificate2();
}
private static HttpConfiguration CreateConfiguration()
{
return new HttpConfiguration();
}
private static IOwinRequest CreateDummyOwinRequest()
{
return new Mock<IOwinRequest>(MockBehavior.Strict).Object;
}
private static IPrincipal CreateDummyPrincipal()
{
return new Mock<IPrincipal>(MockBehavior.Strict).Object;
}
private static UrlHelper CreateDummyUrlHelper()
{
return new Mock<UrlHelper>(MockBehavior.Strict).Object;
}
private static Mock<IOwinContext> CreateOwinContextMock()
{
Mock<IOwinContext> mock = new Mock<IOwinContext>(MockBehavior.Strict);
mock.Setup(c => c.Request).Returns(CreateDummyOwinRequest());
return mock;
}
private static OwinHttpRequestContext CreateProductUnderTest(IOwinContext context, HttpRequestMessage request)
{
return new OwinHttpRequestContext(context, request);
}
private static HttpRequestMessage CreateRequest()
{
return new HttpRequestMessage();
}
private static IOwinContext CreateStubOwinContext()
{
return CreateOwinContextMock().Object;
}
private static IOwinContext CreateStubOwinContext(IOwinRequest request)
{
Mock<IOwinContext> mock = new Mock<IOwinContext>(MockBehavior.Strict);
mock.Setup(c => c.Request).Returns(request);
return mock.Object;
}
}
}

Просмотреть файл

@ -45,6 +45,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="OwinHttpRequestContextTests.cs" />
<Compile Include="HostAuthenticationAttributeTest.cs" />
<Compile Include="HostAuthenticationFilterTest.cs" />
<Compile Include="OwinHttpConfigurationExtensionsTest.cs" />

Просмотреть файл

@ -8,7 +8,9 @@ using System.Net.Http;
using System.Net.Http.Formatting;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using Microsoft.TestCommon;
namespace System.Web.Http.SelfHost
@ -163,6 +165,59 @@ namespace System.Web.Http.SelfHost
Assert.Throws<HttpRequestException>(() => task.Wait());
}
[Fact]
public void SendAsync_ServiceModel_AddsSelfHostHttpRequestContext()
{
// Arrange
// Use a different port from 50231 to prevent conflicts with concurrent tests.
string baseUri = "http://localhost:50232/";
HttpRequestContext context = null;
Uri via = null;
Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> sendAsync = (r, c) =>
{
if (r != null)
{
context = r.GetRequestContext();
}
SelfHostHttpRequestContext typedContext = context as SelfHostHttpRequestContext;
if (typedContext != null)
{
via = typedContext.RequestContext.RequestMessage.Properties.Via;
}
return Task.FromResult(new HttpResponseMessage());
};
using (HttpSelfHostConfiguration expectedConfiguration = new HttpSelfHostConfiguration(baseUri))
{
expectedConfiguration.HostNameComparisonMode = HostNameComparisonMode.Exact;
using (HttpMessageHandler dispatcher = new LambdaHttpMessageHandler(sendAsync))
using (HttpSelfHostServer server = new HttpSelfHostServer(expectedConfiguration, dispatcher))
using (HttpClient client = new HttpClient())
using (HttpRequestMessage expectedRequest = new HttpRequestMessage(HttpMethod.Get, baseUri))
{
server.OpenAsync().Wait();
// Act
using (HttpResponseMessage ignore = client.SendAsync(expectedRequest).Result)
{
// Assert
SelfHostHttpRequestContext typedContext = (SelfHostHttpRequestContext)context;
Assert.Equal(expectedRequest.RequestUri, via);
Assert.Same(expectedConfiguration, context.Configuration);
Assert.Equal(expectedRequest.RequestUri, typedContext.Request.RequestUri);
server.CloseAsync().Wait();
}
}
}
}
internal class ThrowsBeforeTaskObjectContent : ObjectContent
{
public ThrowsBeforeTaskObjectContent()
@ -257,6 +312,23 @@ namespace System.Web.Http.SelfHost
? String.Format("http://{0}:{1}/stream", machineName, TestPort)
: String.Format("http://{0}:{1}", machineName, TestPort);
}
private class LambdaHttpMessageHandler : HttpMessageHandler
{
private readonly Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> _sendAsync;
public LambdaHttpMessageHandler(
Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> sendAsync)
{
_sendAsync = sendAsync;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken)
{
return _sendAsync.Invoke(request, cancellationToken);
}
}
}
public class SelfHostServerTestController : ApiController

Просмотреть файл

@ -0,0 +1,712 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
using System.IdentityModel.Claims;
using System.IdentityModel.Policy;
using System.IdentityModel.Selectors;
using System.IdentityModel.Tokens;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Security;
using System.Web.Http.Controllers;
using System.Web.Http.Routing;
using Microsoft.TestCommon;
using Moq;
using Moq.Protected;
namespace System.Web.Http.SelfHost
{
public class SelfHostHttpRequestContextTests
{
[Fact]
public void RequestContextGet_ReturnsProvidedInstance()
{
// Arrange
using (RequestContext expectedServiceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
SelfHostHttpRequestContext context = CreateProductUnderTest(expectedServiceModelContext, configuration,
request);
// Act
RequestContext serviceModelContext = context.RequestContext;
// Assert
Assert.Same(expectedServiceModelContext, serviceModelContext);
}
}
[Fact]
public void RequestGet_ReturnsProvidedInstance()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage expectedRequest = CreateRequest())
{
SelfHostHttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration,
expectedRequest);
// Act
HttpRequestMessage request = context.Request;
// Assert
Assert.Same(expectedRequest, request);
}
}
[Fact]
public void ClientCertificateGet_ReturnsNull_ByDefault()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
// Act
X509Certificate2 certificate = context.ClientCertificate;
// Assert
Assert.Null(certificate);
}
}
[Fact]
public void ClientCertificateGet_ReturnsContextClientCertificate()
{
// Arrange
X509Certificate2 expectedCertificate = CreateCertificate();
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
request.Properties[HttpSelfHostServer.SecurityKey] = CreateSecurityProperty(expectedCertificate);
// Act
X509Certificate2 certificate = context.ClientCertificate;
// Assert
// WCF clones the certificate instance, so NotNull is the best we can check without creating a real
// certificate.
Assert.NotNull(certificate);
}
}
[Fact]
public void ClientCertificateSet_UpdatesClientCertificate()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
X509Certificate2 expectedCertificate = CreateCertificate();
// Act
context.ClientCertificate = expectedCertificate;
// Assert
X509Certificate2 certificate = context.ClientCertificate;
Assert.Same(expectedCertificate, certificate);
}
}
[Fact]
public void ClientCertificateSet_UpdatesClientCertificate_WhenNull()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
request.Properties[HttpSelfHostServer.SecurityKey] = CreateSecurityProperty(CreateCertificate());
// Act
context.ClientCertificate = null;
// Assert
X509Certificate2 certificate = context.ClientCertificate;
Assert.Null(certificate);
}
}
[Fact]
public void ConfigurationGet_ReturnsProvidedInstance()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration expectedConfiguration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, expectedConfiguration,
request);
// Act
HttpConfiguration configuration = context.Configuration;
// Assert
Assert.Same(expectedConfiguration, configuration);
}
}
[Fact]
public void ConfigurationSet_UpdatesConfiguration()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration initialConfiguration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
using (HttpConfiguration expectedConfiguration = CreateConfiguration())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, initialConfiguration,
request);
// Act
context.Configuration = expectedConfiguration;
// Assert
HttpConfiguration configuration = context.Configuration;
Assert.Same(expectedConfiguration, configuration);
}
}
[Fact]
public void IncludeErrorDetailGet_ReturnsFalse_ByDefault()
{
// Arrange
using (Message message = CreateMessage())
using (RequestContext serviceModelContext = CreateStubServiceModelContext(message))
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
context.Configuration = null;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(false, includeErrorDetail);
}
}
[Fact]
public void IncludeErrorDetailGet_ReturnsTrue_WhenUnconfiguredAndIsLocal()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
context.Configuration = null;
context.IsLocal = true;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(true, includeErrorDetail);
}
}
[Theory]
[InlineData(true, IncludeErrorDetailPolicy.Always, true)]
[InlineData(true, IncludeErrorDetailPolicy.Always, false)]
[InlineData(false, IncludeErrorDetailPolicy.Never, true)]
[InlineData(false, IncludeErrorDetailPolicy.Never, false)]
[InlineData(true, IncludeErrorDetailPolicy.LocalOnly, true)]
[InlineData(false, IncludeErrorDetailPolicy.LocalOnly, false)]
[InlineData(true, IncludeErrorDetailPolicy.Default, true)]
[InlineData(false, IncludeErrorDetailPolicy.Default, false)]
public void IncludeErrorDetailGet_ForPolicy(bool expected, IncludeErrorDetailPolicy policy, bool isLocal)
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
context.IsLocal = isLocal;
configuration.IncludeErrorDetailPolicy = policy;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(expected, includeErrorDetail);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailSet_UpdatesIncludeErrorDetail(bool expectedIncludeErrorDetail)
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
// Act
context.IncludeErrorDetail = expectedIncludeErrorDetail;
// Assert
bool includeErrorDetail = context.IncludeErrorDetail;
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
}
[Theory]
[InlineData(true, IncludeErrorDetailPolicy.Never)]
[InlineData(false, IncludeErrorDetailPolicy.Always)]
public void IncludeErrorDetailSet_OverridesPolicy(bool expected, IncludeErrorDetailPolicy policy)
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
configuration.IncludeErrorDetailPolicy = policy;
// Act
context.IncludeErrorDetail = expected;
// Assert
bool includeErrorDetail = context.IncludeErrorDetail;
Assert.Equal(expected, includeErrorDetail);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailGet_ReturnsFirstObservedValue(bool expectedIncludeErrorDetail)
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
context.IsLocal = expectedIncludeErrorDetail;
bool ignore = context.IncludeErrorDetail;
context.IsLocal = !expectedIncludeErrorDetail;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
}
[Fact]
public void IsLocalGet_ReturnsFalse_ByDefault()
{
// Arrange
using (Message message = CreateMessage())
using (RequestContext serviceModelContext = CreateStubServiceModelContext(message))
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
// Act
bool isLocal = context.IsLocal;
// Assert
Assert.Equal(false, isLocal);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void IsLocal_ReturnsContextIsLocalValue(bool expectedIsLocal)
{
// Arrange
using (Message message = CreateMessage(expectedIsLocal))
using (RequestContext serviceModelContext = CreateStubServiceModelContext(message))
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
// Act
bool isLocal = context.IsLocal;
// Assert
Assert.Equal(expectedIsLocal, isLocal);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsLocalSet_UpdatesIsLocal(bool expectedIsLocal)
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
// Act
context.IsLocal = expectedIsLocal;
// Assert
bool isLocal = context.IsLocal;
Assert.Equal(expectedIsLocal, isLocal);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void IsLocalGet_ReturnsFirstObservedValue(bool expectedIsLocal)
{
// Arrange
using (Message message = CreateMessage(expectedIsLocal))
using (RequestContext serviceModelContext = CreateStubServiceModelContext(message))
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
bool ignore = context.IsLocal;
SetIsLocal(message, !expectedIsLocal);
// Act
bool isLocal = context.IsLocal;
// Assert
Assert.Equal(expectedIsLocal, isLocal);
}
}
[Fact]
public void UrlGet_ReturnsUrlHelperForRequest()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage expectedRequest = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, expectedRequest);
// Act
UrlHelper url = context.Url;
// Assert
Assert.NotNull(url);
Assert.Same(expectedRequest, url.Request);
}
}
[Fact]
public void UrlGet_ReturnsSameInstance()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
UrlHelper firstUrl = context.Url;
// Act
UrlHelper url = context.Url;
// Assert
Assert.Same(firstUrl, url);
}
}
[Fact]
public void UrlSet_UpdatesUrl()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
UrlHelper expectedUrl = CreateDummyUrlHelper();
// Act
context.Url = expectedUrl;
// Assert
UrlHelper url = context.Url;
Assert.Same(expectedUrl, url);
}
}
[Fact]
public void UrlSet_UpdatesUrl_WhenNull()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
// Act
context.Url = null;
// Assert
UrlHelper url = context.Url;
Assert.Null(url);
}
}
[Fact]
public void VirtualPathRootGet_ReturnsSlash_ByDefault()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Equal("/", virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootGet_ReturnsSlash_WhenConfigurationIsNull()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
context.Configuration = null;
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Equal("/", virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootGet_ReturnsConfigurationVirtualPathRoot()
{
// Arrange
string expectedVirtualPathRoot = "/foo";
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration(expectedVirtualPathRoot))
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Equal(expectedVirtualPathRoot, virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootSet_UpdatesVirtualPathRoot()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
string expectedVirtualPathRoot = "foo";
// Act
context.VirtualPathRoot = expectedVirtualPathRoot;
// Assert
string virtualPathRoot = context.VirtualPathRoot;
Assert.Same(expectedVirtualPathRoot, virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootSet_UpdatesVirtualPathRoot_WhenNull()
{
// Arrange
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration())
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
// Act
context.VirtualPathRoot = null;
// Assert
string virtualPathRoot = context.VirtualPathRoot;
Assert.Null(virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootGet_ReturnsFirstObservedConfigurationVirtualPathRoot()
{
// Arrange
string expectedVirtualPathRoot = "/expected";
using (RequestContext serviceModelContext = CreateStubServiceModelContext())
using (HttpConfiguration configuration = CreateConfiguration(expectedVirtualPathRoot))
using (HttpRequestMessage request = CreateRequest())
using (HttpConfiguration otherConfiguration = CreateConfiguration("/other"))
{
HttpRequestContext context = CreateProductUnderTest(serviceModelContext, configuration, request);
string ignore = context.VirtualPathRoot;
context.Configuration = otherConfiguration;
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Equal(expectedVirtualPathRoot, virtualPathRoot);
}
}
private static X509Certificate2 CreateCertificate()
{
return new X509Certificate2();
}
private static HttpConfiguration CreateConfiguration()
{
return new HttpConfiguration();
}
private static HttpConfiguration CreateConfiguration(string virtualPathRoot)
{
return new HttpConfiguration(new HttpRouteCollection(virtualPathRoot));
}
private static UrlHelper CreateDummyUrlHelper()
{
return new Mock<UrlHelper>(MockBehavior.Strict).Object;
}
private static Message CreateMessage()
{
return Message.CreateMessage(MessageVersion.None, null);
}
private static Message CreateMessage(bool isLocal)
{
Message message = CreateMessage();
SetIsLocal(message, isLocal);
return message;
}
private static SelfHostHttpRequestContext CreateProductUnderTest(RequestContext requestContext,
HttpConfiguration configuration, HttpRequestMessage request)
{
return new SelfHostHttpRequestContext(requestContext, configuration, request);
}
private static HttpRequestMessage CreateRequest()
{
return new HttpRequestMessage();
}
private static SecurityMessageProperty CreateSecurityProperty(X509Certificate2 certificate)
{
AuthorizationContext authorizationContext = new X509AuthorizationContext(certificate);
ServiceSecurityContext securityContext = new ServiceSecurityContext(authorizationContext);
SecurityMessageProperty securityProperty = new SecurityMessageProperty();
securityProperty.ServiceSecurityContext = securityContext;
return securityProperty;
}
private static Mock<RequestContext> CreateServiceModelContextMock()
{
Mock<RequestContext> mock = new Mock<RequestContext>(MockBehavior.Strict);
mock.Protected().Setup("Dispose", true);
return mock;
}
private static RequestContext CreateStubServiceModelContext()
{
return CreateServiceModelContextMock().Object;
}
private static RequestContext CreateStubServiceModelContext(Message requestMessage)
{
Mock<RequestContext> mock = CreateServiceModelContextMock();
mock.Setup(c => c.RequestMessage).Returns(requestMessage);
return mock.Object;
}
private static void SetIsLocal(Message message, bool value)
{
IPAddress address = value ? IPAddress.Loopback : IPAddress.None;
message.Properties[RemoteEndpointMessageProperty.Name] = new RemoteEndpointMessageProperty(
address.ToString(), 0);
}
private class X509AuthorizationContext : AuthorizationContext
{
private readonly ReadOnlyCollection<ClaimSet> _claimSets;
public X509AuthorizationContext(X509Certificate2 certificate)
{
Contract.Assert(certificate != null);
_claimSets = new ReadOnlyCollection<ClaimSet>(new List<ClaimSet>(new ClaimSet[] {
new X509CertificateClaimSet(certificate) }));
}
public override ReadOnlyCollection<ClaimSet> ClaimSets
{
get { return _claimSets; }
}
public override DateTime ExpirationTime
{
get { throw new NotImplementedException(); }
}
public override string Id
{
get { throw new NotImplementedException(); }
}
public override IDictionary<string, object> Properties
{
get { throw new NotImplementedException(); }
}
}
}
}

Просмотреть файл

@ -55,6 +55,7 @@
<Compile Include="HttpSelfHostServerTest.cs" />
<Compile Include="MaxHttpCollectionKeyTests.cs" />
<Compile Include="HttpSelfHostResponseTest.cs" />
<Compile Include="SelfHostHttpRequestContextTests.cs" />
<Compile Include="ServiceModel\TransferModeHelperTest.cs" />
<Compile Include="ServiceModel\HostNameComparisonModeHelperTest.cs" />
</ItemGroup>

Просмотреть файл

@ -0,0 +1,342 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Web.Http.Controllers;
using System.Web.Http.Routing;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.Batch
{
public class BatchHttpRequestContextTests
{
[Fact]
public void Constructor_Throws_WhenBatchContextIsNull()
{
// Arrange
HttpRequestContext batchContext = null;
// Act & Assert
Assert.ThrowsArgumentNull(() => CreateProductUnderTest(batchContext), "batchContext");
}
[Fact]
public void BatchContext_ReturnsProvidedInstance()
{
// Arrange
HttpRequestContext expectedBatchContext = CreateDummyContext();
BatchHttpRequestContext context = CreateProductUnderTest(expectedBatchContext);
// Act
HttpRequestContext batchContext = context.BatchContext;
// Assert
Assert.Same(expectedBatchContext, batchContext);
}
[Fact]
public void ClientCertificateGet_ReturnsBatchContextClientCertificate()
{
// Arrange
X509Certificate2 expectedCertificate = CreateCertificate();
HttpRequestContext batchContext = CreateContext();
batchContext.ClientCertificate = expectedCertificate;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
X509Certificate2 certificate = context.ClientCertificate;
// Assert
Assert.Same(expectedCertificate, certificate);
}
[Fact]
public void ClientCertificateSet_UpdatesBatchContextClientCertificate()
{
// Arrange
X509Certificate2 expectedCertificate = CreateCertificate();
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.ClientCertificate = expectedCertificate;
// Assert
X509Certificate2 certificate = batchContext.ClientCertificate;
Assert.Same(expectedCertificate, certificate);
}
[Fact]
public void ConfigurationGet_DoesNotReturnBatchContextConfiguration()
{
// Arrange
using (HttpConfiguration unexpectedConfiguration = CreateConfiguration())
{
HttpRequestContext batchContext = CreateContext();
batchContext.Configuration = unexpectedConfiguration;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
HttpConfiguration configuration = context.Configuration;
// Assert
Assert.NotSame(unexpectedConfiguration, configuration);
}
}
[Fact]
public void ConfigurationSet_UpdatesConfiguration()
{
// Arrange
using (HttpConfiguration expectedConfiguration = CreateConfiguration())
{
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.Configuration = expectedConfiguration;
// Assert
HttpConfiguration configuration = context.Configuration;
Assert.Same(expectedConfiguration, configuration);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailGet_ReturnsBatchContextIncludeErrorDetail(bool expectedIncludeErrorDetail)
{
// Arrange
HttpRequestContext batchContext = CreateContext();
batchContext.IncludeErrorDetail = expectedIncludeErrorDetail;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailSet_UpdatesBatchContextIncludeErrorDetail(bool expectedIncludeErrorDetail)
{
// Arrange
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.IncludeErrorDetail = expectedIncludeErrorDetail;
// Assert
bool includeErrorDetail = batchContext.IncludeErrorDetail;
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsLocalGet_ReturnsBatchContextIsLocal(bool expectedIsLocal)
{
// Arrange
HttpRequestContext batchContext = CreateContext();
batchContext.IsLocal = expectedIsLocal;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
bool isLocal = context.IsLocal;
// Assert
Assert.Equal(expectedIsLocal, isLocal);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsLocalSet_UpdatesBatchContextIsLocal(bool expectedIsLocal)
{
// Arrange
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.IsLocal = expectedIsLocal;
// Assert
bool isLocal = batchContext.IsLocal;
Assert.Equal(expectedIsLocal, isLocal);
}
[Fact]
public void PrincipalGet_ReturnsBatchContextPrincipal()
{
// Arrange
IPrincipal expectedPrincipal = CreateDummyPrincipal();
HttpRequestContext batchContext = CreateContext();
batchContext.Principal = expectedPrincipal;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
IPrincipal principal = context.Principal;
// Assert
Assert.Same(expectedPrincipal, principal);
}
[Fact]
public void PrincipalSet_UpdatesBatchContextPrincipal()
{
// Arrange
IPrincipal expectedPrincipal = CreateDummyPrincipal();
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.Principal = expectedPrincipal;
// Assert
IPrincipal principal = batchContext.Principal;
Assert.Same(expectedPrincipal, principal);
}
[Fact]
public void RouteDataGet_DoesNotReturnBatchContextRouteData()
{
// Arrange
IHttpRouteData unexpectedRouteData = CreateDummyRouteData();
HttpRequestContext batchContext = CreateContext();
batchContext.RouteData = unexpectedRouteData;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
IHttpRouteData routeData = context.RouteData;
// Assert
Assert.NotSame(unexpectedRouteData, routeData);
}
[Fact]
public void RouteDataSet_UpdatesRouteData()
{
// Arrange
IHttpRouteData expectedRouteData = CreateDummyRouteData();
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.RouteData = expectedRouteData;
// Assert
IHttpRouteData routeData = context.RouteData;
Assert.Same(expectedRouteData, routeData);
}
[Fact]
public void UrlGet_DoesNotReturnBatchContextUrl()
{
// Arrange
UrlHelper unexpectedUrl = CreateDummyUrlHelper();
HttpRequestContext batchContext = CreateContext();
batchContext.Url = unexpectedUrl;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
UrlHelper url = context.Url;
// Assert
Assert.NotSame(unexpectedUrl, url);
}
[Fact]
public void UrlSet_UpdatesUrl()
{
// Arrange
UrlHelper expectedUrl = CreateDummyUrlHelper();
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.Url = expectedUrl;
// Assert
UrlHelper url = context.Url;
Assert.Same(expectedUrl, url);
}
[Fact]
public void VirtualPathRootGet_ReturnsBatchContextVirtualPathRoot()
{
// Arrange
string expectedVirtualPathRoot = "foo";
HttpRequestContext batchContext = CreateContext();
batchContext.VirtualPathRoot = expectedVirtualPathRoot;
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Same(expectedVirtualPathRoot, virtualPathRoot);
}
[Fact]
public void VirtualPathRootSet_UpdatesBatchContextVirtualPathRoot()
{
// Arrange
string expectedVirtualPathRoot = "foo";
HttpRequestContext batchContext = CreateContext();
HttpRequestContext context = CreateProductUnderTest(batchContext);
// Act
context.VirtualPathRoot = expectedVirtualPathRoot;
// Assert
string virtualPathRoot = batchContext.VirtualPathRoot;
Assert.Same(expectedVirtualPathRoot, virtualPathRoot);
}
private static X509Certificate2 CreateCertificate()
{
return new X509Certificate2();
}
private static HttpConfiguration CreateConfiguration()
{
return new HttpConfiguration();
}
private static HttpRequestContext CreateContext()
{
return new HttpRequestContext();
}
private static HttpRequestContext CreateDummyContext()
{
return new Mock<HttpRequestContext>(MockBehavior.Strict).Object;
}
private static IPrincipal CreateDummyPrincipal()
{
return new Mock<IPrincipal>(MockBehavior.Strict).Object;
}
private static IHttpRouteData CreateDummyRouteData()
{
return new Mock<IHttpRouteData>(MockBehavior.Strict).Object;
}
private static UrlHelper CreateDummyUrlHelper()
{
return new Mock<UrlHelper>(MockBehavior.Strict).Object;
}
private static BatchHttpRequestContext CreateProductUnderTest(HttpRequestContext batchContext)
{
return new BatchHttpRequestContext(batchContext);
}
}
}

Просмотреть файл

@ -1,6 +1,7 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Net.Http;
using System.Web.Http.Controllers;
using Microsoft.TestCommon;
namespace System.Web.Http.Batch
@ -30,5 +31,25 @@ namespace System.Web.Http.Batch
Assert.Contains(notSpecialPropertyName, childRequest.Properties.Keys);
Assert.DoesNotContain(specialPropertyName, childRequest.Properties.Keys);
}
[Fact]
public void CopyBatchRequestProperties_AddsBatchHttpRequestContext()
{
using (HttpRequestMessage subRequest = new HttpRequestMessage())
using (HttpRequestMessage batchRequest = new HttpRequestMessage())
{
HttpRequestContext expectedOriginalContext = new HttpRequestContext();
subRequest.SetRequestContext(expectedOriginalContext);
// Act
BatchHttpRequestMessageExtensions.CopyBatchRequestProperties(subRequest, batchRequest);
// Assert
HttpRequestContext context = subRequest.GetRequestContext();
Assert.IsType<BatchHttpRequestContext>(context);
BatchHttpRequestContext typedContext = (BatchHttpRequestContext)context;
Assert.Same(expectedOriginalContext, typedContext.BatchContext);
}
}
}
}

Просмотреть файл

@ -73,6 +73,7 @@
</Compile>
<Compile Include="AuthorizeAttributeTest.cs" />
<Compile Include="Batch\BatchHttpRequestMessageExtensionsTest.cs" />
<Compile Include="Batch\BatchHttpRequestContextTests.cs" />
<Compile Include="Batch\HttpBatchHandlerTest.cs" />
<Compile Include="Batch\DefaultHttpBatchHandlerTest.cs" />
<Compile Include="Controllers\ActionFilterResultTests.cs" />

Просмотреть файл

@ -14,7 +14,7 @@ using System.Runtime.Remoting;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Hosting;
using System.Web.Http.Controllers;
using Microsoft.TestCommon;
using Moq;
using Newtonsoft.Json.Linq;
@ -231,6 +231,33 @@ namespace System.Web.Http.WebHost
Assert.True(results.inputStreamCalledAfterContentIsRead);
}
[Fact]
public void ConvertRequest_AddsWebHostHttpRequestContext()
{
// Arrange
Mock<HttpRequestBase> requestBaseMock = new Mock<HttpRequestBase>(MockBehavior.Strict);
requestBaseMock.Setup(r => r.HttpMethod).Returns("IGNORED");
requestBaseMock.Setup(r => r.Url).Returns(new Uri("http://ignore"));
requestBaseMock.Setup(r => r.Headers).Returns(new NameValueCollection());
HttpRequestBase requestBase = requestBaseMock.Object;
Mock<HttpContextBase> contextBaseMock = new Mock<HttpContextBase>(MockBehavior.Strict);
contextBaseMock.Setup(c => c.Request).Returns(requestBase);
contextBaseMock.Setup(c => c.Items).Returns((IDictionary)null);
HttpContextBase contextBase = contextBaseMock.Object;
// Act
using (HttpRequestMessage expectedRequest = HttpControllerHandler.ConvertRequest(contextBase))
{
// Assert
HttpRequestContext context = expectedRequest.GetRequestContext();
Assert.IsType<WebHostHttpRequestContext>(context);
WebHostHttpRequestContext typedContext = (WebHostHttpRequestContext)context;
Assert.Same(contextBase, typedContext.Context);
Assert.Same(requestBase, typedContext.WebRequest);
Assert.Same(expectedRequest, typedContext.Request);
}
}
[Fact]
public void ConvertResponse_IfResponseHasNoCacheControlDefined_SetsNoCacheCacheabilityOnAspNetResponse()
{
@ -240,7 +267,7 @@ namespace System.Web.Http.WebHost
HttpRequestMessage request = new HttpRequestMessage();
// Act
HttpControllerHandler.ConvertResponse(contextMock.Object, response, request);
HttpControllerHandler.ConvertResponse(contextMock.Object, response, request).Wait();
// Assert
contextMock.Verify(c => c.Response.Cache.SetCacheability(HttpCacheability.NoCache));
@ -256,7 +283,7 @@ namespace System.Web.Http.WebHost
response.Headers.CacheControl = new CacheControlHeaderValue { Public = true };
// Act
HttpControllerHandler.ConvertResponse(contextMock.Object, response, request);
HttpControllerHandler.ConvertResponse(contextMock.Object, response, request).Wait();
// Assert
contextMock.Verify(c => c.Response.Cache.SetCacheability(HttpCacheability.NoCache), Times.Never());

Просмотреть файл

@ -38,6 +38,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="WebHostHttpRequestContextTests.cs" />
<Compile Include="RemoteHttpControllerHandlerTest.cs" />
<Compile Include="GlobalConfigurationTest.cs" />
<Compile Include="HttpRequestMessageExtensions.cs" />

Просмотреть файл

@ -0,0 +1,924 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.IO;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Security.Principal;
using System.Web.Http.Controllers;
using System.Web.Http.Routing;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.WebHost
{
public class WebHostHttpRequestContextTests
{
private const string Base64Certificate = "MIFRMIFHAgEAMAIGADAJMQcwBQYAEwFhMB4XDTEzMDkxMDE5NTQ0OVoXDTM5MTIzMT" +
"IzNTk1OVowCTEHMAUGABMBYTCBBzACBgADgQAwAgYAA4EA";
[Fact]
public void ContextGet_ReturnsProvidedInstance()
{
// Arrange
HttpContextBase expectedWebContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
WebHostHttpRequestContext context = CreateProductUnderTest(expectedWebContext, webRequest, request);
// Act
HttpContextBase webContext = context.Context;
// Assert
Assert.Same(expectedWebContext, webContext);
}
}
[Fact]
public void WebRequestGet_ReturnsProvidedInstance()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase expectedWebRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
WebHostHttpRequestContext context = CreateProductUnderTest(webContext, expectedWebRequest, request);
// Act
HttpRequestBase webRequest = context.WebRequest;
// Assert
Assert.Same(expectedWebRequest, webRequest);
}
}
[Fact]
public void RequestGet_ReturnsProvidedInstance()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage expectedRequest = CreateRequest())
{
WebHostHttpRequestContext context = CreateProductUnderTest(webContext, webRequest, expectedRequest);
// Act
HttpRequestMessage request = context.Request;
// Assert
Assert.Same(expectedRequest, request);
}
}
[Fact]
public void ClientCertificateGet_ReturnsNull_WhenRequestBaseClientCertificateIsNull()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
Mock<HttpRequestBase> webRequestMock = new Mock<HttpRequestBase>(MockBehavior.Strict);
webRequestMock.Setup(r => r.ClientCertificate).Returns((HttpClientCertificate)null);
HttpRequestBase webRequest = webRequestMock.Object;
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
X509Certificate2 certificate = context.ClientCertificate;
// Assert
Assert.Null(certificate);
}
}
[Fact]
public void ClientCertificateGet_ReturnsNull_WhenRequestBaseClientCertificateCertificateIsNull()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpClientCertificate clientCertificate = CreateHttpClientCertificate(null);
HttpRequestBase webRequest = CreateStubWebRequest(clientCertificate);
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
X509Certificate2 certificate = context.ClientCertificate;
// Assert
Assert.Null(certificate);
}
}
[Fact]
public void ClientCertificateGet_ReturnsNull_WhenRequestBaseClientCertificateCertificateIsEmpty()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpClientCertificate clientCertificate = CreateHttpClientCertificate(new byte[0]);
HttpRequestBase webRequest = CreateStubWebRequest(clientCertificate);
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
X509Certificate2 certificate = context.ClientCertificate;
// Assert
Assert.Null(certificate);
}
}
[Fact]
public void ClientCertificateGet_ReturnsRequestBaseClientCertificate()
{
// Arrange
byte[] expectedCertificateBytes = Convert.FromBase64String(Base64Certificate);
HttpContextBase webContext = CreateDummyWebContext();
HttpClientCertificate clientCertificate = CreateHttpClientCertificate(expectedCertificateBytes);
HttpRequestBase webRequest = CreateStubWebRequest(clientCertificate);
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
X509Certificate2 certificate = context.ClientCertificate;
// Assert
Assert.NotNull(certificate);
Assert.Equal(expectedCertificateBytes, certificate.RawData);
}
}
[Fact]
public void ClientCertificateSet_UpdatesClientCertificate()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
X509Certificate2 expectedCertificate = CreateCertificate();
// Act
context.ClientCertificate = expectedCertificate;
// Assert
X509Certificate2 certificate = context.ClientCertificate;
Assert.Same(expectedCertificate, certificate);
}
}
[Fact]
public void ClientCertificateSet_UpdatesClientCertificate_WhenNull()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
context.ClientCertificate = null;
// Assert
X509Certificate2 certificate = context.ClientCertificate;
Assert.Null(certificate);
}
}
[Fact]
public void ConfigurationGet_ReturnsGlobalConfiguration_ByDefault()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
HttpConfiguration configuration = context.Configuration;
// Assert
Assert.Same(GlobalConfiguration.Configuration, configuration);
}
}
[Fact]
public void ConfigurationSet_UpdatesConfiguration()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
using (HttpConfiguration expectedConfiguration = CreateConfiguration())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
context.Configuration = expectedConfiguration;
// Assert
HttpConfiguration configuration = context.Configuration;
Assert.Same(expectedConfiguration, configuration);
}
}
[Fact]
public void ConfigurationSet_UpdatesConfiguration_WhenNull()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
context.Configuration = null;
// Assert
HttpConfiguration configuration = context.Configuration;
Assert.Null(configuration);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailGet_ReturnsNoCustomErrorEnabled_WhenUnconfigured(bool expectedIncludeErrorDetail)
{
// Arrange
Mock<HttpContextBase> webContextMock = new Mock<HttpContextBase>(MockBehavior.Strict);
webContextMock.Setup(r => r.IsCustomErrorEnabled).Returns(!expectedIncludeErrorDetail);
HttpContextBase webContext = webContextMock.Object;
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
context.Configuration = null;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailGet_ReturnsNoCustomErrorEnabled_ForDefaultPolicy(bool expectedIncludeErrorDetail)
{
// Arrange
Mock<HttpContextBase> webContextMock = new Mock<HttpContextBase>(MockBehavior.Strict);
webContextMock.Setup(r => r.IsCustomErrorEnabled).Returns(!expectedIncludeErrorDetail);
HttpContextBase webContext = webContextMock.Object;
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
using (HttpConfiguration configuration = CreateConfiguration())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
context.Configuration = configuration;
configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Default;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailGet_ReturnsIsLocal_ForLocalOnlyPolicy(bool expectedIncludeErrorDetail)
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
using (HttpConfiguration configuration = CreateConfiguration())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
context.Configuration = configuration;
configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly;
context.IsLocal = expectedIncludeErrorDetail;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
}
[Fact]
public void IncludeErrorDetailGet_ReturnsTrue_ForAlwaysPolicy()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
using (HttpConfiguration configuration = CreateConfiguration())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
context.Configuration = configuration;
configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(true, includeErrorDetail);
}
}
[Fact]
public void IncludeErrorDetailGet_ReturnsFalse_ForNeverPolicy()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
using (HttpConfiguration configuration = CreateConfiguration())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
context.Configuration = configuration;
configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Never;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(false, includeErrorDetail);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailSet_UpdatesIncludeErrorDetail(bool expectedIncludeErrorDetail)
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
context.IncludeErrorDetail = expectedIncludeErrorDetail;
// Assert
bool includeErrorDetail = context.IncludeErrorDetail;
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
}
[Theory]
[InlineData(true, IncludeErrorDetailPolicy.Never)]
[InlineData(false, IncludeErrorDetailPolicy.Always)]
public void IncludeErrorDetailSet_OverridesPolicy(bool expected, IncludeErrorDetailPolicy policy)
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
using (HttpConfiguration configuration = CreateConfiguration())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
context.Configuration = configuration;
configuration.IncludeErrorDetailPolicy = policy;
// Act
context.IncludeErrorDetail = expected;
// Assert
bool includeErrorDetail = context.IncludeErrorDetail;
Assert.Equal(expected, includeErrorDetail);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IncludeErrorDetailGet_ReturnsFirstObservedValue(bool expectedIncludeErrorDetail)
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
using (HttpConfiguration configuration = CreateConfiguration())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
context.Configuration = configuration;
configuration.IncludeErrorDetailPolicy = expectedIncludeErrorDetail
? IncludeErrorDetailPolicy.Always : IncludeErrorDetailPolicy.Never;
bool ignore = context.IncludeErrorDetail;
configuration.IncludeErrorDetailPolicy = expectedIncludeErrorDetail
? IncludeErrorDetailPolicy.Never : IncludeErrorDetailPolicy.Always;
// Act
bool includeErrorDetail = context.IncludeErrorDetail;
// Assert
Assert.Equal(expectedIncludeErrorDetail, includeErrorDetail);
}
}
[Fact]
public void IsLocalGet_ReturnsFalse_ByDefault()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = new Mock<HttpRequestBase>().Object;
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
bool isLocal = context.IsLocal;
// Assert
Assert.Equal(false, isLocal);
}
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void IsLocal_ReturnsWebRequestIsLocalValue(bool expectedIsLocal)
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
Mock<HttpRequestBase> webRequestMock = new Mock<HttpRequestBase>(MockBehavior.Strict);
webRequestMock.Setup(r => r.IsLocal).Returns(expectedIsLocal);
HttpRequestBase webRequest = webRequestMock.Object;
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
bool isLocal = context.IsLocal;
// Assert
Assert.Equal(expectedIsLocal, isLocal);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsLocalSet_UpdatesIsLocal(bool expectedIsLocal)
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
context.IsLocal = expectedIsLocal;
// Assert
bool isLocal = context.IsLocal;
Assert.Equal(expectedIsLocal, isLocal);
}
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void IsLocalGet_ReturnsFirstObservedValue(bool expectedIsLocal)
{
// Arrange
bool currentWebRequestIsLocal = expectedIsLocal;
HttpContextBase webContext = CreateDummyWebContext();
Mock<HttpRequestBase> webRequestMock = new Mock<HttpRequestBase>(MockBehavior.Strict);
webRequestMock.Setup(r => r.IsLocal).Returns(() => currentWebRequestIsLocal);
HttpRequestBase webRequest = webRequestMock.Object;
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
bool ignore = context.IsLocal;
currentWebRequestIsLocal = !expectedIsLocal;
// Act
bool isLocal = context.IsLocal;
// Assert
Assert.Equal(expectedIsLocal, isLocal);
}
}
[Fact]
public void PrincipalGet_ReturnsNull_ByDefault()
{
// Arrange
HttpContextBase webContext = new Mock<HttpContextBase>().Object;
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
IPrincipal principal = context.Principal;
// Assert
Assert.Null(principal);
}
}
[Fact]
public void PrincipalGet_ReturnsWebContextUser()
{
// Arrange
IPrincipal expectedPrincipal = CreateDummyPrincipal();
Mock<HttpContextBase> webContextMock = new Mock<HttpContextBase>(MockBehavior.Strict);
webContextMock.Setup(r => r.User).Returns(expectedPrincipal);
HttpContextBase webContext = webContextMock.Object;
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
IPrincipal principal = context.Principal;
// Assert
Assert.Same(expectedPrincipal, principal);
}
}
[Fact]
public void PrincipalSet_UpdatesPrincipal()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
IPrincipal expectedPrincipal = CreateDummyPrincipal();
// Act
context.Principal = expectedPrincipal;
// Assert
IPrincipal principal = context.Principal;
Assert.Same(expectedPrincipal, principal);
}
}
[Fact]
public void PrincipalSet_UpdatesPrincipal_WhenNull()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
context.Principal = null;
// Assert
IPrincipal principal = context.Principal;
Assert.Null(principal);
}
}
[Fact]
public void PrincipalGet_ReturnsFirstObservedContextRequestUser()
{
// Arrange
IPrincipal expectedPrincipal = CreateDummyPrincipal();
IPrincipal currentPrincipal = expectedPrincipal;
Mock<HttpContextBase> webContextMock = new Mock<HttpContextBase>(MockBehavior.Strict);
webContextMock.Setup(r => r.User).Returns(() => currentPrincipal);
HttpContextBase webContext = webContextMock.Object;
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
IPrincipal ignore = context.Principal;
currentPrincipal = CreateDummyPrincipal();
// Act
IPrincipal principal = context.Principal;
// Assert
Assert.Same(expectedPrincipal, principal);
}
}
[Fact]
public void UrlGet_ReturnsUrlHelperForRequest()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage expectedRequest = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, expectedRequest);
// Act
UrlHelper url = context.Url;
// Assert
Assert.NotNull(url);
Assert.Same(expectedRequest, url.Request);
}
}
[Fact]
public void UrlGet_ReturnsSameInstance()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
UrlHelper firstUrl = context.Url;
// Act
UrlHelper url = context.Url;
// Assert
Assert.Same(firstUrl, url);
}
}
[Fact]
public void UrlSet_UpdatesUrl()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
UrlHelper expectedUrl = CreateDummyUrlHelper();
// Act
context.Url = expectedUrl;
// Assert
UrlHelper url = context.Url;
Assert.Same(expectedUrl, url);
}
}
[Fact]
public void UrlSet_UpdatesUrl_WhenNull()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
context.Url = null;
// Assert
UrlHelper url = context.Url;
Assert.Null(url);
}
}
[Fact]
public void VirtualPathRootGet_ReturnsNull_ByDefault()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Null(virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootGet_ReturnsNull_WhenConfigurationIsNull()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
context.Configuration = null;
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Null(virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootGet_ReturnsConfigurationVirtualPathRoot()
{
// Arrange
string expectedVirtualPathRoot = "/foo";
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
using (HttpConfiguration configuration = CreateConfiguration(expectedVirtualPathRoot))
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
context.Configuration = configuration;
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Equal(expectedVirtualPathRoot, virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootSet_UpdatesVirtualPathRoot()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
string expectedVirtualPathRoot = "foo";
// Act
context.VirtualPathRoot = expectedVirtualPathRoot;
// Assert
string virtualPathRoot = context.VirtualPathRoot;
Assert.Same(expectedVirtualPathRoot, virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootSet_UpdatesVirtualPathRoot_WhenNull()
{
// Arrange
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
using (HttpConfiguration configuration = CreateConfiguration("/other"))
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
context.Configuration = configuration;
// Act
context.VirtualPathRoot = null;
// Assert
string virtualPathRoot = context.VirtualPathRoot;
Assert.Null(virtualPathRoot);
}
}
[Fact]
public void VirtualPathRootGet_ReturnsFirstObservedConfigurationVirtualPathRoot()
{
// Arrange
string expectedVirtualPathRoot = "/foo";
HttpContextBase webContext = CreateDummyWebContext();
HttpRequestBase webRequest = CreateDummyWebRequest();
using (HttpRequestMessage request = CreateRequest())
using (HttpConfiguration configuration = CreateConfiguration(expectedVirtualPathRoot))
using (HttpConfiguration otherConfiguration = CreateConfiguration("/other"))
{
HttpRequestContext context = CreateProductUnderTest(webContext, webRequest, request);
context.Configuration = configuration;
string ignore = context.VirtualPathRoot;
context.Configuration = otherConfiguration;
// Act
string virtualPathRoot = context.VirtualPathRoot;
// Assert
Assert.Equal(expectedVirtualPathRoot, virtualPathRoot);
}
}
private static X509Certificate2 CreateCertificate()
{
return new X509Certificate2();
}
private static HttpConfiguration CreateConfiguration()
{
return new HttpConfiguration();
}
private static HttpConfiguration CreateConfiguration(string virtualPathRoot)
{
return new HttpConfiguration(new HttpRouteCollection(virtualPathRoot));
}
private static HttpContextBase CreateDummyWebContext()
{
return new Mock<HttpContextBase>(MockBehavior.Strict).Object;
}
private static HttpRequestBase CreateDummyWebRequest()
{
return new Mock<HttpRequestBase>(MockBehavior.Strict).Object;
}
private static IPrincipal CreateDummyPrincipal()
{
return new Mock<IPrincipal>(MockBehavior.Strict).Object;
}
private static UrlHelper CreateDummyUrlHelper()
{
return new Mock<UrlHelper>(MockBehavior.Strict).Object;
}
private static HttpClientCertificate CreateHttpClientCertificate(byte[] bytes)
{
Mock<HttpWorkerRequest> workerRequestMock = new Mock<HttpWorkerRequest>();
workerRequestMock.Setup(wr => wr.GetRawUrl()).Returns("/");
workerRequestMock.Setup(wr => wr.GetServerVariable("CERT_FLAGS")).Returns("1");
workerRequestMock.Setup(wr => wr.GetClientCertificate()).Returns(bytes);
HttpContext context = new HttpContext(workerRequestMock.Object);
return context.Request.ClientCertificate;
}
private static WebHostHttpRequestContext CreateProductUnderTest(HttpContextBase contextBase,
HttpRequestBase requestBase, HttpRequestMessage request)
{
return new WebHostHttpRequestContext(contextBase, requestBase, request);
}
private static HttpRequestMessage CreateRequest()
{
return new HttpRequestMessage();
}
private static HttpRequestBase CreateStubWebRequest(HttpClientCertificate clientCertificate)
{
Mock<HttpRequestBase> mock = new Mock<HttpRequestBase>(MockBehavior.Strict);
mock.Setup(r => r.ClientCertificate).Returns(clientCertificate);
return mock.Object;
}
}
}