Skip to content

Commit 3c2fd84

Browse files
DamianEdwardsradical
authored andcommitted
Make *.localhost resource endpoint URLs the primary endpoint URL (#12466)
* Make *.localhost resource endpoint URLs the primary endpoint URL Fixes #12465 * Update test
1 parent 56b54eb commit 3c2fd84

3 files changed

Lines changed: 33 additions & 11 deletions

File tree

src/Aspire.Hosting/Orchestrator/ApplicationOrchestrator.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ private async Task ProcessResourceUrlCallbacks(IResource resource, CancellationT
267267
// If the additional URL is a *.localhost address we want to highlight that URL in the dashboard
268268
additionalUrl.DisplayLocation = UrlDisplayLocation.SummaryAndDetails;
269269
url.DisplayLocation = UrlDisplayLocation.DetailsOnly;
270+
271+
// Swap so that the *.localhost URL is the primary URL shown in the dashboard and targeted by `WithUrlForEndpoint` calls.
272+
(additionalUrl, url) = (url, additionalUrl);
270273
}
271274
else if ((string.Equals(endpoint.UriScheme, "http", StringComparison.OrdinalIgnoreCase) || string.Equals(endpoint.UriScheme, "https", StringComparison.OrdinalIgnoreCase))
272275
&& additionalUrl is null && EndpointHostHelpers.IsDevLocalhostTld(_dashboardUri))
@@ -278,14 +281,18 @@ private async Task ProcessResourceUrlCallbacks(IResource resource, CancellationT
278281
// Strip any "apphost" suffix that might be present on the dashboard name.
279282
subdomainSuffix = TrimSuffix(subdomainSuffix, "apphost");
280283

281-
additionalUrl = new ResourceUrlAnnotation
284+
// Make the existing localhost URL the additional URL so it's not the primary endpoint URL shown in the dashboard or targeted by `WithUrlForEndpoint` calls.
285+
additionalUrl = url;
286+
additionalUrl.DisplayLocation = UrlDisplayLocation.DetailsOnly;
287+
288+
// Create the new primary URL using the *.dev.localhost pattern.
289+
url = new ResourceUrlAnnotation
282290
{
283291
// <scheme>://<resource-name>-<subdomain-suffix>.dev.localhost:<port>
284292
Url = $"{allocatedEndpoint.UriScheme}://{resource.Name.ToLowerInvariant()}-{subdomainSuffix}.dev.localhost:{allocatedEndpoint.Port}",
285293
Endpoint = endpointReference,
286294
DisplayLocation = UrlDisplayLocation.SummaryAndDetails
287295
};
288-
url.DisplayLocation = UrlDisplayLocation.DetailsOnly;
289296

290297
static string TrimSuffix(string value, string suffix)
291298
{

tests/Aspire.Hosting.Tests/WithEndpointTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,8 @@ public async Task LocalhostTopLevelDomainSetsAnnotationValues()
637637

638638
var urls = projectA.Resource.Annotations.OfType<ResourceUrlAnnotation>();
639639
Assert.Collection(urls,
640-
url => Assert.StartsWith("https://localhost:", url.Url),
641-
url => Assert.StartsWith("https://example.localhost:", url.Url));
640+
url => Assert.StartsWith("https://example.localhost:", url.Url),
641+
url => Assert.StartsWith("https://localhost:", url.Url));
642642

643643
EndpointAnnotation endpoint = Assert.Single(projectA.Resource.Annotations.OfType<EndpointAnnotation>());
644644
Assert.NotNull(endpoint.AllocatedEndpoint);

tests/Aspire.Hosting.Tests/WithUrlsTests.cs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ public async Task EndpointsGetDevLocalhostUrlsWhenDashboardHasDevLocalhostUrl(st
250250
var tcs = new TaskCompletionSource();
251251
var projectB = builder.AddProject<ProjectB>("projectb")
252252
.WithEndpoint(scheme: "tcp")
253+
.WithUrlForEndpoint("http", u => u.DisplayText = "Custom Display Text")
253254
.OnBeforeResourceStarted((_, _, _) =>
254255
{
255256
tcs.SetResult();
@@ -261,13 +262,27 @@ public async Task EndpointsGetDevLocalhostUrlsWhenDashboardHasDevLocalhostUrl(st
261262
await tcs.Task.DefaultTimeout();
262263

263264
var urls = projectB.Resource.Annotations.OfType<ResourceUrlAnnotation>();
264-
Assert.Equal(3, urls.Count());
265-
Assert.Single(urls, u => u.Url.StartsWith("http://localhost") && u.Endpoint?.EndpointName == "http" && u.DisplayLocation == UrlDisplayLocation.DetailsOnly);
266-
Assert.Single(urls, u => u.Url.StartsWith($"http://{projectB.Resource.Name.ToLowerInvariant()}{expectedHostSuffix}") && u.Url.EndsWith("/sub-path")
267-
&& u.Endpoint?.EndpointName == "http" && u.DisplayLocation == UrlDisplayLocation.SummaryAndDetails);
268-
269-
Assert.Single(urls, u => u.Url.StartsWith("tcp://localhost") && u.Endpoint?.EndpointName == "tcp");
270-
Assert.DoesNotContain(urls, u => u.Url.Contains(expectedHostSuffix) && u.Endpoint?.EndpointName == "tcp");
265+
Assert.Collection(urls,
266+
u =>
267+
{
268+
Assert.StartsWith($"http://{projectB.Resource.Name.ToLowerInvariant()}{expectedHostSuffix}", u.Url);
269+
Assert.EndsWith("/sub-path", u.Url);
270+
Assert.Equal("http", u.Endpoint?.EndpointName);
271+
Assert.Equal(UrlDisplayLocation.SummaryAndDetails, u.DisplayLocation);
272+
Assert.Equal("Custom Display Text", u.DisplayText);
273+
},
274+
u =>
275+
{
276+
Assert.StartsWith("http://localhost", u.Url);
277+
Assert.Equal("http", u.Endpoint?.EndpointName);
278+
Assert.Equal(UrlDisplayLocation.DetailsOnly, u.DisplayLocation);
279+
},
280+
u =>
281+
{
282+
Assert.StartsWith("tcp://localhost", u.Url);
283+
Assert.Equal("tcp", u.Endpoint?.EndpointName);
284+
}
285+
);
271286

272287
await app.StopAsync();
273288
}

0 commit comments

Comments
 (0)