Skip to content

Commit 039f225

Browse files
committed
test
1 parent 68ec468 commit 039f225

File tree

2 files changed

+101
-35
lines changed

2 files changed

+101
-35
lines changed

src/Aspire.Hosting/Orchestrator/ApplicationOrchestrator.cs

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -229,54 +229,54 @@ private async Task ProcessResourceUrlCallbacks(IResource resource, CancellationT
229229
}
230230
}
231231
}
232+
}
232233

233-
if (resource.TryGetUrls(out var existingUrls))
234-
{
235-
// Static URLs added to the resource via WithUrl(string name, string url), i.e. not callback-based
236-
urls.AddRange(existingUrls);
237-
}
234+
if (resource.TryGetUrls(out var existingUrls))
235+
{
236+
// Static URLs added to the resource via WithUrl(string name, string url), i.e. not callback-based
237+
urls.AddRange(existingUrls);
238+
}
238239

239-
// Run the URL callbacks
240-
if (resource.TryGetAnnotationsOfType<ResourceUrlsCallbackAnnotation>(out var callbacks))
240+
// Run the URL callbacks
241+
if (resource.TryGetAnnotationsOfType<ResourceUrlsCallbackAnnotation>(out var callbacks))
242+
{
243+
var urlsCallbackContext = new ResourceUrlsCallbackContext(_executionContext, resource, urls, cancellationToken)
241244
{
242-
var urlsCallbackContext = new ResourceUrlsCallbackContext(_executionContext, resource, urls, cancellationToken)
243-
{
244-
Logger = _loggerService.GetLogger(resource.Name)
245-
};
246-
foreach (var callback in callbacks)
247-
{
248-
await callback.Callback(urlsCallbackContext).ConfigureAwait(false);
249-
}
245+
Logger = _loggerService.GetLogger(resource.Name)
246+
};
247+
foreach (var callback in callbacks)
248+
{
249+
await callback.Callback(urlsCallbackContext).ConfigureAwait(false);
250250
}
251+
}
251252

252-
// Clear existing URLs
253-
if (existingUrls is not null)
253+
// Clear existing URLs
254+
if (existingUrls is not null)
255+
{
256+
var existing = existingUrls.ToArray();
257+
for (var i = existing.Length - 1; i >= 0; i--)
254258
{
255-
var existing = existingUrls.ToArray();
256-
for (var i = existing.Length - 1; i >= 0; i--)
257-
{
258-
var url = existing[i];
259-
resource.Annotations.Remove(url);
260-
}
259+
var url = existing[i];
260+
resource.Annotations.Remove(url);
261261
}
262+
}
262263

263-
// Convert relative endpoint URLs to absolute URLs
264-
foreach (var url in urls)
264+
// Convert relative endpoint URLs to absolute URLs
265+
foreach (var url in urls)
266+
{
267+
if (url.Endpoint is { } endpoint)
265268
{
266-
if (url.Endpoint is { } endpoint)
269+
if (url.Url.StartsWith('/') && endpoint.AllocatedEndpoint is { } allocatedEndpoint)
267270
{
268-
if (url.Url.StartsWith('/') && endpoint.AllocatedEndpoint is { } allocatedEndpoint)
269-
{
270-
url.Url = allocatedEndpoint.UriString.TrimEnd('/') + url.Url;
271-
}
271+
url.Url = allocatedEndpoint.UriString.TrimEnd('/') + url.Url;
272272
}
273273
}
274+
}
274275

275-
// Add URLs
276-
foreach (var url in urls)
277-
{
278-
resource.Annotations.Add(url);
279-
}
276+
// Add URLs
277+
foreach (var url in urls)
278+
{
279+
resource.Annotations.Add(url);
280280
}
281281
}
282282

tests/Aspire.Hosting.Tests/WithEndpointTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,72 @@ public void WithEndpoint_WithAllArguments_ForwardsAllArguments()
617617
Assert.Equal(System.Net.Sockets.ProtocolType.Tcp, endpoint.Protocol);
618618
}
619619

620+
[Fact]
621+
public async Task LocalhostTopLevelDomainSetsAnnotationValues()
622+
{
623+
using var builder = TestDistributedApplicationBuilder.Create();
624+
625+
var tcs = new TaskCompletionSource();
626+
var projectA = builder.AddProject<ProjectA>("projecta")
627+
.WithHttpsEndpoint()
628+
.WithEndpoint("https", e => e.TargetHost = "example.localhost", createIfNotExists: false)
629+
.OnBeforeResourceStarted((_, _, _) =>
630+
{
631+
tcs.SetResult();
632+
return Task.CompletedTask;
633+
});
634+
635+
var app = await builder.BuildAsync();
636+
await app.StartAsync();
637+
await tcs.Task;
638+
639+
var urls = projectA.Resource.Annotations.OfType<ResourceUrlAnnotation>();
640+
Assert.Collection(urls,
641+
url => Assert.StartsWith("https://localhost:", url.Url),
642+
url => Assert.StartsWith("https://example.localhost:", url.Url));
643+
644+
EndpointAnnotation endpoint = Assert.Single(projectA.Resource.Annotations.OfType<EndpointAnnotation>());
645+
Assert.NotNull(endpoint.AllocatedEndpoint);
646+
Assert.Equal(EndpointBindingMode.SingleAddress, endpoint.AllocatedEndpoint.BindingMode);
647+
Assert.Equal("localhost", endpoint.AllocatedEndpoint.Address);
648+
649+
await app.StopAsync();
650+
}
651+
652+
[Theory]
653+
[InlineData("0.0.0.0", EndpointBindingMode.IPv4AnyAddresses)]
654+
//[InlineData("::", EndpointBindingMode.IPv6AnyAddresses)] // Need to figure out a good way to check that Ipv6 binding is supported
655+
public async Task TopLevelDomainSetsAnnotationValues(string host, EndpointBindingMode endpointBindingMode)
656+
{
657+
using var builder = TestDistributedApplicationBuilder.Create();
658+
659+
var tcs = new TaskCompletionSource();
660+
var projectA = builder.AddProject<ProjectA>("projecta")
661+
.WithHttpsEndpoint()
662+
.WithEndpoint("https", e => e.TargetHost = host, createIfNotExists: false)
663+
.OnBeforeResourceStarted((_, _, _) =>
664+
{
665+
tcs.SetResult();
666+
return Task.CompletedTask;
667+
});
668+
669+
var app = await builder.BuildAsync();
670+
await app.StartAsync();
671+
await tcs.Task;
672+
673+
var urls = projectA.Resource.Annotations.OfType<ResourceUrlAnnotation>();
674+
Assert.Collection(urls,
675+
url => Assert.StartsWith("https://localhost:", url.Url),
676+
url => Assert.StartsWith($"https://{Environment.MachineName}:", url.Url));
677+
678+
EndpointAnnotation endpoint = Assert.Single(projectA.Resource.Annotations.OfType<EndpointAnnotation>());
679+
Assert.NotNull(endpoint.AllocatedEndpoint);
680+
Assert.Equal(endpointBindingMode, endpoint.AllocatedEndpoint.BindingMode);
681+
Assert.Equal("localhost", endpoint.AllocatedEndpoint.Address);
682+
683+
await app.StopAsync();
684+
}
685+
620686
private sealed class TestProject : IProjectMetadata
621687
{
622688
public string ProjectPath => "projectpath";

0 commit comments

Comments
 (0)