diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2012-03-05 17:38:00 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2012-03-05 17:38:00 -0800 |
commit | 9a3885e6992462122057f532b7cbcda3695ca6bd (patch) | |
tree | 679678fe05814b95e8aaf8e3ca441c3410f1c8c5 /src/DotNetOpenAuth.Test | |
parent | a292822196d0911a68fc56597ed52a8c84a41cbe (diff) | |
download | DotNetOpenAuth-9a3885e6992462122057f532b7cbcda3695ca6bd.zip DotNetOpenAuth-9a3885e6992462122057f532b7cbcda3695ca6bd.tar.gz DotNetOpenAuth-9a3885e6992462122057f532b7cbcda3695ca6bd.tar.bz2 |
Replaced API requirements for HttpRequestInfo with HttpRequestBase (new in .NET 3.5 SP1).
This makes us more friendly to MVC as well as mock-based unit testing.
Diffstat (limited to 'src/DotNetOpenAuth.Test')
14 files changed, 138 insertions, 156 deletions
diff --git a/src/DotNetOpenAuth.Test/Messaging/HttpRequestInfoTests.cs b/src/DotNetOpenAuth.Test/Messaging/HttpRequestInfoTests.cs index b2f2b14..fbe1d6b 100644 --- a/src/DotNetOpenAuth.Test/Messaging/HttpRequestInfoTests.cs +++ b/src/DotNetOpenAuth.Test/Messaging/HttpRequestInfoTests.cs @@ -13,25 +13,6 @@ namespace DotNetOpenAuth.Test.Messaging { [TestFixture] public class HttpRequestInfoTests : TestBase { - [Test] - public void CtorDefault() { - HttpRequestInfo info = new HttpRequestInfo(); - Assert.AreEqual("GET", info.HttpMethod); - } - - [Test] - public void CtorRequest() { - HttpRequest request = new HttpRequest("file", "http://someserver?a=b", "a=b"); - ////request.Headers["headername"] = "headervalue"; // PlatformNotSupportedException prevents us mocking this up - HttpRequestInfo info = new HttpRequestInfo(request); - Assert.AreEqual(request.Headers["headername"], info.Headers["headername"]); - Assert.AreEqual(request.Url.Query, info.Query); - Assert.AreEqual(request.QueryString["a"], info.QueryString["a"]); - Assert.AreEqual(request.Url, info.Url); - Assert.AreEqual(request.Url, info.UrlBeforeRewriting); - Assert.AreEqual(request.HttpMethod, info.HttpMethod); - } - // All these tests are ineffective because ServerVariables[] cannot be set. ////[Test] ////public void CtorRequestWithDifferentPublicHttpHost() { @@ -77,21 +58,11 @@ namespace DotNetOpenAuth.Test.Messaging { ////} /// <summary> - /// Checks that a property dependent on another null property - /// doesn't generate a NullReferenceException. - /// </summary> - [Test] - public void QueryBeforeSettingUrl() { - HttpRequestInfo info = new HttpRequestInfo(); - Assert.IsNull(info.Query); - } - - /// <summary> /// Verifies that looking up a querystring variable is gracefully handled without a query in the URL. /// </summary> [Test] public void QueryStringLookupWithoutQuery() { - HttpRequestInfo info = new HttpRequestInfo(); + var info = new HttpRequestInfo("GET", new Uri("http://somehost/somepath")); Assert.IsNull(info.QueryString["hi"]); } @@ -104,7 +75,7 @@ namespace DotNetOpenAuth.Test.Messaging { var serverVariables = new NameValueCollection(); serverVariables["HTTP_X_FORWARDED_PROTO"] = "https"; serverVariables["HTTP_HOST"] = "somehost"; - Uri actual = HttpRequestInfo.GetPublicFacingUrl(req, serverVariables); + Uri actual = new HttpRequestWrapper(req).GetPublicFacingUrl(serverVariables); Uri expected = new Uri("https://somehost/a.aspx?a=b"); Assert.AreEqual(expected, actual); } @@ -118,7 +89,7 @@ namespace DotNetOpenAuth.Test.Messaging { var serverVariables = new NameValueCollection(); serverVariables["HTTP_X_FORWARDED_PROTO"] = "https"; serverVariables["HTTP_HOST"] = "somehost:999"; - Uri actual = HttpRequestInfo.GetPublicFacingUrl(req, serverVariables); + Uri actual = new HttpRequestWrapper(req).GetPublicFacingUrl(serverVariables); Uri expected = new Uri("https://somehost:999/a.aspx?a=b"); Assert.AreEqual(expected, actual); } @@ -131,7 +102,7 @@ namespace DotNetOpenAuth.Test.Messaging { HttpRequest req = new HttpRequest("a.aspx", "http://someinternalhost/a.aspx?a=b", "a=b"); var serverVariables = new NameValueCollection(); serverVariables["HTTP_HOST"] = "somehost"; - Uri actual = HttpRequestInfo.GetPublicFacingUrl(req, serverVariables); + Uri actual = new HttpRequestWrapper(req).GetPublicFacingUrl(serverVariables); Uri expected = new Uri("http://somehost/a.aspx?a=b"); Assert.AreEqual(expected, actual); } @@ -144,7 +115,7 @@ namespace DotNetOpenAuth.Test.Messaging { HttpRequest req = new HttpRequest("a.aspx", "http://someinternalhost/a.aspx?a=b", "a=b"); var serverVariables = new NameValueCollection(); serverVariables["HTTP_HOST"] = "somehost:79"; - Uri actual = HttpRequestInfo.GetPublicFacingUrl(req, serverVariables); + Uri actual = new HttpRequestWrapper(req).GetPublicFacingUrl(serverVariables); Uri expected = new Uri("http://somehost:79/a.aspx?a=b"); Assert.AreEqual(expected, actual); } diff --git a/src/DotNetOpenAuth.Test/Messaging/MessagingTestBase.cs b/src/DotNetOpenAuth.Test/Messaging/MessagingTestBase.cs index e3700b8..b7c0980 100644 --- a/src/DotNetOpenAuth.Test/Messaging/MessagingTestBase.cs +++ b/src/DotNetOpenAuth.Test/Messaging/MessagingTestBase.cs @@ -7,6 +7,7 @@ namespace DotNetOpenAuth.Test { using System; using System.Collections.Generic; + using System.Collections.Specialized; using System.IO; using System.Net; using System.Xml; @@ -19,6 +20,8 @@ namespace DotNetOpenAuth.Test { /// The base class that all messaging test classes inherit from. /// </summary> public class MessagingTestBase : TestBase { + protected internal const string DefaultUrlForHttpRequestInfo = "http://localhost/path"; + internal enum FieldFill { /// <summary> /// An empty dictionary is returned. @@ -53,29 +56,19 @@ namespace DotNetOpenAuth.Test { } internal static HttpRequestInfo CreateHttpRequestInfo(string method, IDictionary<string, string> fields) { - string query = MessagingUtilities.CreateQueryString(fields); - UriBuilder requestUri = new UriBuilder("http://localhost/path"); - WebHeaderCollection headers = new WebHeaderCollection(); - MemoryStream ms = new MemoryStream(); + var requestUri = new UriBuilder(DefaultUrlForHttpRequestInfo); + var headers = new NameValueCollection(); + NameValueCollection form = null; if (method == "POST") { - headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded"); - StreamWriter sw = new StreamWriter(ms); - sw.Write(query); - sw.Flush(); - ms.Position = 0; + form = fields.ToNameValueCollection(); + headers.Add(HttpRequestHeaders.ContentType, Channel.HttpFormUrlEncoded); } else if (method == "GET") { - requestUri.Query = query; + requestUri.Query = MessagingUtilities.CreateQueryString(fields); } else { throw new ArgumentOutOfRangeException("method", method, "Expected POST or GET"); } - HttpRequestInfo request = new HttpRequestInfo { - HttpMethod = method, - UrlBeforeRewriting = requestUri.Uri, - Headers = headers, - InputStream = ms, - }; - return request; + return new HttpRequestInfo(method, requestUri.Uri, form: form, headers: headers); } internal static Channel CreateChannel(MessageProtections capabilityAndRecognition) { diff --git a/src/DotNetOpenAuth.Test/Mocks/CoordinatingChannel.cs b/src/DotNetOpenAuth.Test/Mocks/CoordinatingChannel.cs index 8d5295b..10bd59a 100644 --- a/src/DotNetOpenAuth.Test/Mocks/CoordinatingChannel.cs +++ b/src/DotNetOpenAuth.Test/Mocks/CoordinatingChannel.cs @@ -11,6 +11,8 @@ namespace DotNetOpenAuth.Test.Mocks { using System.Linq; using System.Text; using System.Threading; + using System.Web; + using DotNetOpenAuth.Messaging; using DotNetOpenAuth.Messaging.Reflection; using DotNetOpenAuth.Test.OpenId; @@ -146,7 +148,7 @@ namespace DotNetOpenAuth.Test.Mocks { this.incomingMessageSignal.Set(); } - protected internal override HttpRequestInfo GetRequestFromContext() { + protected internal override HttpRequestBase GetRequestFromContext() { MessageReceivingEndpoint recipient; var messageData = this.AwaitIncomingMessage(out recipient); if (messageData != null) { @@ -191,12 +193,13 @@ namespace DotNetOpenAuth.Test.Mocks { return this.PrepareDirectResponse(message); } - protected override IDirectedProtocolMessage ReadFromRequestCore(HttpRequestInfo request) { - if (request.Message != null) { - this.ProcessMessageFilter(request.Message, false); + protected override IDirectedProtocolMessage ReadFromRequestCore(HttpRequestBase request) { + var mockRequest = (CoordinatingHttpRequestInfo)request; + if (mockRequest.Message != null) { + this.ProcessMessageFilter(mockRequest.Message, false); } - return request.Message; + return mockRequest.Message; } protected override IDictionary<string, string> ReadFromResponseCore(IncomingWebResponse response) { diff --git a/src/DotNetOpenAuth.Test/Mocks/CoordinatingHttpRequestInfo.cs b/src/DotNetOpenAuth.Test/Mocks/CoordinatingHttpRequestInfo.cs index bfb9017..1917ce6 100644 --- a/src/DotNetOpenAuth.Test/Mocks/CoordinatingHttpRequestInfo.cs +++ b/src/DotNetOpenAuth.Test/Mocks/CoordinatingHttpRequestInfo.cs @@ -5,15 +5,21 @@ //----------------------------------------------------------------------- namespace DotNetOpenAuth.Test.Mocks { + using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using DotNetOpenAuth.Messaging; internal class CoordinatingHttpRequestInfo : HttpRequestInfo { - private IDictionary<string, string> messageData; - private IMessageFactory messageFactory; - private MessageReceivingEndpoint recipient; - private Channel channel; + private readonly Channel channel; + + private readonly IDictionary<string, string> messageData; + + private readonly IMessageFactory messageFactory; + + private readonly MessageReceivingEndpoint recipient; + + private IDirectedProtocolMessage message; /// <summary> /// Initializes a new instance of the <see cref="CoordinatingHttpRequestInfo"/> class @@ -23,14 +29,18 @@ namespace DotNetOpenAuth.Test.Mocks { /// <param name="messageFactory">The message factory.</param> /// <param name="messageData">The message data.</param> /// <param name="recipient">The recipient.</param> - internal CoordinatingHttpRequestInfo(Channel channel, IMessageFactory messageFactory, IDictionary<string, string> messageData, MessageReceivingEndpoint recipient) + internal CoordinatingHttpRequestInfo( + Channel channel, + IMessageFactory messageFactory, + IDictionary<string, string> messageData, + MessageReceivingEndpoint recipient) : this(recipient) { Contract.Requires(channel != null); Contract.Requires(messageFactory != null); Contract.Requires(messageData != null); this.channel = channel; - this.messageFactory = messageFactory; this.messageData = messageData; + this.messageFactory = messageFactory; } /// <summary> @@ -38,35 +48,56 @@ namespace DotNetOpenAuth.Test.Mocks { /// that will not generate any message. /// </summary> /// <param name="recipient">The recipient.</param> - internal CoordinatingHttpRequestInfo(MessageReceivingEndpoint recipient) { + internal CoordinatingHttpRequestInfo(MessageReceivingEndpoint recipient) + : base(GetHttpVerb(recipient), recipient != null ? recipient.Location : new Uri("http://host/path")) { this.recipient = recipient; - if (recipient != null) { - this.UrlBeforeRewriting = recipient.Location; - } + } - if (recipient == null || (recipient.AllowedMethods & HttpDeliveryMethods.GetRequest) != 0) { - this.HttpMethod = "GET"; - } else if ((recipient.AllowedMethods & HttpDeliveryMethods.PostRequest) != 0) { - this.HttpMethod = "POST"; - } + /// <summary> + /// Initializes a new instance of the <see cref="CoordinatingHttpRequestInfo"/> class. + /// </summary> + /// <param name="message">The message being passed in through a mock transport. May be null.</param> + /// <param name="httpMethod">The HTTP method that the incoming request came in on, whether or not <paramref name="message"/> is null.</param> + internal CoordinatingHttpRequestInfo(IDirectedProtocolMessage message, HttpDeliveryMethods httpMethod) + : base(GetHttpVerb(httpMethod), message.Recipient) { + this.message = message; } - internal override IDirectedProtocolMessage Message { + /// <summary> + /// Gets the message deserialized from the remote channel. + /// </summary> + internal IDirectedProtocolMessage Message { get { - if (base.Message == null && this.messageData != null) { - IDirectedProtocolMessage message = this.messageFactory.GetNewRequestMessage(this.recipient, this.messageData); + if (this.message == null && this.messageData != null) { + var message = messageFactory.GetNewRequestMessage(recipient, this.messageData); if (message != null) { this.channel.MessageDescriptions.GetAccessor(message).Deserialize(this.messageData); + this.message = message; } - base.Message = message; } - return base.Message; + return this.message; + } + } + + private static string GetHttpVerb(MessageReceivingEndpoint recipient) { + if (recipient == null) { + return "GET"; } - set { - base.Message = value; + return GetHttpVerb(recipient.AllowedMethods); + } + + private static string GetHttpVerb(HttpDeliveryMethods httpMethod) { + if ((httpMethod & HttpDeliveryMethods.GetRequest) != 0) { + return "GET"; } + + if ((httpMethod & HttpDeliveryMethods.PostRequest) != 0) { + return "POST"; + } + + throw new ArgumentOutOfRangeException(); } } } diff --git a/src/DotNetOpenAuth.Test/Mocks/CoordinatingOAuthConsumerChannel.cs b/src/DotNetOpenAuth.Test/Mocks/CoordinatingOAuthConsumerChannel.cs index 6cc5819..e145952 100644 --- a/src/DotNetOpenAuth.Test/Mocks/CoordinatingOAuthConsumerChannel.cs +++ b/src/DotNetOpenAuth.Test/Mocks/CoordinatingOAuthConsumerChannel.cs @@ -8,6 +8,8 @@ namespace DotNetOpenAuth.Test.Mocks { using System; using System.Diagnostics.Contracts; using System.Threading; + using System.Web; + using DotNetOpenAuth.Messaging; using DotNetOpenAuth.Messaging.Bindings; using DotNetOpenAuth.OAuth.ChannelElements; @@ -49,7 +51,7 @@ namespace DotNetOpenAuth.Test.Mocks { internal OutgoingWebResponse RequestProtectedResource(AccessProtectedResourceRequest request) { ((ITamperResistantOAuthMessage)request).HttpMethod = this.GetHttpMethod(((ITamperResistantOAuthMessage)request).HttpMethods); this.ProcessOutgoingMessage(request); - HttpRequestInfo requestInfo = this.SpoofHttpMethod(request); + var requestInfo = this.SpoofHttpMethod(request); TestBase.TestLogger.InfoFormat("Sending protected resource request: {0}", requestInfo.Message); // Drop the outgoing message in the other channel's in-slot and let them know it's there. this.RemoteChannel.IncomingMessage = requestInfo.Message; @@ -57,13 +59,13 @@ namespace DotNetOpenAuth.Test.Mocks { return this.AwaitIncomingRawResponse(); } - protected internal override HttpRequestInfo GetRequestFromContext() { + protected internal override HttpRequestBase GetRequestFromContext() { var directedMessage = (IDirectedProtocolMessage)this.AwaitIncomingMessage(); - return new HttpRequestInfo(directedMessage, directedMessage.HttpMethods); + return new CoordinatingHttpRequestInfo(directedMessage, directedMessage.HttpMethods); } protected override IProtocolMessage RequestCore(IDirectedProtocolMessage request) { - HttpRequestInfo requestInfo = this.SpoofHttpMethod(request); + var requestInfo = this.SpoofHttpMethod(request); // Drop the outgoing message in the other channel's in-slot and let them know it's there. this.RemoteChannel.IncomingMessage = requestInfo.Message; this.RemoteChannel.IncomingMessageSignal.Set(); @@ -72,7 +74,7 @@ namespace DotNetOpenAuth.Test.Mocks { } protected override OutgoingWebResponse PrepareDirectResponse(IProtocolMessage response) { - this.RemoteChannel.IncomingMessage = CloneSerializedParts(response, null); + this.RemoteChannel.IncomingMessage = this.CloneSerializedParts(response); this.RemoteChannel.IncomingMessageSignal.Set(); return new OutgoingWebResponse(); // not used, but returning null is not allowed } @@ -82,8 +84,9 @@ namespace DotNetOpenAuth.Test.Mocks { return this.PrepareDirectResponse(message); } - protected override IDirectedProtocolMessage ReadFromRequestCore(HttpRequestInfo request) { - return request.Message; + protected override IDirectedProtocolMessage ReadFromRequestCore(HttpRequestBase request) { + var mockRequest = (CoordinatingHttpRequestInfo)request; + return mockRequest.Message; } /// <summary> @@ -91,19 +94,14 @@ namespace DotNetOpenAuth.Test.Mocks { /// </summary> /// <param name="message">The message to add a pretend HTTP method to.</param> /// <returns>A spoofed HttpRequestInfo that wraps the new message.</returns> - private HttpRequestInfo SpoofHttpMethod(IDirectedProtocolMessage message) { - HttpRequestInfo requestInfo = new HttpRequestInfo(message, message.HttpMethods); - + private CoordinatingHttpRequestInfo SpoofHttpMethod(IDirectedProtocolMessage message) { var signedMessage = message as ITamperResistantOAuthMessage; if (signedMessage != null) { string httpMethod = this.GetHttpMethod(signedMessage.HttpMethods); - requestInfo.HttpMethod = httpMethod; - requestInfo.UrlBeforeRewriting = message.Recipient; signedMessage.HttpMethod = httpMethod; } - requestInfo.Message = this.CloneSerializedParts(message, requestInfo); - + var requestInfo = new CoordinatingHttpRequestInfo(this.CloneSerializedParts(message), message.HttpMethods); return requestInfo; } @@ -121,7 +119,7 @@ namespace DotNetOpenAuth.Test.Mocks { return response; } - private T CloneSerializedParts<T>(T message, HttpRequestInfo requestInfo) where T : class, IProtocolMessage { + private T CloneSerializedParts<T>(T message) where T : class, IProtocolMessage { Requires.NotNull(message, "message"); IProtocolMessage clonedMessage; diff --git a/src/DotNetOpenAuth.Test/Mocks/CoordinatingOAuthServiceProviderChannel.cs b/src/DotNetOpenAuth.Test/Mocks/CoordinatingOAuthServiceProviderChannel.cs index ad5c695..012173c 100644 --- a/src/DotNetOpenAuth.Test/Mocks/CoordinatingOAuthServiceProviderChannel.cs +++ b/src/DotNetOpenAuth.Test/Mocks/CoordinatingOAuthServiceProviderChannel.cs @@ -8,10 +8,13 @@ namespace DotNetOpenAuth.Test.Mocks { using System; using System.Diagnostics.Contracts; using System.Threading; + using System.Web; + using DotNetOpenAuth.Messaging; using DotNetOpenAuth.Messaging.Bindings; using DotNetOpenAuth.OAuth.ChannelElements; using DotNetOpenAuth.OAuth.Messages; + using NUnit.Framework; /// <summary> /// A special channel used in test simulations to pass messages directly between two parties. @@ -48,9 +51,9 @@ namespace DotNetOpenAuth.Test.Mocks { internal CoordinatingOAuthConsumerChannel RemoteChannel { get; set; } internal OutgoingWebResponse RequestProtectedResource(AccessProtectedResourceRequest request) { - ((ITamperResistantOAuthMessage)request).HttpMethod = this.GetHttpMethod(((ITamperResistantOAuthMessage)request).HttpMethods); + ((ITamperResistantOAuthMessage)request).HttpMethod = GetHttpMethod(((ITamperResistantOAuthMessage)request).HttpMethods); this.ProcessOutgoingMessage(request); - HttpRequestInfo requestInfo = this.SpoofHttpMethod(request); + var requestInfo = this.SpoofHttpMethod(request); TestBase.TestLogger.InfoFormat("Sending protected resource request: {0}", requestInfo.Message); // Drop the outgoing message in the other channel's in-slot and let them know it's there. this.RemoteChannel.IncomingMessage = requestInfo.Message; @@ -63,13 +66,13 @@ namespace DotNetOpenAuth.Test.Mocks { this.RemoteChannel.IncomingMessageSignal.Set(); } - protected internal override HttpRequestInfo GetRequestFromContext() { + protected internal override HttpRequestBase GetRequestFromContext() { var directedMessage = (IDirectedProtocolMessage)this.AwaitIncomingMessage(); - return new HttpRequestInfo(directedMessage, directedMessage.HttpMethods); + return new CoordinatingHttpRequestInfo(directedMessage, directedMessage.HttpMethods); } protected override IProtocolMessage RequestCore(IDirectedProtocolMessage request) { - HttpRequestInfo requestInfo = this.SpoofHttpMethod(request); + var requestInfo = this.SpoofHttpMethod(request); // Drop the outgoing message in the other channel's in-slot and let them know it's there. this.RemoteChannel.IncomingMessage = requestInfo.Message; this.RemoteChannel.IncomingMessageSignal.Set(); @@ -78,7 +81,7 @@ namespace DotNetOpenAuth.Test.Mocks { } protected override OutgoingWebResponse PrepareDirectResponse(IProtocolMessage response) { - this.RemoteChannel.IncomingMessage = CloneSerializedParts(response, null); + this.RemoteChannel.IncomingMessage = this.CloneSerializedParts(response); this.RemoteChannel.IncomingMessageSignal.Set(); return new OutgoingWebResponse(); // not used, but returning null is not allowed } @@ -88,8 +91,13 @@ namespace DotNetOpenAuth.Test.Mocks { return this.PrepareDirectResponse(message); } - protected override IDirectedProtocolMessage ReadFromRequestCore(HttpRequestInfo request) { - return request.Message; + protected override IDirectedProtocolMessage ReadFromRequestCore(HttpRequestBase request) { + var mockRequest = (CoordinatingHttpRequestInfo)request; + return mockRequest.Message; + } + + private static string GetHttpMethod(HttpDeliveryMethods methods) { + return (methods & HttpDeliveryMethods.PostRequest) != 0 ? "POST" : "GET"; } /// <summary> @@ -97,24 +105,20 @@ namespace DotNetOpenAuth.Test.Mocks { /// </summary> /// <param name="message">The message to add a pretend HTTP method to.</param> /// <returns>A spoofed HttpRequestInfo that wraps the new message.</returns> - private HttpRequestInfo SpoofHttpMethod(IDirectedProtocolMessage message) { - HttpRequestInfo requestInfo = new HttpRequestInfo(message, message.HttpMethods); - + private CoordinatingHttpRequestInfo SpoofHttpMethod(IDirectedProtocolMessage message) { var signedMessage = message as ITamperResistantOAuthMessage; if (signedMessage != null) { - string httpMethod = this.GetHttpMethod(signedMessage.HttpMethods); - requestInfo.HttpMethod = httpMethod; - requestInfo.UrlBeforeRewriting = message.Recipient; + string httpMethod = GetHttpMethod(signedMessage.HttpMethods); signedMessage.HttpMethod = httpMethod; } - requestInfo.Message = this.CloneSerializedParts(message, requestInfo); - + var requestInfo = new CoordinatingHttpRequestInfo(this.CloneSerializedParts(message), message.HttpMethods); return requestInfo; } private IProtocolMessage AwaitIncomingMessage() { this.IncomingMessageSignal.WaitOne(); + Assert.That(this.IncomingMessage, Is.Not.Null, "Incoming message signaled, but none supplied."); IProtocolMessage response = this.IncomingMessage; this.IncomingMessage = null; return response; @@ -127,7 +131,7 @@ namespace DotNetOpenAuth.Test.Mocks { return response; } - private T CloneSerializedParts<T>(T message, HttpRequestInfo requestInfo) where T : class, IProtocolMessage { + private T CloneSerializedParts<T>(T message) where T : class, IProtocolMessage { Requires.NotNull(message, "message"); IProtocolMessage clonedMessage; @@ -155,9 +159,5 @@ namespace DotNetOpenAuth.Test.Mocks { return (T)clonedMessage; } - - private string GetHttpMethod(HttpDeliveryMethods methods) { - return (methods & HttpDeliveryMethods.PostRequest) != 0 ? "POST" : "GET"; - } } } diff --git a/src/DotNetOpenAuth.Test/Mocks/TestBadChannel.cs b/src/DotNetOpenAuth.Test/Mocks/TestBadChannel.cs index 5344304..263f0fd 100644 --- a/src/DotNetOpenAuth.Test/Mocks/TestBadChannel.cs +++ b/src/DotNetOpenAuth.Test/Mocks/TestBadChannel.cs @@ -7,6 +7,7 @@ namespace DotNetOpenAuth.Test.Mocks { using System; using System.Collections.Generic; + using System.Web; using DotNetOpenAuth.Messaging; /// <summary> @@ -33,7 +34,7 @@ namespace DotNetOpenAuth.Test.Mocks { return base.Receive(fields, recipient); } - internal new IProtocolMessage ReadFromRequest(HttpRequestInfo request) { + internal new IProtocolMessage ReadFromRequest(HttpRequestBase request) { return base.ReadFromRequest(request); } diff --git a/src/DotNetOpenAuth.Test/OAuth/ChannelElements/OAuthChannelTests.cs b/src/DotNetOpenAuth.Test/OAuth/ChannelElements/OAuthChannelTests.cs index dda5452..7999a44 100644 --- a/src/DotNetOpenAuth.Test/OAuth/ChannelElements/OAuthChannelTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth/ChannelElements/OAuthChannelTests.cs @@ -78,23 +78,24 @@ namespace DotNetOpenAuth.Test.OAuth.ChannelElements { [Test] public void ReadFromRequestAuthorizationScattered() { // Start by creating a standard POST HTTP request. - var fields = new Dictionary<string, string> { + var postedFields = new Dictionary<string, string> { { "age", "15" }, }; - HttpRequestInfo requestInfo = CreateHttpRequestInfo(HttpDeliveryMethods.PostRequest, fields); // Now add another field to the request URL - UriBuilder builder = new UriBuilder(requestInfo.UrlBeforeRewriting); + var builder = new UriBuilder(MessagingTestBase.DefaultUrlForHttpRequestInfo); builder.Query = "Name=Andrew"; - requestInfo.UrlBeforeRewriting = builder.Uri; - requestInfo.RawUrl = builder.Path + builder.Query + builder.Fragment; // Finally, add an Authorization header - fields = new Dictionary<string, string> { + var authHeaderFields = new Dictionary<string, string> { { "Location", "http://hostb/pathB" }, { "Timestamp", XmlConvert.ToString(DateTime.UtcNow, XmlDateTimeSerializationMode.Utc) }, }; - requestInfo.Headers.Add(HttpRequestHeader.Authorization, CreateAuthorizationHeader(fields)); + var headers = new NameValueCollection(); + headers.Add(HttpRequestHeaders.Authorization, CreateAuthorizationHeader(authHeaderFields)); + headers.Add(HttpRequestHeaders.ContentType, Channel.HttpFormUrlEncoded); + + var requestInfo = new HttpRequestInfo("POST", builder.Uri, form: postedFields.ToNameValueCollection(), headers: headers); IDirectedProtocolMessage requestMessage = this.channel.ReadFromRequest(requestInfo); @@ -266,51 +267,33 @@ namespace DotNetOpenAuth.Test.OAuth.ChannelElements { } private static HttpRequestInfo CreateHttpRequestInfo(HttpDeliveryMethods scheme, IDictionary<string, string> fields) { - string query = MessagingUtilities.CreateQueryString(fields); - UriBuilder requestUri = new UriBuilder("http://localhost/path"); - WebHeaderCollection headers = new WebHeaderCollection(); - MemoryStream ms = new MemoryStream(); + var requestUri = new UriBuilder(MessagingTestBase.DefaultUrlForHttpRequestInfo); + var headers = new NameValueCollection(); + NameValueCollection form = null; string method; switch (scheme) { case HttpDeliveryMethods.PostRequest: method = "POST"; - headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded"); - StreamWriter sw = new StreamWriter(ms); - sw.Write(query); - sw.Flush(); - ms.Position = 0; + form = fields.ToNameValueCollection(); + headers.Add(HttpRequestHeaders.ContentType, Channel.HttpFormUrlEncoded); break; case HttpDeliveryMethods.GetRequest: method = "GET"; - requestUri.Query = query; + requestUri.Query = MessagingUtilities.CreateQueryString(fields); break; case HttpDeliveryMethods.AuthorizationHeaderRequest: method = "GET"; - headers.Add(HttpRequestHeader.Authorization, CreateAuthorizationHeader(fields)); + headers.Add(HttpRequestHeaders.Authorization, CreateAuthorizationHeader(fields)); break; default: throw new ArgumentOutOfRangeException("scheme", scheme, "Unexpected value"); } - HttpRequestInfo request = new HttpRequestInfo { - HttpMethod = method, - UrlBeforeRewriting = requestUri.Uri, - RawUrl = requestUri.Path + requestUri.Query + requestUri.Fragment, - Headers = headers, - InputStream = ms, - }; - return request; + return new HttpRequestInfo(method, requestUri.Uri, form: form, headers: headers); } private static HttpRequestInfo ConvertToRequestInfo(HttpWebRequest request, Stream postEntity) { - HttpRequestInfo info = new HttpRequestInfo { - HttpMethod = request.Method, - UrlBeforeRewriting = request.RequestUri, - RawUrl = request.RequestUri.AbsolutePath + request.RequestUri.Query + request.RequestUri.Fragment, - Headers = request.Headers, - InputStream = postEntity, - }; - return info; + return new HttpRequestInfo(request.Method, request.RequestUri, request.Headers, postEntity); } private void ParameterizedRequestTest(HttpDeliveryMethods scheme) { diff --git a/src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs b/src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs index 1f56b32..b00cd8e 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/UserAgentClientAuthorizeTests.cs @@ -39,6 +39,7 @@ namespace DotNetOpenAuth.Test.OAuth2 { }, server => { var request = server.ReadAuthorizationRequest(); + Assert.That(request, Is.Not.Null); server.ApproveAuthorizationRequest(request, ResourceOwnerUsername); var tokenRequest = server.ReadAccessTokenRequest(); IAccessTokenRequest accessTokenRequest = tokenRequest; @@ -70,6 +71,7 @@ namespace DotNetOpenAuth.Test.OAuth2 { }, server => { var request = server.ReadAuthorizationRequest(); + Assert.That(request, Is.Not.Null); IAccessTokenRequest accessTokenRequest = (EndUserAuthorizationImplicitRequest)request; Assert.That(accessTokenRequest.ClientAuthenticated, Is.False); server.ApproveAuthorizationRequest(request, ResourceOwnerUsername); diff --git a/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs b/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs index d7439d9..0bb4378 100644 --- a/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs +++ b/src/DotNetOpenAuth.Test/OAuth2/WebServerClientAuthorizeTests.cs @@ -35,6 +35,7 @@ namespace DotNetOpenAuth.Test.OAuth2 { }, server => { var request = server.ReadAuthorizationRequest(); + Assert.That(request, Is.Not.Null); server.ApproveAuthorizationRequest(request, ResourceOwnerUsername); var tokenRequest = server.ReadAccessTokenRequest(); IAccessTokenRequest accessTokenRequest = tokenRequest; diff --git a/src/DotNetOpenAuth.Test/OpenId/AssociationHandshakeTests.cs b/src/DotNetOpenAuth.Test/OpenId/AssociationHandshakeTests.cs index e8c955e..029447d 100644 --- a/src/DotNetOpenAuth.Test/OpenId/AssociationHandshakeTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/AssociationHandshakeTests.cs @@ -82,6 +82,7 @@ namespace DotNetOpenAuth.Test.OpenId { // Receive initial request for an HMAC-SHA256 association. AutoResponsiveRequest req = (AutoResponsiveRequest)op.GetRequest(); AssociateRequest associateRequest = (AssociateRequest)req.RequestMessage; + Assert.That(associateRequest, Is.Not.Null); Assert.AreEqual(protocol.Args.SignatureAlgorithm.HMAC_SHA256, associateRequest.AssociationType); // Ensure that the response is a suggestion that the RP try again with HMAC-SHA1 diff --git a/src/DotNetOpenAuth.Test/OpenId/Provider/AuthenticationRequestTest.cs b/src/DotNetOpenAuth.Test/OpenId/Provider/AuthenticationRequestTest.cs index 2819e40..8cc7116 100644 --- a/src/DotNetOpenAuth.Test/OpenId/Provider/AuthenticationRequestTest.cs +++ b/src/DotNetOpenAuth.Test/OpenId/Provider/AuthenticationRequestTest.cs @@ -34,7 +34,7 @@ namespace DotNetOpenAuth.Test.OpenId.Provider { Assert.IsNotNull(userSetupUrl); // Now construct a new request as if it had just come in. - HttpRequestInfo httpRequest = new HttpRequestInfo { UrlBeforeRewriting = userSetupUrl }; + HttpRequestInfo httpRequest = new HttpRequestInfo("GET", userSetupUrl); var setupRequest = (AuthenticationRequest)provider.GetRequest(httpRequest); var setupRequestMessage = (CheckIdRequest)setupRequest.RequestMessage; diff --git a/src/DotNetOpenAuth.Test/OpenId/Provider/OpenIdProviderTests.cs b/src/DotNetOpenAuth.Test/OpenId/Provider/OpenIdProviderTests.cs index d981e71..598aeb7 100644 --- a/src/DotNetOpenAuth.Test/OpenId/Provider/OpenIdProviderTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/Provider/OpenIdProviderTests.cs @@ -92,10 +92,9 @@ namespace DotNetOpenAuth.Test.OpenId.Provider { /// </summary> [Test] public void GetRequest() { - HttpRequestInfo httpInfo = new HttpRequestInfo(); - httpInfo.UrlBeforeRewriting = new Uri("http://someUri"); + var httpInfo = new HttpRequestInfo("GET", new Uri("http://someUri")); Assert.IsNull(this.provider.GetRequest(httpInfo), "An irrelevant request should return null."); - var providerDescription = new ProviderEndpointDescription(OpenIdTestBase.OPUri, Protocol.Default.Version); + var providerDescription = new ProviderEndpointDescription(OPUri, Protocol.Default.Version); // Test some non-empty request scenario. OpenIdCoordinator coordinator = new OpenIdCoordinator( diff --git a/src/DotNetOpenAuth.Test/OpenId/Provider/PerformanceTests.cs b/src/DotNetOpenAuth.Test/OpenId/Provider/PerformanceTests.cs index 27e65cc..e2c719d 100644 --- a/src/DotNetOpenAuth.Test/OpenId/Provider/PerformanceTests.cs +++ b/src/DotNetOpenAuth.Test/OpenId/Provider/PerformanceTests.cs @@ -102,7 +102,7 @@ namespace DotNetOpenAuth.Test.OpenId.Provider { ms.Position = 0; var headers = new WebHeaderCollection(); headers.Add(HttpRequestHeader.ContentType, Channel.HttpFormUrlEncoded); - var httpRequest = new HttpRequestInfo("POST", opEndpoint, opEndpoint.PathAndQuery, headers, ms); + var httpRequest = new HttpRequestInfo("POST", opEndpoint, headers, ms); return httpRequest; } @@ -122,8 +122,7 @@ namespace DotNetOpenAuth.Test.OpenId.Provider { Channel rpChannel = rp.Channel; UriBuilder receiver = new UriBuilder(OPUri); receiver.Query = MessagingUtilities.CreateQueryString(rpChannel.MessageDescriptions.GetAccessor(checkidMessage)); - var headers = new WebHeaderCollection(); - var httpRequest = new HttpRequestInfo("GET", receiver.Uri, receiver.Uri.PathAndQuery, headers, null); + var httpRequest = new HttpRequestInfo("GET", receiver.Uri); return httpRequest; } } |