3
Cors
Stef Heyenrath edited this page 2022-01-12 19:13:00 +01:00
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Issue

When calling WireMock.Net Server from a frontend applicatie (React / Angular), a CORS error is returned:

Cross-Origin Request Blocked: 
The Same Origin Policy disallows reading the remote resource at http://localhost:9091/__admin/mappings.
(Reason: CORS header Access-Control-Allow-Origin missing). Status code: 200.

Solution

Cors support is not enabled by default, you can enable it when configuring WireMock.Net Server.

Option 1

var settings = new WireMockServerSettings
{
   CorsPolicyOptions = CorsPolicyOptions.AllowAll
};

Note that these options are only available when running in .NET Core (3.1, 5.0 or higher)

## Option 2
Configure it manually:

``` c#
var settings = new WireMockServerSettings
{
   // Other settings
};

/* Enable Cors */
var policyName = "MyPolicy";
settings.AdditionalServiceRegistration = services =>
{
    services.AddCors(corsOptions =>
        corsOptions.AddPolicy(policyName, // ◀️ MyPolicy
            corsPolicyBuilder =>
            {
                corsPolicyBuilder
                    .AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowAnyOrigin();
            }));

    settings.Logger.Debug("Enable Cors");
};

/* Use Cors */
settings.PreWireMockMiddlewareInit = app =>
{
    var appBuilder = (IApplicationBuilder)app;
    appBuilder.UseCors(policyName); // ◀️ MyPolicy

    settings.Logger.Debug("Use Cors");
};

// Start Server
var server = WireMockServer.Start(settings);

See also WireMock.Net.StandAlone.NETCoreApp/Program.cs.

📚 References