[Fixes #33]AddCors should adopt new pattern
This commit is contained in:
parent
dca5829b29
commit
96c7850e4c
|
|
@ -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")));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
/// <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>
|
||||
/// <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>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection ConfigureCors(
|
||||
/// <returns>The updated <see cref="IServiceCollection"/>.</returns>
|
||||
public static IServiceCollection AddCors(
|
||||
this IServiceCollection serviceCollection,
|
||||
Action<CorsOptions> configure)
|
||||
{
|
||||
|
|
@ -32,20 +53,8 @@ namespace Microsoft.Framework.DependencyInjection
|
|||
throw new ArgumentNullException(nameof(configure));
|
||||
}
|
||||
|
||||
return serviceCollection.Configure(configure);
|
||||
}
|
||||
|
||||
/// <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;
|
||||
serviceCollection.Configure(configure);
|
||||
return serviceCollection.AddCors();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<ICorsService>();
|
||||
var mockProvider = new Mock<ICorsPolicyProvider>();
|
||||
mockProvider.Setup(o => o.GetPolicyAsync(It.IsAny<HttpContext>(), It.IsAny<string>()))
|
||||
|
|
@ -184,7 +183,7 @@ namespace Microsoft.AspNet.Cors.Test
|
|||
[Fact]
|
||||
public async Task DoesNotSetHeaders_ForNoPolicy()
|
||||
{
|
||||
// Arrange
|
||||
// Arrange
|
||||
var corsService = Mock.Of<ICorsService>();
|
||||
var mockProvider = new Mock<ICorsPolicyProvider>();
|
||||
mockProvider.Setup(o => o.GetPolicyAsync(It.IsAny<HttpContext>(), It.IsAny<string>()))
|
||||
|
|
|
|||
Loading…
Reference in New Issue