diff --git a/src/Microsoft.AspNet.Server.WebListener/MessagePump.cs b/src/Microsoft.AspNet.Server.WebListener/MessagePump.cs index 712fab8..acc5247 100644 --- a/src/Microsoft.AspNet.Server.WebListener/MessagePump.cs +++ b/src/Microsoft.AspNet.Server.WebListener/MessagePump.cs @@ -16,25 +16,28 @@ // permissions and limitations under the License. using System; +using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNet.Hosting.Server; +using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; +using Microsoft.AspNet.Server.Features; using Microsoft.Extensions.Logging; using Microsoft.Net.Http.Server; namespace Microsoft.AspNet.Server.WebListener { - using AppFunc = Func; - - internal class MessagePump : IDisposable + internal class MessagePump : IServer { private static readonly int DefaultMaxAccepts = 5 * Environment.ProcessorCount; private readonly Microsoft.Net.Http.Server.WebListener _listener; private readonly ILogger _logger; + private readonly IHttpContextFactory _httpContextFactory; - private AppFunc _appFunc; + private RequestDelegate _appFunc; private int _maxAccepts; private int _acceptorCounts; @@ -43,14 +46,19 @@ namespace Microsoft.AspNet.Server.WebListener private bool _stopping; private int _outstandingRequests; private ManualResetEvent _shutdownSignal; - - // TODO: private IDictionary _capabilities; - - internal MessagePump(Microsoft.Net.Http.Server.WebListener listener, ILoggerFactory loggerFactory) + + internal MessagePump(Microsoft.Net.Http.Server.WebListener listener, ILoggerFactory loggerFactory, IFeatureCollection features, IHttpContextFactory httpContextFactory) { + if (features == null) + { + throw new ArgumentNullException(nameof(Features)); + } + Contract.Assert(listener != null); _listener = listener; _logger = LogHelper.CreateLogger(loggerFactory, typeof(MessagePump)); + _httpContextFactory = httpContextFactory; + Features = features; _processRequest = new Action(ProcessRequestAsync); _maxAccepts = DefaultMaxAccepts; @@ -80,8 +88,23 @@ namespace Microsoft.AspNet.Server.WebListener internal bool EnableResponseCaching { get; set; } = true; - internal void Start(AppFunc app) + public IFeatureCollection Features { get; } + + public void Start(RequestDelegate app) { + if (app == null) + { + throw new ArgumentNullException(nameof(app)); + } + + var addressesFeature = Features.Get(); + if (addressesFeature == null) + { + throw new InvalidOperationException($"{nameof(IServerAddressesFeature)} is missing."); + } + + ParseAddresses(addressesFeature.Addresses, Listener); + // Can't call Start twice Contract.Assert(_appFunc == null); @@ -159,11 +182,14 @@ namespace Microsoft.AspNet.Server.WebListener SetFatalResponse(requestContext, 503); return; } + + HttpContext httpContext = null; try { Interlocked.Increment(ref _outstandingRequests); FeatureContext featureContext = new FeatureContext(requestContext, EnableResponseCaching); - await _appFunc(featureContext.Features).SupressContext(); + httpContext = _httpContextFactory.Create(featureContext.Features); + await _appFunc(httpContext).SupressContext(); requestContext.Dispose(); } catch (Exception ex) @@ -182,6 +208,10 @@ namespace Microsoft.AspNet.Server.WebListener } finally { + if (httpContext != null) + { + _httpContextFactory.Dispose(httpContext); + } if (Interlocked.Decrement(ref _outstandingRequests) == 0 && _stopping) { _shutdownSignal.Set(); @@ -202,6 +232,15 @@ namespace Microsoft.AspNet.Server.WebListener context.Dispose(); } + private void ParseAddresses(ICollection addresses, Microsoft.Net.Http.Server.WebListener listener) + { + foreach (var value in addresses) + { + listener.UrlPrefixes.Add(UrlPrefix.Create(value)); + } + } + + public void Dispose() { _stopping = true; diff --git a/src/Microsoft.AspNet.Server.WebListener/ServerFactory.cs b/src/Microsoft.AspNet.Server.WebListener/ServerFactory.cs index 7600059..4775cad 100644 --- a/src/Microsoft.AspNet.Server.WebListener/ServerFactory.cs +++ b/src/Microsoft.AspNet.Server.WebListener/ServerFactory.cs @@ -35,79 +35,44 @@ // limitations under the License. using System; -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Threading.Tasks; using Microsoft.AspNet.Hosting.Server; +using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Server.Features; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; -using Microsoft.Net.Http.Server; namespace Microsoft.AspNet.Server.WebListener { - using AppFunc = Func; - /// /// Implements the setup process for this server. /// public class ServerFactory : IServerFactory { private ILoggerFactory _loggerFactory; + private IHttpContextFactory _httpContextFactory; - public ServerFactory(ILoggerFactory loggerFactory) + public ServerFactory(ILoggerFactory loggerFactory, IHttpContextFactory httpContextFactory) { _loggerFactory = loggerFactory; + _httpContextFactory = httpContextFactory; } /// /// Creates a configurable instance of the server. /// - /// + /// + /// The server. Invoke Dispose to shut down. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposed by caller")] - public IFeatureCollection Initialize(IConfiguration configuration) + public IServer CreateServer(IConfiguration configuration) { Microsoft.Net.Http.Server.WebListener listener = new Microsoft.Net.Http.Server.WebListener(_loggerFactory); var serverFeatures = new FeatureCollection(); serverFeatures.Set(listener); - serverFeatures.Set(new MessagePump(listener, _loggerFactory)); serverFeatures.Set(SplitAddresses(configuration)); - return serverFeatures; - } - /// - /// - /// The per-request application entry point. - /// The value returned - /// The server. Invoke Dispose to shut down. - public IDisposable Start(IFeatureCollection serverFeatures, AppFunc app) - { - if (serverFeatures == null) - { - throw new ArgumentNullException("serverFeatures"); - } - if (app == null) - { - throw new ArgumentNullException("app"); - } - - var messagePump = serverFeatures.Get(); - if (messagePump == null) - { - throw new InvalidOperationException("messagePump"); - } - - var addressesFeature = serverFeatures.Get(); - if (addressesFeature == null) - { - throw new InvalidOperationException("IServerAddressesFeature"); - } - - ParseAddresses(addressesFeature.Addresses, messagePump.Listener); - - messagePump.Start(app); - return messagePump; + return new MessagePump(listener, _loggerFactory, serverFeatures, _httpContextFactory); } private IServerAddressesFeature SplitAddresses(IConfiguration config) @@ -123,13 +88,5 @@ namespace Microsoft.AspNet.Server.WebListener } return addressesFeature; } - - private void ParseAddresses(ICollection addresses, Microsoft.Net.Http.Server.WebListener listener) - { - foreach (var value in addresses) - { - listener.UrlPrefixes.Add(UrlPrefix.Create(value)); - } - } } } diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/AuthenticationTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/AuthenticationTests.cs index b2ab2bf..89dac6c 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/AuthenticationTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/AuthenticationTests.cs @@ -40,11 +40,11 @@ namespace Microsoft.AspNet.Server.WebListener public async Task AuthTypes_AllowAnonymous_NoChallenge(AuthenticationSchemes authType) { string address; - using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, env => + using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.False(context.User.Identity.IsAuthenticated); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.False(httpContext.User.Identity.IsAuthenticated); return Task.FromResult(0); })) { @@ -62,7 +62,7 @@ namespace Microsoft.AspNet.Server.WebListener public async Task AuthType_RequireAuth_ChallengesAdded(AuthenticationSchemes authType) { string address; - using (Utilities.CreateHttpAuthServer(authType, out address, env => + using (Utilities.CreateHttpAuthServer(authType, out address, httpContext => { throw new NotImplementedException(); })) @@ -81,12 +81,12 @@ namespace Microsoft.AspNet.Server.WebListener public async Task AuthType_AllowAnonymousButSpecify401_ChallengesAdded(AuthenticationSchemes authType) { string address; - using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, env => + using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.False(context.User.Identity.IsAuthenticated); - context.Response.StatusCode = 401; + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.False(httpContext.User.Identity.IsAuthenticated); + httpContext.Response.StatusCode = 401; return Task.FromResult(0); })) { @@ -107,12 +107,12 @@ namespace Microsoft.AspNet.Server.WebListener | AuthenticationSchemes.Basic | AuthenticationSchemes.AllowAnonymous, out address, - env => + httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.False(context.User.Identity.IsAuthenticated); - context.Response.StatusCode = 401; + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.False(httpContext.User.Identity.IsAuthenticated); + httpContext.Response.StatusCode = 401; return Task.FromResult(0); })) { @@ -132,18 +132,18 @@ namespace Microsoft.AspNet.Server.WebListener { string address; int requestId = 0; - using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, env => + using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); if (requestId == 0) { - Assert.False(context.User.Identity.IsAuthenticated); - context.Response.StatusCode = 401; + Assert.False(httpContext.User.Identity.IsAuthenticated); + httpContext.Response.StatusCode = 401; } else if (requestId == 1) { - Assert.True(context.User.Identity.IsAuthenticated); + Assert.True(httpContext.User.Identity.IsAuthenticated); } else { @@ -167,11 +167,11 @@ namespace Microsoft.AspNet.Server.WebListener public async Task AuthTypes_RequireAuth_Success(AuthenticationSchemes authType) { string address; - using (Utilities.CreateHttpAuthServer(authType, out address, env => + using (Utilities.CreateHttpAuthServer(authType, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.True(context.User.Identity.IsAuthenticated); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.True(httpContext.User.Identity.IsAuthenticated); return Task.FromResult(0); })) { @@ -189,10 +189,9 @@ namespace Microsoft.AspNet.Server.WebListener public async Task AuthTypes_GetSingleDescriptions(AuthenticationSchemes authType) { string address; - using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, env => + using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - var resultList = context.Authentication.GetAuthenticationSchemes(); + var resultList = httpContext.Authentication.GetAuthenticationSchemes(); if (authType == AuthenticationSchemes.AllowAnonymous) { Assert.Equal(0, resultList.Count()); @@ -223,10 +222,9 @@ namespace Microsoft.AspNet.Server.WebListener | AuthenticationSchemes.NTLM | /*AuthenticationSchemes.Digest |*/ AuthenticationSchemes.Basic; - using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, env => + using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - var resultList = context.Authentication.GetAuthenticationSchemes(); + var resultList = httpContext.Authentication.GetAuthenticationSchemes(); Assert.Equal(3, resultList.Count()); return Task.FromResult(0); })) @@ -247,14 +245,14 @@ namespace Microsoft.AspNet.Server.WebListener { string address; var authTypeList = authType.ToString().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); - using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, async env => + using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, async httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.False(context.User.Identity.IsAuthenticated); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.False(httpContext.User.Identity.IsAuthenticated); foreach (var scheme in authTypeList) { - var authResults = await context.Authentication.AuthenticateAsync(scheme); + var authResults = await httpContext.Authentication.AuthenticateAsync(scheme); Assert.Null(authResults); } })) @@ -275,15 +273,15 @@ namespace Microsoft.AspNet.Server.WebListener { string address; var authTypeList = authType.ToString().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); - using (Utilities.CreateHttpAuthServer(authType, out address, async env => + using (Utilities.CreateHttpAuthServer(authType, out address, async httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.True(context.User.Identity.IsAuthenticated); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.True(httpContext.User.Identity.IsAuthenticated); var count = 0; foreach (var scheme in authTypeList) { - var authResults = await context.Authentication.AuthenticateAsync(scheme); + var authResults = await httpContext.Authentication.AuthenticateAsync(scheme); if (authResults != null) { count++; @@ -307,12 +305,12 @@ namespace Microsoft.AspNet.Server.WebListener { string address; var authTypeList = authType.ToString().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); - using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, env => + using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.False(context.User.Identity.IsAuthenticated); - return context.Authentication.ChallengeAsync(); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.False(httpContext.User.Identity.IsAuthenticated); + return httpContext.Authentication.ChallengeAsync(); })) { var response = await SendRequestAsync(address); @@ -331,14 +329,14 @@ namespace Microsoft.AspNet.Server.WebListener { string address; var authTypeList = authType.ToString().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); - using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, async env => + using (Utilities.CreateHttpAuthServer(authType | AuthenticationSchemes.AllowAnonymous, out address, async httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.False(context.User.Identity.IsAuthenticated); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.False(httpContext.User.Identity.IsAuthenticated); foreach (var scheme in authTypeList) { - await context.Authentication.ChallengeAsync(scheme); + await httpContext.Authentication.ChallengeAsync(scheme); } })) { @@ -357,12 +355,12 @@ namespace Microsoft.AspNet.Server.WebListener { string address; var authTypes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM | /*AuthenticationSchemes.Digest |*/ AuthenticationSchemes.Basic; - using (Utilities.CreateHttpAuthServer(authTypes | AuthenticationSchemes.AllowAnonymous, out address, env => + using (Utilities.CreateHttpAuthServer(authTypes | AuthenticationSchemes.AllowAnonymous, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.False(context.User.Identity.IsAuthenticated); - return context.Authentication.ChallengeAsync(authType.ToString()); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.False(httpContext.User.Identity.IsAuthenticated); + return httpContext.Authentication.ChallengeAsync(authType.ToString()); })) { var response = await SendRequestAsync(address); @@ -383,12 +381,12 @@ namespace Microsoft.AspNet.Server.WebListener var authTypes = AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM | /*AuthenticationSchemes.Digest |*/ AuthenticationSchemes.Basic; authTypes = authTypes & ~authType; var authTypeList = authType.ToString().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries); - using (Utilities.CreateHttpAuthServer(authTypes | AuthenticationSchemes.AllowAnonymous, out address, env => + using (Utilities.CreateHttpAuthServer(authTypes | AuthenticationSchemes.AllowAnonymous, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.False(context.User.Identity.IsAuthenticated); - return Assert.ThrowsAsync(() => context.Authentication.ChallengeAsync(authType.ToString())); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.False(httpContext.User.Identity.IsAuthenticated); + return Assert.ThrowsAsync(() => httpContext.Authentication.ChallengeAsync(authType.ToString())); })) { var response = await SendRequestAsync(address); @@ -406,12 +404,12 @@ namespace Microsoft.AspNet.Server.WebListener { string address; var authTypes = AuthenticationSchemes.AllowAnonymous | AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM | /*AuthenticationSchemes.Digest |*/ AuthenticationSchemes.Basic; - using (Utilities.CreateHttpAuthServer(authTypes, out address, env => + using (Utilities.CreateHttpAuthServer(authTypes, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.False(context.User.Identity.IsAuthenticated); - return context.Authentication.ForbidAsync(authType.ToString()); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.False(httpContext.User.Identity.IsAuthenticated); + return httpContext.Authentication.ForbidAsync(authType.ToString()); })) { var response = await SendRequestAsync(address); @@ -428,12 +426,12 @@ namespace Microsoft.AspNet.Server.WebListener public async Task AuthTypes_ChallengeAuthenticatedAuthType_Forbidden(AuthenticationSchemes authType) { string address; - using (Utilities.CreateHttpAuthServer(authType, out address, env => + using (Utilities.CreateHttpAuthServer(authType, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.True(context.User.Identity.IsAuthenticated); - return context.Authentication.ChallengeAsync(authType.ToString()); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.True(httpContext.User.Identity.IsAuthenticated); + return httpContext.Authentication.ChallengeAsync(authType.ToString()); })) { var response = await SendRequestAsync(address, useDefaultCredentials: true); @@ -451,12 +449,12 @@ namespace Microsoft.AspNet.Server.WebListener public async Task AuthTypes_ChallengeAuthenticatedAuthTypeWithEmptyChallenge_Forbidden(AuthenticationSchemes authType) { string address; - using (Utilities.CreateHttpAuthServer(authType, out address, env => + using (Utilities.CreateHttpAuthServer(authType, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.True(context.User.Identity.IsAuthenticated); - return context.Authentication.ChallengeAsync(); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.True(httpContext.User.Identity.IsAuthenticated); + return httpContext.Authentication.ChallengeAsync(); })) { var response = await SendRequestAsync(address, useDefaultCredentials: true); @@ -474,12 +472,12 @@ namespace Microsoft.AspNet.Server.WebListener public async Task AuthTypes_UnathorizedAuthenticatedAuthType_Unauthorized(AuthenticationSchemes authType) { string address; - using (Utilities.CreateHttpAuthServer(authType, out address, env => + using (Utilities.CreateHttpAuthServer(authType, out address, httpContext => { - var context = new DefaultHttpContext((IFeatureCollection)env); - Assert.NotNull(context.User); - Assert.True(context.User.Identity.IsAuthenticated); - return context.Authentication.ChallengeAsync(authType.ToString(), null, ChallengeBehavior.Unauthorized); + Assert.NotNull(httpContext.User); + Assert.NotNull(httpContext.User.Identity); + Assert.True(httpContext.User.Identity.IsAuthenticated); + return httpContext.Authentication.ChallengeAsync(authType.ToString(), null, ChallengeBehavior.Unauthorized); })) { var response = await SendRequestAsync(address, useDefaultCredentials: true); diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/HttpsTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/HttpsTests.cs index 4ac233d..8a45957 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/HttpsTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/HttpsTests.cs @@ -34,7 +34,7 @@ namespace Microsoft.AspNet.Server.WebListener [Fact(Skip = "TODO: Add trait filtering support so these SSL tests don't get run on teamcity or the command line."), Trait("scheme", "https")] public async Task Https_200OK_Success() { - using (Utilities.CreateHttpsServer(env => + using (Utilities.CreateHttpsServer(httpContext => { return Task.FromResult(0); })) @@ -47,9 +47,8 @@ namespace Microsoft.AspNet.Server.WebListener [Fact(Skip = "TODO: Add trait filtering support so these SSL tests don't get run on teamcity or the command line."), Trait("scheme", "https")] public async Task Https_SendHelloWorld_Success() { - using (Utilities.CreateHttpsServer(env => + using (Utilities.CreateHttpsServer(httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] body = Encoding.UTF8.GetBytes("Hello World"); httpContext.Response.ContentLength = body.Length; return httpContext.Response.Body.WriteAsync(body, 0, body.Length); @@ -63,9 +62,8 @@ namespace Microsoft.AspNet.Server.WebListener [Fact(Skip = "TODO: Add trait filtering support so these SSL tests don't get run on teamcity or the command line."), Trait("scheme", "https")] public async Task Https_EchoHelloWorld_Success() { - using (Utilities.CreateHttpsServer(env => + using (Utilities.CreateHttpsServer(httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); string input = new StreamReader(httpContext.Request.Body).ReadToEnd(); Assert.Equal("Hello World", input); byte[] body = Encoding.UTF8.GetBytes("Hello World"); @@ -82,9 +80,8 @@ namespace Microsoft.AspNet.Server.WebListener [Fact(Skip = "TODO: Add trait filtering support so these SSL tests don't get run on teamcity or the command line."), Trait("scheme", "https")] public async Task Https_ClientCertNotSent_ClientCertNotPresent() { - using (Utilities.CreateHttpsServer(async env => + using (Utilities.CreateHttpsServer(async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var tls = httpContext.Features.Get(); Assert.NotNull(tls); var cert = await tls.GetClientCertificateAsync(CancellationToken.None); @@ -100,9 +97,8 @@ namespace Microsoft.AspNet.Server.WebListener [Fact(Skip = "TODO: Add trait filtering support so these SSL tests don't get run on teamcity or the command line."), Trait("scheme", "https")] public async Task Https_ClientCertRequested_ClientCertPresent() { - using (Utilities.CreateHttpsServer(async env => + using (Utilities.CreateHttpsServer(async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var tls = httpContext.Features.Get(); Assert.NotNull(tls); var cert = await tls.GetClientCertificateAsync(CancellationToken.None); diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/OpaqueUpgradeTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/OpaqueUpgradeTests.cs index 5b72b73..4949bd1 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/OpaqueUpgradeTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/OpaqueUpgradeTests.cs @@ -37,9 +37,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task OpaqueUpgrade_SupportKeys_Present() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); try { var opaqueFeature = httpContext.Features.Get(); @@ -66,9 +65,8 @@ namespace Microsoft.AspNet.Server.WebListener { bool? upgradeThrew = null; string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); await httpContext.Response.WriteAsync("Hello World"); await httpContext.Response.Body.FlushAsync(); try @@ -98,9 +96,8 @@ namespace Microsoft.AspNet.Server.WebListener ManualResetEvent waitHandle = new ManualResetEvent(false); bool? upgraded = null; string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Headers["Upgrade"] = "websocket"; // Win8.1 blocks anything but WebSockets var opaqueFeature = httpContext.Features.Get(); Assert.NotNull(opaqueFeature); @@ -146,9 +143,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task OpaqueUpgrade_VariousMethodsUpgradeSendAndReceive_Success(string method, string extraHeader) { string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); try { httpContext.Response.Headers["Upgrade"] = "websocket"; // Win8.1 blocks anything but WebSockets @@ -190,9 +186,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task OpaqueUpgrade_InvalidMethodUpgrade_Disconnected(string method, string extraHeader) { string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); try { var opaqueFeature = httpContext.Features.Get(); diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestBodyTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestBodyTests.cs index c5edcdf..b176394 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestBodyTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestBodyTests.cs @@ -35,9 +35,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task RequestBody_ReadSync_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] input = new byte[100]; int read = httpContext.Request.Body.Read(input, 0, input.Length); httpContext.Response.ContentLength = read; @@ -54,9 +53,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task RequestBody_ReadAync_Success() { string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] input = new byte[100]; int read = await httpContext.Request.Body.ReadAsync(input, 0, input.Length); httpContext.Response.ContentLength = read; @@ -72,9 +70,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task RequestBody_ReadBeginEnd_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] input = new byte[100]; int read = httpContext.Request.Body.EndRead(httpContext.Request.Body.BeginRead(input, 0, input.Length, null, null)); httpContext.Response.ContentLength = read; @@ -92,9 +89,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task RequestBody_InvalidBuffer_ArgumentException() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] input = new byte[100]; Assert.Throws("buffer", () => httpContext.Request.Body.Read(null, 0, 1)); Assert.Throws("offset", () => httpContext.Request.Body.Read(input, -1, 1)); @@ -116,9 +112,8 @@ namespace Microsoft.AspNet.Server.WebListener { StaggardContent content = new StaggardContent(); string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] input = new byte[10]; int read = httpContext.Request.Body.Read(input, 0, input.Length); Assert.Equal(5, read); @@ -138,9 +133,8 @@ namespace Microsoft.AspNet.Server.WebListener { StaggardContent content = new StaggardContent(); string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] input = new byte[10]; int read = await httpContext.Request.Body.ReadAsync(input, 0, input.Length); Assert.Equal(5, read); @@ -158,9 +152,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task RequestBody_PostWithImidateBody_Success() { string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] input = new byte[11]; int read = await httpContext.Request.Body.ReadAsync(input, 0, input.Length); Assert.Equal(10, read); diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestHeaderTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestHeaderTests.cs index d05ca0b..f4fd3c3 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestHeaderTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestHeaderTests.cs @@ -33,9 +33,9 @@ namespace Microsoft.AspNet.Server.WebListener public async Task RequestHeaders_ClientSendsDefaultHeaders_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var requestHeaders = new DefaultHttpContext((IFeatureCollection)env).Request.Headers; + var requestHeaders = httpContext.Request.Headers; // NOTE: The System.Net client only sends the Connection: keep-alive header on the first connection per service-point. // Assert.Equal(2, requestHeaders.Count); // Assert.Equal("Keep-Alive", requestHeaders.Get("Connection")); @@ -53,9 +53,9 @@ namespace Microsoft.AspNet.Server.WebListener public async Task RequestHeaders_ClientSendsCustomHeaders_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var requestHeaders = new DefaultHttpContext((IFeatureCollection)env).Request.Headers; + var requestHeaders = httpContext.Request.Headers; Assert.Equal(4, requestHeaders.Count); Assert.False(StringValues.IsNullOrEmpty(requestHeaders["Host"])); Assert.Equal("close", requestHeaders["Connection"]); diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestTests.cs index 0ebbfb6..424f536 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/RequestTests.cs @@ -20,6 +20,9 @@ using System.IO; using System.Net.Http; using System.Text; using System.Threading.Tasks; +using Microsoft.AspNet.Hosting.Builder; +using Microsoft.AspNet.Hosting.Server; +using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Internal; using Microsoft.Net.Http.Server; @@ -27,17 +30,14 @@ using Xunit; namespace Microsoft.AspNet.Server.WebListener { - using AppFunc = Func; - public class RequestTests { [Fact] public async Task Request_SimpleGet_Success() { string root; - using (Utilities.CreateHttpServerReturnRoot("/basepath", out root, env => + using (Utilities.CreateHttpServerReturnRoot("/basepath", out root, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); try { var requestInfo = httpContext.Features.Get(); @@ -53,7 +53,7 @@ namespace Microsoft.AspNet.Server.WebListener Assert.Equal("HTTP/1.1", requestInfo.Protocol); // Server Keys - // TODO: Assert.NotNull(env.Get>("server.Capabilities")); + // TODO: Assert.NotNull(httpContext.Get>("server.Capabilities")); var connectionInfo = httpContext.Features.Get(); Assert.Equal("::1", connectionInfo.RemoteIpAddress.ToString()); @@ -92,9 +92,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Request_PathSplitting(string pathBase, string requestPath, string expectedPathBase, string expectedPath) { string root; - using (Utilities.CreateHttpServerReturnRoot(pathBase, out root, env => + using (Utilities.CreateHttpServerReturnRoot(pathBase, out root, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); try { var requestInfo = httpContext.Features.Get(); @@ -140,9 +139,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Request_MultiplePrefixes(string requestPath, string expectedPathBase, string expectedPath) { string root; - using (CreateServer(out root, env => + using (CreateServer(out root, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var requestInfo = httpContext.Features.Get(); var requestIdentifierFeature = httpContext.Features.Get(); try @@ -167,22 +165,23 @@ namespace Microsoft.AspNet.Server.WebListener } } - private IDisposable CreateServer(out string root, AppFunc app) + private IServer CreateServer(out string root, RequestDelegate app) { // TODO: We're just doing this to get a dynamic port. This can be removed later when we add support for hot-adding prefixes. - var server = Utilities.CreateHttpServerReturnRoot("/", out root, app); - server.Dispose(); + var dynamicServer = Utilities.CreateHttpServerReturnRoot("/", out root, app); + dynamicServer.Dispose(); var rootUri = new Uri(root); - var factory = new ServerFactory(loggerFactory: null); - var serverFeatures = factory.Initialize(configuration: null); - var listener = serverFeatures.Get(); + var factory = new ServerFactory(loggerFactory: null, httpContextFactory: new HttpContextFactory(new HttpContextAccessor())); + var server = factory.CreateServer(configuration: null); + var listener = server.Features.Get(); foreach (string path in new[] { "/", "/11", "/2/3", "/2", "/11/2" }) { listener.UrlPrefixes.Add(UrlPrefix.Create(rootUri.Scheme, rootUri.Host, rootUri.Port, path)); } - return factory.Start(serverFeatures, app); + server.Start(app); + return server; } private async Task SendRequestAsync(string uri) diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseBodyTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseBodyTests.cs index af31ca7..2b5800f 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseBodyTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseBodyTests.cs @@ -35,9 +35,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseBody_WriteNoHeaders_BuffersAndSetsContentLength() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Body.Write(new byte[10], 0, 10); return httpContext.Response.Body.WriteAsync(new byte[10], 0, 10); })) @@ -56,9 +55,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseBody_WriteNoHeadersAndFlush_DefaultsToChunked() { string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Body.Write(new byte[10], 0, 10); await httpContext.Response.Body.WriteAsync(new byte[10], 0, 10); await httpContext.Response.Body.FlushAsync(); @@ -78,9 +76,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseBody_WriteChunked_ManuallyChunked() { string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); 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"); @@ -101,9 +98,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseBody_WriteContentLength_PassedThrough() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Headers["Content-lenGth"] = " 30 "; Stream stream = httpContext.Response.Body; stream.EndWrite(stream.BeginWrite(new byte[10], 0, 10, null, null)); @@ -126,9 +122,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseBody_WriteContentLengthNoneWritten_Throws() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Headers["Content-lenGth"] = " 20 "; return Task.FromResult(0); })) @@ -141,9 +136,8 @@ namespace Microsoft.AspNet.Server.WebListener public void ResponseBody_WriteContentLengthNotEnoughWritten_Throws() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Headers["Content-lenGth"] = " 20 "; httpContext.Response.Body.Write(new byte[5], 0, 5); return Task.FromResult(0); @@ -157,9 +151,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseBody_WriteContentLengthTooMuchWritten_Throws() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Headers["Content-lenGth"] = " 10 "; httpContext.Response.Body.Write(new byte[5], 0, 5); httpContext.Response.Body.Write(new byte[6], 0, 6); @@ -177,11 +170,10 @@ namespace Microsoft.AspNet.Server.WebListener ManualResetEvent waitHandle = new ManualResetEvent(false); bool? appThrew = null; string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { try { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Headers["Content-lenGth"] = " 10 "; httpContext.Response.Body.Write(new byte[10], 0, 10); httpContext.Response.Body.Write(new byte[9], 0, 9); diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseCachingTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseCachingTests.cs index 6378a7e..0c71360 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseCachingTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseCachingTests.cs @@ -17,9 +17,8 @@ namespace Microsoft.AspNet.Server.WebListener.FunctionalTests { var requestCount = 1; string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentType = "some/thing"; // Http.Sys requires content-type for caching httpContext.Response.Headers["x-request-count"] = (requestCount++).ToString(); return httpContext.Response.Body.WriteAsync(new byte[10], 0, 10); @@ -35,9 +34,8 @@ namespace Microsoft.AspNet.Server.WebListener.FunctionalTests { var requestCount = 1; string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentType = "some/thing"; // Http.Sys requires content-type for caching httpContext.Response.Headers["x-request-count"] = (requestCount++).ToString(); httpContext.Response.Headers["Cache-Control"] = "public"; @@ -54,9 +52,8 @@ namespace Microsoft.AspNet.Server.WebListener.FunctionalTests { var requestCount = 1; string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentType = "some/thing"; // Http.Sys requires content-type for caching httpContext.Response.Headers["x-request-count"] = (requestCount++).ToString(); httpContext.Response.Headers["Cache-Control"] = "public, max-age=10"; @@ -73,9 +70,8 @@ namespace Microsoft.AspNet.Server.WebListener.FunctionalTests { var requestCount = 1; string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentType = "some/thing"; // Http.Sys requires content-type for caching httpContext.Response.Headers["x-request-count"] = (requestCount++).ToString(); httpContext.Response.Headers["Cache-Control"] = "public, s-maxage=10"; @@ -92,9 +88,8 @@ namespace Microsoft.AspNet.Server.WebListener.FunctionalTests { var requestCount = 1; string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentType = "some/thing"; // Http.Sys requires content-type for caching httpContext.Response.Headers["x-request-count"] = (requestCount++).ToString(); httpContext.Response.Headers["Cache-Control"] = "public, max-age=0, s-maxage=10"; @@ -111,9 +106,8 @@ namespace Microsoft.AspNet.Server.WebListener.FunctionalTests { var requestCount = 1; string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentType = "some/thing"; // Http.Sys requires content-type for caching httpContext.Response.Headers["x-request-count"] = (requestCount++).ToString(); httpContext.Response.Headers["Cache-Control"] = "public"; @@ -134,9 +128,8 @@ namespace Microsoft.AspNet.Server.WebListener.FunctionalTests { var requestCount = 1; string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentType = "some/thing"; // Http.Sys requires content-type for caching httpContext.Response.Headers["x-request-count"] = (requestCount++).ToString(); httpContext.Response.Headers["Cache-Control"] = "public, max-age=10"; @@ -156,9 +149,8 @@ namespace Microsoft.AspNet.Server.WebListener.FunctionalTests { var requestCount = 1; string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentType = "some/thing"; // Http.Sys requires content-type for caching httpContext.Response.Headers["x-request-count"] = (requestCount++).ToString(); httpContext.Response.Headers["Cache-Control"] = "public"; @@ -176,9 +168,8 @@ namespace Microsoft.AspNet.Server.WebListener.FunctionalTests { var requestCount = 1; string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentType = "some/thing"; // Http.Sys requires content-type for caching httpContext.Response.Headers["x-request-count"] = (requestCount++).ToString(); httpContext.Response.Headers["Expires"] = (DateTime.UtcNow + TimeSpan.FromSeconds(10)).ToString("r"); @@ -195,9 +186,8 @@ namespace Microsoft.AspNet.Server.WebListener.FunctionalTests { var requestCount = 1; string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentType = "some/thing"; // Http.Sys requires content-type for caching httpContext.Response.Headers["x-request-count"] = (requestCount++).ToString(); httpContext.Response.Headers["Cache-Control"] = "public, max-age=10"; diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseHeaderTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseHeaderTests.cs index f0db3b7..bd8bb9c 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseHeaderTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseHeaderTests.cs @@ -34,7 +34,7 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseHeaders_ServerSendsDefaultHeaders_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { return Task.FromResult(0); })) @@ -54,9 +54,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseHeaders_ServerSendsSingleValueKnownHeaders_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var responseInfo = httpContext.Features.Get(); var responseHeaders = responseInfo.Headers; responseHeaders["WWW-Authenticate"] = new string[] { "custom1" }; @@ -79,9 +78,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseHeaders_ServerSendsMultiValueKnownHeaders_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var responseInfo = httpContext.Features.Get(); var responseHeaders = responseInfo.Headers; responseHeaders["WWW-Authenticate"] = new string[] { "custom1, and custom2", "custom3" }; @@ -104,9 +102,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseHeaders_ServerSendsCustomHeaders_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var responseInfo = httpContext.Features.Get(); var responseHeaders = responseInfo.Headers; responseHeaders["Custom-Header1"] = new string[] { "custom1, and custom2", "custom3" }; @@ -129,9 +126,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseHeaders_ServerSendsConnectionClose_Closed() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var responseInfo = httpContext.Features.Get(); var responseHeaders = responseInfo.Headers; responseHeaders["Connection"] = new string[] { "Close" }; @@ -154,7 +150,7 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseHeaders_HTTP10Request_Gets11Close() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { return Task.FromResult(0); })) @@ -177,9 +173,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseHeaders_HTTP10RequestWithChunkedHeader_ManualChunking() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var responseInfo = httpContext.Features.Get(); var responseHeaders = responseInfo.Headers; responseHeaders["Transfer-Encoding"] = new string[] { "chunked" }; @@ -207,9 +202,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Headers_FlushSendsHeaders_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var responseInfo = httpContext.Features.Get(); var responseHeaders = responseInfo.Headers; responseHeaders.Add("Custom1", new string[] { "value1a", "value1b" }); @@ -239,9 +233,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Headers_FlushAsyncSendsHeaders_Success() { string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var responseInfo = httpContext.Features.Get(); var responseHeaders = responseInfo.Headers; responseHeaders.Add("Custom1", new string[] { "value1a", "value1b" }); diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseSendFileTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseSendFileTests.cs index c52ffc8..751d4d0 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseSendFileTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseSendFileTests.cs @@ -47,13 +47,12 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseSendFile_SupportKeys_Present() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); try { /* TODO: - IDictionary capabilities = env.Get>("server.Capabilities"); + IDictionary capabilities = httpContext.Get>("server.Capabilities"); Assert.NotNull(capabilities); Assert.Equal("1.0", capabilities.Get("sendfile.Version")); @@ -91,9 +90,8 @@ namespace Microsoft.AspNet.Server.WebListener ManualResetEvent waitHandle = new ManualResetEvent(false); bool? appThrew = null; string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.Features.Get(); try { @@ -124,9 +122,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseSendFile_NoHeaders_DefaultsToChunked() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.Features.Get(); return sendFile.SendFileAsync(AbsoluteFilePath, 0, null, CancellationToken.None); })) @@ -144,9 +141,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseSendFile_RelativeFile_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.Features.Get(); return sendFile.SendFileAsync(RelativeFilePath, 0, null, CancellationToken.None); })) @@ -164,9 +160,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseSendFile_Unspecified_Chunked() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.Features.Get(); return sendFile.SendFileAsync(AbsoluteFilePath, 0, null, CancellationToken.None); })) @@ -184,9 +179,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseSendFile_MultipleWrites_Chunked() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.Features.Get(); sendFile.SendFileAsync(AbsoluteFilePath, 0, null, CancellationToken.None).Wait(); return sendFile.SendFileAsync(AbsoluteFilePath, 0, null, CancellationToken.None); @@ -205,9 +199,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseSendFile_HalfOfFile_Chunked() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.Features.Get(); return sendFile.SendFileAsync(AbsoluteFilePath, 0, FileLength / 2, CancellationToken.None); })) @@ -225,9 +218,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseSendFile_OffsetOutOfRange_Throws() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.Features.Get(); return sendFile.SendFileAsync(AbsoluteFilePath, 1234567, null, CancellationToken.None); })) @@ -241,9 +233,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseSendFile_CountOutOfRange_Throws() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.Features.Get(); return sendFile.SendFileAsync(AbsoluteFilePath, 0, 1234567, CancellationToken.None); })) @@ -257,9 +248,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseSendFile_Count0_Chunked() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.Features.Get(); return sendFile.SendFileAsync(AbsoluteFilePath, 0, 0, CancellationToken.None); })) @@ -277,9 +267,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseSendFile_ContentLength_PassedThrough() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.Features.Get(); httpContext.Response.Headers["Content-lenGth"] = FileLength.ToString(); return sendFile.SendFileAsync(AbsoluteFilePath, 0, null, CancellationToken.None); @@ -299,9 +288,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseSendFile_ContentLengthSpecific_PassedThrough() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.Features.Get(); httpContext.Response.Headers["Content-lenGth"] = "10"; return sendFile.SendFileAsync(AbsoluteFilePath, 0, 10, CancellationToken.None); @@ -321,9 +309,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task ResponseSendFile_ContentLength0_PassedThrough() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.Features.Get(); httpContext.Response.Headers["Content-lenGth"] = "0"; return sendFile.SendFileAsync(AbsoluteFilePath, 0, 0, CancellationToken.None); diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseTests.cs index 40ed946..516b2dc 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ResponseTests.cs @@ -31,9 +31,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Response_ServerSendsDefaultResponse_ServerProvidesStatusCodeAndReasonPhrase() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); Assert.Equal(200, httpContext.Response.StatusCode); Assert.False(httpContext.Response.HasStarted); return Task.FromResult(0); @@ -51,11 +50,10 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Response_ServerSendsSpecificStatus_ServerProvidesReasonPhrase() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.StatusCode = 201; - // TODO: env["owin.ResponseProtocol"] = "HTTP/1.0"; // Http.Sys ignores this value + // TODO: httpContext["owin.ResponseProtocol"] = "HTTP/1.0"; // Http.Sys ignores this value return Task.FromResult(0); })) { @@ -71,12 +69,11 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Response_ServerSendsSpecificStatusAndReasonPhrase_PassedThrough() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.StatusCode = 201; httpContext.Features.Get().ReasonPhrase = "CustomReasonPhrase"; // TODO? - // TODO: env["owin.ResponseProtocol"] = "HTTP/1.0"; // Http.Sys ignores this value + // TODO: httpContext["owin.ResponseProtocol"] = "HTTP/1.0"; // Http.Sys ignores this value return Task.FromResult(0); })) { @@ -92,9 +89,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Response_ServerSendsCustomStatus_NoReasonPhrase() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.StatusCode = 901; return Task.FromResult(0); })) @@ -110,9 +106,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Response_100_Throws() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.StatusCode = 100; return Task.FromResult(0); })) @@ -126,9 +121,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Response_0_Throws() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.StatusCode = 0; return Task.FromResult(0); })) diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ServerTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ServerTests.cs index b787d5e..8912776 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ServerTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/ServerTests.cs @@ -24,6 +24,7 @@ using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; +using Microsoft.AspNet.Hosting.Builder; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Internal; @@ -38,7 +39,7 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Server_200OK_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { return Task.FromResult(0); })) @@ -52,9 +53,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Server_SendHelloWorld_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentLength = 11; return httpContext.Response.WriteAsync("Hello World"); })) @@ -68,9 +68,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task Server_EchoHelloWorld_Success() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); string input = new StreamReader(httpContext.Request.Body).ReadToEnd(); Assert.Equal("Hello World", input); httpContext.Response.ContentLength = 11; @@ -88,10 +87,9 @@ namespace Microsoft.AspNet.Server.WebListener Task responseTask; ManualResetEvent received = new ManualResetEvent(false); string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { received.Set(); - var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentLength = 11; return httpContext.Response.WriteAsync("Hello World"); })) @@ -107,7 +105,7 @@ namespace Microsoft.AspNet.Server.WebListener public void Server_AppException_ClientReset() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { throw new InvalidOperationException(); })) @@ -129,7 +127,7 @@ namespace Microsoft.AspNet.Server.WebListener TaskCompletionSource tcs = new TaskCompletionSource(); string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { if (Interlocked.Increment(ref requestCount) == requestLimit) { @@ -162,7 +160,7 @@ namespace Microsoft.AspNet.Server.WebListener TaskCompletionSource tcs = new TaskCompletionSource(); string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { if (Interlocked.Increment(ref requestCount) == requestLimit) { @@ -193,9 +191,8 @@ namespace Microsoft.AspNet.Server.WebListener ManualResetEvent canceled = new ManualResetEvent(false); string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); CancellationToken ct = httpContext.RequestAborted; Assert.True(ct.CanBeCanceled, "CanBeCanceled"); Assert.False(ct.IsCancellationRequested, "IsCancellationRequested"); @@ -228,9 +225,8 @@ namespace Microsoft.AspNet.Server.WebListener ManualResetEvent canceled = new ManualResetEvent(false); string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); CancellationToken ct = httpContext.RequestAborted; Assert.True(ct.CanBeCanceled, "CanBeCanceled"); Assert.False(ct.IsCancellationRequested, "IsCancellationRequested"); @@ -255,16 +251,17 @@ namespace Microsoft.AspNet.Server.WebListener { // This is just to get a dynamic port string address; - using (Utilities.CreateHttpServer(out address, env => Task.FromResult(0))) { } + using (Utilities.CreateHttpServer(out address, httpContext => Task.FromResult(0))) { } - var factory = new ServerFactory(loggerFactory: null); - var serverFeatures = factory.Initialize(configuration: null); - var listener = serverFeatures.Get(); + var factory = new ServerFactory(loggerFactory: null, httpContextFactory: new HttpContextFactory(new HttpContextAccessor())); + var server = factory.CreateServer(configuration: null); + var listener = server.Features.Get(); listener.UrlPrefixes.Add(UrlPrefix.Create(address)); listener.SetRequestQueueLimit(1001); - using (factory.Start(serverFeatures, env => Task.FromResult(0))) + using (server) { + server.Start(httpContext => Task.FromResult(0)); string response = await SendRequestAsync(address); Assert.Equal(string.Empty, response); } diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/Utilities.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/Utilities.cs index 899c18b..31eb61e 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/Utilities.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/Utilities.cs @@ -16,43 +16,44 @@ // permissions and limitations under the License. using System; -using System.Threading.Tasks; +using Microsoft.AspNet.Hosting.Server; +using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; +using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Server.Features; using Microsoft.Net.Http.Server; namespace Microsoft.AspNet.Server.WebListener { - using AppFunc = Func; - internal static class Utilities { private const int BasePort = 5001; private const int MaxPort = 8000; private static int NextPort = BasePort; private static object PortLock = new object(); + private static IHttpContextFactory Factory = new HttpContextFactory(new HttpContextAccessor()); - internal static IDisposable CreateHttpServer(out string baseAddress, AppFunc app) + internal static IServer CreateHttpServer(out string baseAddress, RequestDelegate app) { string root; return CreateDynamicHttpServer(string.Empty, AuthenticationSchemes.AllowAnonymous, out root, out baseAddress, app); } - internal static IDisposable CreateHttpServerReturnRoot(string path, out string root, AppFunc app) + internal static IServer CreateHttpServerReturnRoot(string path, out string root, RequestDelegate app) { string baseAddress; return CreateDynamicHttpServer(path, AuthenticationSchemes.AllowAnonymous, out root, out baseAddress, app); } - internal static IDisposable CreateHttpAuthServer(AuthenticationSchemes authType, out string baseAddress, AppFunc app) + internal static IServer CreateHttpAuthServer(AuthenticationSchemes authType, out string baseAddress, RequestDelegate app) { string root; return CreateDynamicHttpServer(string.Empty, authType, out root, out baseAddress, app); } - internal static IDisposable CreateDynamicHttpServer(string basePath, AuthenticationSchemes authType, out string root, out string baseAddress, AppFunc app) + internal static IServer CreateDynamicHttpServer(string basePath, AuthenticationSchemes authType, out string root, out string baseAddress, RequestDelegate app) { - var factory = new ServerFactory(loggerFactory: null); + var factory = new ServerFactory(loggerFactory: null, httpContextFactory: Factory); lock (PortLock) { while (NextPort < MaxPort) @@ -63,13 +64,14 @@ namespace Microsoft.AspNet.Server.WebListener root = prefix.Scheme + "://" + prefix.Host + ":" + prefix.Port; baseAddress = prefix.ToString(); - var serverFeatures = factory.Initialize(configuration: null); - var listener = serverFeatures.Get(); + var server = factory.CreateServer(configuration: null); + var listener = server.Features.Get(); listener.UrlPrefixes.Add(prefix); listener.AuthenticationManager.AuthenticationSchemes = authType; try { - return factory.Start(serverFeatures, app); + server.Start(app); + return server; } catch (WebListenerException) { @@ -80,17 +82,18 @@ namespace Microsoft.AspNet.Server.WebListener throw new Exception("Failed to locate a free port."); } - internal static IDisposable CreateHttpsServer(AppFunc app) + internal static IServer CreateHttpsServer(RequestDelegate app) { return CreateServer("https", "localhost", 9090, string.Empty, app); } - internal static IDisposable CreateServer(string scheme, string host, int port, string path, AppFunc app) + internal static IServer CreateServer(string scheme, string host, int port, string path, RequestDelegate app) { - var factory = new ServerFactory(loggerFactory: null); - var serverFeatures = factory.Initialize(configuration: null); - serverFeatures.Get().Addresses.Add(UrlPrefix.Create(scheme, host, port, path).ToString()); - return factory.Start(serverFeatures, app); + var factory = new ServerFactory(loggerFactory: null, httpContextFactory: Factory); + var server = factory.CreateServer(configuration: null); + server.Features.Get().Addresses.Add(UrlPrefix.Create(scheme, host, port, path).ToString()); + server.Start(app); + return server; } } } diff --git a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/WebSocketTests.cs b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/WebSocketTests.cs index c099e3a..b6e55b1 100644 --- a/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/WebSocketTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.FunctionalTests/WebSocketTests.cs @@ -35,9 +35,8 @@ namespace Microsoft.AspNet.Server.WebListener public async Task WebSocketTests_SupportKeys_Present() { string address; - using (Utilities.CreateHttpServer(out address, env => + using (Utilities.CreateHttpServer(out address, httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); try { var webSocketFeature = httpContext.Features.Get(); @@ -64,9 +63,8 @@ namespace Microsoft.AspNet.Server.WebListener { bool? upgradeThrew = null; string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); await httpContext.Response.WriteAsync("Hello World"); try { @@ -94,9 +92,8 @@ namespace Microsoft.AspNet.Server.WebListener ManualResetEvent waitHandle = new ManualResetEvent(false); bool? upgraded = null; string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var webSocketFeature = httpContext.Features.Get(); Assert.NotNull(webSocketFeature); Assert.True(webSocketFeature.IsWebSocketRequest); @@ -120,9 +117,8 @@ namespace Microsoft.AspNet.Server.WebListener { byte[] clientBuffer = new byte[] { 0x00, 0x01, 0xFF, 0x00, 0x00 }; string address; - using (Utilities.CreateHttpServer(out address, async env => + using (Utilities.CreateHttpServer(out address, async httpContext => { - var httpContext = new DefaultHttpContext((IFeatureCollection)env); var webSocketFeature = httpContext.Features.Get(); Assert.NotNull(webSocketFeature); Assert.True(webSocketFeature.IsWebSocketRequest);