Fixes null key handling in incoming query strings.
[dotnetoauth.git] / src / DotNetOpenAuth.Test / Messaging / MessagingUtilitiesTests.cs
blob782ce0d36d9f98be3420d2ab1f55de0f97908a37
1 //-----------------------------------------------------------------------
2 // <copyright file="MessagingUtilitiesTests.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth.Test.Messaging
9 using System;
10 using System.Collections.Generic;
11 using System.Collections.Specialized;
12 using System.IO;
13 using System.Net;
14 using System.Web;
15 using DotNetOpenAuth.Messaging;
16 using Microsoft.VisualStudio.TestTools.UnitTesting;
18 [TestClass]
19 public class MessagingUtilitiesTests : TestBase {
20 [TestMethod]
21 public void CreateQueryString() {
22 var args = new Dictionary<string, string>();
23 args.Add("a", "b");
24 args.Add("c/d", "e/f");
25 Assert.AreEqual("a=b&c%2fd=e%2ff", MessagingUtilities.CreateQueryString(args));
28 [TestMethod]
29 public void CreateQueryStringEmptyCollection() {
30 Assert.AreEqual(0, MessagingUtilities.CreateQueryString(new Dictionary<string, string>()).Length);
33 [TestMethod, ExpectedException(typeof(ArgumentNullException))]
34 public void CreateQueryStringNullDictionary() {
35 MessagingUtilities.CreateQueryString(null);
38 [TestMethod]
39 public void AppendQueryArgs() {
40 UriBuilder uri = new UriBuilder("http://baseline.org/page");
41 var args = new Dictionary<string, string>();
42 args.Add("a", "b");
43 args.Add("c/d", "e/f");
44 MessagingUtilities.AppendQueryArgs(uri, args);
45 Assert.AreEqual("http://baseline.org/page?a=b&c%2fd=e%2ff", uri.Uri.AbsoluteUri);
46 args.Clear();
47 args.Add("g", "h");
48 MessagingUtilities.AppendQueryArgs(uri, args);
49 Assert.AreEqual("http://baseline.org/page?a=b&c%2fd=e%2ff&g=h", uri.Uri.AbsoluteUri);
52 [TestMethod, ExpectedException(typeof(ArgumentNullException))]
53 public void AppendQueryArgsNullUriBuilder() {
54 MessagingUtilities.AppendQueryArgs(null, new Dictionary<string, string>());
57 [TestMethod]
58 public void AppendQueryArgsNullDictionary() {
59 MessagingUtilities.AppendQueryArgs(new UriBuilder(), null);
62 [TestMethod]
63 public void ToDictionary() {
64 NameValueCollection nvc = new NameValueCollection();
65 nvc["a"] = "b";
66 nvc["c"] = "d";
67 nvc[string.Empty] = "emptykey";
68 Dictionary<string, string> actual = MessagingUtilities.ToDictionary(nvc);
69 Assert.AreEqual(nvc.Count, actual.Count);
70 Assert.AreEqual(nvc["a"], actual["a"]);
71 Assert.AreEqual(nvc["c"], actual["c"]);
74 [TestMethod, ExpectedException(typeof(ArgumentException))]
75 public void ToDictionaryWithNullKey() {
76 NameValueCollection nvc = new NameValueCollection();
77 nvc[null] = "a";
78 nvc["b"] = "c";
79 nvc.ToDictionary(true);
82 [TestMethod]
83 public void ToDictionaryWithSkippedNullKey() {
84 NameValueCollection nvc = new NameValueCollection();
85 nvc[null] = "a";
86 nvc["b"] = "c";
87 var dictionary = nvc.ToDictionary(false);
88 Assert.AreEqual(1, dictionary.Count);
89 Assert.AreEqual(nvc["b"], dictionary["b"]);
92 [TestMethod]
93 public void ToDictionaryNull() {
94 Assert.IsNull(MessagingUtilities.ToDictionary(null));
97 [TestMethod, ExpectedException(typeof(ArgumentNullException))]
98 public void ApplyHeadersToResponseNullResponse() {
99 MessagingUtilities.ApplyHeadersToResponse(new WebHeaderCollection(), null);
102 [TestMethod, ExpectedException(typeof(ArgumentNullException))]
103 public void ApplyHeadersToResponseNullHeaders() {
104 MessagingUtilities.ApplyHeadersToResponse(null, new HttpResponse(new StringWriter()));
107 [TestMethod]
108 public void ApplyHeadersToResponse() {
109 var headers = new WebHeaderCollection();
110 headers[HttpResponseHeader.ContentType] = "application/binary";
112 var response = new HttpResponse(new StringWriter());
113 MessagingUtilities.ApplyHeadersToResponse(headers, response);
115 Assert.AreEqual(headers[HttpResponseHeader.ContentType], response.ContentType);