4f44b5417a | ||
---|---|---|
.github | ||
src | ||
.gitignore | ||
code_of_conduct.md | ||
license.txt | ||
readme.md |
readme.md
Verify.AspNetCore
Extends Verify to allow verification of AspNetCore bits.
Part of the .NET Foundation
NuGet package
https://nuget.org/packages/Verify.AspNetCore/
Usage
Enable VerifyAspNetCore once at assembly load time:
VerifyAspNetCore.Enable();
Controller
Given the following controller:
using Microsoft.AspNetCore.Mvc;
public class MyController :
Controller
{
public ActionResult<List<DataItem>> Method(string input)
{
var headers = HttpContext.Response.Headers;
headers.Add("headerKey", "headerValue");
headers.Add("receivedInput", input);
var cookies = HttpContext.Response.Cookies;
cookies.Append("cookieKey", "cookieValue");
var items = new List<DataItem>
{
new("Value1"),
new("Value2")
};
return new(items);
}
public class DataItem
{
public string Value { get; }
public DataItem(string value)
{
Value = value;
}
}
}
This test:
[Fact]
public Task Test()
{
var context = new ControllerContext
{
HttpContext = new DefaultHttpContext()
};
var controller = new MyController
{
ControllerContext = context
};
var result = controller.Method("inputValue");
return Verifier.Verify(
new
{
result,
context
});
}
Will result in the following verified file:
{
result: [
{
Value: Value1
},
{
Value: Value2
}
],
context: {
Headers: {
headerKey: headerValue,
receivedInput: inputValue
},
Cookies: {
cookieKey: cookieValue
}
}
}
Middleware
Given the following middleware:
using Microsoft.AspNetCore.Http;
public class MyMiddleware
{
RequestDelegate next;
public MyMiddleware(RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(HttpContext context)
{
context.Response.Headers.Add("headerKey", "headerValue");
await next(context);
}
}
This test:
[Fact]
public async Task Test()
{
var nextCalled = false;
var middleware = new MyMiddleware(
_ =>
{
nextCalled = true;
return Task.CompletedTask;
});
var context = new DefaultHttpContext();
await middleware.Invoke(context);
await Verifier.Verify(
new
{
context.Response,
nextCalled
});
}
Will result in the following verified file:
{
Response: {
Headers: {
headerKey: headerValue
}
},
nextCalled: true
}
Icon
Spider designed by marialuisa iborra from The Noun Project.