diff options
Diffstat (limited to 'src/DotNetOAuth.Test/Messaging')
-rw-r--r-- | src/DotNetOAuth.Test/Messaging/ChannelTests.cs | 123 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Messaging/HttpRequestInfoTests.cs | 36 | ||||
-rw-r--r-- | src/DotNetOAuth.Test/Messaging/ResponseTest.cs | 22 |
3 files changed, 177 insertions, 4 deletions
diff --git a/src/DotNetOAuth.Test/Messaging/ChannelTests.cs b/src/DotNetOAuth.Test/Messaging/ChannelTests.cs index 26155cb..dc433cf 100644 --- a/src/DotNetOAuth.Test/Messaging/ChannelTests.cs +++ b/src/DotNetOAuth.Test/Messaging/ChannelTests.cs @@ -30,7 +30,7 @@ namespace DotNetOAuth.Test.Messaging { public void CtorNull() {
// This bad channel is deliberately constructed to pass null to
// its protected base class' constructor.
- new TestBadChannel();
+ new TestBadChannel(true);
}
[TestMethod]
@@ -53,6 +53,24 @@ namespace DotNetOAuth.Test.Messaging { this.channel.Send(null);
}
+ [TestMethod, ExpectedException(typeof(ArgumentException))]
+ public void SendIndirectedUndirectedMessage() {
+ IProtocolMessage message = new TestMessage(MessageTransport.Indirect);
+ this.channel.Send(message);
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentException))]
+ public void SendDirectedNoRecipientMessage() {
+ IProtocolMessage message = new TestDirectedMessage(MessageTransport.Indirect);
+ this.channel.Send(message);
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentException))]
+ public void SendInvalidMessageTransport() {
+ IProtocolMessage message = new TestDirectedMessage((MessageTransport)100);
+ this.channel.Send(message);
+ }
+
[TestMethod]
public void SendIndirectMessage301Get() {
IProtocolMessage message = new TestDirectedMessage(MessageTransport.Indirect) {
@@ -70,6 +88,27 @@ namespace DotNetOAuth.Test.Messaging { StringAssert.Contains(response.Headers[HttpResponseHeader.Location], "Location=http%3a%2f%2fhost%2fpath");
}
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void SendIndirectMessage301GetNullMessage() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ badChannel.Create301RedirectResponse(null, new Dictionary<string, string>());
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentException))]
+ public void SendIndirectMessage301GetEmptyRecipient() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ var message = new TestDirectedMessage(MessageTransport.Indirect);
+ badChannel.Create301RedirectResponse(message, new Dictionary<string, string>());
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void SendIndirectMessage301GetNullFields() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ var message = new TestDirectedMessage(MessageTransport.Indirect);
+ message.Recipient = new Uri("http://someserver");
+ badChannel.Create301RedirectResponse(message, null);
+ }
+
[TestMethod]
public void SendIndirectMessageFormPost() {
// We craft a very large message to force fallback to form POST.
@@ -85,7 +124,7 @@ namespace DotNetOAuth.Test.Messaging { Response response = this.channel.DequeueIndirectOrResponseMessage();
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 = Encoding.UTF8.GetString(response.Body);
+ string body = response.Body;
StringAssert.Contains(body, "<form ");
StringAssert.Contains(body, "action=\"http://provider/path\"");
StringAssert.Contains(body, "method=\"post\"");
@@ -95,6 +134,27 @@ namespace DotNetOAuth.Test.Messaging { StringAssert.Contains(body, ".submit()", "There should be some javascript to automate form submission.");
}
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void SendIndirectMessageFormPostNullMessage() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ badChannel.CreateFormPostResponse(null, new Dictionary<string, string>());
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentException))]
+ public void SendIndirectMessageFormPostEmptyRecipient() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ var message = new TestDirectedMessage(MessageTransport.Indirect);
+ badChannel.CreateFormPostResponse(message, new Dictionary<string, string>());
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void SendIndirectMessageFormPostNullFields() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ var message = new TestDirectedMessage(MessageTransport.Indirect);
+ message.Recipient = new Uri("http://someserver");
+ badChannel.CreateFormPostResponse(message, null);
+ }
+
/// <summary>
/// Tests that a direct message is sent when the appropriate message type is provided.
/// </summary>
@@ -112,7 +172,62 @@ namespace DotNetOAuth.Test.Messaging { this.channel.Send(message);
}
- private static HttpRequestInfo CreateHttpRequest(string method, IDictionary<string, string> fields) {
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void QueueIndirectOrResponseMessageNull() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ badChannel.QueueIndirectOrResponseMessage(null);
+ }
+
+ [TestMethod, ExpectedException(typeof(InvalidOperationException))]
+ public void QueueIndirectOrResponseMessageTwice() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ Response response = new Response();
+ badChannel.QueueIndirectOrResponseMessage(new Response());
+ badChannel.QueueIndirectOrResponseMessage(new Response());
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void SendIndirectMessageNull() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ badChannel.SendIndirectMessage(null);
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void ReceiveNull() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ badChannel.Receive(null);
+ }
+
+ [TestMethod]
+ public void ReceiveUnrecognizedMessage() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ Assert.IsNull(badChannel.Receive(new Dictionary<string, string>()));
+ }
+
+ [TestMethod]
+ public void ReadFromRequestWithContext() {
+ // TODO: make this a request with a message in it.
+ HttpRequest request = new HttpRequest("somefile", "http://someurl", "age=15");
+ HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
+ IProtocolMessage message = this.channel.ReadFromRequest();
+ Assert.IsNotNull(message);
+ Assert.IsInstanceOfType(message, typeof(TestMessage));
+ Assert.AreEqual(15, ((TestMessage)message).Age);
+ }
+
+ [TestMethod, ExpectedException(typeof(InvalidOperationException))]
+ public void ReadFromRequestNoContext() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ badChannel.ReadFromRequest();
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void ReadFromRequestNull() {
+ TestBadChannel badChannel = new TestBadChannel(false);
+ badChannel.ReadFromRequest(null);
+ }
+
+ private 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();
@@ -144,7 +259,7 @@ namespace DotNetOAuth.Test.Messaging { { "Name", "Andrew" },
{ "Location", "http://hostb/pathB" },
};
- IProtocolMessage requestMessage = this.channel.ReadFromRequest(CreateHttpRequest(method, fields));
+ IProtocolMessage requestMessage = this.channel.ReadFromRequest(CreateHttpRequestInfo(method, fields));
Assert.IsNotNull(requestMessage);
Assert.IsInstanceOfType(requestMessage, typeof(TestMessage));
TestMessage testMessage = (TestMessage)requestMessage;
diff --git a/src/DotNetOAuth.Test/Messaging/HttpRequestInfoTests.cs b/src/DotNetOAuth.Test/Messaging/HttpRequestInfoTests.cs new file mode 100644 index 0000000..108a147 --- /dev/null +++ b/src/DotNetOAuth.Test/Messaging/HttpRequestInfoTests.cs @@ -0,0 +1,36 @@ +//-----------------------------------------------------------------------
+// <copyright file="HttpRequestInfoTests.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+using System.Web;
+using DotNetOAuth.Messaging;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace DotNetOAuth.Test.Messaging {
+ [TestClass]
+ public class HttpRequestInfoTests : TestBase {
+ [TestMethod]
+ 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.HttpMethod, info.HttpMethod);
+ }
+
+ /// <summary>
+ /// Checks that a property dependent on another null property
+ /// doesn't generate a NullReferenceException.
+ /// </summary>
+ [TestMethod]
+ public void QueryBeforeSettingUrl() {
+ HttpRequestInfo info = new HttpRequestInfo();
+ Assert.IsNull(info.Query);
+ }
+ }
+}
diff --git a/src/DotNetOAuth.Test/Messaging/ResponseTest.cs b/src/DotNetOAuth.Test/Messaging/ResponseTest.cs index c1c7dff..6b49e62 100644 --- a/src/DotNetOAuth.Test/Messaging/ResponseTest.cs +++ b/src/DotNetOAuth.Test/Messaging/ResponseTest.cs @@ -8,6 +8,10 @@ namespace DotNetOAuth.Test.Messaging { using System;
using DotNetOAuth.Messaging;
using Microsoft.VisualStudio.TestTools.UnitTesting;
+ using System.Web;
+ using System.IO;
+ using System.Threading;
+ using System.Text;
[TestClass]
public class ResponseTest : TestBase {
@@ -15,5 +19,23 @@ namespace DotNetOAuth.Test.Messaging { public void SendWithoutAspNetContext() {
new Response().Send();
}
+
+ [TestMethod]
+ public void Send() {
+ StringWriter writer = new StringWriter();
+ HttpRequest httpRequest = new HttpRequest("file", "http://server", "");
+ HttpResponse httpResponse = new HttpResponse(writer);
+ HttpContext context = new HttpContext(httpRequest, httpResponse);
+ HttpContext.Current = context;
+
+ Response response = new Response();
+ response.Status = System.Net.HttpStatusCode.OK;
+ response.Headers["someHeaderName"] = "someHeaderValue";
+ response.Body = "some body";
+ response.Send();
+ string results = writer.ToString();
+ // For some reason the only output in test is the body... the headers require a web host
+ Assert.AreEqual(response.Body, results);
+ }
}
}
|