[Fixes #33]AddCors should adopt new pattern

This commit is contained in:
Kiran Challa 2015-09-11 10:35:14 -07:00
parent dca5829b29
commit 96c7850e4c
3 changed files with 33 additions and 27 deletions

View File

@ -9,9 +9,7 @@ namespace UseOptions
{ {
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddCors(); services.AddCors(options =>
services.ConfigureCors(
options =>
options.AddPolicy("allowSingleOrigin", builder => builder.WithOrigins("http://example.com"))); options.AddPolicy("allowSingleOrigin", builder => builder.WithOrigins("http://example.com")));
} }

View File

@ -2,8 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using Microsoft.AspNet.Cors;
using Microsoft.AspNet.Cors.Core; using Microsoft.AspNet.Cors.Core;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection.Extensions; using Microsoft.Framework.DependencyInjection.Extensions;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection namespace Microsoft.Framework.DependencyInjection
{ {
@ -13,12 +16,30 @@ namespace Microsoft.Framework.DependencyInjection
public static class CorsServiceCollectionExtensions public static class CorsServiceCollectionExtensions
{ {
/// <summary> /// <summary>
/// Can be used to configure services in the <paramref name="serviceCollection"/>. /// Add services needed to support CORS to the given <paramref name="serviceCollection"/>.
/// </summary> /// </summary>
/// <param name="serviceCollection">The service collection which needs to be configured.</param> /// <param name="serviceCollection">The service collection to which CORS services are added.</param>
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddCors(this IServiceCollection serviceCollection)
{
if (serviceCollection == null)
{
throw new ArgumentNullException(nameof(serviceCollection));
}
serviceCollection.AddOptions();
serviceCollection.TryAdd(ServiceDescriptor.Transient<ICorsService, CorsService>());
serviceCollection.TryAdd(ServiceDescriptor.Transient<ICorsPolicyProvider, DefaultCorsPolicyProvider>());
return serviceCollection;
}
/// <summary>
/// Add services needed to support CORS to the given <paramref name="serviceCollection"/>.
/// </summary>
/// <param name="serviceCollection">The service collection to which CORS services are added.</param>
/// <param name="configure">A delegate which is run to configure the services.</param> /// <param name="configure">A delegate which is run to configure the services.</param>
/// <returns></returns> /// <returns>The updated <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection ConfigureCors( public static IServiceCollection AddCors(
this IServiceCollection serviceCollection, this IServiceCollection serviceCollection,
Action<CorsOptions> configure) Action<CorsOptions> configure)
{ {
@ -32,20 +53,8 @@ namespace Microsoft.Framework.DependencyInjection
throw new ArgumentNullException(nameof(configure)); throw new ArgumentNullException(nameof(configure));
} }
return serviceCollection.Configure(configure); serviceCollection.Configure(configure);
} return serviceCollection.AddCors();
/// <summary>
/// Add services needed to support CORS to the given <paramref name="serviceCollection"/>.
/// </summary>
/// <param name="serviceCollection">The service collection to which CORS services are added.</param>
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddCors(this IServiceCollection serviceCollection)
{
serviceCollection.AddOptions();
serviceCollection.TryAdd(ServiceDescriptor.Transient<ICorsService, CorsService>());
serviceCollection.TryAdd(ServiceDescriptor.Transient<ICorsPolicyProvider, DefaultCorsPolicyProvider>());
return serviceCollection;
} }
} }
} }

View File

@ -23,7 +23,7 @@ namespace Microsoft.AspNet.Cors.Test
// Arrange // Arrange
using (var server = TestServer.Create(app => using (var server = TestServer.Create(app =>
{ {
app.UseCors(builder => app.UseCors(builder =>
builder.WithOrigins("http://localhost:5001") builder.WithOrigins("http://localhost:5001")
.WithMethods("PUT") .WithMethods("PUT")
.WithHeaders("Header1") .WithHeaders("Header1")
@ -70,8 +70,7 @@ namespace Microsoft.AspNet.Cors.Test
}, },
services => services =>
{ {
services.AddCors(); services.AddCors(options =>
services.ConfigureCors(options =>
{ {
options.AddPolicy("customPolicy", policy); options.AddPolicy("customPolicy", policy);
}); });
@ -129,7 +128,7 @@ namespace Microsoft.AspNet.Cors.Test
// Arrange // Arrange
using (var server = TestServer.Create(app => using (var server = TestServer.Create(app =>
{ {
app.UseCors(builder => app.UseCors(builder =>
builder.WithOrigins("http://localhost:5001") builder.WithOrigins("http://localhost:5001")
.WithMethods("PUT") .WithMethods("PUT")
.WithHeaders("Header1") .WithHeaders("Header1")
@ -156,7 +155,7 @@ namespace Microsoft.AspNet.Cors.Test
[Fact] [Fact]
public async Task Uses_PolicyProvider_AsFallback() public async Task Uses_PolicyProvider_AsFallback()
{ {
// Arrange // Arrange
var corsService = Mock.Of<ICorsService>(); var corsService = Mock.Of<ICorsService>();
var mockProvider = new Mock<ICorsPolicyProvider>(); var mockProvider = new Mock<ICorsPolicyProvider>();
mockProvider.Setup(o => o.GetPolicyAsync(It.IsAny<HttpContext>(), It.IsAny<string>())) mockProvider.Setup(o => o.GetPolicyAsync(It.IsAny<HttpContext>(), It.IsAny<string>()))
@ -184,7 +183,7 @@ namespace Microsoft.AspNet.Cors.Test
[Fact] [Fact]
public async Task DoesNotSetHeaders_ForNoPolicy() public async Task DoesNotSetHeaders_ForNoPolicy()
{ {
// Arrange // Arrange
var corsService = Mock.Of<ICorsService>(); var corsService = Mock.Of<ICorsService>();
var mockProvider = new Mock<ICorsPolicyProvider>(); var mockProvider = new Mock<ICorsPolicyProvider>();
mockProvider.Setup(o => o.GetPolicyAsync(It.IsAny<HttpContext>(), It.IsAny<string>())) mockProvider.Setup(o => o.GetPolicyAsync(It.IsAny<HttpContext>(), It.IsAny<string>()))