diff options
Diffstat (limited to 'src/DotNetOpenAuth.Test/Messaging/ChannelTests.cs')
-rw-r--r-- | src/DotNetOpenAuth.Test/Messaging/ChannelTests.cs | 159 |
1 files changed, 85 insertions, 74 deletions
diff --git a/src/DotNetOpenAuth.Test/Messaging/ChannelTests.cs b/src/DotNetOpenAuth.Test/Messaging/ChannelTests.cs index 5646a7e..9050fad 100644 --- a/src/DotNetOpenAuth.Test/Messaging/ChannelTests.cs +++ b/src/DotNetOpenAuth.Test/Messaging/ChannelTests.cs @@ -9,29 +9,41 @@ namespace DotNetOpenAuth.Test.Messaging { using System.Collections.Generic; using System.IO; using System.Net; + using System.Net.Http; + using System.Threading; + using System.Threading.Tasks; using System.Web; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.Messaging.Bindings; + using DotNetOpenAuth.OpenId; using DotNetOpenAuth.Test.Mocks; using NUnit.Framework; [TestFixture] public class ChannelTests : MessagingTestBase { [Test, ExpectedException(typeof(ArgumentNullException))] - public void CtorNull() { - // This bad channel is deliberately constructed to pass null to - // its protected base class' constructor. - new TestBadChannel(true); + public void CtorNullFirstParameter() { + new TestBadChannel(null, new IChannelBindingElement[0], new DefaultOpenIdHostFactories()); + } + + [Test, ExpectedException(typeof(ArgumentNullException))] + public void CtorNullSecondParameter() { + new TestBadChannel(new TestMessageFactory(), null, new DefaultOpenIdHostFactories()); + } + + [Test, ExpectedException(typeof(ArgumentNullException))] + public void CtorNullThirdParameter() { + new TestBadChannel(new TestMessageFactory(), new IChannelBindingElement[0], null); } [Test] - public void ReadFromRequestQueryString() { - this.ParameterizedReceiveTest("GET"); + public async Task ReadFromRequestQueryString() { + await this.ParameterizedReceiveTestAsync(HttpMethod.Get); } [Test] - public void ReadFromRequestForm() { - this.ParameterizedReceiveTest("POST"); + public async Task ReadFromRequestForm() { + await this.ParameterizedReceiveTestAsync(HttpMethod.Post); } /// <summary> @@ -39,78 +51,78 @@ namespace DotNetOpenAuth.Test.Messaging { /// will reject messages that come with an unexpected HTTP verb. /// </summary> [Test, ExpectedException(typeof(ProtocolException))] - public void ReadFromRequestDisallowedHttpMethod() { + public async Task ReadFromRequestDisallowedHttpMethod() { var fields = GetStandardTestFields(FieldFill.CompleteBeforeBindings); fields["GetOnly"] = "true"; - this.Channel.ReadFromRequest(CreateHttpRequestInfo("POST", fields)); + await this.Channel.ReadFromRequestAsync(CreateHttpRequestInfo(HttpMethod.Post, fields), CancellationToken.None); } [Test, ExpectedException(typeof(ArgumentNullException))] - public void SendNull() { - this.Channel.PrepareResponse(null); + public async Task SendNull() { + await this.Channel.PrepareResponseAsync(null); } [Test, ExpectedException(typeof(ArgumentException))] - public void SendIndirectedUndirectedMessage() { + public async Task SendIndirectedUndirectedMessage() { IProtocolMessage message = new TestDirectedMessage(MessageTransport.Indirect); - this.Channel.PrepareResponse(message); + await this.Channel.PrepareResponseAsync(message); } [Test, ExpectedException(typeof(ArgumentException))] - public void SendDirectedNoRecipientMessage() { + public async Task SendDirectedNoRecipientMessage() { IProtocolMessage message = new TestDirectedMessage(MessageTransport.Indirect); - this.Channel.PrepareResponse(message); + await this.Channel.PrepareResponseAsync(message); } [Test, ExpectedException(typeof(ArgumentException))] - public void SendInvalidMessageTransport() { + public async Task SendInvalidMessageTransport() { IProtocolMessage message = new TestDirectedMessage((MessageTransport)100); - this.Channel.PrepareResponse(message); + await this.Channel.PrepareResponseAsync(message); } [Test] - public void SendIndirectMessage301Get() { + public async Task SendIndirectMessage301Get() { TestDirectedMessage message = new TestDirectedMessage(MessageTransport.Indirect); GetStandardTestMessage(FieldFill.CompleteBeforeBindings, message); message.Recipient = new Uri("http://provider/path"); var expected = GetStandardTestFields(FieldFill.CompleteBeforeBindings); - OutgoingWebResponse response = this.Channel.PrepareResponse(message); - Assert.AreEqual(HttpStatusCode.Redirect, response.Status); - Assert.AreEqual("text/html; charset=utf-8", response.Headers[HttpResponseHeader.ContentType]); - Assert.IsTrue(response.Body != null && response.Body.Length > 0); // a non-empty body helps get passed filters like WebSense - StringAssert.StartsWith("http://provider/path", response.Headers[HttpResponseHeader.Location]); + var response = await this.Channel.PrepareResponseAsync(message); + Assert.AreEqual(HttpStatusCode.Redirect, response.StatusCode); + Assert.AreEqual("text/html; charset=utf-8", response.Content.Headers.ContentType.ToString()); + Assert.IsTrue(response.Content != null && response.Content.Headers.ContentLength > 0); // a non-empty body helps get passed filters like WebSense + StringAssert.StartsWith("http://provider/path", response.Headers.Location.AbsoluteUri); foreach (var pair in expected) { string key = MessagingUtilities.EscapeUriDataStringRfc3986(pair.Key); string value = MessagingUtilities.EscapeUriDataStringRfc3986(pair.Value); string substring = string.Format("{0}={1}", key, value); - StringAssert.Contains(substring, response.Headers[HttpResponseHeader.Location]); + StringAssert.Contains(substring, response.Headers.Location.AbsoluteUri); } } [Test, ExpectedException(typeof(ArgumentNullException))] public void SendIndirectMessage301GetNullMessage() { - TestBadChannel badChannel = new TestBadChannel(false); + TestBadChannel badChannel = new TestBadChannel(); badChannel.Create301RedirectResponse(null, new Dictionary<string, string>()); } [Test, ExpectedException(typeof(ArgumentException))] public void SendIndirectMessage301GetEmptyRecipient() { - TestBadChannel badChannel = new TestBadChannel(false); + TestBadChannel badChannel = new TestBadChannel(); var message = new TestDirectedMessage(MessageTransport.Indirect); badChannel.Create301RedirectResponse(message, new Dictionary<string, string>()); } [Test, ExpectedException(typeof(ArgumentNullException))] public void SendIndirectMessage301GetNullFields() { - TestBadChannel badChannel = new TestBadChannel(false); + TestBadChannel badChannel = new TestBadChannel(); var message = new TestDirectedMessage(MessageTransport.Indirect); message.Recipient = new Uri("http://someserver"); badChannel.Create301RedirectResponse(message, null); } [Test] - public void SendIndirectMessageFormPost() { + public async Task SendIndirectMessageFormPost() { // We craft a very large message to force fallback to form POST. // We'll also stick some HTML reserved characters in the string value // to test proper character escaping. @@ -120,10 +132,10 @@ namespace DotNetOpenAuth.Test.Messaging { Location = new Uri("http://host/path"), Recipient = new Uri("http://provider/path"), }; - OutgoingWebResponse response = this.Channel.PrepareResponse(message); - Assert.AreEqual(HttpStatusCode.OK, response.Status, "A form redirect should be an HTTP successful response."); - Assert.IsNull(response.Headers[HttpResponseHeader.Location], "There should not be a redirection header in the response."); - string body = response.Body; + var response = await this.Channel.PrepareResponseAsync(message); + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "A form redirect should be an HTTP successful response."); + Assert.IsNull(response.Headers.Location, "There should not be a redirection header in the response."); + string body = await response.Content.ReadAsStringAsync(); StringAssert.Contains("<form ", body); StringAssert.Contains("action=\"http://provider/path\"", body); StringAssert.Contains("method=\"post\"", body); @@ -135,20 +147,20 @@ namespace DotNetOpenAuth.Test.Messaging { [Test, ExpectedException(typeof(ArgumentNullException))] public void SendIndirectMessageFormPostNullMessage() { - TestBadChannel badChannel = new TestBadChannel(false); + TestBadChannel badChannel = new TestBadChannel(); badChannel.CreateFormPostResponse(null, new Dictionary<string, string>()); } [Test, ExpectedException(typeof(ArgumentException))] public void SendIndirectMessageFormPostEmptyRecipient() { - TestBadChannel badChannel = new TestBadChannel(false); + TestBadChannel badChannel = new TestBadChannel(); var message = new TestDirectedMessage(MessageTransport.Indirect); badChannel.CreateFormPostResponse(message, new Dictionary<string, string>()); } [Test, ExpectedException(typeof(ArgumentNullException))] public void SendIndirectMessageFormPostNullFields() { - TestBadChannel badChannel = new TestBadChannel(false); + TestBadChannel badChannel = new TestBadChannel(); var message = new TestDirectedMessage(MessageTransport.Indirect); message.Recipient = new Uri("http://someserver"); badChannel.CreateFormPostResponse(message, null); @@ -162,101 +174,103 @@ namespace DotNetOpenAuth.Test.Messaging { /// we just check that the right method was called. /// </remarks> [Test, ExpectedException(typeof(NotImplementedException))] - public void SendDirectMessageResponse() { + public async Task SendDirectMessageResponse() { IProtocolMessage message = new TestDirectedMessage { Age = 15, Name = "Andrew", Location = new Uri("http://host/path"), }; - this.Channel.PrepareResponse(message); + await this.Channel.PrepareResponseAsync(message); } [Test, ExpectedException(typeof(ArgumentNullException))] public void SendIndirectMessageNull() { - TestBadChannel badChannel = new TestBadChannel(false); + TestBadChannel badChannel = new TestBadChannel(); badChannel.PrepareIndirectResponse(null); } [Test, ExpectedException(typeof(ArgumentNullException))] public void ReceiveNull() { - TestBadChannel badChannel = new TestBadChannel(false); + TestBadChannel badChannel = new TestBadChannel(); badChannel.Receive(null, null); } [Test] public void ReceiveUnrecognizedMessage() { - TestBadChannel badChannel = new TestBadChannel(false); + TestBadChannel badChannel = new TestBadChannel(); Assert.IsNull(badChannel.Receive(new Dictionary<string, string>(), null)); } [Test] - public void ReadFromRequestWithContext() { + public async Task ReadFromRequestWithContext() { var fields = GetStandardTestFields(FieldFill.AllRequired); TestMessage expectedMessage = GetStandardTestMessage(FieldFill.AllRequired); HttpRequest request = new HttpRequest("somefile", "http://someurl", MessagingUtilities.CreateQueryString(fields)); HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter())); - IProtocolMessage message = this.Channel.ReadFromRequest(); + var requestBase = this.Channel.GetRequestFromContext(); + IProtocolMessage message = await this.Channel.ReadFromRequestAsync(requestBase.AsHttpRequestMessage(), CancellationToken.None); Assert.IsNotNull(message); Assert.IsInstanceOf<TestMessage>(message); Assert.AreEqual(expectedMessage.Age, ((TestMessage)message).Age); } [Test, ExpectedException(typeof(InvalidOperationException))] - public void ReadFromRequestNoContext() { + public void GetRequestFromContextNoContext() { HttpContext.Current = null; - TestBadChannel badChannel = new TestBadChannel(false); - badChannel.ReadFromRequest(); + TestBadChannel badChannel = new TestBadChannel(); + badChannel.GetRequestFromContext(); } [Test, ExpectedException(typeof(ArgumentNullException))] - public void ReadFromRequestNull() { - TestBadChannel badChannel = new TestBadChannel(false); - badChannel.ReadFromRequest(null); + public async Task ReadFromRequestNull() { + TestBadChannel badChannel = new TestBadChannel(); + await badChannel.ReadFromRequestAsync(null, CancellationToken.None); } [Test] - public void SendReplayProtectedMessageSetsNonce() { + public async Task SendReplayProtectedMessageSetsNonce() { TestReplayProtectedMessage message = new TestReplayProtectedMessage(MessageTransport.Indirect); message.Recipient = new Uri("http://localtest"); this.Channel = CreateChannel(MessageProtections.ReplayProtection); - this.Channel.PrepareResponse(message); + await this.Channel.PrepareResponseAsync(message); Assert.IsNotNull(((IReplayProtectedProtocolMessage)message).Nonce); } [Test, ExpectedException(typeof(InvalidSignatureException))] - public void ReceivedInvalidSignature() { + public async Task ReceivedInvalidSignature() { this.Channel = CreateChannel(MessageProtections.TamperProtection); - this.ParameterizedReceiveProtectedTest(DateTime.UtcNow, true); + await this.ParameterizedReceiveProtectedTestAsync(DateTime.UtcNow, true); } [Test] - public void ReceivedReplayProtectedMessageJustOnce() { + public async Task ReceivedReplayProtectedMessageJustOnce() { this.Channel = CreateChannel(MessageProtections.ReplayProtection); - this.ParameterizedReceiveProtectedTest(DateTime.UtcNow, false); + await this.ParameterizedReceiveProtectedTestAsync(DateTime.UtcNow, false); } [Test, ExpectedException(typeof(ReplayedMessageException))] - public void ReceivedReplayProtectedMessageTwice() { + public async Task ReceivedReplayProtectedMessageTwice() { this.Channel = CreateChannel(MessageProtections.ReplayProtection); - this.ParameterizedReceiveProtectedTest(DateTime.UtcNow, false); - this.ParameterizedReceiveProtectedTest(DateTime.UtcNow, false); + await this.ParameterizedReceiveProtectedTestAsync(DateTime.UtcNow, false); + await this.ParameterizedReceiveProtectedTestAsync(DateTime.UtcNow, false); } [Test, ExpectedException(typeof(ProtocolException))] public void MessageExpirationWithoutTamperResistance() { new TestChannel( new TestMessageFactory(), - new StandardExpirationBindingElement()); + new IChannelBindingElement[] { new StandardExpirationBindingElement() }, + new DefaultOpenIdHostFactories()); } [Test, ExpectedException(typeof(ProtocolException))] - public void TooManyBindingElementsProvidingSameProtection() { + public async Task TooManyBindingElementsProvidingSameProtection() { Channel channel = new TestChannel( new TestMessageFactory(), - new MockSigningBindingElement(), - new MockSigningBindingElement()); - channel.ProcessOutgoingMessageTestHook(new TestSignedDirectedMessage()); + new IChannelBindingElement[] { new MockSigningBindingElement(), new MockSigningBindingElement() }, + new DefaultOpenIdHostFactories()); + await channel.ProcessOutgoingMessageTestHookAsync(new TestSignedDirectedMessage()); } [Test] @@ -269,11 +283,8 @@ namespace DotNetOpenAuth.Test.Messaging { Channel channel = new TestChannel( new TestMessageFactory(), - sign, - replay, - expire, - transformB, - transformA); + new[] { sign, replay, expire, transformB, transformA }, + new DefaultOpenIdHostFactories()); Assert.AreEqual(5, channel.BindingElements.Count); Assert.AreSame(transformB, channel.BindingElements[0]); @@ -284,22 +295,22 @@ namespace DotNetOpenAuth.Test.Messaging { } [Test, ExpectedException(typeof(UnprotectedMessageException))] - public void InsufficientlyProtectedMessageSent() { + public async Task InsufficientlyProtectedMessageSent() { var message = new TestSignedDirectedMessage(MessageTransport.Direct); message.Recipient = new Uri("http://localtest"); - this.Channel.PrepareResponse(message); + await this.Channel.PrepareResponseAsync(message); } [Test, ExpectedException(typeof(UnprotectedMessageException))] - public void InsufficientlyProtectedMessageReceived() { + public async Task InsufficientlyProtectedMessageReceived() { this.Channel = CreateChannel(MessageProtections.None, MessageProtections.TamperProtection); - this.ParameterizedReceiveProtectedTest(DateTime.Now, false); + await this.ParameterizedReceiveProtectedTestAsync(DateTime.Now, false); } [Test, ExpectedException(typeof(ProtocolException))] - public void IncomingMessageMissingRequiredParameters() { + public async Task IncomingMessageMissingRequiredParameters() { var fields = GetStandardTestFields(FieldFill.IdentifiableButNotAllRequired); - this.Channel.ReadFromRequest(CreateHttpRequestInfo("GET", fields)); + await this.Channel.ReadFromRequestAsync(CreateHttpRequestInfo(HttpMethod.Get, fields), CancellationToken.None); } } } |