diff --git a/samples/SessionSample/Startup.cs b/samples/SessionSample/Startup.cs index fdf43a4881..8ea3a5af80 100644 --- a/samples/SessionSample/Startup.cs +++ b/samples/SessionSample/Startup.cs @@ -3,6 +3,7 @@ using System; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -74,5 +75,15 @@ namespace SessionSample await context.Response.WriteAsync(""); }); } + + public static void Main(string[] args) + { + var application = new WebApplicationBuilder() + .UseConfiguration(WebApplicationConfiguration.GetDefault(args)) + .UseStartup() + .Build(); + + application.Run(); + } } } \ No newline at end of file diff --git a/samples/SessionSample/hosting.json b/samples/SessionSample/hosting.json new file mode 100644 index 0000000000..f8ef14574d --- /dev/null +++ b/samples/SessionSample/hosting.json @@ -0,0 +1,3 @@ +{ + "server": "Microsoft.AspNet.Server.Kestrel" +} diff --git a/samples/SessionSample/project.json b/samples/SessionSample/project.json index 5b354a5d16..e8bf22b968 100644 --- a/samples/SessionSample/project.json +++ b/samples/SessionSample/project.json @@ -1,19 +1,20 @@ { - "commands": { - "web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001" - }, - "dependencies": { - "Microsoft.AspNet.Server.IIS": "1.0.0-*", - "Microsoft.AspNet.Server.WebListener": "1.0.0-*", - "Microsoft.AspNet.Session": "1.0.0-*", - "Microsoft.Extensions.Caching.Memory": "1.0.0-*", - "Microsoft.Extensions.Caching.Redis": "1.0.0-*", - "Microsoft.Extensions.Caching.SqlServer": "1.0.0-*", - "Microsoft.Extensions.Logging.Console": "1.0.0-*" - }, - "exclude": "wwwroot/**/*.*", - "frameworks": { - "dnx451": { } - }, - "webroot": "wwwroot" + "compilationOptions": { + "emitEntryPoint": true + }, + "commands": { + "web": "SessionSample" + }, + "dependencies": { + "Microsoft.AspNet.Server.Kestrel": "1.0.0-*", + "Microsoft.AspNet.Session": "1.0.0-*", + "Microsoft.Extensions.Caching.Memory": "1.0.0-*", + "Microsoft.Extensions.Caching.Redis": "1.0.0-*", + "Microsoft.Extensions.Caching.SqlServer": "1.0.0-*", + "Microsoft.Extensions.Logging.Console": "1.0.0-*" + }, + "exclude": "wwwroot/**/*.*", + "frameworks": { + "dnx451": { } + } } diff --git a/samples/SessionSample/wwwroot/web.config b/samples/SessionSample/wwwroot/web.config new file mode 100644 index 0000000000..9a0d90abf8 --- /dev/null +++ b/samples/SessionSample/wwwroot/web.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Session.Tests/SessionTests.cs b/test/Microsoft.AspNet.Session.Tests/SessionTests.cs index 7268f386ed..287a7eae8d 100644 --- a/test/Microsoft.AspNet.Session.Tests/SessionTests.cs +++ b/test/Microsoft.AspNet.Session.Tests/SessionTests.cs @@ -4,10 +4,10 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Net; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNet.Builder; +using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Http; using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.TestHost; @@ -27,21 +27,24 @@ namespace Microsoft.AspNet.Session [Fact] public async Task ReadingEmptySessionDoesNotCreateCookie() { - using (var server = TestServer.Create(app => - { - app.UseSession(); - - app.Run(context => + var builder = new WebApplicationBuilder() + .Configure(app => { - Assert.Null(context.Session.GetString("NotFound")); - return Task.FromResult(0); + app.UseSession(); + + app.Run(context => + { + Assert.Null(context.Session.GetString("NotFound")); + return Task.FromResult(0); + }); + }) + .ConfigureServices(services => + { + services.AddCaching(); + services.AddSession(); }); - }, - services => - { - services.AddCaching(); - services.AddSession(); - })) + + using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync(string.Empty); @@ -54,22 +57,25 @@ namespace Microsoft.AspNet.Session [Fact] public async Task SettingAValueCausesTheCookieToBeCreated() { - using (var server = TestServer.Create(app => - { - app.UseSession(); - app.Run(context => + var builder = new WebApplicationBuilder() + .Configure(app => { - Assert.Null(context.Session.GetString("Key")); - context.Session.SetString("Key", "Value"); - Assert.Equal("Value", context.Session.GetString("Key")); - return Task.FromResult(0); + app.UseSession(); + app.Run(context => + { + Assert.Null(context.Session.GetString("Key")); + context.Session.SetString("Key", "Value"); + Assert.Equal("Value", context.Session.GetString("Key")); + return Task.FromResult(0); + }); + }) + .ConfigureServices(services => + { + services.AddCaching(); + services.AddSession(); }); - }, - services => - { - services.AddCaching(); - services.AddSession(); - })) + + using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync(string.Empty); @@ -84,27 +90,30 @@ namespace Microsoft.AspNet.Session [Fact] public async Task SessionCanBeAccessedOnTheNextRequest() { - using (var server = TestServer.Create(app => - { - app.UseSession(); - app.Run(context => + var builder = new WebApplicationBuilder() + .Configure(app => { - int? value = context.Session.GetInt32("Key"); - if (context.Request.Path == new PathString("/first")) + app.UseSession(); + app.Run(context => { - Assert.False(value.HasValue); - value = 0; - } - Assert.True(value.HasValue); - context.Session.SetInt32("Key", value.Value + 1); - return context.Response.WriteAsync(value.Value.ToString()); + int? value = context.Session.GetInt32("Key"); + if (context.Request.Path == new PathString("/first")) + { + Assert.False(value.HasValue); + value = 0; + } + Assert.True(value.HasValue); + context.Session.SetInt32("Key", value.Value + 1); + return context.Response.WriteAsync(value.Value.ToString()); + }); + }) + .ConfigureServices(services => + { + services.AddCaching(); + services.AddSession(); }); - }, - services => - { - services.AddCaching(); - services.AddSession(); - })) + + using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync("first"); @@ -123,37 +132,41 @@ namespace Microsoft.AspNet.Session [Fact] public async Task RemovedItemCannotBeAccessedAgain() { - using (var server = TestServer.Create(app => - { - app.UseSession(); - app.Run(context => + var builder = new WebApplicationBuilder() + .Configure(app => { - int? value = context.Session.GetInt32("Key"); - if (context.Request.Path == new PathString("/first")) + app.UseSession(); + app.Run(context => { - Assert.False(value.HasValue); - value = 0; - context.Session.SetInt32("Key", 1); - } - else if (context.Request.Path == new PathString("/second")) - { - Assert.True(value.HasValue); - Assert.Equal(1, value); - context.Session.Remove("Key"); - } - else if (context.Request.Path == new PathString("/third")) - { - Assert.False(value.HasValue); - value = 2; - } - return context.Response.WriteAsync(value.Value.ToString()); + int? value = context.Session.GetInt32("Key"); + if (context.Request.Path == new PathString("/first")) + { + Assert.False(value.HasValue); + value = 0; + context.Session.SetInt32("Key", 1); + } + else if (context.Request.Path == new PathString("/second")) + { + Assert.True(value.HasValue); + Assert.Equal(1, value); + context.Session.Remove("Key"); + } + else if (context.Request.Path == new PathString("/third")) + { + Assert.False(value.HasValue); + value = 2; + } + return context.Response.WriteAsync(value.Value.ToString()); + }); + }) + .ConfigureServices( + services => + { + services.AddCaching(); + services.AddSession(); }); - }, - services => - { - services.AddCaching(); - services.AddSession(); - })) + + using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync("first"); @@ -171,37 +184,40 @@ namespace Microsoft.AspNet.Session [Fact] public async Task ClearedItemsCannotBeAccessedAgain() { - using (var server = TestServer.Create(app => - { - app.UseSession(); - app.Run(context => + var builder = new WebApplicationBuilder() + .Configure(app => { - int? value = context.Session.GetInt32("Key"); - if (context.Request.Path == new PathString("/first")) + app.UseSession(); + app.Run(context => { - Assert.False(value.HasValue); - value = 0; - context.Session.SetInt32("Key", 1); - } - else if (context.Request.Path == new PathString("/second")) - { - Assert.True(value.HasValue); - Assert.Equal(1, value); - context.Session.Clear(); - } - else if (context.Request.Path == new PathString("/third")) - { - Assert.False(value.HasValue); - value = 2; - } - return context.Response.WriteAsync(value.Value.ToString()); + int? value = context.Session.GetInt32("Key"); + if (context.Request.Path == new PathString("/first")) + { + Assert.False(value.HasValue); + value = 0; + context.Session.SetInt32("Key", 1); + } + else if (context.Request.Path == new PathString("/second")) + { + Assert.True(value.HasValue); + Assert.Equal(1, value); + context.Session.Clear(); + } + else if (context.Request.Path == new PathString("/third")) + { + Assert.False(value.HasValue); + value = 2; + } + return context.Response.WriteAsync(value.Value.ToString()); + }); + }) + .ConfigureServices(services => + { + services.AddCaching(); + services.AddSession(); }); - }, - services => - { - services.AddCaching(); - services.AddSession(); - })) + + using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync("first"); @@ -221,21 +237,24 @@ namespace Microsoft.AspNet.Session { var sink = new TestSink(); var loggerFactory = new TestLoggerFactory(sink, enabled: true); - using (var server = TestServer.Create(app => - { - app.UseSession(); - app.Run(context => + var builder = new WebApplicationBuilder() + .Configure(app => { - context.Session.SetString("Key", "Value"); - return Task.FromResult(0); + app.UseSession(); + app.Run(context => + { + context.Session.SetString("Key", "Value"); + return Task.FromResult(0); + }); + }) + .ConfigureServices(services => + { + services.AddSingleton(typeof(ILoggerFactory), loggerFactory); + services.AddCaching(); + services.AddSession(); }); - }, - services => - { - services.AddSingleton(typeof(ILoggerFactory), loggerFactory); - services.AddCaching(); - services.AddSession(); - })) + + using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync(string.Empty); @@ -254,32 +273,35 @@ namespace Microsoft.AspNet.Session { var sink = new TestSink(); var loggerFactory = new TestLoggerFactory(sink, enabled: true); - using (var server = TestServer.Create(app => - { - app.UseSession(); - app.Run(context => + var builder = new WebApplicationBuilder() + .Configure(app => { - int? value = context.Session.GetInt32("Key"); - if (context.Request.Path == new PathString("/first")) + app.UseSession(); + app.Run(context => { - Assert.False(value.HasValue); - value = 1; - context.Session.SetInt32("Key", 1); - } - else if (context.Request.Path == new PathString("/second")) - { - Assert.False(value.HasValue); - value = 2; - } - return context.Response.WriteAsync(value.Value.ToString()); + int? value = context.Session.GetInt32("Key"); + if (context.Request.Path == new PathString("/first")) + { + Assert.False(value.HasValue); + value = 1; + context.Session.SetInt32("Key", 1); + } + else if (context.Request.Path == new PathString("/second")) + { + Assert.False(value.HasValue); + value = 2; + } + return context.Response.WriteAsync(value.Value.ToString()); + }); + }) + .ConfigureServices(services => + { + services.AddSingleton(typeof(ILoggerFactory), loggerFactory); + services.AddCaching(); + services.AddSession(o => o.IdleTimeout = TimeSpan.FromMilliseconds(30)); }); - }, - services => - { - services.AddSingleton(typeof(ILoggerFactory), loggerFactory); - services.AddCaching(); - services.AddSession(o => o.IdleTimeout = TimeSpan.FromMilliseconds(30)); - })) + + using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync("first"); @@ -305,37 +327,40 @@ namespace Microsoft.AspNet.Session public async Task RefreshesSession_WhenSessionData_IsNotModified() { var clock = new TestClock(); - using (var server = TestServer.Create(app => - { - app.UseSession(); - app.Run(context => + var builder = new WebApplicationBuilder() + .Configure(app => { - string responseData = string.Empty; - if (context.Request.Path == new PathString("/AddDataToSession")) + app.UseSession(); + app.Run(context => { - context.Session.SetInt32("Key", 10); - responseData = "added data to session"; - } - else if (context.Request.Path == new PathString("/AccessSessionData")) - { - var value = context.Session.GetInt32("Key"); - responseData = (value == null) ? "No value found in session." : value.ToString(); - } - else if (context.Request.Path == new PathString("/DoNotAccessSessionData")) - { - responseData = "did not access session data"; - } + string responseData = string.Empty; + if (context.Request.Path == new PathString("/AddDataToSession")) + { + context.Session.SetInt32("Key", 10); + responseData = "added data to session"; + } + else if (context.Request.Path == new PathString("/AccessSessionData")) + { + var value = context.Session.GetInt32("Key"); + responseData = (value == null) ? "No value found in session." : value.ToString(); + } + else if (context.Request.Path == new PathString("/DoNotAccessSessionData")) + { + responseData = "did not access session data"; + } - return context.Response.WriteAsync(responseData); + return context.Response.WriteAsync(responseData); + }); + }) + .ConfigureServices(services => + { + services.AddSingleton(typeof(ILoggerFactory), new NullLoggerFactory()); + services.AddCaching(); + services.AddSession(o => o.IdleTimeout = TimeSpan.FromMinutes(20)); + services.Configure(o => o.Clock = clock); }); - }, - services => - { - services.AddSingleton(typeof(ILoggerFactory), new NullLoggerFactory()); - services.AddCaching(); - services.AddSession(o => o.IdleTimeout = TimeSpan.FromMinutes(20)); - services.Configure(o => o.Clock = clock); - })) + + using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync("AddDataToSession"); @@ -360,28 +385,31 @@ namespace Microsoft.AspNet.Session [Fact] public async Task SessionFeature_IsUnregistered_WhenResponseGoingOut() { - using (var server = TestServer.Create(app => - { - app.Use(async (httpContext, next) => + var builder = new WebApplicationBuilder() + .Configure(app => { - await next(); + app.Use(async (httpContext, next) => + { + await next(); - Assert.Null(httpContext.Features.Get()); + Assert.Null(httpContext.Features.Get()); + }); + + app.UseSession(); + + app.Run(context => + { + context.Session.SetString("key", "value"); + return Task.FromResult(0); + }); + }) + .ConfigureServices(services => + { + services.AddCaching(); + services.AddSession(); }); - app.UseSession(); - - app.Run(context => - { - context.Session.SetString("key", "value"); - return Task.FromResult(0); - }); - }, - services => - { - services.AddCaching(); - services.AddSession(); - })) + using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync(string.Empty); @@ -392,36 +420,39 @@ namespace Microsoft.AspNet.Session [Fact] public async Task SessionFeature_IsUnregistered_WhenResponseGoingOut_AndAnUnhandledExcetionIsThrown() { - using (var server = TestServer.Create(app => - { - app.Use(async (httpContext, next) => + var builder = new WebApplicationBuilder() + .Configure(app => { - var exceptionThrown = false; - try + app.Use(async (httpContext, next) => { - await next(); - } - catch - { - exceptionThrown = true; - } + var exceptionThrown = false; + try + { + await next(); + } + catch + { + exceptionThrown = true; + } - Assert.True(exceptionThrown); - Assert.Null(httpContext.Features.Get()); + Assert.True(exceptionThrown); + Assert.Null(httpContext.Features.Get()); + }); + + app.UseSession(); + + app.Run(context => + { + throw new InvalidOperationException("An error occurred."); + }); + }) + .ConfigureServices(services => + { + services.AddCaching(); + services.AddSession(); }); - app.UseSession(); - - app.Run(context => - { - throw new InvalidOperationException("An error occurred."); - }); - }, - services => - { - services.AddCaching(); - services.AddSession(); - })) + using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync(string.Empty); @@ -434,15 +465,18 @@ namespace Microsoft.AspNet.Session // Arrange, Act & Assert var exception = await Assert.ThrowsAsync(async () => { - using (var server = TestServer.Create(app => - { - app.UseSession(); - }, - services => - { - services.AddSingleton(); - services.AddSession(); - })) + var builder = new WebApplicationBuilder() + .Configure(app => + { + app.UseSession(); + }) + .ConfigureServices(services => + { + services.AddSingleton(); + services.AddSession(); + }); + + using (var server = new TestServer(builder)) { var client = server.CreateClient(); await client.GetAsync(string.Empty); @@ -455,23 +489,26 @@ namespace Microsoft.AspNet.Session [Fact] public async Task SessionKeys_AreCaseSensitive() { - using (var server = TestServer.Create(app => - { - app.UseSession(); - app.Run(context => + var builder = new WebApplicationBuilder() + .Configure(app => { - context.Session.SetString("KEY", "VALUE"); - context.Session.SetString("key", "value"); - Assert.Equal("VALUE", context.Session.GetString("KEY")); - Assert.Equal("value", context.Session.GetString("key")); - return Task.FromResult(0); + app.UseSession(); + app.Run(context => + { + context.Session.SetString("KEY", "VALUE"); + context.Session.SetString("key", "value"); + Assert.Equal("VALUE", context.Session.GetString("KEY")); + Assert.Equal("value", context.Session.GetString("key")); + return Task.FromResult(0); + }); + }) + .ConfigureServices(services => + { + services.AddCaching(); + services.AddSession(); }); - }, - services => - { - services.AddCaching(); - services.AddSession(); - })) + + using (var server = new TestServer(builder)) { var client = server.CreateClient(); var response = await client.GetAsync(string.Empty);