diff --git a/src/Microsoft.AspNet.Server.WebListener/NativeInterop/SocketAddress.cs b/src/Microsoft.AspNet.Server.WebListener/NativeInterop/SocketAddress.cs index 78b52c13c7..26d92e61e0 100644 --- a/src/Microsoft.AspNet.Server.WebListener/NativeInterop/SocketAddress.cs +++ b/src/Microsoft.AspNet.Server.WebListener/NativeInterop/SocketAddress.cs @@ -170,7 +170,7 @@ namespace Microsoft.AspNet.Server.WebListener } return _hash; } - +#if NET45 internal IPAddress GetIPAddress() { if (Family == AddressFamily.InterNetworkV6) @@ -200,7 +200,7 @@ namespace Microsoft.AspNet.Server.WebListener Contract.Assert(Size >= IPv4AddressSize); return new IPAddress(new byte[] { _buffer[4], _buffer[5], _buffer[6], _buffer[7] }); } - +#endif public override string ToString() { StringBuilder bytes = new StringBuilder(); diff --git a/src/Microsoft.AspNet.Server.WebListener/OwinServerFactory.cs b/src/Microsoft.AspNet.Server.WebListener/OwinServerFactory.cs deleted file mode 100644 index fa90780375..0000000000 --- a/src/Microsoft.AspNet.Server.WebListener/OwinServerFactory.cs +++ /dev/null @@ -1,108 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -// ----------------------------------------------------------------------- -// Copyright 2011-2012 Katana contributors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Diagnostics.CodeAnalysis; -using System.Reflection; -using System.Threading.Tasks; - -namespace Microsoft.AspNet.Server.WebListener -{ - using AppFunc = Func; - using LoggerFactoryFunc = Func, bool>>; - - /// - /// Implements the Katana setup pattern for this server. - /// - public static class OwinServerFactory - { - /// - /// Populates the server capabilities. - /// Also included is a configurable instance of the server. - /// - /// - [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposed by caller")] - public static void Initialize(IDictionary properties) - { - if (properties == null) - { - throw new ArgumentNullException("properties"); - } - - properties[Constants.VersionKey] = Constants.OwinVersion; - - IDictionary capabilities = - properties.Get>(Constants.ServerCapabilitiesKey) - ?? new Dictionary(); - properties[Constants.ServerCapabilitiesKey] = capabilities; - - // SendFile - capabilities[Constants.SendFileVersionKey] = Constants.SendFileVersion; - IDictionary sendfileSupport = new Dictionary(); - sendfileSupport[Constants.SendFileConcurrencyKey] = Constants.Overlapped; - capabilities[Constants.SendFileSupportKey] = sendfileSupport; - - // Opaque - if (ComNetOS.IsWin8orLater) - { - capabilities[Constants.OpaqueVersionKey] = Constants.OpaqueVersion; - } - - // Directly expose the server for advanced configuration. - properties[typeof(OwinWebListener).FullName] = new OwinWebListener(); - } - - /// - /// Creates a server and starts listening on the given addresses. - /// - /// The application entry point. - /// The configuration. - /// The server. Invoke Dispose to shut down. - [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "By design")] - [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposed by caller")] - public static IDisposable Create(AppFunc app, IDictionary properties) - { - if (app == null) - { - throw new ArgumentNullException("app"); - } - if (properties == null) - { - throw new ArgumentNullException("properties"); - } - - var addresses = properties.Get>>("host.Addresses") - ?? new List>(); - - OwinWebListener server = properties.Get(typeof(OwinWebListener).FullName) - ?? new OwinWebListener(); - - var capabilities = - properties.Get>(Constants.ServerCapabilitiesKey) - ?? new Dictionary(); - - var loggerFactory = properties.Get(Constants.ServerLoggerFactoryKey); - - server.Start(app, addresses, capabilities, loggerFactory); - return server; - } - } -} diff --git a/src/Microsoft.AspNet.Server.WebListener/RequestProcessing/NilEnvDictionary.cs b/src/Microsoft.AspNet.Server.WebListener/RequestProcessing/NilEnvDictionary.cs deleted file mode 100644 index af6cc6aff5..0000000000 --- a/src/Microsoft.AspNet.Server.WebListener/RequestProcessing/NilEnvDictionary.cs +++ /dev/null @@ -1,115 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -// ----------------------------------------------------------------------- -// Copyright 2011-2012 Katana contributors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -using System; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.Linq; - -namespace Microsoft.AspNet.Server.WebListener -{ - internal class NilEnvDictionary : IDictionary - { - private static readonly string[] EmptyKeys = new string[0]; - private static readonly object[] EmptyValues = new object[0]; - private static readonly IEnumerable> EmptyKeyValuePairs = Enumerable.Empty>(); - - public int Count - { - get { return 0; } - } - - public bool IsReadOnly - { - get { return false; } - } - - public ICollection Keys - { - get { return EmptyKeys; } - } - - public ICollection Values - { - get { return EmptyValues; } - } - - [SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations", Justification = "Not Implemented")] - public object this[string key] - { - get { throw new NotImplementedException(); } - set { throw new NotImplementedException(); } - } - - public IEnumerator> GetEnumerator() - { - return EmptyKeyValuePairs.GetEnumerator(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return EmptyKeyValuePairs.GetEnumerator(); - } - - public void Add(KeyValuePair item) - { - throw new NotImplementedException(); - } - - public void Clear() - { - } - - public bool Contains(KeyValuePair item) - { - return false; - } - - public void CopyTo(KeyValuePair[] array, int arrayIndex) - { - } - - public bool Remove(KeyValuePair item) - { - return false; - } - - public bool ContainsKey(string key) - { - return false; - } - - public void Add(string key, object value) - { - throw new NotImplementedException(); - } - - public bool Remove(string key) - { - return false; - } - - public bool TryGetValue(string key, out object value) - { - value = null; - return false; - } - } -} diff --git a/src/Microsoft.AspNet.Server.WebListener/ServerConfiguration.cs b/src/Microsoft.AspNet.Server.WebListener/ServerConfiguration.cs new file mode 100644 index 0000000000..a050ec4810 --- /dev/null +++ b/src/Microsoft.AspNet.Server.WebListener/ServerConfiguration.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNet.Hosting.Server; + +namespace Microsoft.AspNet.Server.WebListener +{ + internal class ServerConfiguration : IServerConfiguration + { + internal ServerConfiguration() + { + Addresses = new List>(1); + } + + public IList> Addresses + { + get; + internal set; + } + + public object AdvancedConfiguration + { + get; + internal set; + } + } +} diff --git a/src/Microsoft.AspNet.Server.WebListener/ServerFactory.cs b/src/Microsoft.AspNet.Server.WebListener/ServerFactory.cs new file mode 100644 index 0000000000..9a3fa32269 --- /dev/null +++ b/src/Microsoft.AspNet.Server.WebListener/ServerFactory.cs @@ -0,0 +1,83 @@ +// ----------------------------------------------------------------------- +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +// ----------------------------------------------------------------------- +// Copyright 2011-2012 Katana contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.AspNet.Hosting.Server; + +namespace Microsoft.AspNet.Server.WebListener +{ + using AppFunc = Func; + using LoggerFactoryFunc = Func, bool>>; + + /// + /// Implements the Katana setup pattern for this server. + /// + public class ServerFactory : IServerFactory + { + private LoggerFactoryFunc _loggerFactory; + + public ServerFactory() + { + // TODO: Get services from DI, like logger factory. + } + + /// + /// Populates the server capabilities. + /// Also included is a configurable instance of the server. + /// + /// + [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposed by caller")] + public IServerConfiguration CreateConfiguration() + { + ServerConfiguration serverConfig = new ServerConfiguration(); + serverConfig.AdvancedConfiguration = new OwinWebListener(); + return serverConfig; + } + + /// + /// Creates a server and starts listening on the given addresses. + /// + /// The application entry point. + /// The configuration. + /// The server. Invoke Dispose to shut down. + public IDisposable Start(IServerConfiguration serverConfig, AppFunc app) + { + if (serverConfig == null) + { + throw new ArgumentNullException("serverConfig"); + } + if (app == null) + { + throw new ArgumentNullException("app"); + } + + OwinWebListener server = (OwinWebListener)serverConfig.AdvancedConfiguration; + + var capabilities = new Dictionary(); + + server.Start(app, serverConfig.Addresses, capabilities, _loggerFactory); + return server; + } + } +} diff --git a/src/Microsoft.AspNet.Server.WebListener/fx/AssemblyNeutralAttribute.cs b/src/Microsoft.AspNet.Server.WebListener/fx/AssemblyNeutralAttribute.cs new file mode 100644 index 0000000000..9f503ddd3b --- /dev/null +++ b/src/Microsoft.AspNet.Server.WebListener/fx/AssemblyNeutralAttribute.cs @@ -0,0 +1,10 @@ +using System; + +namespace Microsoft.Net.Runtime +{ + [AssemblyNeutralAttribute] + [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)] + public sealed class AssemblyNeutralAttribute : Attribute + { + } +} diff --git a/src/Microsoft.AspNet.Server.WebListener/fx/IServerConfiguration.cs b/src/Microsoft.AspNet.Server.WebListener/fx/IServerConfiguration.cs new file mode 100644 index 0000000000..1eaf850ffd --- /dev/null +++ b/src/Microsoft.AspNet.Server.WebListener/fx/IServerConfiguration.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.Net.Runtime; + +namespace Microsoft.AspNet.Hosting.Server +{ + [AssemblyNeutral] + public interface IServerConfiguration + { + IList> Addresses { get; } + object AdvancedConfiguration { get; } + } +} diff --git a/src/Microsoft.AspNet.Server.WebListener/fx/IServerFactory.cs b/src/Microsoft.AspNet.Server.WebListener/fx/IServerFactory.cs new file mode 100644 index 0000000000..02f525dff9 --- /dev/null +++ b/src/Microsoft.AspNet.Server.WebListener/fx/IServerFactory.cs @@ -0,0 +1,13 @@ +using System; +using System.Threading.Tasks; +using Microsoft.Net.Runtime; + +namespace Microsoft.AspNet.Hosting.Server +{ + [AssemblyNeutral] + public interface IServerFactory + { + IServerConfiguration CreateConfiguration(); + IDisposable Start(IServerConfiguration serverConfig, Func app); + } +} diff --git a/test/Microsoft.AspNet.Server.WebListener.Test/AuthenticationTests.cs b/test/Microsoft.AspNet.Server.WebListener.Test/AuthenticationTests.cs index 0259f84f43..24a3587f15 100644 --- a/test/Microsoft.AspNet.Server.WebListener.Test/AuthenticationTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.Test/AuthenticationTests.cs @@ -14,7 +14,7 @@ using Microsoft.AspNet.PipelineCore; using Xunit; using Xunit.Extensions; -namespace Microsoft.AspNet.Server.WebListener.Tests +namespace Microsoft.AspNet.Server.WebListener.Test { using AppFunc = Func; @@ -31,7 +31,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [InlineData(AuthenticationType.Kerberos | AuthenticationType.Negotiate | AuthenticationType.Ntlm | AuthenticationType.Digest | AuthenticationType.Basic)] public async Task AuthTypes_EnabledButNotChalleneged_PassThrough(AuthenticationType authType) { - using (CreateServer(authType, env => + using (Utilities.CreateAuthServer(authType, env => { return Task.FromResult(0); })) @@ -49,7 +49,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [InlineData(AuthenticationType.Basic)] public async Task AuthType_Specify401_ChallengesAdded(AuthenticationType authType) { - using (CreateServer(authType, env => + using (Utilities.CreateAuthServer(authType, env => { new DefaultHttpContext((IFeatureCollection)env).Response.StatusCode = 401; return Task.FromResult(0); @@ -64,8 +64,13 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task MultipleAuthTypes_Specify401_ChallengesAdded() { - // TODO: Not implemented - Digest - using (CreateServer(AuthenticationType.Kerberos | AuthenticationType.Negotiate | AuthenticationType.Ntlm | /*AuthenticationType.Digest |*/ AuthenticationType.Basic, env => + using (Utilities.CreateAuthServer( + AuthenticationType.Kerberos + | AuthenticationType.Negotiate + | AuthenticationType.Ntlm + /* | AuthenticationType.Digest TODO: Not implemented */ + | AuthenticationType.Basic, + env => { new DefaultHttpContext((IFeatureCollection)env).Response.StatusCode = 401; return Task.FromResult(0); @@ -87,7 +92,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests public async Task AuthTypes_Login_Success(AuthenticationType authType) { int requestCount = 0; - using (CreateServer(authType, env => + using (Utilities.CreateAuthServer(authType, env => { requestCount++; / * // TODO: Expose user as feature. @@ -105,26 +110,6 @@ namespace Microsoft.AspNet.Server.WebListener.Tests } } */ - private IDisposable CreateServer(AuthenticationType authType, AppFunc app) - { - IDictionary properties = new Dictionary(); - OwinServerFactory.Initialize(properties); - OwinWebListener listener = (OwinWebListener)properties[typeof(OwinWebListener).FullName]; - listener.AuthenticationManager.AuthenticationTypes = authType; - - 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; - - return OwinServerFactory.Create(app, properties); - } private async Task SendRequestAsync(string uri, bool useDefaultCredentials = false) { diff --git a/test/Microsoft.AspNet.Server.WebListener.Test/DictionaryExtensions.cs b/test/Microsoft.AspNet.Server.WebListener.Test/DictionaryExtensions.cs deleted file mode 100644 index b999b1fc4f..0000000000 --- a/test/Microsoft.AspNet.Server.WebListener.Test/DictionaryExtensions.cs +++ /dev/null @@ -1,31 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright (c) Microsoft Corporation. All rights reserved. -// -// ----------------------------------------------------------------------- - -namespace System.Collections.Generic -{ - internal static class DictionaryExtensions - { - internal static string Get(this IDictionary dictionary, string key) - { - string[] values; - if (dictionary.TryGetValue(key, out values)) - { - return string.Join(", ", values); - } - return null; - } - - internal static T Get(this IDictionary dictionary, string key) - { - object values; - if (dictionary.TryGetValue(key, out values)) - { - return (T)values; - } - return default(T); - } - } -} diff --git a/test/Microsoft.AspNet.Server.WebListener.Test/HttpsTests.cs b/test/Microsoft.AspNet.Server.WebListener.Test/HttpsTests.cs index c2e29eccde..f29b417a1b 100644 --- a/test/Microsoft.AspNet.Server.WebListener.Test/HttpsTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.Test/HttpsTests.cs @@ -16,7 +16,7 @@ using Microsoft.AspNet.HttpFeature; using Microsoft.AspNet.PipelineCore; using Xunit; -namespace Microsoft.AspNet.Server.WebListener.Tests +namespace Microsoft.AspNet.Server.WebListener.Test { using AppFunc = Func; @@ -27,7 +27,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Https_200OK_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpsServer(env => { return Task.FromResult(0); })) @@ -40,7 +40,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Https_SendHelloWorld_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpsServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] body = Encoding.UTF8.GetBytes("Hello World"); @@ -56,7 +56,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Https_EchoHelloWorld_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpsServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); string input = new StreamReader(httpContext.Request.Body).ReadToEnd(); @@ -75,7 +75,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Https_ClientCertNotSent_ClientCertNotPresent() { - using (CreateServer(async env => + using (Utilities.CreateHttpsServer(async env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var tls = httpContext.GetFeature(); @@ -92,7 +92,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Https_ClientCertRequested_ClientCertPresent() { - using (CreateServer(async env => + using (Utilities.CreateHttpsServer(async env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var tls = httpContext.GetFeature(); @@ -108,23 +108,6 @@ namespace Microsoft.AspNet.Server.WebListener.Tests } } - 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"] = "https"; - address["host"] = "localhost"; - address["port"] = "9090"; - address["path"] = string.Empty; - - return OwinServerFactory.Create(app, properties); - } - private async Task SendRequestAsync(string uri, X509Certificate cert = null) { diff --git a/test/Microsoft.AspNet.Server.WebListener.Test/RequestBodyTests.cs b/test/Microsoft.AspNet.Server.WebListener.Test/RequestBodyTests.cs index 640d0dc489..59df978897 100644 --- a/test/Microsoft.AspNet.Server.WebListener.Test/RequestBodyTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.Test/RequestBodyTests.cs @@ -15,7 +15,7 @@ using Microsoft.AspNet.FeatureModel; using Microsoft.AspNet.PipelineCore; using Xunit; -namespace Microsoft.AspNet.Server.WebListener.Tests +namespace Microsoft.AspNet.Server.WebListener.Test { using AppFunc = Func; @@ -26,7 +26,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task RequestBody_ReadSync_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] input = new byte[100]; @@ -44,7 +44,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task RequestBody_ReadAync_Success() { - using (CreateServer(async env => + using (Utilities.CreateHttpServer(async env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] input = new byte[100]; @@ -61,7 +61,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task RequestBody_ReadBeginEnd_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] input = new byte[100]; @@ -80,7 +80,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests public async Task RequestBody_ReadSyncPartialBody_Success() { StaggardContent content = new StaggardContent(); - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] input = new byte[10]; @@ -101,7 +101,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests public async Task RequestBody_ReadAsyncPartialBody_Success() { StaggardContent content = new StaggardContent(); - using (CreateServer(async env => + using (Utilities.CreateHttpServer(async env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); byte[] input = new byte[10]; @@ -117,23 +117,6 @@ namespace Microsoft.AspNet.Server.WebListener.Tests } } - 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; - - return OwinServerFactory.Create(app, properties); - } - private Task SendRequestAsync(string uri, string upload) { return SendRequestAsync(uri, new StringContent(upload)); diff --git a/test/Microsoft.AspNet.Server.WebListener.Test/RequestHeaderTests.cs b/test/Microsoft.AspNet.Server.WebListener.Test/RequestHeaderTests.cs index 9e2828c50d..3ebb24eaca 100644 --- a/test/Microsoft.AspNet.Server.WebListener.Test/RequestHeaderTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.Test/RequestHeaderTests.cs @@ -14,7 +14,7 @@ using Microsoft.AspNet.FeatureModel; using Microsoft.AspNet.PipelineCore; using Xunit; -namespace Microsoft.AspNet.Server.WebListener.Tests +namespace Microsoft.AspNet.Server.WebListener.Test { using AppFunc = Func; @@ -25,7 +25,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task RequestHeaders_ClientSendsDefaultHeaders_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var requestHeaders = new DefaultHttpContext((IFeatureCollection)env).Request.Headers; // NOTE: The System.Net client only sends the Connection: keep-alive header on the first connection per service-point. @@ -44,7 +44,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task RequestHeaders_ClientSendsCustomHeaders_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var requestHeaders = new DefaultHttpContext((IFeatureCollection)env).Request.Headers; Assert.Equal(4, requestHeaders.Count); @@ -64,23 +64,6 @@ namespace Microsoft.AspNet.Server.WebListener.Tests } } - 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; - - return OwinServerFactory.Create(app, properties); - } - private async Task SendRequestAsync(string uri) { using (HttpClient client = new HttpClient()) diff --git a/test/Microsoft.AspNet.Server.WebListener.Test/RequestTests.cs b/test/Microsoft.AspNet.Server.WebListener.Test/RequestTests.cs index 11c0ce59d1..7eb5ce5bca 100644 --- a/test/Microsoft.AspNet.Server.WebListener.Test/RequestTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.Test/RequestTests.cs @@ -12,12 +12,13 @@ using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.FeatureModel; +using Microsoft.AspNet.Hosting.Server; using Microsoft.AspNet.HttpFeature; using Microsoft.AspNet.PipelineCore; using Xunit; using Xunit.Extensions; -namespace Microsoft.AspNet.Server.WebListener.Tests +namespace Microsoft.AspNet.Server.WebListener.Test { using AppFunc = Func; @@ -28,7 +29,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Request_SimpleGet_Success() { - using (CreateServer(env => + using (Utilities.CreateServer("http", "localhost", "8080", "/basepath", env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); try @@ -66,7 +67,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests httpContext.Response.Body.Write(body, 0, body.Length); } return Task.FromResult(0); - }, "http", "localhost", "8080", "/basepath")) + })) { string response = await SendRequestAsync(Address + "/basepath/SomePath?SomeQuery"); Assert.Equal(string.Empty, response); @@ -74,16 +75,15 @@ namespace Microsoft.AspNet.Server.WebListener.Tests } [Theory] - [InlineData("http", "localhost", "8080", "/", "http://localhost:8080/", "", "/")] - [InlineData("http", "localhost", "8080", "/basepath/", "http://localhost:8080/basepath", "/basepath", "")] - [InlineData("http", "localhost", "8080", "/basepath/", "http://localhost:8080/basepath/", "/basepath", "/")] - [InlineData("http", "localhost", "8080", "/basepath/", "http://localhost:8080/basepath/subpath", "/basepath", "/subpath")] - [InlineData("http", "localhost", "8080", "/base path/", "http://localhost:8080/base%20path/sub path", "/base path", "/sub path")] - [InlineData("http", "localhost", "8080", "/base葉path/", "http://localhost:8080/base%E8%91%89path/sub%E8%91%89path", "/base葉path", "/sub葉path")] - public async Task Request_PathSplitting(string scheme, string host, string port, string pathBase, string requestUri, - string expectedPathBase, string expectedPath) + [InlineData("/", "http://localhost:8080/", "", "/")] + [InlineData("/basepath/", "http://localhost:8080/basepath", "/basepath", "")] + [InlineData("/basepath/", "http://localhost:8080/basepath/", "/basepath", "/")] + [InlineData("/basepath/", "http://localhost:8080/basepath/subpath", "/basepath", "/subpath")] + [InlineData("/base path/", "http://localhost:8080/base%20path/sub path", "/base path", "/sub path")] + [InlineData("/base葉path/", "http://localhost:8080/base%E8%91%89path/sub%E8%91%89path", "/base葉path", "/sub葉path")] + public async Task Request_PathSplitting(string pathBase, string requestUri, string expectedPathBase, string expectedPath) { - using (CreateServer(env => + using (Utilities.CreateServer("http", "localhost", "8080", pathBase, env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); try @@ -92,11 +92,11 @@ namespace Microsoft.AspNet.Server.WebListener.Tests var connectionInfo = httpContext.GetFeature(); // Request Keys - Assert.Equal(scheme, requestInfo.Scheme); + Assert.Equal("http", requestInfo.Scheme); Assert.Equal(expectedPath, requestInfo.Path); Assert.Equal(expectedPathBase, requestInfo.PathBase); Assert.Equal(string.Empty, requestInfo.QueryString); - Assert.Equal(port, connectionInfo.LocalPort.ToString()); + Assert.Equal(8080, connectionInfo.LocalPort); } catch (Exception ex) { @@ -104,7 +104,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests httpContext.Response.Body.Write(body, 0, body.Length); } return Task.FromResult(0); - }, scheme, host, port, pathBase)) + })) { string response = await SendRequestAsync(requestUri); Assert.Equal(string.Empty, response); @@ -148,41 +148,23 @@ namespace Microsoft.AspNet.Server.WebListener.Tests } } - private IDisposable CreateServer(AppFunc app, string scheme, string host, string port, string path) - { - IDictionary properties = new Dictionary(); - IList> addresses = new List>(); - properties["host.Addresses"] = addresses; - - IDictionary address = new Dictionary(); - addresses.Add(address); - - address["scheme"] = scheme; - address["host"] = host; - address["port"] = port; - address["path"] = path; - - return OwinServerFactory.Create(app, properties); - } - private IDisposable CreateServer(AppFunc app) { - IDictionary properties = new Dictionary(); - IList> addresses = new List>(); - properties["host.Addresses"] = addresses; + ServerFactory factory = new ServerFactory(); + IServerConfiguration config = factory.CreateConfiguration(); foreach (string path in new[] { "/", "/11", "/2/3", "/2", "/11/2" }) { IDictionary address = new Dictionary(); - addresses.Add(address); - address["scheme"] = "http"; address["host"] = "localhost"; address["port"] = "8080"; address["path"] = path; + + config.Addresses.Add(address); } - return OwinServerFactory.Create(app, properties); + return factory.Start(config, app); } private async Task SendRequestAsync(string uri) diff --git a/test/Microsoft.AspNet.Server.WebListener.Test/ResponseBodyTests.cs b/test/Microsoft.AspNet.Server.WebListener.Test/ResponseBodyTests.cs index ddcdc4aa2e..0372b63a39 100644 --- a/test/Microsoft.AspNet.Server.WebListener.Test/ResponseBodyTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.Test/ResponseBodyTests.cs @@ -16,7 +16,7 @@ using Microsoft.AspNet.HttpFeature; using Microsoft.AspNet.PipelineCore; using Xunit; -namespace Microsoft.AspNet.Server.WebListener.Tests +namespace Microsoft.AspNet.Server.WebListener.Test { using AppFunc = Func; @@ -27,7 +27,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseBody_WriteNoHeaders_DefaultsToChunked() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Body.Write(new byte[10], 0, 10); @@ -47,7 +47,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseBody_WriteChunked_Chunked() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Request.Headers["transfeR-Encoding"] = " CHunked "; @@ -70,7 +70,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseBody_WriteContentLength_PassedThrough() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Headers["Content-lenGth"] = " 30 "; @@ -94,7 +94,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseBody_Http10WriteNoHeaders_DefaultsConnectionClose() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { env["owin.ResponseProtocol"] = "HTTP/1.0"; env.Get("owin.ResponseBody").Write(new byte[10], 0, 10); @@ -114,7 +114,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public void ResponseBody_WriteContentLengthNoneWritten_Throws() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Headers["Content-lenGth"] = " 20 "; @@ -128,7 +128,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public void ResponseBody_WriteContentLengthNotEnoughWritten_Throws() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Headers["Content-lenGth"] = " 20 "; @@ -143,7 +143,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public void ResponseBody_WriteContentLengthTooMuchWritten_Throws() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.Headers["Content-lenGth"] = " 10 "; @@ -161,7 +161,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests { ManualResetEvent waitHandle = new ManualResetEvent(false); bool? appThrew = null; - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { try { @@ -195,23 +195,6 @@ namespace Microsoft.AspNet.Server.WebListener.Tests } } - 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; - - return OwinServerFactory.Create(app, properties); - } - private async Task SendRequestAsync(string uri) { using (HttpClient client = new HttpClient()) diff --git a/test/Microsoft.AspNet.Server.WebListener.Test/ResponseHeaderTests.cs b/test/Microsoft.AspNet.Server.WebListener.Test/ResponseHeaderTests.cs index a4e9126992..25f8b29e62 100644 --- a/test/Microsoft.AspNet.Server.WebListener.Test/ResponseHeaderTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.Test/ResponseHeaderTests.cs @@ -15,7 +15,7 @@ using Microsoft.AspNet.HttpFeature; using Microsoft.AspNet.PipelineCore; using Xunit; -namespace Microsoft.AspNet.Server.WebListener.Tests +namespace Microsoft.AspNet.Server.WebListener.Test { using AppFunc = Func; @@ -26,7 +26,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseHeaders_ServerSendsDefaultHeaders_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { return Task.FromResult(0); })) @@ -45,7 +45,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseHeaders_ServerSendsCustomHeaders_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var responseInfo = httpContext.GetFeature(); @@ -69,7 +69,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseHeaders_ServerSendsConnectionClose_Closed() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var responseInfo = httpContext.GetFeature(); @@ -88,7 +88,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseHeaders_SendsHttp10_Gets11Close() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { env["owin.ResponseProtocol"] = "HTTP/1.0"; return Task.FromResult(0); @@ -105,7 +105,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseHeaders_SendsHttp10WithBody_Gets11Close() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { env["owin.ResponseProtocol"] = "HTTP/1.0"; return env.Get("owin.ResponseBody").WriteAsync(new byte[10], 0, 10); @@ -125,7 +125,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseHeaders_HTTP10Request_Gets11Close() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { return Task.FromResult(0); })) @@ -146,7 +146,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseHeaders_HTTP10Request_RemovesChunkedHeader() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var responseInfo = httpContext.GetFeature(); @@ -173,7 +173,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Headers_FlushSendsHeaders_Success() { - using (CreateServer( + using (Utilities.CreateHttpServer( env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); @@ -183,7 +183,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests responseHeaders.Add("Custom2", new string[] { "value2a, value2b" }); var body = responseInfo.Body; body.Flush(); - responseInfo.StatusCode = 404; // Ignored + Assert.Throws(() => responseInfo.StatusCode = 404); responseHeaders.Add("Custom3", new string[] { "value3a, value3b", "value3c" }); // Ignored return Task.FromResult(0); })) @@ -203,7 +203,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Headers_FlushAsyncSendsHeaders_Success() { - using (CreateServer( + using (Utilities.CreateHttpServer( async env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); @@ -213,7 +213,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests responseHeaders.Add("Custom2", new string[] { "value2a, value2b" }); var body = responseInfo.Body; await body.FlushAsync(); - responseInfo.StatusCode = 404; // Ignored + Assert.Throws(() => responseInfo.StatusCode = 404); responseHeaders.Add("Custom3", new string[] { "value3a, value3b", "value3c" }); // Ignored })) { @@ -229,23 +229,6 @@ namespace Microsoft.AspNet.Server.WebListener.Tests } } - 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; - - return OwinServerFactory.Create(app, properties); - } - private async Task SendRequestAsync(string uri) { using (HttpClient client = new HttpClient()) diff --git a/test/Microsoft.AspNet.Server.WebListener.Test/ResponseSendFileTests.cs b/test/Microsoft.AspNet.Server.WebListener.Test/ResponseSendFileTests.cs index 18912bbba2..21ec5eb331 100644 --- a/test/Microsoft.AspNet.Server.WebListener.Test/ResponseSendFileTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.Test/ResponseSendFileTests.cs @@ -18,7 +18,7 @@ using Microsoft.AspNet.HttpFeature; using Microsoft.AspNet.PipelineCore; using Xunit; -namespace Microsoft.AspNet.Server.WebListener.Tests +namespace Microsoft.AspNet.Server.WebListener.Test { using AppFunc = Func; @@ -32,7 +32,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseSendFile_SupportKeys_Present() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); try @@ -75,7 +75,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests { ManualResetEvent waitHandle = new ManualResetEvent(false); bool? appThrew = null; - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.GetFeature(); @@ -107,7 +107,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseSendFile_NoHeaders_DefaultsToChunked() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.GetFeature(); @@ -126,7 +126,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseSendFile_RelativeFile_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.GetFeature(); @@ -145,7 +145,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseSendFile_Chunked_Chunked() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.GetFeature(); @@ -165,7 +165,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseSendFile_MultipleChunks_Chunked() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.GetFeature(); @@ -186,7 +186,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseSendFile_ChunkedHalfOfFile_Chunked() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.GetFeature(); @@ -205,7 +205,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseSendFile_ChunkedOffsetOutOfRange_Throws() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.GetFeature(); @@ -220,7 +220,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseSendFile_ChunkedCountOutOfRange_Throws() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.GetFeature(); @@ -235,7 +235,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseSendFile_ChunkedCount0_Chunked() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.GetFeature(); @@ -254,7 +254,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseSendFile_ContentLength_PassedThrough() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.GetFeature(); @@ -275,7 +275,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseSendFile_ContentLengthSpecific_PassedThrough() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.GetFeature(); @@ -296,7 +296,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task ResponseSendFile_ContentLength0_PassedThrough() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); var sendFile = httpContext.GetFeature(); @@ -313,25 +313,6 @@ namespace Microsoft.AspNet.Server.WebListener.Tests Assert.Equal(0, (await response.Content.ReadAsByteArrayAsync()).Length); } } - - private IDisposable CreateServer(AppFunc app) - { - IList> addresses = new List>(); - IDictionary properties = new Dictionary(); - 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) { diff --git a/test/Microsoft.AspNet.Server.WebListener.Test/ResponseTests.cs b/test/Microsoft.AspNet.Server.WebListener.Test/ResponseTests.cs index ae9562a94d..a9f33067aa 100644 --- a/test/Microsoft.AspNet.Server.WebListener.Test/ResponseTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.Test/ResponseTests.cs @@ -14,9 +14,10 @@ using Microsoft.AspNet.HttpFeature; using Microsoft.AspNet.PipelineCore; using Xunit; -namespace Microsoft.AspNet.Server.WebListener.Tests +namespace Microsoft.AspNet.Server.WebListener.Test { using AppFunc = Func; + using Microsoft.AspNet.Hosting.Server; public class ResponseTests { @@ -25,7 +26,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Response_ServerSendsDefaultResponse_ServerProvidesStatusCodeAndReasonPhrase() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); Assert.Equal(200, httpContext.Response.StatusCode); @@ -43,7 +44,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Response_ServerSendsSpecificStatus_ServerProvidesReasonPhrase() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.StatusCode = 201; @@ -62,7 +63,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Response_ServerSendsSpecificStatusAndReasonPhrase_PassedThrough() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.StatusCode = 201; @@ -82,7 +83,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Response_ServerSendsCustomStatus_NoReasonPhrase() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.StatusCode = 901; @@ -99,7 +100,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Response_100_Throws() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.StatusCode = 100; @@ -114,7 +115,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Response_0_Throws() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.StatusCode = 0; @@ -125,23 +126,6 @@ namespace Microsoft.AspNet.Server.WebListener.Tests Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); } } - - 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; - - return OwinServerFactory.Create(app, properties); - } private async Task SendRequestAsync(string uri) { diff --git a/test/Microsoft.AspNet.Server.WebListener.Test/ServerTests.cs b/test/Microsoft.AspNet.Server.WebListener.Test/ServerTests.cs index f1c5c3e22a..a8b31ae327 100644 --- a/test/Microsoft.AspNet.Server.WebListener.Test/ServerTests.cs +++ b/test/Microsoft.AspNet.Server.WebListener.Test/ServerTests.cs @@ -17,9 +17,10 @@ using Microsoft.AspNet.FeatureModel; using Microsoft.AspNet.PipelineCore; using Xunit; -namespace Microsoft.AspNet.Server.WebListener.Tests +namespace Microsoft.AspNet.Server.WebListener.Test { using AppFunc = Func; + using Microsoft.AspNet.Hosting.Server; public class ServerTests { @@ -28,7 +29,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Server_200OK_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { return Task.FromResult(0); })) @@ -41,7 +42,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Server_SendHelloWorld_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); httpContext.Response.ContentLength = 11; @@ -56,7 +57,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public async Task Server_EchoHelloWorld_Success() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { var httpContext = new DefaultHttpContext((IFeatureCollection)env); string input = new StreamReader(httpContext.Request.Body).ReadToEnd(); @@ -73,7 +74,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests [Fact] public void Server_AppException_ClientReset() { - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { throw new InvalidOperationException(); })) @@ -94,7 +95,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests int requestCount = 0; TaskCompletionSource tcs = new TaskCompletionSource(); - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { if (Interlocked.Increment(ref requestCount) == requestLimit) { @@ -131,7 +132,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests int requestCount = 0; TaskCompletionSource tcs = new TaskCompletionSource(); - using (CreateServer(async env => + using (Utilities.CreateHttpServer(async env => { if (Interlocked.Increment(ref requestCount) == requestLimit) { @@ -161,7 +162,7 @@ namespace Microsoft.AspNet.Server.WebListener.Tests ManualResetEvent aborted = new ManualResetEvent(false); ManualResetEvent canceled = new ManualResetEvent(false); - using (CreateServer(env => + using (Utilities.CreateHttpServer(env => { CancellationToken ct = env.Get("owin.CallCancelled"); Assert.True(ct.CanBeCanceled, "CanBeCanceled"); @@ -185,39 +186,29 @@ namespace Microsoft.AspNet.Server.WebListener.Tests Assert.True(canceled.WaitOne(interval), "canceled"); } } + */ [Fact] public async Task Server_SetQueueLimit_Success() { - using (CreateServer(env => - { - // There's no good way to validate this in code. Just execute it to make sure it doesn't crash. - // Run "netsh http show servicestate" to see the current value - var listener = env.Get("Microsoft.AspNet.Server.WebListener.OwinWebListener"); - listener.SetRequestQueueLimit(1001); - return Task.FromResult(0); - })) - { - string response = await SendRequestAsync(Address); - Assert.Equal(string.Empty, response); - } - } - */ - 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; - return OwinServerFactory.Create(app, properties); + ServerFactory factory = new ServerFactory(); + IServerConfiguration config = factory.CreateConfiguration(); + config.Addresses.Add(address); + + OwinWebListener listener = (OwinWebListener)config.AdvancedConfiguration; + listener.SetRequestQueueLimit(1001); + + using (factory.Start(config, env => Task.FromResult(0))) + { + string response = await SendRequestAsync(Address); + Assert.Equal(string.Empty, response); + } } private async Task SendRequestAsync(string uri) diff --git a/test/Microsoft.AspNet.Server.WebListener.Test/Utilities.cs b/test/Microsoft.AspNet.Server.WebListener.Test/Utilities.cs new file mode 100644 index 0000000000..280f8e4536 --- /dev/null +++ b/test/Microsoft.AspNet.Server.WebListener.Test/Utilities.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Microsoft.AspNet.Hosting.Server; + +namespace Microsoft.AspNet.Server.WebListener.Test +{ + using AppFunc = Func; + + internal static class Utilities + { + internal static IDisposable CreateHttpServer(AppFunc app) + { + return CreateServer("http", "localhost", "8080", string.Empty, app); + } + + internal static IDisposable CreateHttpsServer(AppFunc app) + { + return CreateServer("https", "localhost", "9090", string.Empty, app); + } + + internal static IDisposable CreateAuthServer(AuthenticationType authType, AppFunc app) + { + return CreateServer("http", "localhost", "8080", string.Empty, authType, app); + } + + internal static IDisposable CreateServer(string scheme, string host, string port, string path, AppFunc app) + { + return CreateServer(scheme, host, port, path, AuthenticationType.None, app); + } + + internal static IDisposable CreateServer(string scheme, string host, string port, string path, AuthenticationType authType, AppFunc app) + { + IDictionary address = new Dictionary(); + address["scheme"] = scheme; + address["host"] = host; + address["port"] = port; + address["path"] = path; + + ServerFactory factory = new ServerFactory(); + IServerConfiguration config = factory.CreateConfiguration(); + config.Addresses.Add(address); + + OwinWebListener listener = (OwinWebListener)config.AdvancedConfiguration; + listener.AuthenticationManager.AuthenticationTypes = authType; + + return factory.Start(config, app); + } + } +}