I am trying to use the DefaultHttpContext object for the unit test of my middleware to handle exceptions.
My verification method is as follows:
[Fact] public async Task Invoke_ProductionNonSuredException_ReturnsProductionRequestError() { var logger = new Mock<ILogger<ExceptionHandlerMiddleware>>(); var middleWare = new ExceptionHandlerMiddleware(next: async (innerHttpContext) => { await Task.Run(() => { throw new Exception(); }); }, logger: logger.Object); var mockEnv = new Mock<IHostingEnvironment>(); mockEnv.Setup(u => u.EnvironmentName).Returns("Production"); var context = new DefaultHttpContext(); await middleWare.Invoke(context, mockEnv.Object); var reader = new StreamReader(context.Response.Body); var streamText = reader.ReadToEnd();
In my middleware, I write in context as follows:
public static Task WriteResponse(HttpContext context, HttpStatusCode statusCode, object responseData, Formatting jsonFormatting) { context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)statusCode; return context.Response.WriteAsync(JsonConvert.SerializeObject(responseData, jsonFormatting)); }
To give you more information about the middleware approach that I took, I use the approach found in here, here .
Works well when the application starts a regular pipeline. However, using the DefaultHttpContext method in the test, the response body is always returned empty, and ContentLength is null. So my streamText variable in the test is an empty string.
Is it possible to verify that middleware writes in context in this situation? Is this right, or is there a better way.
Chris source share