From 905b5bcfc2a037bc06378aa2339b96a03b56fcbc Mon Sep 17 00:00:00 2001 From: Cesar Blum Silveira Date: Wed, 16 Dec 2015 19:20:18 -0800 Subject: [PATCH] Enable tests on CoreCLR (#143). --- .../AuthenticationTests.cs | 19 ++++++--- .../HttpsTests.cs | 12 +++++- .../OpaqueUpgradeTests.cs | 4 +- .../RequestBodyTests.cs | 3 +- .../RequestHeaderTests.cs | 2 +- .../ResponseBodyTests.cs | 11 +++-- .../ResponseHeaderTests.cs | 10 ++++- .../ResponseSendFileTests.cs | 3 +- .../ServerTests.cs | 12 ++++-- .../project.json | 7 ++++ .../AuthenticationTests.cs | 12 +++--- .../HttpsTests.cs | 8 ++++ .../OpaqueUpgradeTests.cs | 2 +- .../RequestBodyTests.cs | 2 +- .../RequestHeaderTests.cs | 2 +- .../ResponseBodyTests.cs | 19 +++++---- .../ResponseCachingTests.cs | 41 ++++++++++++------- .../ResponseHeaderTests.cs | 10 ++++- .../ResponseSendFileTests.cs | 6 +-- .../ServerTests.cs | 6 +-- .../SkipOffDomainAttribute.cs | 5 ++- .../project.json | 8 ++++ 22 files changed, 143 insertions(+), 61 deletions(-) diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/AuthenticationTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/AuthenticationTests.cs index d93c8b6579..2b5d570844 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/AuthenticationTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/AuthenticationTests.cs @@ -21,6 +21,7 @@ using System.Net; using System.Net.Http; using System.Threading.Tasks; using Microsoft.AspNet.Http.Features.Authentication; +using Microsoft.AspNet.Testing.xunit; using Xunit; using AuthenticationSchemes = Microsoft.Net.Http.Server.AuthenticationSchemes; @@ -52,11 +53,12 @@ namespace Microsoft.AspNet.Server.WebListener } } - [Theory] + [ConditionalTheory] [InlineData(AuthenticationSchemes.Negotiate)] [InlineData(AuthenticationSchemes.NTLM)] // [InlineData(AuthenticationSchemes.Digest)] // TODO: Not implemented [InlineData(AuthenticationSchemes.Basic)] + [FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "HttpClientHandler issue (https://github.com/dotnet/corefx/issues/5045).")] public async Task AuthType_RequireAuth_ChallengesAdded(AuthenticationSchemes authType) { string address; @@ -71,11 +73,12 @@ namespace Microsoft.AspNet.Server.WebListener } } - [Theory] + [ConditionalTheory] [InlineData(AuthenticationSchemes.Negotiate)] [InlineData(AuthenticationSchemes.NTLM)] // [InlineData(AuthenticationSchemes.Digest)] // TODO: Not implemented [InlineData(AuthenticationSchemes.Basic)] + [FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "HttpClientHandler issue (https://github.com/dotnet/corefx/issues/5045).")] public async Task AuthType_AllowAnonymousButSpecify401_ChallengesAdded(AuthenticationSchemes authType) { string address; @@ -94,7 +97,8 @@ namespace Microsoft.AspNet.Server.WebListener } } - [Fact] + [ConditionalFact] + [FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "HttpClientHandler issue (https://github.com/dotnet/corefx/issues/5045).")] public async Task MultipleAuthTypes_AllowAnonymousButSpecify401_ChallengesAdded() { string address; @@ -293,12 +297,13 @@ namespace Microsoft.AspNet.Server.WebListener } } - [Theory] + [ConditionalTheory] [InlineData(AuthenticationSchemes.Negotiate)] [InlineData(AuthenticationSchemes.NTLM)] // [InlineData(AuthenticationSchemes.Digest)] [InlineData(AuthenticationSchemes.Basic)] [InlineData(AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM | /*AuthenticationSchemes.Digest |*/ AuthenticationSchemes.Basic)] + [FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "HttpClientHandler issue (https://github.com/dotnet/corefx/issues/5045).")] public async Task AuthTypes_ChallengeWithoutAuthTypes_AllChallengesSent(AuthenticationSchemes authType) { string address; @@ -317,12 +322,13 @@ namespace Microsoft.AspNet.Server.WebListener } } - [Theory] + [ConditionalTheory] [InlineData(AuthenticationSchemes.Negotiate)] [InlineData(AuthenticationSchemes.NTLM)] // [InlineData(AuthenticationSchemes.Digest)] [InlineData(AuthenticationSchemes.Basic)] [InlineData(AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM | /*AuthenticationSchemes.Digest |*/ AuthenticationSchemes.Basic)] + [FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "HttpClientHandler issue (https://github.com/dotnet/corefx/issues/5045).")] public async Task AuthTypes_ChallengeWithAllAuthTypes_AllChallengesSent(AuthenticationSchemes authType) { string address; @@ -344,11 +350,12 @@ namespace Microsoft.AspNet.Server.WebListener } } - [Theory] + [ConditionalTheory] [InlineData(AuthenticationSchemes.Negotiate)] [InlineData(AuthenticationSchemes.NTLM)] // [InlineData(AuthenticationSchemes.Digest)] [InlineData(AuthenticationSchemes.Basic)] + [FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "HttpClientHandler issue (https://github.com/dotnet/corefx/issues/5045).")] public async Task AuthTypes_ChallengeOneAuthType_OneChallengeSent(AuthenticationSchemes authType) { string address; diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/HttpsTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/HttpsTests.cs index ba18f67160..3027f4bc70 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/HttpsTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/HttpsTests.cs @@ -115,7 +115,11 @@ namespace Microsoft.AspNet.Server.WebListener private async Task SendRequestAsync(string uri, X509Certificate cert = null) { - WebRequestHandler handler = new WebRequestHandler(); +#if DNX451 + var handler = new WebRequestHandler(); +#else + var handler = new WinHttpHandler(); +#endif handler.ServerCertificateValidationCallback = (a, b, c, d) => true; if (cert != null) { @@ -129,7 +133,11 @@ namespace Microsoft.AspNet.Server.WebListener private async Task SendRequestAsync(string uri, string upload) { - WebRequestHandler handler = new WebRequestHandler(); +#if DNX451 + var handler = new WebRequestHandler(); +#else + var handler = new WinHttpHandler(); +#endif handler.ServerCertificateValidationCallback = (a, b, c, d) => true; using (HttpClient client = new HttpClient(handler)) { diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/OpaqueUpgradeTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/OpaqueUpgradeTests.cs index 4949bd10bb..0db487cd09 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/OpaqueUpgradeTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/OpaqueUpgradeTests.cs @@ -24,7 +24,6 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; -using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Testing.xunit; using Xunit; @@ -218,6 +217,7 @@ namespace Microsoft.AspNet.Server.WebListener // Connect with a socket Uri uri = new Uri(address); TcpClient client = new TcpClient(); + try { await client.ConnectAsync(uri.Host, uri.Port); @@ -235,7 +235,7 @@ namespace Microsoft.AspNet.Server.WebListener } catch (Exception) { - client.Close(); + ((IDisposable)client).Dispose(); throw; } } diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestBodyTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestBodyTests.cs index b17639491e..58a1de5318 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestBodyTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestBodyTests.cs @@ -191,6 +191,7 @@ namespace Microsoft.AspNet.Server.WebListener // Connect with a socket Uri uri = new Uri(address); TcpClient client = new TcpClient(); + try { await client.ConnectAsync(uri.Host, uri.Port); @@ -204,7 +205,7 @@ namespace Microsoft.AspNet.Server.WebListener } catch (Exception) { - client.Close(); + ((IDisposable)client).Dispose(); throw; } } diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestHeaderTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestHeaderTests.cs index f4fd3c378c..62f7a36708 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestHeaderTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestHeaderTests.cs @@ -107,7 +107,7 @@ namespace Microsoft.AspNet.Server.WebListener byte[] response = new byte[1024 * 5]; await Task.Run(() => socket.Receive(response)); - socket.Close(); + socket.Dispose(); } } } diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseBodyTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseBodyTests.cs index 2b5800f205..68a214cc83 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseBodyTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseBodyTests.cs @@ -25,6 +25,7 @@ using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Internal; +using Microsoft.AspNet.Testing.xunit; using Xunit; namespace Microsoft.AspNet.Server.WebListener @@ -78,7 +79,7 @@ namespace Microsoft.AspNet.Server.WebListener string address; using (Utilities.CreateHttpServer(out address, async httpContext => { - httpContext.Response.Headers["transfeR-Encoding"] = " CHunked "; + httpContext.Response.Headers["transfeR-Encoding"] = "CHunked"; Stream stream = httpContext.Response.Body; var responseBytes = Encoding.ASCII.GetBytes("10\r\nManually Chunked\r\n0\r\n\r\n"); await stream.WriteAsync(responseBytes, 0, responseBytes.Length); @@ -98,13 +99,17 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseBody_WriteContentLength_PassedThrough() { string address; - using (Utilities.CreateHttpServer(out address, httpContext => + using (Utilities.CreateHttpServer(out address, async httpContext => { httpContext.Response.Headers["Content-lenGth"] = " 30 "; Stream stream = httpContext.Response.Body; +#if DNX451 stream.EndWrite(stream.BeginWrite(new byte[10], 0, 10, null, null)); +#else + await stream.WriteAsync(new byte[10], 0, 10); +#endif stream.Write(new byte[10], 0, 10); - return stream.WriteAsync(new byte[10], 0, 10); + await stream.WriteAsync(new byte[10], 0, 10); })) { HttpResponseMessage response = await SendRequestAsync(address); diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseHeaderTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseHeaderTests.cs index 08b422d815..65cc7ba379 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseHeaderTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseHeaderTests.cs @@ -70,7 +70,7 @@ namespace Microsoft.AspNet.Server.WebListener Assert.Equal(0, response.ContentLength); Assert.NotNull(response.Headers["Date"]); Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers["Server"]); - Assert.Equal(new string[] { "custom1" }, response.Headers.GetValues("WWW-Authenticate")); + Assert.Equal("custom1", response.Headers["WWW-Authenticate"]); } } @@ -94,7 +94,11 @@ namespace Microsoft.AspNet.Server.WebListener Assert.Equal(0, response.ContentLength); Assert.NotNull(response.Headers["Date"]); Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers["Server"]); +#if DNXCORE50 // WebHeaderCollection.GetValues() not available in CoreCLR. + Assert.Equal("custom1, and custom2, custom3", response.Headers["WWW-Authenticate"]); +#else Assert.Equal(new string[] { "custom1, and custom2", "custom3" }, response.Headers.GetValues("WWW-Authenticate")); +#endif } } @@ -118,7 +122,11 @@ namespace Microsoft.AspNet.Server.WebListener Assert.Equal(0, response.ContentLength); Assert.NotNull(response.Headers["Date"]); Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers["Server"]); +#if DNXCORE50 // WebHeaderCollection.GetValues() not available in CoreCLR. + Assert.Equal("custom1, and custom2, custom3", response.Headers["Custom-Header1"]); +#else Assert.Equal(new string[] { "custom1, and custom2", "custom3" }, response.Headers.GetValues("Custom-Header1")); +#endif } } diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseSendFileTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseSendFileTests.cs index 751d4d0e40..48ef151349 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseSendFileTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseSendFileTests.cs @@ -25,7 +25,6 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.Http.Features; -using Microsoft.AspNet.Http.Internal; using Xunit; namespace Microsoft.AspNet.Server.WebListener @@ -38,7 +37,7 @@ namespace Microsoft.AspNet.Server.WebListener public ResponseSendFileTests() { - AbsoluteFilePath = Directory.GetFiles(Environment.CurrentDirectory).First(); + AbsoluteFilePath = Directory.GetFiles(Directory.GetCurrentDirectory()).First(); RelativeFilePath = Path.GetFileName(AbsoluteFilePath); FileLength = new FileInfo(AbsoluteFilePath).Length; } diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ServerTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ServerTests.cs index d06ee8841c..15ca0a7754 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ServerTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ServerTests.cs @@ -207,7 +207,11 @@ namespace Microsoft.AspNet.Server.WebListener using (Socket socket = await SendHungRequestAsync("GET", address)) { Assert.True(received.WaitOne(interval), "Receive Timeout"); - socket.Close(0); // Force a RST + + // Force a RST + socket.LingerState = new LingerOption(true, 0); + socket.Dispose(); + aborted.Set(); } Assert.True(canceled.WaitOne(interval), "canceled"); @@ -267,7 +271,6 @@ namespace Microsoft.AspNet.Server.WebListener private async Task SendRequestAsync(string uri) { - ServicePointManager.DefaultConnectionLimit = 100; using (HttpClient client = new HttpClient()) { return await client.GetStringAsync(uri); @@ -289,6 +292,7 @@ namespace Microsoft.AspNet.Server.WebListener // Connect with a socket Uri uri = new Uri(address); TcpClient client = new TcpClient(); + try { await client.ConnectAsync(uri.Host, uri.Port); @@ -297,13 +301,13 @@ namespace Microsoft.AspNet.Server.WebListener // Send an HTTP GET request byte[] requestBytes = BuildGetRequest(method, uri); await stream.WriteAsync(requestBytes, 0, requestBytes.Length); - + // Return the opaque network stream return client.Client; } catch (Exception) { - client.Close(); + ((IDisposable)client).Dispose(); throw; } } diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/project.json b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/project.json index 1edc19d889..9fee80e2f2 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/project.json +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/project.json @@ -13,6 +13,13 @@ "System.Net.Http": "", "System.Net.Http.WebRequest": "" } + }, + "dnxcore50": { + "dependencies": { + "System.Net.Http.WinHttpHandler": "4.0.0-*", + "System.Net.Requests": "4.0.11-*", + "System.Net.WebHeaderCollection": "4.0.1-*" + } } } } diff --git a/test/Microsoft.Net.Http.Server.FunctionalTests/AuthenticationTests.cs b/test/Microsoft.Net.Http.Server.FunctionalTests/AuthenticationTests.cs index 7c4272cd51..8281eb53fa 100644 --- a/test/Microsoft.Net.Http.Server.FunctionalTests/AuthenticationTests.cs +++ b/test/Microsoft.Net.Http.Server.FunctionalTests/AuthenticationTests.cs @@ -44,11 +44,12 @@ namespace Microsoft.Net.Http.Server } } - [Theory] + [ConditionalTheory] [InlineData(AuthenticationSchemes.Negotiate)] [InlineData(AuthenticationSchemes.NTLM)] // [InlineData(AuthenticationType.Digest)] // TODO: Not implemented [InlineData(AuthenticationSchemes.Basic)] + [FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "HttpClientHandler issue (https://github.com/dotnet/corefx/issues/5045).")] public async Task AuthType_RequireAuth_ChallengesAdded(AuthenticationSchemes authType) { string address; @@ -57,18 +58,18 @@ namespace Microsoft.Net.Http.Server Task responseTask = SendRequestAsync(address); var contextTask = server.GetContextAsync(); // Fails when the server shuts down, the challenge happens internally. - var response = await responseTask; Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode); Assert.Equal(authType.ToString(), response.Headers.WwwAuthenticate.ToString(), StringComparer.OrdinalIgnoreCase); } } - [Theory] + [ConditionalTheory] [InlineData(AuthenticationSchemes.Negotiate)] [InlineData(AuthenticationSchemes.NTLM)] // [InlineData(AuthenticationSchemes.Digest)] // TODO: Not implemented [InlineData(AuthenticationSchemes.Basic)] + [FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "HttpClientHandler issue (https://github.com/dotnet/corefx/issues/5045).")] public async Task AuthType_AllowAnonymousButSpecify401_ChallengesAdded(AuthenticationSchemes authType) { string address; @@ -89,7 +90,8 @@ namespace Microsoft.Net.Http.Server } } - [Fact] + [ConditionalFact] + [FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "HttpClientHandler issue (https://github.com/dotnet/corefx/issues/5045).")] public async Task MultipleAuthTypes_AllowAnonymousButSpecify401_ChallengesAdded() { string address; @@ -216,7 +218,7 @@ namespace Microsoft.Net.Http.Server { HttpClientHandler handler = new HttpClientHandler(); handler.UseDefaultCredentials = useDefaultCredentials; - using (HttpClient client = new HttpClient(handler)) + using (HttpClient client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(5) }) { return await client.GetAsync(uri); } diff --git a/test/Microsoft.Net.Http.Server.FunctionalTests/HttpsTests.cs b/test/Microsoft.Net.Http.Server.FunctionalTests/HttpsTests.cs index 5483589211..05931b0ad1 100644 --- a/test/Microsoft.Net.Http.Server.FunctionalTests/HttpsTests.cs +++ b/test/Microsoft.Net.Http.Server.FunctionalTests/HttpsTests.cs @@ -107,7 +107,11 @@ namespace Microsoft.Net.Http.Server private async Task SendRequestAsync(string uri, X509Certificate cert = null) { +#if DNX451 WebRequestHandler handler = new WebRequestHandler(); +#else + WinHttpHandler handler = new WinHttpHandler(); +#endif handler.ServerCertificateValidationCallback = (a, b, c, d) => true; if (cert != null) { @@ -121,7 +125,11 @@ namespace Microsoft.Net.Http.Server private async Task SendRequestAsync(string uri, string upload) { +#if DNX451 WebRequestHandler handler = new WebRequestHandler(); +#else + WinHttpHandler handler = new WinHttpHandler(); +#endif handler.ServerCertificateValidationCallback = (a, b, c, d) => true; using (HttpClient client = new HttpClient(handler)) { diff --git a/test/Microsoft.Net.Http.Server.FunctionalTests/OpaqueUpgradeTests.cs b/test/Microsoft.Net.Http.Server.FunctionalTests/OpaqueUpgradeTests.cs index 04a17e4241..da14565de8 100644 --- a/test/Microsoft.Net.Http.Server.FunctionalTests/OpaqueUpgradeTests.cs +++ b/test/Microsoft.Net.Http.Server.FunctionalTests/OpaqueUpgradeTests.cs @@ -167,7 +167,7 @@ namespace Microsoft.Net.Http.Server } catch (Exception) { - client.Close(); + ((IDisposable)client).Dispose(); throw; } } diff --git a/test/Microsoft.Net.Http.Server.FunctionalTests/RequestBodyTests.cs b/test/Microsoft.Net.Http.Server.FunctionalTests/RequestBodyTests.cs index 98f1b28e04..b9423dac99 100644 --- a/test/Microsoft.Net.Http.Server.FunctionalTests/RequestBodyTests.cs +++ b/test/Microsoft.Net.Http.Server.FunctionalTests/RequestBodyTests.cs @@ -352,7 +352,7 @@ namespace Microsoft.Net.Http.Server } catch (Exception) { - client.Close(); + ((IDisposable)client).Dispose(); throw; } } diff --git a/test/Microsoft.Net.Http.Server.FunctionalTests/RequestHeaderTests.cs b/test/Microsoft.Net.Http.Server.FunctionalTests/RequestHeaderTests.cs index 54ad7c54eb..f28e6850a5 100644 --- a/test/Microsoft.Net.Http.Server.FunctionalTests/RequestHeaderTests.cs +++ b/test/Microsoft.Net.Http.Server.FunctionalTests/RequestHeaderTests.cs @@ -99,7 +99,7 @@ namespace Microsoft.Net.Http.Server byte[] response = new byte[1024 * 5]; await Task.Run(() => socket.Receive(response)); - socket.Close(); + socket.Dispose(); } } } \ No newline at end of file diff --git a/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseBodyTests.cs b/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseBodyTests.cs index 8cae679baa..95ebe7e7a6 100644 --- a/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseBodyTests.cs +++ b/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseBodyTests.cs @@ -8,6 +8,7 @@ using System.Net.Http; using System.Text; using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNet.Testing.xunit; using Xunit; namespace Microsoft.Net.Http.Server @@ -94,7 +95,7 @@ namespace Microsoft.Net.Http.Server Task responseTask = SendRequestAsync(address); var context = await server.GetContextAsync(); - context.Response.Headers["transfeR-Encoding"] = " CHunked "; + context.Response.Headers["transfeR-Encoding"] = "CHunked"; Stream stream = context.Response.Body; var responseBytes = Encoding.ASCII.GetBytes("10\r\nManually Chunked\r\n0\r\n\r\n"); await stream.WriteAsync(responseBytes, 0, responseBytes.Length); @@ -121,7 +122,11 @@ namespace Microsoft.Net.Http.Server var context = await server.GetContextAsync(); context.Response.Headers["Content-lenGth"] = " 30 "; Stream stream = context.Response.Body; +#if DNX451 stream.EndWrite(stream.BeginWrite(new byte[10], 0, 10, null, null)); +#else + await stream.WriteAsync(new byte[10], 0, 10); +#endif stream.Write(new byte[10], 0, 10); await stream.WriteAsync(new byte[10], 0, 10); context.Dispose(); @@ -148,12 +153,12 @@ namespace Microsoft.Net.Http.Server var context = await server.GetContextAsync(); context.Response.Headers["Content-lenGth"] = " 20 "; context.Dispose(); - +#if !DNXCORE50 // HttpClient retries the request because it didn't get a response. context = await server.GetContextAsync(); context.Response.Headers["Content-lenGth"] = " 20 "; context.Dispose(); - +#endif await Assert.ThrowsAsync(() => responseTask); } } @@ -170,13 +175,13 @@ namespace Microsoft.Net.Http.Server context.Response.Headers["Content-lenGth"] = " 20 "; context.Response.Body.Write(new byte[5], 0, 5); context.Dispose(); - +#if !DNXCORE50 // HttpClient retries the request because it didn't get a response. context = await server.GetContextAsync(); context.Response.Headers["Content-lenGth"] = " 20 "; context.Response.Body.Write(new byte[5], 0, 5); context.Dispose(); - +#endif await Assert.ThrowsAsync(() => responseTask); } } @@ -194,14 +199,14 @@ namespace Microsoft.Net.Http.Server context.Response.Body.Write(new byte[5], 0, 5); Assert.Throws(() => context.Response.Body.Write(new byte[6], 0, 6)); context.Dispose(); - +#if !DNXCORE50 // HttpClient retries the request because it didn't get a response. context = await server.GetContextAsync(); context.Response.Headers["Content-lenGth"] = " 10 "; context.Response.Body.Write(new byte[5], 0, 5); Assert.Throws(() => context.Response.Body.Write(new byte[6], 0, 6)); context.Dispose(); - +#endif await Assert.ThrowsAsync(() => responseTask); } } diff --git a/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseCachingTests.cs b/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseCachingTests.cs index fca4fd66fc..0d9d9e9638 100644 --- a/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseCachingTests.cs +++ b/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseCachingTests.cs @@ -6,9 +6,10 @@ using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNet.Testing.xunit; using Xunit; -namespace Microsoft.Net.Http.Server.FunctionalTests +namespace Microsoft.Net.Http.Server { public class ResponseCachingTests { @@ -17,7 +18,7 @@ namespace Microsoft.Net.Http.Server.FunctionalTests public ResponseCachingTests() { - _absoluteFilePath = Directory.GetFiles(Environment.CurrentDirectory).First(); + _absoluteFilePath = Directory.GetFiles(Directory.GetCurrentDirectory()).First(); _fileLength = new FileInfo(_absoluteFilePath).Length; } @@ -473,6 +474,12 @@ namespace Microsoft.Net.Http.Server.FunctionalTests // Http.Sys will cache any status code. for (int status = 200; status < 600; status++) { + // 407 (Proxy Authentication Required) makes CoreCLR's HttpClient throw + if (status == 407) + { + continue; + } + var responseTask = SendRequestAsync(address + status); var context = await server.GetContextAsync(); @@ -935,7 +942,8 @@ namespace Microsoft.Net.Http.Server.FunctionalTests } // http://tools.ietf.org/html/rfc7233#section-4.1 - [Fact] + [ConditionalFact] + [FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "Cached response contains duplicate Content-Length headers (#167).")] public async Task Caching_RequestRangeFromCache_RangeServedFromCache() { string address; @@ -956,11 +964,11 @@ namespace Microsoft.Net.Http.Server.FunctionalTests Assert.Equal("1", response.Headers.GetValues("x-request-count").FirstOrDefault()); Assert.Equal(new byte[100], await response.Content.ReadAsByteArrayAsync()); - response = await SendRequestAsync(address, "GET", "Range", "bytes=0-10"); + response = await SendRequestAsync(address, "GET", "Range", "bytes=0-10", HttpCompletionOption.ResponseHeadersRead); Assert.Equal(206, (int)response.StatusCode); Assert.Equal("1", response.Headers.GetValues("x-request-count").FirstOrDefault()); Assert.Equal("bytes 0-10/100", response.Content.Headers.GetValues("content-range").FirstOrDefault()); - Assert.Equal(new byte[11], await response.Content.ReadAsByteArrayAsync()); + Assert.Equal(11, response.Content.Headers.ContentLength); } } @@ -993,7 +1001,8 @@ namespace Microsoft.Net.Http.Server.FunctionalTests } } - [Fact] + [ConditionalFact] + [FrameworkSkipCondition(RuntimeFrameworks.CoreCLR, SkipReason = "Cached response contains duplicate Content-Length headers (#167).")] public async Task Caching_RequestRangeFromCachedFile_ServedFromCache() { string address; @@ -1017,7 +1026,7 @@ namespace Microsoft.Net.Http.Server.FunctionalTests // Send a second request and make sure we get the same response (without listening for one on the server). var rangeLength = responseLength / 2; - response = await SendRequestAsync(address, "GET", "Range", "bytes=0-" + (rangeLength - 1)); + response = await SendRequestAsync(address, "GET", "Range", "bytes=0-" + (rangeLength - 1), HttpCompletionOption.ResponseHeadersRead); Assert.Equal(206, (int)response.StatusCode); Assert.Equal("1", response.Headers.GetValues("x-request-count").FirstOrDefault()); Assert.Equal(rangeLength, response.Content.Headers.ContentLength); @@ -1046,26 +1055,28 @@ namespace Microsoft.Net.Http.Server.FunctionalTests Assert.Equal(200, (int)response.StatusCode); Assert.Equal("1", response.Headers.GetValues("x-request-count").FirstOrDefault()); Assert.Equal(responseLength, response.Content.Headers.ContentLength); - // Send a second request and make sure we get the same response (without listening for one on the server). var rangeLength = responseLength / 4; - response = await SendRequestAsync(address, "GET", "Range", "bytes=0-" + (rangeLength - 1) + "," + rangeLength + "-" + (rangeLength + rangeLength - 1)); + response = await SendRequestAsync(address, "GET", "Range", "bytes=0-" + (rangeLength - 1) + "," + rangeLength + "-" + (rangeLength + rangeLength - 1), HttpCompletionOption.ResponseHeadersRead); Assert.Equal(206, (int)response.StatusCode); Assert.Equal("1", response.Headers.GetValues("x-request-count").FirstOrDefault()); Assert.True(response.Content.Headers.GetValues("content-type").First().StartsWith("multipart/byteranges;")); } } - private async Task SendRequestAsync(string uri, string method = "GET", string extraHeader = null, string extraHeaderValue = null) + private async Task SendRequestAsync(string uri, string method = "GET", string extraHeader = null, string extraHeaderValue = null, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead) { - using (var client = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) }) + using (var handler = new HttpClientHandler() { AllowAutoRedirect = false }) { - var request = new HttpRequestMessage(new HttpMethod(method), uri); - if (!string.IsNullOrEmpty(extraHeader)) + using (var client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(5) }) { - request.Headers.Add(extraHeader, extraHeaderValue); + var request = new HttpRequestMessage(new HttpMethod(method), uri); + if (!string.IsNullOrEmpty(extraHeader)) + { + request.Headers.Add(extraHeader, extraHeaderValue); + } + return await client.SendAsync(request, httpCompletionOption); } - return await client.SendAsync(request); } } } diff --git a/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseHeaderTests.cs b/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseHeaderTests.cs index 46c471c607..ffaa2442d6 100644 --- a/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseHeaderTests.cs +++ b/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseHeaderTests.cs @@ -192,7 +192,7 @@ namespace Microsoft.Net.Http.Server Assert.Equal(0, response.ContentLength); Assert.NotNull(response.Headers["Date"]); Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers["Server"]); - Assert.Equal(new string[] { "custom1" }, response.Headers.GetValues("WWW-Authenticate")); + Assert.Equal("custom1", response.Headers["WWW-Authenticate"]); } } @@ -217,7 +217,11 @@ namespace Microsoft.Net.Http.Server Assert.Equal(0, response.ContentLength); Assert.NotNull(response.Headers["Date"]); Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers["Server"]); +#if DNXCORE50 // WebHeaderCollection.GetValues() not available in CoreCLR. + Assert.Equal("custom1, and custom2, custom3", response.Headers["WWW-Authenticate"]); +#else Assert.Equal(new string[] { "custom1, and custom2", "custom3" }, response.Headers.GetValues("WWW-Authenticate")); +#endif } } @@ -242,7 +246,11 @@ namespace Microsoft.Net.Http.Server Assert.Equal(0, response.ContentLength); Assert.NotNull(response.Headers["Date"]); Assert.Equal("Microsoft-HTTPAPI/2.0", response.Headers["Server"]); +#if DNXCORE50 // WebHeaderCollection.GetValues() not available in CoreCLR. + Assert.Equal("custom1, and custom2, custom3", response.Headers["Custom-Header1"]); +#else Assert.Equal(new string[] { "custom1, and custom2", "custom3" }, response.Headers.GetValues("Custom-Header1")); +#endif } } diff --git a/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseSendFileTests.cs b/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseSendFileTests.cs index 66014c92db..4428dfc086 100644 --- a/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseSendFileTests.cs +++ b/test/Microsoft.Net.Http.Server.FunctionalTests/ResponseSendFileTests.cs @@ -19,7 +19,7 @@ namespace Microsoft.Net.Http.Server public ResponseSendFileTests() { - AbsoluteFilePath = Directory.GetFiles(Environment.CurrentDirectory).First(); + AbsoluteFilePath = Directory.GetFiles(Directory.GetCurrentDirectory()).First(); RelativeFilePath = Path.GetFileName(AbsoluteFilePath); FileLength = new FileInfo(AbsoluteFilePath).Length; } @@ -205,9 +205,9 @@ namespace Microsoft.Net.Http.Server [Fact] public async Task ResponseSendFile_EmptyFileCountUnspecified_SetsChunkedAndFlushesHeaders() { - var emptyFilePath = Path.Combine(Environment.CurrentDirectory, "zz_" + Guid.NewGuid().ToString() + "EmptyTestFile.txt"); + var emptyFilePath = Path.Combine(Directory.GetCurrentDirectory(), "zz_" + Guid.NewGuid().ToString() + "EmptyTestFile.txt"); var emptyFile = File.Create(emptyFilePath, 1024); - emptyFile.Close(); + emptyFile.Dispose(); string address; using (var server = Utilities.CreateHttpServer(out address)) diff --git a/test/Microsoft.Net.Http.Server.FunctionalTests/ServerTests.cs b/test/Microsoft.Net.Http.Server.FunctionalTests/ServerTests.cs index 8b9ed10838..a388436b1c 100644 --- a/test/Microsoft.Net.Http.Server.FunctionalTests/ServerTests.cs +++ b/test/Microsoft.Net.Http.Server.FunctionalTests/ServerTests.cs @@ -8,6 +8,7 @@ using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNet.Testing.xunit; using Xunit; namespace Microsoft.Net.Http.Server @@ -122,11 +123,11 @@ namespace Microsoft.Net.Http.Server context.Abort(); Assert.True(canceled.WaitOne(interval), "Aborted"); Assert.True(ct.IsCancellationRequested, "IsCancellationRequested"); - +#if !DNXCORE50 // HttpClient re-tries the request because it doesn't know if the request was received. context = await server.GetContextAsync(); context.Abort(); - +#endif await Assert.ThrowsAsync(() => responseTask); } } @@ -246,7 +247,6 @@ namespace Microsoft.Net.Http.Server private async Task SendRequestAsync(string uri) { - ServicePointManager.DefaultConnectionLimit = 100; using (HttpClient client = new HttpClient()) { return await client.GetStringAsync(uri); diff --git a/test/Microsoft.Net.Http.Server.FunctionalTests/SkipOffDomainAttribute.cs b/test/Microsoft.Net.Http.Server.FunctionalTests/SkipOffDomainAttribute.cs index 11a7bb82fb..5b8ed56e53 100644 --- a/test/Microsoft.Net.Http.Server.FunctionalTests/SkipOffDomainAttribute.cs +++ b/test/Microsoft.Net.Http.Server.FunctionalTests/SkipOffDomainAttribute.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.DirectoryServices.ActiveDirectory; using Microsoft.AspNet.Testing.xunit; namespace Microsoft.Net.Http.Server @@ -19,7 +18,9 @@ namespace Microsoft.Net.Http.Server { try { - return !string.IsNullOrEmpty(Domain.GetComputerDomain().Name); +#if DNX451 + return !string.IsNullOrEmpty(System.DirectoryServices.ActiveDirectory.Domain.GetComputerDomain().Name); +#endif } catch { diff --git a/test/Microsoft.Net.Http.Server.FunctionalTests/project.json b/test/Microsoft.Net.Http.Server.FunctionalTests/project.json index aba7cd14c8..9e55f02a07 100644 --- a/test/Microsoft.Net.Http.Server.FunctionalTests/project.json +++ b/test/Microsoft.Net.Http.Server.FunctionalTests/project.json @@ -15,6 +15,14 @@ "System.Net.Http": "", "System.Net.Http.WebRequest": "" } + }, + "dnxcore50": { + "dependencies": { + "System.Net.Http": "4.0.1-*", + "System.Net.Http.WinHttpHandler": "4.0.0-*", + "System.Net.Requests": "4.0.11-*", + "System.Net.WebSockets.Client": "4.0.0-*" + } } } }