在本地计算机上使用ReSharper 8.2,在构建服务器上使用NUnit 2.6.3,发现在ReSharper中传递了一些测试并在NUnit中失败.在本地安装NUnit并且结果相同,因此计算机之间没有区别.我的两个同事进行了相同的测试并得到了相同的结果,所以看起来似乎没有弄乱我的电脑.
测试的简化版本:
[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)
解决方法
最初,它是.NET Framework中的一个错误. URI scaping行为取决于环境(.NET Framework版本和配置).该错误已在.NET Framework 4.5中修复.例如,让我们拿代码
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)