|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Net; |
| 5 | +using Aspire.Hosting; |
| 6 | +using Xunit; |
| 7 | +using Xunit.Abstractions; |
| 8 | + |
| 9 | +namespace Aspire.Dashboard.Tests.Integration; |
| 10 | + |
| 11 | +public class OtlpCorsHttpServiceTests |
| 12 | +{ |
| 13 | + private readonly ITestOutputHelper _testOutputHelper; |
| 14 | + |
| 15 | + public OtlpCorsHttpServiceTests(ITestOutputHelper testOutputHelper) |
| 16 | + { |
| 17 | + _testOutputHelper = testOutputHelper; |
| 18 | + } |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public async Task ReceivePreflight_OtlpHttpEndPoint_NoCorsConfiguration_NotFound() |
| 22 | + { |
| 23 | + // Arrange |
| 24 | + await using var app = IntegrationTestHelpers.CreateDashboardWebApplication(_testOutputHelper); |
| 25 | + await app.StartAsync(); |
| 26 | + |
| 27 | + using var httpClient = IntegrationTestHelpers.CreateHttpClient($"http://{app.OtlpServiceHttpEndPointAccessor().EndPoint}"); |
| 28 | + |
| 29 | + var preflightRequest = new HttpRequestMessage(HttpMethod.Options, "/v1/logs"); |
| 30 | + preflightRequest.Headers.TryAddWithoutValidation("Access-Control-Request-Method", "POST"); |
| 31 | + preflightRequest.Headers.TryAddWithoutValidation("Access-Control-Request-Headers", "x-requested-with,x-custom,Content-Type"); |
| 32 | + preflightRequest.Headers.TryAddWithoutValidation("Origin", "http://localhost:8000"); |
| 33 | + |
| 34 | + // Act |
| 35 | + var responseMessage = await httpClient.SendAsync(preflightRequest); |
| 36 | + |
| 37 | + // Assert |
| 38 | + Assert.Equal(HttpStatusCode.NotFound, responseMessage.StatusCode); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public async Task ReceivePreflight_OtlpHttpEndPoint_ValidCorsOrigin_Success() |
| 43 | + { |
| 44 | + // Arrange |
| 45 | + await using var app = IntegrationTestHelpers.CreateDashboardWebApplication(_testOutputHelper, config => |
| 46 | + { |
| 47 | + config[DashboardConfigNames.DashboardOtlpCorsAllowedOriginsKeyName.ConfigKey] = "http://localhost:8000, http://localhost:8001"; |
| 48 | + }); |
| 49 | + await app.StartAsync(); |
| 50 | + |
| 51 | + using var httpClient = IntegrationTestHelpers.CreateHttpClient($"http://{app.OtlpServiceHttpEndPointAccessor().EndPoint}"); |
| 52 | + |
| 53 | + // Act 1 |
| 54 | + var preflightRequest1 = new HttpRequestMessage(HttpMethod.Options, "/v1/logs"); |
| 55 | + preflightRequest1.Headers.TryAddWithoutValidation("Access-Control-Request-Method", "POST"); |
| 56 | + preflightRequest1.Headers.TryAddWithoutValidation("Access-Control-Request-Headers", "x-requested-with,x-custom,Content-Type"); |
| 57 | + preflightRequest1.Headers.TryAddWithoutValidation("Origin", "http://localhost:8000"); |
| 58 | + |
| 59 | + var responseMessage1 = await httpClient.SendAsync(preflightRequest1); |
| 60 | + |
| 61 | + // Assert 1 |
| 62 | + Assert.Equal(HttpStatusCode.NoContent, responseMessage1.StatusCode); |
| 63 | + Assert.Equal("http://localhost:8000", responseMessage1.Headers.GetValues("Access-Control-Allow-Origin").Single()); |
| 64 | + Assert.Equal("POST", responseMessage1.Headers.GetValues("Access-Control-Allow-Methods").Single()); |
| 65 | + Assert.Equal("X-Requested-With", responseMessage1.Headers.GetValues("Access-Control-Allow-Headers").Single()); |
| 66 | + |
| 67 | + // Act 2 |
| 68 | + var preflightRequest2 = new HttpRequestMessage(HttpMethod.Options, "/v1/logs"); |
| 69 | + preflightRequest2.Headers.TryAddWithoutValidation("Access-Control-Request-Method", "POST"); |
| 70 | + preflightRequest2.Headers.TryAddWithoutValidation("Access-Control-Request-Headers", "x-requested-with,x-custom,Content-Type"); |
| 71 | + preflightRequest2.Headers.TryAddWithoutValidation("Origin", "http://localhost:8001"); |
| 72 | + |
| 73 | + var responseMessage2 = await httpClient.SendAsync(preflightRequest2); |
| 74 | + |
| 75 | + // Assert 2 |
| 76 | + Assert.Equal(HttpStatusCode.NoContent, responseMessage2.StatusCode); |
| 77 | + Assert.Equal("http://localhost:8001", responseMessage2.Headers.GetValues("Access-Control-Allow-Origin").Single()); |
| 78 | + Assert.Equal("POST", responseMessage2.Headers.GetValues("Access-Control-Allow-Methods").Single()); |
| 79 | + Assert.Equal("X-Requested-With", responseMessage2.Headers.GetValues("Access-Control-Allow-Headers").Single()); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public async Task ReceivePreflight_OtlpHttpEndPoint_InvalidCorsOrigin_NoCorsHeadersReturned() |
| 84 | + { |
| 85 | + // Arrange |
| 86 | + await using var app = IntegrationTestHelpers.CreateDashboardWebApplication(_testOutputHelper, config => |
| 87 | + { |
| 88 | + config[DashboardConfigNames.DashboardOtlpCorsAllowedOriginsKeyName.ConfigKey] = "http://localhost:8000"; |
| 89 | + }); |
| 90 | + await app.StartAsync(); |
| 91 | + |
| 92 | + using var httpClient = IntegrationTestHelpers.CreateHttpClient($"http://{app.OtlpServiceHttpEndPointAccessor().EndPoint}"); |
| 93 | + |
| 94 | + var preflightRequest = new HttpRequestMessage(HttpMethod.Options, "/v1/logs"); |
| 95 | + preflightRequest.Headers.TryAddWithoutValidation("Access-Control-Request-Method", "POST"); |
| 96 | + preflightRequest.Headers.TryAddWithoutValidation("Access-Control-Request-Headers", "x-requested-with,x-custom,Content-Type"); |
| 97 | + preflightRequest.Headers.TryAddWithoutValidation("Origin", "http://localhost:8001"); |
| 98 | + |
| 99 | + // Act |
| 100 | + var responseMessage = await httpClient.SendAsync(preflightRequest); |
| 101 | + |
| 102 | + // Assert |
| 103 | + Assert.Equal(HttpStatusCode.NoContent, responseMessage.StatusCode); |
| 104 | + Assert.False(responseMessage.Headers.Contains("Access-Control-Allow-Origin")); |
| 105 | + Assert.False(responseMessage.Headers.Contains("Access-Control-Allow-Methods")); |
| 106 | + Assert.False(responseMessage.Headers.Contains("Access-Control-Allow-Headers")); |
| 107 | + } |
| 108 | + |
| 109 | + [Fact] |
| 110 | + public async Task ReceivePreflight_OtlpHttpEndPoint_AnyOrigin_Success() |
| 111 | + { |
| 112 | + // Arrange |
| 113 | + await using var app = IntegrationTestHelpers.CreateDashboardWebApplication(_testOutputHelper, config => |
| 114 | + { |
| 115 | + config[DashboardConfigNames.DashboardOtlpCorsAllowedOriginsKeyName.ConfigKey] = "*"; |
| 116 | + config[DashboardConfigNames.DashboardOtlpCorsAllowedHeadersKeyName.ConfigKey] = "*"; |
| 117 | + }); |
| 118 | + await app.StartAsync(); |
| 119 | + |
| 120 | + using var httpClient = IntegrationTestHelpers.CreateHttpClient($"http://{app.OtlpServiceHttpEndPointAccessor().EndPoint}"); |
| 121 | + |
| 122 | + var preflightRequest = new HttpRequestMessage(HttpMethod.Options, "/v1/logs"); |
| 123 | + preflightRequest.Headers.TryAddWithoutValidation("Access-Control-Request-Method", "POST"); |
| 124 | + preflightRequest.Headers.TryAddWithoutValidation("Access-Control-Request-Headers", "x-requested-with,x-custom,Content-Type"); |
| 125 | + preflightRequest.Headers.TryAddWithoutValidation("Origin", "http://localhost:8000"); |
| 126 | + |
| 127 | + // Act |
| 128 | + var responseMessage = await httpClient.SendAsync(preflightRequest); |
| 129 | + |
| 130 | + // Assert |
| 131 | + Assert.Equal(HttpStatusCode.NoContent, responseMessage.StatusCode); |
| 132 | + Assert.Equal("*", responseMessage.Headers.GetValues("Access-Control-Allow-Origin").Single()); |
| 133 | + Assert.Equal("POST", responseMessage.Headers.GetValues("Access-Control-Allow-Methods").Single()); |
| 134 | + Assert.Equal("x-requested-with,x-custom,Content-Type", responseMessage.Headers.GetValues("Access-Control-Allow-Headers").Single()); |
| 135 | + } |
| 136 | +} |
0 commit comments