diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/OpaqueUpgradeTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/OpaqueUpgradeTests.cs index 3040ddeebc..6a72bffb2c 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/OpaqueUpgradeTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/OpaqueUpgradeTests.cs @@ -15,7 +15,6 @@ // See the Apache 2 License for the specific language governing // permissions and limitations under the License. -/* TODO: Opaque using System; using System.Collections.Generic; using System.IO; @@ -24,14 +23,13 @@ using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNet.FeatureModel; +using Microsoft.AspNet.HttpFeature; +using Microsoft.AspNet.PipelineCore; using Xunit; -using Xunit.Extensions; namespace Microsoft.AspNet.Server.WebListener { - using AppFunc = Func; - using OpaqueUpgrade = Action, Func, Task>>; - public class OpaqueUpgradeTests { private const string Address = "http://localhost:8080/"; @@ -39,22 +37,17 @@ namespace Microsoft.AspNet.Server.WebListener [Fact] public async Task OpaqueUpgrade_SupportKeys_Present() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { + var httpContext = new DefaultHttpContext((IFeatureCollection)env); try { - IDictionary capabilities = env.Get>("server.Capabilities"); - Assert.NotNull(capabilities); - - Assert.Equal("1.0", capabilities.Get("opaque.Version")); - - OpaqueUpgrade opaqueUpgrade = env.Get("opaque.Upgrade"); - Assert.NotNull(opaqueUpgrade); + var opaqueFeature = httpContext.GetFeature(); + Assert.NotNull(opaqueFeature); } catch (Exception ex) { - byte[] body = Encoding.UTF8.GetBytes(ex.ToString()); - env.Get("owin.ResponseBody").Write(body, 0, body.Length); + return httpContext.Response.WriteAsync(ex.ToString()); } return Task.FromResult(0); })) @@ -67,50 +60,25 @@ namespace Microsoft.AspNet.Server.WebListener } } - [Fact] - public async Task OpaqueUpgrade_NullCallback_Throws() - { - using (CreateServer(env => - { - try - { - OpaqueUpgrade opaqueUpgrade = env.Get("opaque.Upgrade"); - opaqueUpgrade(new Dictionary(), null); - } - catch (Exception ex) - { - byte[] body = Encoding.UTF8.GetBytes(ex.ToString()); - env.Get("owin.ResponseBody").Write(body, 0, body.Length); - } - return Task.FromResult(0); - })) - { - HttpResponseMessage response = await SendRequestAsync(Address); - Assert.Equal(200, (int)response.StatusCode); - Assert.True(response.Headers.TransferEncodingChunked.Value, "Chunked"); - Assert.Contains("callback", response.Content.ReadAsStringAsync().Result); - } - } - [Fact] public async Task OpaqueUpgrade_AfterHeadersSent_Throws() { bool? upgradeThrew = null; - using (CreateServer(env => + using (Utilities.CreateHttpServer(async env => { - byte[] body = Encoding.UTF8.GetBytes("Hello World"); - env.Get("owin.ResponseBody").Write(body, 0, body.Length); - OpaqueUpgrade opaqueUpgrade = env.Get("opaque.Upgrade"); + var httpContext = new DefaultHttpContext((IFeatureCollection)env); + await httpContext.Response.WriteAsync("Hello World"); try { - opaqueUpgrade(null, _ => Task.FromResult(0)); + var opaqueFeature = httpContext.GetFeature(); + Assert.NotNull(opaqueFeature); + await opaqueFeature.UpgradeAsync(); upgradeThrew = false; } catch (InvalidOperationException) { upgradeThrew = true; } - return Task.FromResult(0); })) { HttpResponseMessage response = await SendRequestAsync(Address); @@ -124,26 +92,24 @@ namespace Microsoft.AspNet.Server.WebListener public async Task OpaqueUpgrade_GetUpgrade_Success() { ManualResetEvent waitHandle = new ManualResetEvent(false); - bool? callbackInvoked = null; - using (CreateServer(env => + bool? upgraded = null; + using (Utilities.CreateHttpServer(async env => { - var responseHeaders = env.Get>("owin.ResponseHeaders"); - responseHeaders["Upgrade"] = new string[] { "websocket" }; // Win8.1 blocks anything but WebSockets - OpaqueUpgrade opaqueUpgrade = env.Get("opaque.Upgrade"); - opaqueUpgrade(null, opqEnv => - { - callbackInvoked = true; - waitHandle.Set(); - return Task.FromResult(0); - }); - return Task.FromResult(0); + var httpContext = new DefaultHttpContext((IFeatureCollection)env); + httpContext.Response.Headers["Upgrade"] = "websocket"; // Win8.1 blocks anything but WebSockets + var opaqueFeature = httpContext.GetFeature(); + Assert.NotNull(opaqueFeature); + Assert.True(opaqueFeature.IsUpgradableRequest); + await opaqueFeature.UpgradeAsync(); + upgraded = true; + waitHandle.Set(); })) { using (Stream stream = await SendOpaqueRequestAsync("GET", Address)) { Assert.True(waitHandle.WaitOne(TimeSpan.FromSeconds(1)), "Timed out"); - Assert.True(callbackInvoked.HasValue, "CallbackInvoked not set"); - Assert.True(callbackInvoked.Value, "Callback not invoked"); + Assert.True(upgraded.HasValue, "Upgraded not set"); + Assert.True(upgraded.Value, "Upgrade failed"); } } } @@ -173,21 +139,26 @@ namespace Microsoft.AspNet.Server.WebListener [InlineData("PUT", "Content-Length: 0")] public async Task OpaqueUpgrade_VariousMethodsUpgradeSendAndReceive_Success(string method, string extraHeader) { - using (CreateServer(env => + using (Utilities.CreateHttpServer(async env => { - var responseHeaders = env.Get>("owin.ResponseHeaders"); - responseHeaders["Upgrade"] = new string[] { "WebSocket" }; // Win8.1 blocks anything but WebSockets - OpaqueUpgrade opaqueUpgrade = env.Get("opaque.Upgrade"); - opaqueUpgrade(null, async opqEnv => + var httpContext = new DefaultHttpContext((IFeatureCollection)env); + try { - Stream opaqueStream = opqEnv.Get("opaque.Stream"); + httpContext.Response.Headers["Upgrade"] = "websocket"; // Win8.1 blocks anything but WebSockets + var opaqueFeature = httpContext.GetFeature(); + Assert.NotNull(opaqueFeature); + Assert.True(opaqueFeature.IsUpgradableRequest); + var opaqueStream = await opaqueFeature.UpgradeAsync(); byte[] buffer = new byte[100]; int read = await opaqueStream.ReadAsync(buffer, 0, buffer.Length); await opaqueStream.WriteAsync(buffer, 0, read); - }); - return Task.FromResult(0); + } + catch (Exception ex) + { + await httpContext.Response.WriteAsync(ex.ToString()); + } })) { using (Stream stream = await SendOpaqueRequestAsync(method, Address, extraHeader)) @@ -208,54 +179,27 @@ namespace Microsoft.AspNet.Server.WebListener [InlineData("PUT", "Transfer-Encoding: chunked")] [InlineData("CUSTOMVERB", "Content-Length: 10")] [InlineData("CUSTOMVERB", "Transfer-Encoding: chunked")] - public void OpaqueUpgrade_InvalidMethodUpgrade_Disconnected(string method, string extraHeader) + public async Task OpaqueUpgrade_InvalidMethodUpgrade_Disconnected(string method, string extraHeader) { - OpaqueUpgrade opaqueUpgrade = null; - using (CreateServer(env => + using (Utilities.CreateHttpServer(async env => { - opaqueUpgrade = env.Get("opaque.Upgrade"); - if (opaqueUpgrade == null) + var httpContext = new DefaultHttpContext((IFeatureCollection)env); + try { - throw new NotImplementedException(); + var opaqueFeature = httpContext.GetFeature(); + Assert.NotNull(opaqueFeature); + Assert.False(opaqueFeature.IsUpgradableRequest); + } + catch (Exception ex) + { + await httpContext.Response.WriteAsync(ex.ToString()); } - opaqueUpgrade(null, opqEnv => Task.FromResult(0)); - return Task.FromResult(0); })) { - Assert.Throws(() => - { - try - { - return SendOpaqueRequestAsync(method, Address, extraHeader).Result; - } - catch (AggregateException ag) - { - throw ag.GetBaseException(); - } - }); - Assert.Null(opaqueUpgrade); + await Assert.ThrowsAsync(async () => await SendOpaqueRequestAsync(method, Address, extraHeader)); } } - private IDisposable CreateServer(AppFunc app) - { - IDictionary properties = new Dictionary(); - IList> addresses = new List>(); - properties["host.Addresses"] = addresses; - - IDictionary address = new Dictionary(); - addresses.Add(address); - - address["scheme"] = "http"; - address["host"] = "localhost"; - address["port"] = "8080"; - address["path"] = string.Empty; - - OwinServerFactory.Initialize(properties); - - return OwinServerFactory.Create(app, properties); - } - private async Task SendRequestAsync(string uri) { using (HttpClient client = new HttpClient()) @@ -333,5 +277,4 @@ namespace Microsoft.AspNet.Server.WebListener } } } -} -*/ \ No newline at end of file +} \ No newline at end of file diff --git a/test/Microsoft.Net.Server.FunctionalTests/OpaqueUpgradeTests.cs b/test/Microsoft.Net.Server.FunctionalTests/OpaqueUpgradeTests.cs index 031aeeca3b..4b4d82bee6 100644 --- a/test/Microsoft.Net.Server.FunctionalTests/OpaqueUpgradeTests.cs +++ b/test/Microsoft.Net.Server.FunctionalTests/OpaqueUpgradeTests.cs @@ -1,135 +1,57 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. -/* TODO: Opaque using System; -using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Net.Sockets; using System.Text; -using System.Threading; using System.Threading.Tasks; using Xunit; -using Xunit.Extensions; namespace Microsoft.Net.Server { - using AppFunc = Func; - using OpaqueUpgrade = Action, Func, Task>>; - public class OpaqueUpgradeTests { private const string Address = "http://localhost:8080/"; - [Fact] - public async Task OpaqueUpgrade_SupportKeys_Present() - { - using (CreateServer(env => - { - try - { - IDictionary capabilities = env.Get>("server.Capabilities"); - Assert.NotNull(capabilities); - - Assert.Equal("1.0", capabilities.Get("opaque.Version")); - - OpaqueUpgrade opaqueUpgrade = env.Get("opaque.Upgrade"); - Assert.NotNull(opaqueUpgrade); - } - catch (Exception ex) - { - byte[] body = Encoding.UTF8.GetBytes(ex.ToString()); - env.Get("owin.ResponseBody").Write(body, 0, body.Length); - } - return Task.FromResult(0); - })) - { - HttpResponseMessage response = await SendRequestAsync(Address); - Assert.Equal(200, (int)response.StatusCode); - Assert.False(response.Headers.TransferEncodingChunked.HasValue, "Chunked"); - Assert.Equal(0, response.Content.Headers.ContentLength); - Assert.Equal(string.Empty, response.Content.ReadAsStringAsync().Result); - } - } - - [Fact] - public async Task OpaqueUpgrade_NullCallback_Throws() - { - using (CreateServer(env => - { - try - { - OpaqueUpgrade opaqueUpgrade = env.Get("opaque.Upgrade"); - opaqueUpgrade(new Dictionary(), null); - } - catch (Exception ex) - { - byte[] body = Encoding.UTF8.GetBytes(ex.ToString()); - env.Get("owin.ResponseBody").Write(body, 0, body.Length); - } - return Task.FromResult(0); - })) - { - HttpResponseMessage response = await SendRequestAsync(Address); - Assert.Equal(200, (int)response.StatusCode); - Assert.True(response.Headers.TransferEncodingChunked.Value, "Chunked"); - Assert.Contains("callback", response.Content.ReadAsStringAsync().Result); - } - } - [Fact] public async Task OpaqueUpgrade_AfterHeadersSent_Throws() { - bool? upgradeThrew = null; - using (CreateServer(env => + using (var server = Utilities.CreateHttpServer()) { + Task clientTask = SendRequestAsync(Address); + + var context = await server.GetContextAsync(); byte[] body = Encoding.UTF8.GetBytes("Hello World"); - env.Get("owin.ResponseBody").Write(body, 0, body.Length); - OpaqueUpgrade opaqueUpgrade = env.Get("opaque.Upgrade"); - try - { - opaqueUpgrade(null, _ => Task.FromResult(0)); - upgradeThrew = false; - } - catch (InvalidOperationException) - { - upgradeThrew = true; - } - return Task.FromResult(0); - })) - { - HttpResponseMessage response = await SendRequestAsync(Address); + context.Response.Body.Write(body, 0, body.Length); + + context.Response.Headers["Upgrade"] = new[] { "WebSocket" }; // Win8.1 blocks anything but WebSocket + Assert.ThrowsAsync(async () => await context.UpgradeAsync()); + context.Dispose(); + HttpResponseMessage response = await clientTask; Assert.Equal(200, (int)response.StatusCode); Assert.True(response.Headers.TransferEncodingChunked.Value, "Chunked"); - Assert.True(upgradeThrew.Value); + Assert.Equal("Hello World", await response.Content.ReadAsStringAsync()); } } [Fact] public async Task OpaqueUpgrade_GetUpgrade_Success() { - ManualResetEvent waitHandle = new ManualResetEvent(false); - bool? callbackInvoked = null; - using (CreateServer(env => + using (var server = Utilities.CreateHttpServer()) { - var responseHeaders = env.Get>("owin.ResponseHeaders"); - responseHeaders["Upgrade"] = new string[] { "websocket" }; // Win8.1 blocks anything but WebSockets - OpaqueUpgrade opaqueUpgrade = env.Get("opaque.Upgrade"); - opaqueUpgrade(null, opqEnv => - { - callbackInvoked = true; - waitHandle.Set(); - return Task.FromResult(0); - }); - return Task.FromResult(0); - })) - { - using (Stream stream = await SendOpaqueRequestAsync("GET", Address)) - { - Assert.True(waitHandle.WaitOne(TimeSpan.FromSeconds(1)), "Timed out"); - Assert.True(callbackInvoked.HasValue, "CallbackInvoked not set"); - Assert.True(callbackInvoked.Value, "Callback not invoked"); - } + Task clientTask = SendOpaqueRequestAsync("GET", Address); + + var context = await server.GetContextAsync(); + Assert.True(context.IsUpgradableRequest); + context.Response.Headers["Upgrade"] = new[] { "WebSocket" }; // Win8.1 blocks anything but WebSocket + Stream serverStream = await context.UpgradeAsync(); + Assert.True(serverStream.CanRead); + Assert.True(serverStream.CanWrite); + Stream clientStream = await clientTask; + serverStream.Dispose(); + context.Dispose(); + clientStream.Dispose(); } } @@ -158,30 +80,32 @@ namespace Microsoft.Net.Server [InlineData("PUT", "Content-Length: 0")] public async Task OpaqueUpgrade_VariousMethodsUpgradeSendAndReceive_Success(string method, string extraHeader) { - using (CreateServer(env => + using (var server = Utilities.CreateHttpServer()) { - var responseHeaders = env.Get>("owin.ResponseHeaders"); - responseHeaders["Upgrade"] = new string[] { "WebSocket" }; // Win8.1 blocks anything but WebSockets - OpaqueUpgrade opaqueUpgrade = env.Get("opaque.Upgrade"); - opaqueUpgrade(null, async opqEnv => - { - Stream opaqueStream = opqEnv.Get("opaque.Stream"); + Task clientTask = SendOpaqueRequestAsync(method, Address, extraHeader); - byte[] buffer = new byte[100]; - int read = await opaqueStream.ReadAsync(buffer, 0, buffer.Length); + var context = await server.GetContextAsync(); + Assert.True(context.IsUpgradableRequest); + context.Response.Headers["Upgrade"] = new[] { "WebSocket" }; // Win8.1 blocks anything but WebSocket + Stream serverStream = await context.UpgradeAsync(); + Stream clientStream = await clientTask; - await opaqueStream.WriteAsync(buffer, 0, read); - }); - return Task.FromResult(0); - })) - { - using (Stream stream = await SendOpaqueRequestAsync(method, Address, extraHeader)) - { - byte[] data = new byte[100]; - stream.WriteAsync(data, 0, 49).Wait(); - int read = stream.ReadAsync(data, 0, data.Length).Result; - Assert.Equal(49, read); - } + byte[] clientBuffer = new byte[] { 0x00, 0x01, 0xFF, 0x00, 0x00 }; + await clientStream.WriteAsync(clientBuffer, 0, 3); + + byte[] serverBuffer = new byte[clientBuffer.Length]; + int read = await serverStream.ReadAsync(serverBuffer, 0, serverBuffer.Length); + Assert.Equal(clientBuffer, serverBuffer); + + await serverStream.WriteAsync(serverBuffer, 0, read); + + byte[] clientEchoBuffer = new byte[clientBuffer.Length]; + read = await clientStream.ReadAsync(clientEchoBuffer, 0, clientEchoBuffer.Length); + Assert.Equal(clientBuffer, clientEchoBuffer); + + serverStream.Dispose(); + context.Dispose(); + clientStream.Dispose(); } } @@ -193,54 +117,19 @@ namespace Microsoft.Net.Server [InlineData("PUT", "Transfer-Encoding: chunked")] [InlineData("CUSTOMVERB", "Content-Length: 10")] [InlineData("CUSTOMVERB", "Transfer-Encoding: chunked")] - public void OpaqueUpgrade_InvalidMethodUpgrade_Disconnected(string method, string extraHeader) + public async Task OpaqueUpgrade_InvalidMethodUpgrade_Disconnected(string method, string extraHeader) { - OpaqueUpgrade opaqueUpgrade = null; - using (CreateServer(env => + using (var server = Utilities.CreateHttpServer()) { - opaqueUpgrade = env.Get("opaque.Upgrade"); - if (opaqueUpgrade == null) - { - throw new NotImplementedException(); - } - opaqueUpgrade(null, opqEnv => Task.FromResult(0)); - return Task.FromResult(0); - })) - { - Assert.Throws(() => - { - try - { - return SendOpaqueRequestAsync(method, Address, extraHeader).Result; - } - catch (AggregateException ag) - { - throw ag.GetBaseException(); - } - }); - Assert.Null(opaqueUpgrade); + var clientTask = SendOpaqueRequestAsync(method, Address, extraHeader); + var context = await server.GetContextAsync(); + Assert.False(context.IsUpgradableRequest); + context.Dispose(); + + await Assert.ThrowsAsync(async () => await clientTask); } } - private IDisposable CreateServer(AppFunc app) - { - IDictionary properties = new Dictionary(); - IList> addresses = new List>(); - properties["host.Addresses"] = addresses; - - IDictionary address = new Dictionary(); - addresses.Add(address); - - address["scheme"] = "http"; - address["host"] = "localhost"; - address["port"] = "8080"; - address["path"] = string.Empty; - - OwinServerFactory.Initialize(properties); - - return OwinServerFactory.Create(app, properties); - } - private async Task SendRequestAsync(string uri) { using (HttpClient client = new HttpClient()) @@ -318,5 +207,4 @@ namespace Microsoft.Net.Server } } } -} -*/ \ No newline at end of file +} \ No newline at end of file