From 96c7850e4c17d34b8ea35307e551bbf76c21bd49 Mon Sep 17 00:00:00 2001 From: Kiran Challa Date: Fri, 11 Sep 2015 10:35:14 -0700 Subject: [PATCH] [Fixes #33]AddCors should adopt new pattern --- samples/UseOptions/Startup.cs | 4 +- .../CorsServiceCollectionExtensions.cs | 45 +++++++++++-------- .../CorsMiddlewareTests.cs | 11 +++-- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/samples/UseOptions/Startup.cs b/samples/UseOptions/Startup.cs index 5c29d7f67d..732cfbca1e 100644 --- a/samples/UseOptions/Startup.cs +++ b/samples/UseOptions/Startup.cs @@ -9,9 +9,7 @@ namespace UseOptions { public void ConfigureServices(IServiceCollection services) { - services.AddCors(); - services.ConfigureCors( - options => + services.AddCors(options => options.AddPolicy("allowSingleOrigin", builder => builder.WithOrigins("http://example.com"))); } diff --git a/src/Microsoft.AspNet.Cors.Core/CorsServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Cors.Core/CorsServiceCollectionExtensions.cs index e46363ad97..e4e3eccf6b 100644 --- a/src/Microsoft.AspNet.Cors.Core/CorsServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Cors.Core/CorsServiceCollectionExtensions.cs @@ -2,8 +2,11 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using Microsoft.AspNet.Cors; using Microsoft.AspNet.Cors.Core; +using Microsoft.Framework.Configuration; using Microsoft.Framework.DependencyInjection.Extensions; +using Microsoft.Framework.Internal; namespace Microsoft.Framework.DependencyInjection { @@ -13,12 +16,30 @@ namespace Microsoft.Framework.DependencyInjection public static class CorsServiceCollectionExtensions { /// - /// Can be used to configure services in the . + /// Add services needed to support CORS to the given . /// - /// The service collection which needs to be configured. + /// The service collection to which CORS services are added. + /// The updated . + public static IServiceCollection AddCors(this IServiceCollection serviceCollection) + { + if (serviceCollection == null) + { + throw new ArgumentNullException(nameof(serviceCollection)); + } + + serviceCollection.AddOptions(); + serviceCollection.TryAdd(ServiceDescriptor.Transient()); + serviceCollection.TryAdd(ServiceDescriptor.Transient()); + return serviceCollection; + } + + /// + /// Add services needed to support CORS to the given . + /// + /// The service collection to which CORS services are added. /// A delegate which is run to configure the services. - /// - public static IServiceCollection ConfigureCors( + /// The updated . + public static IServiceCollection AddCors( this IServiceCollection serviceCollection, Action configure) { @@ -32,20 +53,8 @@ namespace Microsoft.Framework.DependencyInjection throw new ArgumentNullException(nameof(configure)); } - return serviceCollection.Configure(configure); - } - - /// - /// Add services needed to support CORS to the given . - /// - /// The service collection to which CORS services are added. - /// The updated . - public static IServiceCollection AddCors(this IServiceCollection serviceCollection) - { - serviceCollection.AddOptions(); - serviceCollection.TryAdd(ServiceDescriptor.Transient()); - serviceCollection.TryAdd(ServiceDescriptor.Transient()); - return serviceCollection; + serviceCollection.Configure(configure); + return serviceCollection.AddCors(); } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Cors.Test/CorsMiddlewareTests.cs b/test/Microsoft.AspNet.Cors.Test/CorsMiddlewareTests.cs index 59e693b996..a8906b1f49 100644 --- a/test/Microsoft.AspNet.Cors.Test/CorsMiddlewareTests.cs +++ b/test/Microsoft.AspNet.Cors.Test/CorsMiddlewareTests.cs @@ -23,7 +23,7 @@ namespace Microsoft.AspNet.Cors.Test // Arrange using (var server = TestServer.Create(app => { - app.UseCors(builder => + app.UseCors(builder => builder.WithOrigins("http://localhost:5001") .WithMethods("PUT") .WithHeaders("Header1") @@ -70,8 +70,7 @@ namespace Microsoft.AspNet.Cors.Test }, services => { - services.AddCors(); - services.ConfigureCors(options => + services.AddCors(options => { options.AddPolicy("customPolicy", policy); }); @@ -129,7 +128,7 @@ namespace Microsoft.AspNet.Cors.Test // Arrange using (var server = TestServer.Create(app => { - app.UseCors(builder => + app.UseCors(builder => builder.WithOrigins("http://localhost:5001") .WithMethods("PUT") .WithHeaders("Header1") @@ -156,7 +155,7 @@ namespace Microsoft.AspNet.Cors.Test [Fact] public async Task Uses_PolicyProvider_AsFallback() { - // Arrange + // Arrange var corsService = Mock.Of(); var mockProvider = new Mock(); mockProvider.Setup(o => o.GetPolicyAsync(It.IsAny(), It.IsAny())) @@ -184,7 +183,7 @@ namespace Microsoft.AspNet.Cors.Test [Fact] public async Task DoesNotSetHeaders_ForNoPolicy() { - // Arrange + // Arrange var corsService = Mock.Of(); var mockProvider = new Mock(); mockProvider.Setup(o => o.GetPolicyAsync(It.IsAny(), It.IsAny()))