-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
StringExtensionsTests.cs
97 lines (82 loc) · 3.65 KB
/
StringExtensionsTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using System.Globalization;
using System.Text;
using FluentAssertions;
using NUnit.Framework;
using RestSharp.Extensions;
namespace RestSharp.Tests
{
public class StringExtensionsTests
{
[Test]
public void UrlEncode_Throws_ArgumentNullException_For_Null_Input()
{
const string nullString = null;
Assert.Throws<ArgumentNullException>(() => nullString.UrlEncode());
}
[Test]
public void UrlEncode_Returns_Correct_Length_When_Less_Than_Limit()
{
const int numLessThanLimit = 32766;
var stringWithLimitLength = new string('*', numLessThanLimit);
var encodedAndDecoded = stringWithLimitLength.UrlEncode().UrlDecode();
Assert.AreEqual(numLessThanLimit, encodedAndDecoded.Length);
}
[Test]
public void UrlEncode_Returns_Correct_Length_When_More_Than_Limit()
{
const int numGreaterThanLimit = 65000;
var stringWithLimitLength = new string('*', numGreaterThanLimit);
var encodedAndDecoded = stringWithLimitLength.UrlEncode().UrlDecode();
Assert.AreEqual(numGreaterThanLimit, encodedAndDecoded.Length);
}
[Test]
public void UrlEncode_Does_Not_Fail_When_4_Byte_Unicode_Character_Lies_Between_Chunks()
{
var stringWithLimitLength = new string('*', 32765);
stringWithLimitLength += "😉*****"; // 2 + 5 chars
var encodedAndDecoded = stringWithLimitLength.UrlEncode().UrlDecode();
Assert.AreEqual(stringWithLimitLength, encodedAndDecoded);
// now between another 2 chunks
stringWithLimitLength = new string('*', 32766 * 2 - 1);
stringWithLimitLength += "😉*****"; // 2 + 5 chars
encodedAndDecoded = stringWithLimitLength.UrlEncode().UrlDecode();
Assert.AreEqual(stringWithLimitLength, encodedAndDecoded);
}
[Test]
public void UrlEncodeTest()
{
const string parameter = "ø";
Assert.True(string.Equals("%F8", parameter.UrlEncode(Encoding.GetEncoding("ISO-8859-1")), StringComparison.OrdinalIgnoreCase));
Assert.True(string.Equals("%C3%B8", parameter.UrlEncode(), StringComparison.OrdinalIgnoreCase));
}
[Test, TestCase("this_is_a_test", true, "ThisIsATest"), TestCase("this_is_a_test", false, "This_Is_A_Test")]
public void ToPascalCase(string start, bool removeUnderscores, string finish)
{
var result = start.ToPascalCase(removeUnderscores, CultureInfo.InvariantCulture);
Assert.AreEqual(finish, result);
}
[Test, TestCase("DueDate", "dueDate"), TestCase("ID", "id"), TestCase("IDENTIFIER", "identifier"), TestCase("primaryId", "primaryId"), TestCase("A", "a"), TestCase("ThisIsATest", "thisIsATest")]
public void ToCamelCase(string start, string finish)
{
var result = start.ToCamelCase(CultureInfo.InvariantCulture);
Assert.AreEqual(finish, result);
}
[Test]
public void Does_not_throw_on_invalid_encoding()
{
const string value = "SomeValue";
var bytes = Encoding.UTF8.GetBytes(value);
var decoded = bytes.AsString("blah");
decoded.Should().Be(value);
}
[Test]
public void Does_not_throw_on_missing_encoding()
{
const string value = "SomeValue";
var bytes = Encoding.UTF8.GetBytes(value);
var decoded = bytes.AsString(null);
decoded.Should().Be(value);
}
}
}