Umbraco 15: Get content URL in test with WebApplic...
# help-with-umbraco
d
I have an automated test that tests the response on the URL of a content page. This used to work in Umbraco 13, but no longer works in Umbraco 15: All content items return the same url "/", so I'm calling the wrong URL for my test. Has anyone run into a similar problem and knows a solution? This is what I do: - Boot website in-memory with WebApplicationFactory - Run uSync import on boot to build the content tree - Get umbraco context factory and find the homepage content page - get url from content item, request the URL from the application and check response Example test in first response
This is what the test looks like:
Copy code
csharp
[Fact]
public async Task HomepageDirectLinkRedirectsToRootAsync()
{
    // arrange
    await PrepareAsync(); //<-- make sure site is booted, make sure indexes are built-up
    IUmbracoContextFactory umbracoContextFactory = ServiceProvider.GetRequiredService<IUmbracoContextFactory>();
    var documentNavigationService = this.ServiceProvider.GetRequiredService<IDocumentNavigationQueryService>();
    using Umbraco.Cms.Core.UmbracoContextReference cref = umbracoContextFactory.EnsureUmbracoContext();

    documentNavigationService.TryGetRootKeysOfType(WebsiteRoot.ModelTypeAlias, out var websiteKeys);
    documentNavigationService.TryGetChildrenKeysOfType(websiteKeys.First(), HomePage.ModelTypeAlias, out var homepageKeys);
    WebsiteRoot? root = await cref.UmbracoContext.Content.GetByIdAsync(websiteKeys.First()) as WebsiteRoot;
    HomePage? homepage = await cref.UmbracoContext.Content.GetByIdAsync(homepageKeys.First()) as HomePage;

    HttpClient nonRedirectClient = WebsiteFactory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false });

    // act
    HttpResponseMessage response = await nonRedirectClient.GetAsync(homepage!.Url());

    // assert
    response.StatusCode.ShouldBe(HttpStatusCode.MovedPermanently);
    response.Headers.Location.ShouldNotBeNull();
    response.Headers.Location.ShouldBe(new Uri(root!.Url(), UriKind.RelativeOrAbsolute));
}
The immediate window reveals that the URL from the homepage comes back as "/" inside the test, whereas when I debug, the backoffice shows the correct URL. It seems that this is specifically wrong in the test and not when debugging manually. https://cdn.discordapp.com/attachments/1343246411797364747/1343249226284531773/image.png?ex=67bc95d3&is=67bb4453&hm=22d90494b20ba1d835d50863fbc42b388473b45fcb0de633e803c4ea198af726& https://cdn.discordapp.com/attachments/1343246411797364747/1343249226527805440/image.png?ex=67bc95d3&is=67bb4453&hm=1af17d56e837df9597fb816190b2de6aecad844b90eedd47bd5833f68ad93cec&
I found out that after a uSync import, the content items in the backoffice all lack a URL and shows a notification that the content couldn't be routed. If I click on Save & Publish, the URL then becomes available, so it seems that something isn't updated properly when uSync imports the content
23 Views