public void Ctor_WithUnicodeRanges() { // Arrange UrlEncoder encoder = UrlEncoder.Create(UnicodeRanges.Latin1Supplement, UnicodeRanges.MiscellaneousSymbols); // Act & assert Assert.Equal("%61", encoder.Encode("a")); Assert.Equal("\u00E9", encoder.Encode("\u00E9" /* LATIN SMALL LETTER E WITH ACUTE */)); Assert.Equal("\u2601", encoder.Encode("\u2601" /* CLOUD */)); }
public void UrlEncode_BadSurrogates_ReturnsUnicodeReplacementChar() { // Arrange UrlEncoder encoder = UrlEncoder.Create(UnicodeRanges.All); // allow all codepoints // "a<unpaired leading>b<unpaired trailing>c<trailing before leading>d<unpaired trailing><valid>e<high at end of string>" const string input = "a\uD800b\uDFFFc\uDFFF\uD800d\uDFFF\uD800\uDFFFe\uD800"; const string expected = "a%EF%BF%BDb%EF%BF%BDc%EF%BF%BD%EF%BF%BDd%EF%BF%BD%F0%90%8F%BFe%EF%BF%BD"; // 'D800' 'DFFF' was preserved since it's valid // Act string retVal = encoder.Encode(input); // Assert Assert.Equal(expected, retVal); }
public void Default_EquivalentToBasicLatin() { // Arrange UrlEncoder controlEncoder = UrlEncoder.Create(UnicodeRanges.BasicLatin); UrlEncoder testEncoder = UrlEncoder.Default; // Act & assert for (int i = 0; i <= char.MaxValue; i++) { if (!IsSurrogateCodePoint(i)) { string input = new string((char)i, 1); Assert.Equal(controlEncoder.Encode(input), testEncoder.Encode(input)); } } }
public void Ctor_WithTextEncoderSettings() { // Arrange var filter = new TextEncoderSettings(); filter.AllowCharacters('a', 'b'); filter.AllowCharacters('\0', '&', '\uFFFF', 'd'); UrlEncoder encoder = UrlEncoder.Create(filter); // Act & assert Assert.Equal("a", encoder.Encode("a")); Assert.Equal("b", encoder.Encode("b")); Assert.Equal("%63", encoder.Encode("c")); Assert.Equal("d", encoder.Encode("d")); Assert.Equal("%00", encoder.Encode("\0")); // we still always encode control chars Assert.Equal("%26", encoder.Encode("&")); // we still always encode HTML-special chars Assert.Equal("%EF%BF%BF", encoder.Encode("\uFFFF")); // we still always encode non-chars and other forbidden chars }
public void UrlEncode_DoesNotOutputHtmlSensitiveCharacters() { // Per the design document, we provide additional defense-in-depth // by never emitting HTML-sensitive characters unescaped. // Arrange UrlEncoder urlEncoder = UrlEncoder.Create(UnicodeRanges.All); HtmlEncoder htmlEncoder = HtmlEncoder.Create(UnicodeRanges.All); // Act & assert for (int i = 0; i <= 0x10FFFF; i++) { if (IsSurrogateCodePoint(i)) { continue; // surrogates don't matter here } string urlEncoded = urlEncoder.Encode(char.ConvertFromUtf32(i)); string thenHtmlEncoded = htmlEncoder.Encode(urlEncoded); Assert.Equal(urlEncoded, thenHtmlEncoded); // should have contained no HTML-sensitive characters } }
public void UrlEncode_AllRangesAllowed_StillEncodesForbiddenChars() { // Arrange UrlEncoder encoder = UrlEncoder.Create(UnicodeRanges.All); // Act & assert - BMP chars for (int i = 0; i <= 0xFFFF; i++) { string input = new string((char)i, 1); string expected; if (IsSurrogateCodePoint(i)) { expected = "%EF%BF%BD"; // unpaired surrogate -> Unicode replacement char } else { bool mustEncode = true; // RFC 3987, Sec. 2.2 gives the list of allowed chars // (We allow 'ipchar' except for "'", "&", "+", "%", and "=" if (('a' <= i && i <= 'z') || ('A' <= i && i <= 'Z') || ('0' <= i && i <= '9')) { mustEncode = false; // ALPHA / DIGIT } else if ((0x00A0 <= i && i <= 0xD7FF) | (0xF900 <= i && i <= 0xFDCF) | (0xFDF0 <= i && i <= 0xFFEF)) { mustEncode = !UnicodeTestHelpers.IsCharacterDefined((char)i); // 'ucschar' } else { switch (i) { // iunreserved case '-': case '.': case '_': case '~': // isegment-nz-nc case '@': // sub-delims case '!': case '$': case '(': case ')': case '*': case ',': case ';': mustEncode = false; break; } } if (mustEncode) { expected = GetKnownGoodPercentEncodedValue(i); } else { expected = input; // no encoding } } string retVal = encoder.Encode(input); Assert.Equal(expected, retVal); } // Act & assert - astral chars for (int i = 0x10000; i <= 0x10FFFF; i++) { string input = char.ConvertFromUtf32(i); string expected = GetKnownGoodPercentEncodedValue(i); string retVal = encoder.Encode(input); Assert.Equal(expected, retVal); } }