File tree Expand file tree Collapse file tree 2 files changed +58
-1
lines changed Expand file tree Collapse file tree 2 files changed +58
-1
lines changed Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Globalization ;
4
+ using System . Linq ;
5
+ using System . Text ;
6
+ using RestSharp . Extensions ;
7
+ using Xunit ;
8
+
9
+ namespace RestSharp . Tests
10
+ {
11
+ public class StringExtensionsTests
12
+ {
13
+ [ Fact ]
14
+ public void UrlEncode_Throws_ArgumentNullException_For_Null_Input ( )
15
+ {
16
+ const string nullString = null ;
17
+ Assert . Throws < System . ArgumentNullException > (
18
+ delegate
19
+ {
20
+ nullString . UrlEncode ( ) ;
21
+ } ) ;
22
+ }
23
+
24
+ [ Fact ]
25
+ public void UrlEncode_Returns_Correct_Length_When_Less_Than_Limit ( )
26
+ {
27
+ const int numLessThanLimit = 32766 ;
28
+ string stringWithLimitLength = new string ( '*' , numLessThanLimit ) ;
29
+ Assert . True ( stringWithLimitLength . UrlEncode ( ) . Length == numLessThanLimit ) ;
30
+ }
31
+
32
+ [ Fact ]
33
+ public void UrlEncode_Returns_Correct_Length_When_More_Than_Limit ( )
34
+ {
35
+ const int numGreaterThanLimit = 65000 ;
36
+ string stringWithLimitLength = new string ( '*' , numGreaterThanLimit ) ;
37
+ Assert . True ( stringWithLimitLength . UrlEncode ( ) . Length == numGreaterThanLimit ) ;
38
+ }
39
+ }
40
+ }
Original file line number Diff line number Diff line change @@ -49,7 +49,24 @@ public static string UrlDecode(this string input)
49
49
/// </summary>
50
50
public static string UrlEncode ( this string input )
51
51
{
52
- return Uri . EscapeDataString ( input ) ;
52
+ const int maxLength = 32766 ;
53
+ if ( input == null )
54
+ throw new ArgumentNullException ( "input" ) ;
55
+
56
+ if ( input . Length <= maxLength )
57
+ return Uri . EscapeDataString ( input ) ;
58
+
59
+ StringBuilder sb = new StringBuilder ( input . Length * 2 ) ;
60
+ int index = 0 ;
61
+ while ( index < input . Length )
62
+ {
63
+ int length = Math . Min ( input . Length - index , maxLength ) ;
64
+ string subString = input . Substring ( index , length ) ;
65
+ sb . Append ( Uri . EscapeDataString ( subString ) ) ;
66
+ index += subString . Length ;
67
+ }
68
+
69
+ return sb . ToString ( ) ;
53
70
}
54
71
55
72
public static string HtmlDecode ( this string input )
You can’t perform that action at this time.
0 commit comments