TestServer.SendAsync when testing grpc middleware
See original GitHub issueHey, 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:
- Created a year ago
- Comments:10 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.Closing as answered.