question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TestServer.SendAsync when testing grpc middleware

See original GitHub issue

Hey, my question is related to writing integration tests for grpc middleware. As shown in the documentation Test ASP.NET Core middleware - Send requests with HttpContext :

var context = await server.SendAsync(c =>
{
    c.Request.Method = HttpMethods.Post;
    c.Request.Path = "/and/file.txt";
    c.Request.QueryString = new QueryString("?and=query");
});

It is quite easy to get access to HttpContext on the server side if you are writing tests for a REST Api server. But what about when you are writing integration tests for a grpc server? My attempt:

var httpContext = await factory.Server.SendAsync(context =>
{
    context.Request.Method = HttpMethods.Post;
    context.Request.Headers.Add("grpc-accept-encoding", new StringValues("identity,gzip"));
    context.Request.Headers.Add("Content-Type", new StringValues("application/grpc"));
    context.Request.Path = "/path.to.grpc.service.v1.SomeAPI/DoStuff";
    //
    //complex code for populating `context.Request.Body` with a grpc request payload ??
    //
});

I’m wondering if there is a better way to do this than manually like I’m trying to do above. Or is there an easy way to provide grpc body content?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JamesNKcommented, May 24, 2022

But it is not just that right? It is also the only way to retrieve the HttpContext associated with the request you sent, so that you can assert it in the end.

If you want to assert the HttpContext after a request is complete then register a middleware that captures it to a variable. Assert it after the request is complete.

[Fact]
public async Task MiddlewareTest_ReturnsNotFoundForRequest()
{
    HttpContext lastContext = null;

    using var host = await new HostBuilder()
        .ConfigureWebHost(webBuilder =>
        {
            webBuilder
                .UseTestServer()
                .ConfigureServices(services =>
                {
                    services.AddMyServices();
                })
                .Configure(app =>
                {
                    app.Use((c, next) =>
                    {
                        lastContext = c;
                        next(c);
                    });
                    app.UseMiddleware<MyMiddleware>();
                });
        })
        .StartAsync();

    var response = await host.GetTestClient().GetAsync("/");

    Assert.NotNull(lastContext);
}
0reactions
JamesNKcommented, May 31, 2022

Closing as answered.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Introduction to integration testing with xUnit and TestServer ...
In this post I'm going to demonstrate how to create some simple functional/integration tests using xUnit and TestServer for testing ASP.
Read more >
Test ASP.NET Core middleware
Middleware can be tested in isolation with TestServer. It allows you to: Instantiate an app pipeline containing only the components that you ...
Read more >
Use HttpTest inside asp.net core/grpc integration test
I have a ASP.NET core/gRPC service that I want to integration test. Inside the grpc services I make Flurl calls to 3rd party...
Read more >
Integration testing your ASP.NET Core middleware using ...
SendAsync () method call. In the example below I'm using assertion library Shouldly to assert that the correct status code is emitted and...
Read more >
How to unit test a class that consumes an HttpClient ...
All along this article I will demonstrate how to unit test a class by “mocking” the Http Response. Here is a sample of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found