summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2008-09-03 08:18:46 -0700
committerAndrew <andrewarnott@gmail.com>2008-09-03 08:18:46 -0700
commite8a2ce8cbe1c544514019a32c8fcf3ecfc6f71f3 (patch)
tree5b7aa94bb52723936405042788236bfe2dba0e87
parent2ca76eb03e338554782d66d4c167c8e3cc592d9e (diff)
downloadDotNetOpenAuth-e8a2ce8cbe1c544514019a32c8fcf3ecfc6f71f3.zip
DotNetOpenAuth-e8a2ce8cbe1c544514019a32c8fcf3ecfc6f71f3.tar.gz
DotNetOpenAuth-e8a2ce8cbe1c544514019a32c8fcf3ecfc6f71f3.tar.bz2
Added more tests.
-rw-r--r--src/DotNetOAuth.Test/DotNetOAuth.Test.csproj4
-rw-r--r--src/DotNetOAuth.Test/Messaging/ChannelTests.cs32
-rw-r--r--src/DotNetOAuth.Test/Messaging/DictionaryXmlReaderTests.cs26
-rw-r--r--src/DotNetOAuth.Test/Messaging/ResponseTest.cs19
-rw-r--r--src/DotNetOAuth.Test/MessagingUtilitiesTest.cs99
-rw-r--r--src/DotNetOAuth.Test/Mocks/TestBadChannel.cs42
-rw-r--r--src/DotNetOAuth/Messaging/DictionaryXmlReader.cs10
-rw-r--r--src/DotNetOAuth/Messaging/MessagingUtilities.cs4
8 files changed, 234 insertions, 2 deletions
diff --git a/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj b/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj
index fd01adb..769cd56 100644
--- a/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj
+++ b/src/DotNetOAuth.Test/DotNetOAuth.Test.csproj
@@ -58,13 +58,17 @@
</Reference>
</ItemGroup>
<ItemGroup>
+ <Compile Include="MessagingUtilitiesTest.cs" />
<Compile Include="Messaging\ChannelTests.cs" />
+ <Compile Include="Messaging\DictionaryXmlReaderTests.cs" />
+ <Compile Include="Mocks\TestBadChannel.cs" />
<Compile Include="OAuthChannelTests.cs" />
<Compile Include="Messaging\MessageSerializerTests.cs" />
<Compile Include="Mocks\TestChannel.cs" />
<Compile Include="Mocks\TestMessage.cs" />
<Compile Include="Mocks\TestMessageTypeProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="Messaging\ResponseTest.cs" />
<Compile Include="ServiceProviderTest.cs" />
<Compile Include="TestBase.cs" />
</ItemGroup>
diff --git a/src/DotNetOAuth.Test/Messaging/ChannelTests.cs b/src/DotNetOAuth.Test/Messaging/ChannelTests.cs
index 80d38f7..b4eb810 100644
--- a/src/DotNetOAuth.Test/Messaging/ChannelTests.cs
+++ b/src/DotNetOAuth.Test/Messaging/ChannelTests.cs
@@ -6,6 +6,7 @@
namespace DotNetOAuth.Test.Messaging {
using System;
+ using System.Collections.Generic;
using System.IO;
using System.Net;
using DotNetOAuth.Messaging;
@@ -23,6 +24,13 @@ namespace DotNetOAuth.Test.Messaging {
this.channel = new TestChannel();
}
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void CtorNull() {
+ // This bad channel is deliberately constructed to pass null to
+ // its protected base class' constructor.
+ new TestBadChannel();
+ }
+
[TestMethod]
public void DequeueIndirectOrResponseMessageReturnsNull() {
Assert.IsNull(this.channel.DequeueIndirectOrResponseMessage());
@@ -73,8 +81,28 @@ namespace DotNetOAuth.Test.Messaging {
Assert.AreEqual("http://hostb/pathB", testMessage.Location.AbsoluteUri);
}
- private static HttpRequestInfo CreateHttpRequest() {
- throw new NotImplementedException();
+ private static HttpRequestInfo CreateHttpRequest(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();
+ 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;
+ } else if (method == "GET") {
+ requestUri.Query = query;
+ }
+ HttpRequestInfo request = new HttpRequestInfo {
+ HttpMethod = method,
+ Url = requestUri.Uri,
+ Headers = headers,
+ InputStream = ms,
+ };
+
+ return request;
}
}
}
diff --git a/src/DotNetOAuth.Test/Messaging/DictionaryXmlReaderTests.cs b/src/DotNetOAuth.Test/Messaging/DictionaryXmlReaderTests.cs
new file mode 100644
index 0000000..3dc3238
--- /dev/null
+++ b/src/DotNetOAuth.Test/Messaging/DictionaryXmlReaderTests.cs
@@ -0,0 +1,26 @@
+//-----------------------------------------------------------------------
+// <copyright file="DictionaryXmlReaderTests.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Test.Messaging {
+ using System;
+ using System.Collections.Generic;
+ using System.Xml.Linq;
+ using DotNetOAuth.Messaging;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+ [TestClass]
+ public class DictionaryXmlReaderTests : TestBase {
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void CreateWithNullRootElement() {
+ DictionaryXmlReader.Create(null, new Dictionary<string, string>());
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void CreateWithNullFields() {
+ DictionaryXmlReader.Create(XName.Get("name", "ns"), null);
+ }
+ }
+}
diff --git a/src/DotNetOAuth.Test/Messaging/ResponseTest.cs b/src/DotNetOAuth.Test/Messaging/ResponseTest.cs
new file mode 100644
index 0000000..c1c7dff
--- /dev/null
+++ b/src/DotNetOAuth.Test/Messaging/ResponseTest.cs
@@ -0,0 +1,19 @@
+//-----------------------------------------------------------------------
+// <copyright file="ResponseTest.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Test.Messaging {
+ using System;
+ using DotNetOAuth.Messaging;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+ [TestClass]
+ public class ResponseTest : TestBase {
+ [TestMethod, ExpectedException(typeof(InvalidOperationException))]
+ public void SendWithoutAspNetContext() {
+ new Response().Send();
+ }
+ }
+}
diff --git a/src/DotNetOAuth.Test/MessagingUtilitiesTest.cs b/src/DotNetOAuth.Test/MessagingUtilitiesTest.cs
new file mode 100644
index 0000000..cc3e552
--- /dev/null
+++ b/src/DotNetOAuth.Test/MessagingUtilitiesTest.cs
@@ -0,0 +1,99 @@
+//-----------------------------------------------------------------------
+// <copyright file="MessagingUtilitiesTest.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Test
+{
+ using System;
+ using System.Collections.Generic;
+ using System.Collections.Specialized;
+ using System.IO;
+ using System.Net;
+ using System.Web;
+ using DotNetOAuth.Messaging;
+ using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+ [TestClass]
+ public class MessagingUtilitiesTest : TestBase {
+ [TestMethod]
+ public void CreateQueryString() {
+ var args = new Dictionary<string, string>();
+ args.Add("a", "b");
+ args.Add("c/d", "e/f");
+ Assert.AreEqual("a=b&c%2fd=e%2ff", MessagingUtilities.CreateQueryString(args));
+ }
+
+ [TestMethod]
+ public void CreateQueryStringEmptyCollection() {
+ Assert.AreEqual(0, MessagingUtilities.CreateQueryString(new Dictionary<string, string>()).Length);
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void CreateQueryStringNullDictionary() {
+ MessagingUtilities.CreateQueryString(null);
+ }
+
+ [TestMethod]
+ public void AppendQueryArgs() {
+ UriBuilder uri = new UriBuilder("http://baseline.org/page");
+ var args = new Dictionary<string, string>();
+ args.Add("a", "b");
+ args.Add("c/d", "e/f");
+ MessagingUtilities.AppendQueryArgs(uri, args);
+ Assert.AreEqual("http://baseline.org/page?a=b&c%2fd=e%2ff", uri.Uri.AbsoluteUri);
+ args.Clear();
+ args.Add("g", "h");
+ MessagingUtilities.AppendQueryArgs(uri, args);
+ Assert.AreEqual("http://baseline.org/page?a=b&c%2fd=e%2ff&g=h", uri.Uri.AbsoluteUri);
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void AppendQueryArgsNullUriBuilder() {
+ MessagingUtilities.AppendQueryArgs(null, new Dictionary<string, string>());
+ }
+
+ [TestMethod]
+ public void AppendQueryArgsNullDictionary() {
+ MessagingUtilities.AppendQueryArgs(new UriBuilder(), null);
+ }
+
+ [TestMethod]
+ public void ToDictionary() {
+ NameValueCollection nvc = new NameValueCollection();
+ nvc["a"] = "b";
+ nvc["c"] = "d";
+ Dictionary<string, string> actual = MessagingUtilities.ToDictionary(nvc);
+ Assert.AreEqual(nvc.Count, actual.Count);
+ Assert.AreEqual(nvc["a"], actual["a"]);
+ Assert.AreEqual(nvc["c"], actual["c"]);
+ }
+
+ [TestMethod]
+ public void ToDictionaryNull() {
+ Assert.IsNull(MessagingUtilities.ToDictionary(null));
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void ApplyHeadersToResponseNullResponse() {
+ MessagingUtilities.ApplyHeadersToResponse(new WebHeaderCollection(), null);
+ }
+
+ [TestMethod, ExpectedException(typeof(ArgumentNullException))]
+ public void ApplyHeadersToResponseNullHeaders() {
+ MessagingUtilities.ApplyHeadersToResponse(null, new HttpResponse(new StringWriter()));
+ }
+
+ [TestMethod]
+ public void ApplyHeadersToResponse() {
+ var headers = new WebHeaderCollection();
+ headers[HttpResponseHeader.ContentType] = "application/binary";
+
+ var response = new HttpResponse(new StringWriter());
+ MessagingUtilities.ApplyHeadersToResponse(headers, response);
+
+ Assert.AreEqual(headers[HttpResponseHeader.ContentType], response.ContentType);
+ }
+ }
+}
diff --git a/src/DotNetOAuth.Test/Mocks/TestBadChannel.cs b/src/DotNetOAuth.Test/Mocks/TestBadChannel.cs
new file mode 100644
index 0000000..20fe03a
--- /dev/null
+++ b/src/DotNetOAuth.Test/Mocks/TestBadChannel.cs
@@ -0,0 +1,42 @@
+//-----------------------------------------------------------------------
+// <copyright file="TestBadChannel.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOAuth.Test.Mocks {
+ using System;
+ using System.Collections.Generic;
+ using System.Linq;
+ using System.Text;
+ using DotNetOAuth.Messaging;
+
+ /// <summary>
+ /// A Channel derived type that passes null to the protected constructor.
+ /// </summary>
+ internal class TestBadChannel : Channel {
+ internal TestBadChannel()
+ : base(null) {
+ }
+
+ protected internal override IProtocolMessage Request(IDirectedProtocolMessage request) {
+ throw new NotImplementedException();
+ }
+
+ protected internal override IProtocolMessage ReadFromResponse(System.IO.Stream responseStream) {
+ throw new NotImplementedException();
+ }
+
+ protected override void SendDirectMessageResponse(IProtocolMessage response) {
+ throw new NotImplementedException();
+ }
+
+ protected override void ReportErrorToUser(ProtocolException exception) {
+ throw new NotImplementedException();
+ }
+
+ protected override void ReportErrorAsDirectResponse(ProtocolException exception) {
+ throw new NotImplementedException();
+ }
+ }
+}
diff --git a/src/DotNetOAuth/Messaging/DictionaryXmlReader.cs b/src/DotNetOAuth/Messaging/DictionaryXmlReader.cs
index 5eae4c7..7a5dc21 100644
--- a/src/DotNetOAuth/Messaging/DictionaryXmlReader.cs
+++ b/src/DotNetOAuth/Messaging/DictionaryXmlReader.cs
@@ -24,6 +24,9 @@ namespace DotNetOAuth.Messaging {
/// <param name="fields">The dictionary to read data from.</param>
/// <returns>The XmlReader that will read the data out of the given dictionary.</returns>
internal static XmlReader Create(XName rootElement, IDictionary<string, string> fields) {
+ if (rootElement == null) {
+ throw new ArgumentNullException("rootElement");
+ }
if (fields == null) {
throw new ArgumentNullException("fields");
}
@@ -38,6 +41,10 @@ namespace DotNetOAuth.Messaging {
/// <param name="fields">The dictionary to list values from.</param>
/// <returns>The generated <see cref="XmlReader"/>.</returns>
private static XmlReader CreateRoundtripReader(XName rootElement, IDictionary<string, string> fields) {
+ if (rootElement == null) {
+ throw new ArgumentNullException("rootElement");
+ }
+
MemoryStream stream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(stream);
SerializeDictionaryToXml(writer, rootElement, fields);
@@ -62,6 +69,9 @@ namespace DotNetOAuth.Messaging {
if (writer == null) {
throw new ArgumentNullException("writer");
}
+ if (rootElement == null) {
+ throw new ArgumentNullException("rootElement");
+ }
if (fields == null) {
throw new ArgumentNullException("fields");
}
diff --git a/src/DotNetOAuth/Messaging/MessagingUtilities.cs b/src/DotNetOAuth/Messaging/MessagingUtilities.cs
index c298b2f..e8069f6 100644
--- a/src/DotNetOAuth/Messaging/MessagingUtilities.cs
+++ b/src/DotNetOAuth/Messaging/MessagingUtilities.cs
@@ -82,6 +82,10 @@ namespace DotNetOAuth.Messaging {
/// If null, <paramref name="builder"/> is not changed.
/// </param>
internal static void AppendQueryArgs(UriBuilder builder, IDictionary<string, string> args) {
+ if (builder == null) {
+ throw new ArgumentNullException("builder");
+ }
+
if (args != null && args.Count > 0) {
StringBuilder sb = new StringBuilder(50 + (args.Count * 10));
if (!string.IsNullOrEmpty(builder.Query)) {