Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions RestSharp.Tests/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using RestSharp.Extensions;
using Xunit;

namespace RestSharp.Tests
{
public class StringExtensionsTests
{
[Fact]
public void UrlEncode_Throws_ArgumentNullException_For_Null_Input()
{
const string nullString = null;
Assert.Throws<System.ArgumentNullException>(
delegate
{
nullString.UrlEncode();
});
}

[Fact]
public void UrlEncode_Returns_Correct_Length_When_Less_Than_Limit()
{
const int numLessThanLimit = 32766;
string stringWithLimitLength = new string('*', numLessThanLimit);
Assert.True(stringWithLimitLength.UrlEncode().Length == numLessThanLimit);
}

[Fact]
public void UrlEncode_Returns_Correct_Length_When_More_Than_Limit()
{
const int numGreaterThanLimit = 65000;
string stringWithLimitLength = new string('*', numGreaterThanLimit);
Assert.True(stringWithLimitLength.UrlEncode().Length == numGreaterThanLimit);
}
}
}
19 changes: 18 additions & 1 deletion RestSharp/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,24 @@ public static string UrlDecode(this string input)
/// </summary>
public static string UrlEncode(this string input)
{
return Uri.EscapeDataString(input);
const int maxLength = 32766;
if (input == null)
throw new ArgumentNullException("input");

if (input.Length <= maxLength)
return Uri.EscapeDataString(input);

StringBuilder sb = new StringBuilder(input.Length * 2);
int index = 0;
while (index < input.Length)
{
int length = Math.Min(input.Length - index, maxLength);
string subString = input.Substring(index, length);
sb.Append(Uri.EscapeDataString(subString));
index += subString.Length;
}

return sb.ToString();
}

public static string HtmlDecode(this string input)
Expand Down