Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Added support for RFC3986 with respect to url encoding [SPRNETREST-27]
Browse files Browse the repository at this point in the history
  • Loading branch information
bbaia committed Nov 9, 2012
1 parent 077a943 commit 4fc39fe
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Spring.Rest/Http/HttpUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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");
}
}
}
71 changes: 71 additions & 0 deletions test/Spring.Rest.Tests/Http/HttpUtilsTests.cs
Original file line number Diff line number Diff line change
@@ -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"));
}
}
}
1 change: 1 addition & 0 deletions test/Spring.Rest.Tests/Spring.Rest.Tests.2005-NET20.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
1 change: 1 addition & 0 deletions test/Spring.Rest.Tests/Spring.Rest.Tests.2008-NET20.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
1 change: 1 addition & 0 deletions test/Spring.Rest.Tests/Spring.Rest.Tests.2008-NET35.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
1 change: 1 addition & 0 deletions test/Spring.Rest.Tests/Spring.Rest.Tests.2010-NET40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down

0 comments on commit 4fc39fe

Please sign in to comment.