Permalink
Browse files

Added support for RFC3986 with respect to url encoding [SPRNETREST-27]

  • Loading branch information...
1 parent 5232b8d commit 4c080b3fbdcc731b79a51a36fd5a3f28f4ea48a3 @bbaia bbaia committed Nov 9, 2012
@@ -29,7 +29,7 @@ namespace Spring.Http
public static class HttpUtils
{
/// <summary>
- /// Decodes 'application/x-www-form-urlencoded' data using 'UTF-8' encoding scheme.
+ /// Decodes 'application/x-www-form-urlencoded' data.
/// </summary>
/// <param name="s">The string to decode.</param>
/// <returns>The decoded string.</returns>
@@ -43,7 +43,7 @@ public static string FormDecode(string s)
}
/// <summary>
- /// Encodes 'application/x-www-form-urlencoded' data using 'UTF-8' encoding scheme.
+ /// Encodes 'application/x-www-form-urlencoded' data.
/// </summary>
/// <param name="s">The string to encode.</param>
/// <returns>The encoded string.</returns>
@@ -53,11 +53,11 @@ public static string FormEncode(string s)
{
return null;
}
- return Uri.EscapeDataString(s).Replace("%20", "+");
+ return UrlEncode(s).Replace("%20", "+");
}
/// <summary>
- /// Decodes URI data using 'UTF-8' encoding scheme.
+ /// Decodes URI data according to RFC 3986.
/// </summary>
/// <param name="s">The string to decode.</param>
/// <returns>The decoded string.</returns>
@@ -71,7 +71,7 @@ public static string UrlDecode(string s)
}
/// <summary>
- /// Encodes URI data using 'UTF-8' encoding scheme.
+ /// Encodes URI data according to RFC 3986.
/// </summary>
/// <param name="s">The string to encode.</param>
/// <returns>The encoded string.</returns>
@@ -81,7 +81,12 @@ public static string UrlEncode(string s)
{
return null;
}
- return Uri.EscapeDataString(s);
+ return Uri.EscapeDataString(s)
+ .Replace("!", "%21")
+ .Replace("'", "%27")
+ .Replace("(", "%28")
+ .Replace(")", "%29")
+ .Replace("*", "%2A");
}
}
}
@@ -0,0 +1,71 @@
+#region License
+
+/*
+ * Copyright 2002-2012 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+
+using NUnit.Framework;
+
+namespace Spring.Http
+{
+ /// <summary>
+ /// Unit tests for the HttpUtils class.
+ /// </summary>
+ /// <author>Bruno Baia</author>
+ [TestFixture]
+ public class HttpUtilsTests
+ {
+ [Test]
+ public void UrlEncode()
+ {
+ Assert.AreEqual("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~%21%27%28%29%2A",
+ HttpUtils.UrlEncode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~!'()*"));
+ Assert.AreEqual("%3F%3D%23", HttpUtils.UrlEncode("?=#"));
+ }
+
+ [Test]
+ public void UrlDecode()
+ {
+ Assert.AreEqual("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~!'()*",
+ HttpUtils.UrlDecode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~%21%27%28%29%2A"));
+ Assert.AreEqual("?=#", HttpUtils.UrlDecode("%3F%3D%23"));
+ }
+
+ [Test]
+ public void FormEncode()
+ {
+ Assert.AreEqual("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~%21%27%28%29%2A",
+ HttpUtils.FormEncode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~!'()*"));
+ Assert.AreEqual("%3F%3D%23", HttpUtils.FormEncode("?=#"));
+ Assert.AreEqual("+", HttpUtils.FormEncode(" "));
+ Assert.AreEqual("%2B", HttpUtils.FormEncode("+"));
+ }
+
+ [Test]
+ public void FormDecode()
+ {
+ Assert.AreEqual("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~!'()*",
+ HttpUtils.FormDecode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~%21%27%28%29%2A"));
+ Assert.AreEqual("?=#", HttpUtils.FormDecode("%3F%3D%23"));
+ Assert.AreEqual(" ", HttpUtils.FormDecode("+"));
+ Assert.AreEqual("+", HttpUtils.FormDecode("%2B"));
+ }
+ }
+}
@@ -118,6 +118,7 @@
<Compile Include="Http\Converters\Xml\XmlHttpMessageConverterIntegrationTests.cs" />
<Compile Include="Http\Converters\Xml\XmlSerializableHttpMessageConverterTests.cs" />
<Compile Include="Http\HttpMethodTests.cs" />
+ <Compile Include="Http\HttpUtilsTests.cs" />
<Compile Include="Http\MockHttpOuputMessage.cs" />
<Compile Include="Http\HttpHeadersTests.cs" />
<Compile Include="Http\MediaTypeTests.cs" />
@@ -118,6 +118,7 @@
<Compile Include="Http\Converters\Xml\XmlHttpMessageConverterIntegrationTests.cs" />
<Compile Include="Http\Converters\Xml\XmlSerializableHttpMessageConverterTests.cs" />
<Compile Include="Http\HttpMethodTests.cs" />
+ <Compile Include="Http\HttpUtilsTests.cs" />
<Compile Include="Http\MockHttpOuputMessage.cs" />
<Compile Include="Http\HttpHeadersTests.cs" />
<Compile Include="Http\MediaTypeTests.cs" />
@@ -139,6 +139,7 @@
<Compile Include="Http\Converters\Xml\XmlHttpMessageConverterIntegrationTests.cs" />
<Compile Include="Http\Converters\Xml\XmlSerializableHttpMessageConverterTests.cs" />
<Compile Include="Http\HttpMethodTests.cs" />
+ <Compile Include="Http\HttpUtilsTests.cs" />
<Compile Include="Http\MockHttpOuputMessage.cs" />
<Compile Include="Http\HttpHeadersTests.cs" />
<Compile Include="Http\MediaTypeTests.cs" />
@@ -148,6 +148,7 @@
<Compile Include="Http\Converters\Xml\XmlDocumentHttpMessageConverterTests.cs" />
<Compile Include="Http\HttpHeadersTests.cs" />
<Compile Include="Http\HttpMethodTests.cs" />
+ <Compile Include="Http\HttpUtilsTests.cs" />
<Compile Include="Http\MediaTypeTests.cs" />
<Compile Include="Http\MockHttpInputMessage.cs" />
<Compile Include="Http\MockHttpOuputMessage.cs" />
@@ -151,6 +151,7 @@
<Compile Include="Http\Converters\Xml\XmlDocumentHttpMessageConverterTests.cs" />
<Compile Include="Http\HttpHeadersTests.cs" />
<Compile Include="Http\HttpMethodTests.cs" />
+ <Compile Include="Http\HttpUtilsTests.cs" />
<Compile Include="Http\MediaTypeTests.cs" />
<Compile Include="Http\MockHttpInputMessage.cs" />
<Compile Include="Http\MockHttpOuputMessage.cs" />

0 comments on commit 4c080b3

Please sign in to comment.