测试的简化版本:
[Test] public void Test_UrlQueryString() { var urlInput = "http://www.domain.com/page-with-querystring?url=https://www.domain2.com/page%3Fp%3DPEPE"; var uri = new Uri(urlInput); Assert.AreEqual(urlInput,uri.ToString()); } [Test] public void Test_Dot() { var urlInput = "http://www.domain.com/page-with-dot.?p=google"; var uri = new Uri(urlInput); Assert.AreEqual(urlInput,uri.ToString()); }
Runtime Environment - OS Version: Microsoft Windows NT 6.1.7601 Service Pack 1 CLR Version: 4.0.30319.18444 ( Net 4.5 ) ProcessModel: Default DomainUsage: Single Execution Runtime: net-4.5 ...................F.F......... Tests run: 29,Errors: 0,Failures: 2,Inconclusive: 0,Time: 0.576769973208475 seconds Not run: 0,Invalid: 0,Ignored: 0,Skipped: 0 Errors and Failures: 1) Test Failure : Test.OrganicTest.Test_Dot Expected string length 45 but was 44. Strings differ at index 35. Expected: "http://www.domain.com/page-with-dot.?p=google" But was: "http://www.domain.com/page-with-dot?p=google" ----------------------------------------------^ 2) Test Failure : Test.OrganicTest.Test_UrlQueryString Expected string length 87 but was 83. Strings differ at index 76. Expected: "...-with-querystring?url=https://www.domain2.com/page%3Fp%3DPEPE" But was: "...-with-querystring?url=https://www.domain2.com/page?p=PEPE" ----------------------------------------------------------------^
ReSharper似乎使用相同版本的NUnit(内置NUnit 2.6.3)
解决方法
var uri1 = "http://www.domain.com/page-with-dot.?p=google"; var uri2 = "http://www.domain.com/page-with-querystring?url=https://www.domain2.com/page%3Fp%3DPEPE"; Console.WriteLine(new Uri(uri1).ToString()); Console.WriteLine(new Uri(uri2).ToString());
.NET Framework 3.5中的输出:
http://www.domain.com/page-with-dot?p=google http://www.domain.com/page-with-querystring?url=https://www.domain2.com/page?p=PEPE
.NET Framework 4.5中的输出:
http://www.domain.com/page-with-dot.?p=google http://www.domain.com/page-with-querystring?url=https://www.domain2.com/page%3Fp%3DPEPE
此外,您可以使用.config文件对转义行为进行一些更改.例如,.NET 4.0和斜杠转义有一个选项:
<configuration> <uri> <schemeSettings> <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes" /> </schemeSettings> </uri> </configuration>
我认为,在你的情况下,ReSharper和NUnit只是使用不同的配置.虽然,真的很奇怪,NUnit收到了.NET 4.5的旧的uri转义结果.也许NUnit使用.NET 4.0中的一些微软库.此外,您可以通过手动将Framework选项设置为CLR4.0来使ReSharper中的测试失败.无论如何,您应该使用与环境无关的逻辑重写测试.
有用的链接:
> MS Connect 511010: Erroneous URI parsing for encoded,reserved characters,according to RFC 3986
> Mono Bug 16960
> StackOverflow: Getting a Uri with escaped slashes on mono
> StackOverflow: GETting a URL with an url-encoded slash
> Mono 3.10.0 release notes
> Mike Hadlow: How to stop System.Uri un-escaping forward slash characters
> Arnout’s Eclectica: URL-encoded slashes in System.Uri
> About slash escaping in .NET (In Russian)
原文地址:https://www.jb51.cc/csharp/92534.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。