diff options
4 files changed, 60 insertions, 1 deletions
diff --git a/src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs b/src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs index fc60ef6..782ce0d 100644 --- a/src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs +++ b/src/DotNetOpenAuth.Test/Messaging/MessagingUtilitiesTests.cs @@ -64,12 +64,31 @@ namespace DotNetOpenAuth.Test.Messaging NameValueCollection nvc = new NameValueCollection(); nvc["a"] = "b"; nvc["c"] = "d"; + nvc[string.Empty] = "emptykey"; 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, ExpectedException(typeof(ArgumentException))] + public void ToDictionaryWithNullKey() { + NameValueCollection nvc = new NameValueCollection(); + nvc[null] = "a"; + nvc["b"] = "c"; + nvc.ToDictionary(true); + } + + [TestMethod] + public void ToDictionaryWithSkippedNullKey() { + NameValueCollection nvc = new NameValueCollection(); + nvc[null] = "a"; + nvc["b"] = "c"; + var dictionary = nvc.ToDictionary(false); + Assert.AreEqual(1, dictionary.Count); + Assert.AreEqual(nvc["b"], dictionary["b"]); + } + [TestMethod] public void ToDictionaryNull() { Assert.IsNull(MessagingUtilities.ToDictionary(null)); diff --git a/src/DotNetOpenAuth/Messaging/MessagingStrings.Designer.cs b/src/DotNetOpenAuth/Messaging/MessagingStrings.Designer.cs index d803cb5..79d3aad 100644 --- a/src/DotNetOpenAuth/Messaging/MessagingStrings.Designer.cs +++ b/src/DotNetOpenAuth/Messaging/MessagingStrings.Designer.cs @@ -475,6 +475,15 @@ namespace DotNetOpenAuth.Messaging { } /// <summary> + /// Looks up a localized string similar to A null key was included and is not allowed.. + /// </summary> + internal static string UnexpectedNullKey { + get { + return ResourceManager.GetString("UnexpectedNullKey", resourceCulture); + } + } + + /// <summary> /// Looks up a localized string similar to A null or empty key was included and is not allowed.. /// </summary> internal static string UnexpectedNullOrEmptyKey { diff --git a/src/DotNetOpenAuth/Messaging/MessagingStrings.resx b/src/DotNetOpenAuth/Messaging/MessagingStrings.resx index cac9939..6dc42e0 100644 --- a/src/DotNetOpenAuth/Messaging/MessagingStrings.resx +++ b/src/DotNetOpenAuth/Messaging/MessagingStrings.resx @@ -255,6 +255,9 @@ <data name="UnexpectedMessageReceivedOfMany" xml:space="preserve"> <value>Unexpected message type received.</value> </data> + <data name="UnexpectedNullKey" xml:space="preserve"> + <value>A null key was included and is not allowed.</value> + </data> <data name="UnexpectedNullOrEmptyKey" xml:space="preserve"> <value>A null or empty key was included and is not allowed.</value> </data> diff --git a/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs b/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs index d9e6c23..e303344 100644 --- a/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs +++ b/src/DotNetOpenAuth/Messaging/MessagingUtilities.cs @@ -424,14 +424,42 @@ namespace DotNetOpenAuth.Messaging { /// </summary> /// <param name="nvc">The NameValueCollection to convert. May be null.</param> /// <returns>The generated dictionary, or null if <paramref name="nvc"/> is null.</returns> + /// <remarks> + /// If a <c>null</c> key is encountered, its value is ignored since + /// <c>Dictionary<string, string></c> does not allow null keys. + /// </remarks> internal static Dictionary<string, string> ToDictionary(this NameValueCollection nvc) { + return ToDictionary(nvc, false); + } + + /// <summary> + /// Converts a <see cref="NameValueCollection"/> to an IDictionary<string, string>. + /// </summary> + /// <param name="nvc">The NameValueCollection to convert. May be null.</param> + /// <param name="throwOnNullKey"> + /// A value indicating whether a null key in the <see cref="NameValueCollection"/> should be silently skipped since it is not a valid key in a Dictionary. + /// Use <c>true</c> to throw an exception if a null key is encountered. + /// Use <c>false</c> to silently continue converting the valid keys. + /// </param> + /// <returns>The generated dictionary, or null if <paramref name="nvc"/> is null.</returns> + /// <exception cref="ArgumentException">Thrown if <paramref name="throwOnNullKey"/> is <c>true</c> and a null key is encountered.</exception> + internal static Dictionary<string, string> ToDictionary(this NameValueCollection nvc, bool throwOnNullKey) { if (nvc == null) { return null; } var dictionary = new Dictionary<string, string>(); foreach (string key in nvc) { - dictionary.Add(key, nvc[key]); + // NameValueCollection supports a null key, but Dictionary<K,V> does not. + if (key == null) { + if (throwOnNullKey) { + throw new ArgumentException(MessagingStrings.UnexpectedNullKey); + } else { + Logger.WarnFormat("Null key with value {0} encountered while translating NameValueCollection to Dictionary.", nvc[key]); + } + } else { + dictionary.Add(key, nvc[key]); + } } return dictionary; |