diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2008-11-25 07:32:57 -0800 |
---|---|---|
committer | Andrew <andrewarnott@gmail.com> | 2008-11-25 07:32:57 -0800 |
commit | dbb8e6cf3329dfee52fa78a19026134eaae2bae7 (patch) | |
tree | 7f6930398658fdac29aa6896143f90b1ba037e86 /src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs | |
parent | 3d69557f925cb7e05e85b0b99371faaf9c2109c6 (diff) | |
download | DotNetOpenAuth-dbb8e6cf3329dfee52fa78a19026134eaae2bae7.zip DotNetOpenAuth-dbb8e6cf3329dfee52fa78a19026134eaae2bae7.tar.gz DotNetOpenAuth-dbb8e6cf3329dfee52fa78a19026134eaae2bae7.tar.bz2 |
Hundreds more stylecop fixes.
Mostly just doc bugs now.
Diffstat (limited to 'src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs')
-rw-r--r-- | src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs | 79 |
1 files changed, 41 insertions, 38 deletions
diff --git a/src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs b/src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs index 08e4473..94d6f81 100644 --- a/src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs +++ b/src/DotNetOpenAuth.Test/Mocks/MockHttpRequest.cs @@ -1,4 +1,10 @@ -namespace DotNetOpenAuth.Test.Mocks { +//----------------------------------------------------------------------- +// <copyright file="MockHttpRequest.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.Test.Mocks { using System; using System.Collections.Generic; using System.Globalization; @@ -6,15 +12,22 @@ using System.Net; using System.Text; using System.Web; - using DotNetOpenAuth.OpenId.RelyingParty; - using DotNetOpenAuth.Yadis; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OpenId; + using DotNetOpenAuth.OpenId.RelyingParty; using DotNetOpenAuth.Test.OpenId; + using DotNetOpenAuth.Yadis; internal class MockHttpRequest { private readonly Dictionary<Uri, DirectWebResponse> registeredMockResponses = new Dictionary<Uri, DirectWebResponse>(); + private MockHttpRequest(IDirectSslWebRequestHandler mockHandler) { + ErrorUtilities.VerifyArgumentNotNull(mockHandler, "mockHandler"); + this.MockWebRequestHandler = mockHandler; + } + + internal IDirectSslWebRequestHandler MockWebRequestHandler { get; private set; } + internal static MockHttpRequest CreateUntrustedMockHttpHandler() { TestWebRequestHandler testHandler = new TestWebRequestHandler(); UntrustedWebRequestHandler untrustedHandler = new UntrustedWebRequestHandler(testHandler); @@ -26,33 +39,31 @@ return mock; } - internal IDirectSslWebRequestHandler MockWebRequestHandler { get; private set; } - internal void RegisterMockResponse(DirectWebResponse response) { - if (response == null) throw new ArgumentNullException("response"); - if (registeredMockResponses.ContainsKey(response.RequestUri)) { + ErrorUtilities.VerifyArgumentNotNull(response, "response"); + if (this.registeredMockResponses.ContainsKey(response.RequestUri)) { Logger.WarnFormat("Mock HTTP response already registered for {0}.", response.RequestUri); } else { - registeredMockResponses.Add(response.RequestUri, response); + this.registeredMockResponses.Add(response.RequestUri, response); } } internal void RegisterMockResponse(Uri requestUri, string contentType, string responseBody) { - RegisterMockResponse(requestUri, requestUri, contentType, responseBody); + this.RegisterMockResponse(requestUri, requestUri, contentType, responseBody); } internal void RegisterMockResponse(Uri requestUri, Uri responseUri, string contentType, string responseBody) { - RegisterMockResponse(requestUri, responseUri, contentType, new WebHeaderCollection(), responseBody); + this.RegisterMockResponse(requestUri, responseUri, contentType, new WebHeaderCollection(), responseBody); } internal void RegisterMockResponse(Uri requestUri, Uri responseUri, string contentType, WebHeaderCollection headers, string responseBody) { - if (requestUri == null) throw new ArgumentNullException("requestUri"); - if (responseUri == null) throw new ArgumentNullException("responseUri"); - if (String.IsNullOrEmpty(contentType)) throw new ArgumentNullException("contentType"); + ErrorUtilities.VerifyArgumentNotNull(requestUri, "requestUri"); + ErrorUtilities.VerifyArgumentNotNull(responseUri, "responseUri"); + ErrorUtilities.VerifyNonZeroLength(contentType, "contentType"); // Set up the redirect if appropriate if (requestUri != responseUri) { - RegisterMockRedirect(requestUri, responseUri); + this.RegisterMockRedirect(requestUri, responseUri); } string contentEncoding = null; @@ -61,18 +72,17 @@ sw.Write(responseBody); sw.Flush(); stream.Seek(0, SeekOrigin.Begin); - RegisterMockResponse(new DirectWebResponse(responseUri, responseUri, headers ?? new WebHeaderCollection(), - HttpStatusCode.OK, contentType, contentEncoding, stream)); + this.RegisterMockResponse(new DirectWebResponse(responseUri, responseUri, headers ?? new WebHeaderCollection(), HttpStatusCode.OK, contentType, contentEncoding, stream)); } internal void RegisterMockXrdsResponses(IDictionary<string, string> requestUriAndResponseBody) { foreach (var pair in requestUriAndResponseBody) { - RegisterMockResponse(new Uri(pair.Key), "text/xml; saml=false; https=false; charset=UTF-8", pair.Value); + this.RegisterMockResponse(new Uri(pair.Key), "text/xml; saml=false; https=false; charset=UTF-8", pair.Value); } } internal void RegisterMockXrdsResponse(ServiceEndpoint endpoint) { - if (endpoint == null) throw new ArgumentNullException("endpoint"); + ErrorUtilities.VerifyArgumentNotNull(endpoint, "endpoint"); string identityUri; if (endpoint.ClaimedIdentifier == endpoint.Protocol.ClaimedIdentifierForOPIdentifier) { @@ -80,11 +90,11 @@ } else { identityUri = endpoint.UserSuppliedIdentifier ?? endpoint.ClaimedIdentifier; } - RegisterMockXrdsResponse(new Uri(identityUri), new ServiceEndpoint[] { endpoint }); + this.RegisterMockXrdsResponse(new Uri(identityUri), new ServiceEndpoint[] { endpoint }); } internal void RegisterMockXrdsResponse(Uri respondingUri, IEnumerable<ServiceEndpoint> endpoints) { - if (endpoints == null) throw new ArgumentNullException("endpoints"); + ErrorUtilities.VerifyArgumentNotNull(endpoints, "endpoints"); StringBuilder xrds = new StringBuilder(); xrds.AppendLine(@"<xrds:XRDS xmlns:xrds='xri://$xrds' xmlns:openid='http://openid.net/xmlns/1.0' xmlns='xri://$xrd*($v*2.0)'> @@ -103,7 +113,9 @@ } else { serviceTypeUri = endpoint.Protocol.ClaimedIdentifierServiceTypeURI; } - string xrd = string.Format(CultureInfo.InvariantCulture, template, + string xrd = string.Format( + CultureInfo.InvariantCulture, + template, HttpUtility.HtmlEncode(serviceTypeUri), HttpUtility.HtmlEncode(endpoint.ProviderEndpoint.AbsoluteUri), HttpUtility.HtmlEncode(endpoint.ProviderLocalIdentifier)); @@ -113,7 +125,7 @@ </XRD> </xrds:XRDS>"); - RegisterMockResponse(respondingUri, ContentTypes.Xrds, xrds.ToString()); + this.RegisterMockResponse(respondingUri, ContentTypes.Xrds, xrds.ToString()); } internal void RegisterMockXrdsResponse(UriIdentifier directedIdentityAssignedIdentifier, ServiceEndpoint providerEndpoint) { @@ -124,12 +136,12 @@ new string[] { providerEndpoint.Protocol.ClaimedIdentifierServiceTypeURI }, 10, 10); - RegisterMockXrdsResponse(identityEndpoint); + this.RegisterMockXrdsResponse(identityEndpoint); } internal Identifier RegisterMockXrdsResponse(string embeddedResourcePath) { UriIdentifier id = TestSupport.GetFullUrl(embeddedResourcePath); - RegisterMockResponse(id, "application/xrds+xml", TestSupport.LoadEmbeddedFile(embeddedResourcePath)); + this.RegisterMockResponse(id, "application/xrds+xml", TestSupport.LoadEmbeddedFile(embeddedResourcePath)); return id; } @@ -144,11 +156,9 @@ </Service> </XRD> </xrds:XRDS>"; - string xrds = string.Format(CultureInfo.InvariantCulture, template, - HttpUtility.HtmlEncode(Protocol.V20.RPReturnToTypeURI), - HttpUtility.HtmlEncode(rpRealmUri.AbsoluteUri)); + string xrds = string.Format(CultureInfo.InvariantCulture, template, HttpUtility.HtmlEncode(Protocol.V20.RPReturnToTypeURI), HttpUtility.HtmlEncode(rpRealmUri.AbsoluteUri)); - RegisterMockResponse(rpRealmUri, ContentTypes.Xrds, xrds); + this.RegisterMockResponse(rpRealmUri, ContentTypes.Xrds, xrds); } internal void DeleteResponse(Uri requestUri) { @@ -159,14 +169,8 @@ var redirectionHeaders = new WebHeaderCollection { { HttpResponseHeader.Location, redirectLocation.AbsoluteUri }, }; - DirectWebResponse response = new DirectWebResponse(origin, origin, - redirectionHeaders, HttpStatusCode.Redirect, null, null, new MemoryStream()); - RegisterMockResponse(response); - } - - private MockHttpRequest(IDirectSslWebRequestHandler mockHandler) { - ErrorUtilities.VerifyArgumentNotNull(mockHandler, "mockHandler"); - this.MockWebRequestHandler = mockHandler; + DirectWebResponse response = new DirectWebResponse(origin, origin, redirectionHeaders, HttpStatusCode.Redirect, null, null, new MemoryStream()); + this.RegisterMockResponse(response); } private DirectWebResponse GetMockResponse(HttpWebRequest request) { @@ -178,8 +182,7 @@ } else { ////Assert.Fail("Unexpected HTTP request: {0}", uri); Logger.WarnFormat("Unexpected HTTP request: {0}", request.RequestUri); - return new DirectWebResponse(request.RequestUri, request.RequestUri, new WebHeaderCollection(), HttpStatusCode.NotFound, - "text/html", null, new MemoryStream()); + return new DirectWebResponse(request.RequestUri, request.RequestUri, new WebHeaderCollection(), HttpStatusCode.NotFound, "text/html", null, new MemoryStream()); } } } |