Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a new topic for TestServer scenarios #16953

Closed
danroth27 opened this issue Feb 11, 2020 — with docs.microsoft.com · 11 comments · Fixed by #18114
Closed

Create a new topic for TestServer scenarios #16953

danroth27 opened this issue Feb 11, 2020 — with docs.microsoft.com · 11 comments · Fixed by #18114

Comments

Copy link
Member

danroth27 commented Feb 11, 2020

In .NET Core 3.0 pretty much everything in ASP.NET Core moved to use the new generic host, but this doc still discusses using TestServer with the older web host. This doc should be updated to show how to use TestServer with the generic host. Like this: https://github.com/dotnet/aspnetcore/blob/master/src/Hosting/TestHost/test/TestServerTests.cs#L51

@dotnet-bot dotnet-bot added ⌚ Not Triaged Source - Docs.ms Docs Customer feedback via GitHub Issue labels Feb 11, 2020
@guardrex
Copy link
Collaborator

guardrex commented Feb 11, 2020

@javiercn, do you have any guidance to add for when I reach this issue?

@guardrex
Copy link
Collaborator

@danroth27 I don't understand this pattern in the context of a separate test project. Comparing the example that you linked, the host isn't created in the Arrange step ... it's a separate project that the factory bootstraps. Can you/Javier convert one test for me? ... the basic test below. I'll use that as a template to proceed with the updates. Conversely, point me to an existing sample that tests a separate project, and I can grok the pattern from it.

https://github.com/aspnet/AspNetCore.Docs/blob/master/aspnetcore/test/integration-tests/samples/3.x/IntegrationTestsSample/tests/RazorPagesProject.Tests/IntegrationTests/BasicTests.cs

public class BasicTests
        : IClassFixture<WebApplicationFactory<RazorPagesProject.Startup>>
{
    private readonly WebApplicationFactory<RazorPagesProject.Startup> _factory;

    public BasicTests(WebApplicationFactory<RazorPagesProject.Startup> factory)
    {
        _factory = factory;
    }

    [Theory]
    [InlineData("/")]
    [InlineData("/Index")]
    [InlineData("/About")]
    [InlineData("/Privacy")]
    [InlineData("/Contact")]
    public async Task Get_EndpointsReturnSuccessAndCorrectContentType(string url)
    {
        // Arrange
        var client = _factory.CreateClient();

        // Act
        var response = await client.GetAsync(url);

        // Assert
        response.EnsureSuccessStatusCode(); // Status Code 200-299
        Assert.Equal("text/html; charset=utf-8",
            response.Content.Headers.ContentType.ToString());
    }
}

@danroth27
Copy link
Member Author

Adding @Tratcher and @javiercn to get their thoughts on how this should be documented.

@Tratcher
Copy link
Member

@guardrex WebApplicationFactory is aware of both WebHost and GenericHost and creates the TestServer accordingly. You shouldn't need to modify that sample.

@danroth27 I don't see anything in this article that needs updating. I don't see any articles that deal directly with TestServer for that matter.

@guardrex
Copy link
Collaborator

@danroth27 Ok to close this?

@danroth27
Copy link
Member Author

@Tratcher @guardrex I filed this issue because a customer had pinged me about how to setup their TestServer when using HostBuilder. I pointed the customer to https://github.com/dotnet/aspnetcore/blob/master/src/Hosting/TestHost/test/TestServerTests.cs#L51, but I couldn't find this pattern explained in the docs. Is it covered and I just missed it?

@guardrex
Copy link
Collaborator

@danroth27 That pattern isn't covered in the docs, but that's by-design. IIRC, Javier said something like he/the team doesn't intend for devs to use TestServer directly in most scenarios. If you want, I'll research back and see if I can locate that discussion with exactly what was said at the time.

@Tratcher
Copy link
Member

We should add docs for TestServer, but it's scenarios are different. This integration testing doc is for testing entire applications. TestServer is built to test individual components like middleware.

@guardrex
Copy link
Collaborator

guardrex commented Feb 25, 2020

In that case, I'll mark this for a new doc. We'll need to round up the usual suspects:

  • Title
  • TOC (filename, URL)
  • UID
  • Outline

I'm burning thru Blazor issues at a decent pace. I can work the issue if one of you will provide an outline, main code examples of what should be shown (or point out one or a few framework tests to mirror), and any tips, gotchas, etc.

@guardrex guardrex added doc-idea and removed Source - Docs.ms Docs Customer feedback via GitHub Issue labels Feb 25, 2020
@guardrex guardrex changed the title Update for generic host changes in .NET Core 3.0 and later Create a new topic for TestServer scenarios Feb 25, 2020
@Tratcher
Copy link
Member

Maybe what we need is a testing section added to the middleware authoring doc:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/write?view=aspnetcore-3.1

Intro:
Middleware can be tested in isolation using the TestServer. This allows you to instantiate an app pipeline containing only the components you need to test and send custom requests to verify their behavior. The requests are sent in-memory rather than being serialized over the network and avoid concerns like port management, https certificates, etc.. Exceptions in the middleware can also flow directly back to the calling test.

Example 1: Set up your test app, add your own middleware and services
https://github.com/dotnet/aspnetcore/blob/169f07c6675712aba5cf84c41e61838cebde2906/src/Hosting/TestHost/test/TestServerTests.cs#L53-L60

Example 2: Send a request using HttpClient
https://github.com/dotnet/aspnetcore/blob/169f07c6675712aba5cf84c41e61838cebde2906/src/Hosting/TestHost/test/TestServerTests.cs#L62

Example 3:
Send a request using SendAsync(Action<HttpContext>). This lets you directly configure an HttpContext object rather than using the HttpClient abstractions. Use this when you need to manipulate structures only available on the server like HttpContext.Items or HttpContext.Features.
https://github.com/dotnet/aspnetcore/blob/169f07c6675712aba5cf84c41e61838cebde2906/src/Hosting/TestHost/test/HttpContextBuilderTests.cs#L28-L33

@guardrex
Copy link
Collaborator

guardrex commented Feb 25, 2020

We're not 100% there yet, but we've been trying to coalesce testing guidance in the Test, debug, and troubleshoot node and then cross-link it where it applies.

Thanks for that ☝️. I have enough here to work this now. If things go well with my next two Blazor tasks (CSP with Blazor and EF Core with Blazor), I think I can work this next week.

EDIT Due to high priority Blazor auth topic work, this will be pushed back a few weeks to mid- to late March.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants