Skip to content

Commit

Permalink
Change tests to remove build xunit warning and add OTLP test (#3507)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Apr 9, 2024
1 parent 2525ce1 commit 8c1e37a
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Dashboard.Authentication.OtlpConnection;
using Aspire.Dashboard.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Primitives;
using Xunit;

namespace Aspire.Dashboard.Tests;
Expand All @@ -22,7 +24,7 @@ public async Task InvokeAsync_Development_AllowExternalFetch()
await middleware.InvokeAsync(httpContext);

// Assert
Assert.NotEmpty(httpContext.Response.Headers.ContentSecurityPolicy);
Assert.NotEqual(StringValues.Empty, httpContext.Response.Headers.ContentSecurityPolicy);
Assert.DoesNotContain("default-src", httpContext.Response.Headers.ContentSecurityPolicy.ToString());
}

Expand All @@ -37,10 +39,29 @@ public async Task InvokeAsync_Production_DenyExternalFetch()
await middleware.InvokeAsync(httpContext);

// Assert
Assert.NotEmpty(httpContext.Response.Headers.ContentSecurityPolicy);
Assert.NotEqual(StringValues.Empty, httpContext.Response.Headers.ContentSecurityPolicy);
Assert.Contains("default-src", httpContext.Response.Headers.ContentSecurityPolicy.ToString());
}

[Fact]
public async Task InvokeAsync_Otlp_NotAdded()
{
// Arrange
var middleware = CreateMiddleware(environmentName: "Production");
var httpContext = new DefaultHttpContext();
httpContext.Features.Set<IOtlpConnectionFeature>(new OtlpConnectionFeature());

// Act
await middleware.InvokeAsync(httpContext);

// Assert
Assert.Equal(StringValues.Empty, httpContext.Response.Headers.ContentSecurityPolicy);
}

private sealed class OtlpConnectionFeature : IOtlpConnectionFeature
{
}

private static BrowserSecurityHeadersMiddleware CreateMiddleware(string environmentName) =>
new BrowserSecurityHeadersMiddleware(c => Task.CompletedTask, new TestHostEnvironment { EnvironmentName = environmentName });

Expand Down

0 comments on commit 8c1e37a

Please sign in to comment.