Skip to content
This repository has been archived by the owner on Nov 22, 2018. It is now read-only.

Commit

Permalink
[Fixes #33]AddCors should adopt new pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
kichalla committed Sep 11, 2015
1 parent c2b9fc5 commit a5220ae
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
4 changes: 1 addition & 3 deletions samples/UseOptions/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.ConfigureCors(
options =>
services.AddCors(options =>
options.AddPolicy("allowSingleOrigin", builder => builder.WithOrigins("http://example.com")));
}

Expand Down
27 changes: 14 additions & 13 deletions src/Microsoft.AspNet.Cors.Core/CorsServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,6 @@ namespace Microsoft.Framework.DependencyInjection
/// </summary>
public static class CorsServiceCollectionExtensions
{
/// <summary>
/// Can be used to configure services in the <paramref name="serviceCollection"/>.
/// </summary>
/// <param name="serviceCollection">The service collection which needs to be configured.</param>
/// <param name="configure">A delegate which is run to configure the services.</param>
/// <returns></returns>
public static IServiceCollection ConfigureCors(
[NotNull] this IServiceCollection serviceCollection,
[NotNull] Action<CorsOptions> configure)
{
return serviceCollection.Configure(configure);
}

/// <summary>
/// Add services needed to support CORS to the given <paramref name="serviceCollection"/>.
/// </summary>
Expand All @@ -40,5 +27,19 @@ public static IServiceCollection AddCors(this IServiceCollection serviceCollecti
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>The updated <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddCors(
[NotNull] this IServiceCollection serviceCollection,
[NotNull] Action<CorsOptions> configure)
{
serviceCollection.Configure(configure);
return serviceCollection.AddCors();
}
}
}
11 changes: 5 additions & 6 deletions test/Microsoft.AspNet.Cors.Test/CorsMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public async Task CorsRequest_MatchPolicy_SetsResponseHeaders()
// Arrange
using (var server = TestServer.Create(app =>
{
app.UseCors(builder =>
app.UseCors(builder =>
builder.WithOrigins("http://localhost:5001")
.WithMethods("PUT")
.WithHeaders("Header1")
Expand Down Expand Up @@ -70,8 +70,7 @@ public async Task PreFlight_MatchesPolicy_SetsResponseHeaders()
},
services =>
{
services.AddCors();
services.ConfigureCors(options =>
services.AddCors(options =>
{
options.AddPolicy("customPolicy", policy);
});
Expand Down Expand Up @@ -129,7 +128,7 @@ public async Task CorsRequest_DoesNotMatchPolicy_DoesNotSetHeaders()
// Arrange
using (var server = TestServer.Create(app =>
{
app.UseCors(builder =>
app.UseCors(builder =>
builder.WithOrigins("http://localhost:5001")
.WithMethods("PUT")
.WithHeaders("Header1")
Expand All @@ -156,7 +155,7 @@ public async Task CorsRequest_DoesNotMatchPolicy_DoesNotSetHeaders()
[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>()))
Expand Down Expand Up @@ -184,7 +183,7 @@ public async Task Uses_PolicyProvider_AsFallback()
[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>()))
Expand Down

0 comments on commit a5220ae

Please sign in to comment.