Перейти к файлу
Simon Cropp 4f44b5417a refs 2021-11-21 23:03:09 +11:00
.github . 2021-10-01 15:29:48 +10:00
src refs 2021-11-21 23:03:09 +11:00
.gitignore . 2021-10-01 15:29:48 +10:00
code_of_conduct.md . 2021-10-01 15:29:48 +10:00
license.txt . 2021-10-01 15:29:48 +10:00
readme.md Docs changes 2021-10-11 22:50:05 +00:00

readme.md

Verify.AspNetCore

Build status NuGet Status

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();

snippet source | anchor

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;
        }
    }
}

snippet source | anchor

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
        });
}

snippet source | anchor

Will result in the following verified file:

{
  result: [
    {
      Value: Value1
    },
    {
      Value: Value2
    }
  ],
  context: {
    Headers: {
      headerKey: headerValue,
      receivedInput: inputValue
    },
    Cookies: {
      cookieKey: cookieValue
    }
  }
}

snippet source | anchor

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);
    }
}

snippet source | anchor

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
        });
}

snippet source | anchor

Will result in the following verified file:

{
  Response: {
    Headers: {
      headerKey: headerValue
    }
  },
  nextCalled: true
}

snippet source | anchor

Icon

Spider designed by marialuisa iborra from The Noun Project.