8000 GitHub - VerifyTests/Verify.AspNetCore: Extends Verify to allow verification of AspNetCore bits.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

VerifyTests/Verify.AspNetCore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verify.AspNetCore

Discussions Build status NuGet Status

Extends Verify to allow verification of AspNetCore bits.

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

NuGet

Usage

Enable VerifyAspNetCore once at assembly load time:

[ModuleInitializer]
public static void Initialize() =>
    VerifyAspNetCore.Initialize();

snippet source | anchor

Controller

Given the following controller:

public class MyController :
    Controller
{
    public ActionResult<List<DataItem>> Method(string input)
    {
        var headers = HttpContext.Response.Headers;
        headers["headerKey"] = "headerValue";
        headers["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(string value)
    {
        public string Value { get; } = value;
    }
}

snippet source | anchor

This test:

[Test]
public Task Test()
{
    var context = new ControllerContext
    {
        HttpContext = new DefaultHttpContext()
    };
    var controller = new MyController
    {
        ControllerContext = context
    };

    var result = controller.Method("inputValue");
    return Verify(
        new
        {
            result,
            context
        });
}

snippet source | anchor

Will result in the following verified file:

{
  result: [
    {
      Value: Value1
    },
    {
      Value: Value2
    }
  ],
  context: {
    HttpContext: {
      Request: {},
      Response: {
        StatusCode: OK,
        Headers: {
          headerKey: headerValue,
          receivedInput: inputValue
        },
        Cookies: {
          cookieKey: cookieValue
        }
      }
    }
  }
}

snippet source | anchor

Middleware

Given the following middleware:

public class MyMiddleware(RequestDelegate next)
{
    public Task Invoke(HttpContext context)
    {
        context.Response.Headers["headerKey"] = "headerValue";
        return next(context);
    }
}

snippet source | anchor

This test:

[Test]
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 Verify(
        new
        {
    
A23A
        context.Response,
            nextCalled
        });
}

snippet source | anchor

Will result in the following verified file:

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

snippet source | anchor

Testing a web app with specific controller scenarios

UseSpecificControllers extends IMvcBuilder to allow integration testing of a web app using a specific controller scenario.

[Test]
public async Task ControllerIntegrationTest()
{
    var builder = WebApplication.CreateBuilder();

    var controllers = builder.Services.AddControllers();
    // custom extension
    controllers.UseSpecificControllers(typeof(FooController));

    await using var app = builder.Build();
    app.MapControllers();

    await app.StartAsync();

    using var client = new HttpClient();
    var result = client.GetStringAsync($"{app.Urls.First()}/Foo");

    await Verify(result);
}

[ApiController]
[Route("[controller]")]
public class FooController :
    ControllerBase
{
    [HttpGet]
    public string Get() =>
        "Foo";
}

snippet source | anchor

ScrubAspTextResponse

The ScrubAspTextResponse feature allows modification or scrubbing of the content of an HTTP response before verification. This is useful for scenarios where the response contains dynamic or sensitive data that needs to be replaced or removed.

[Test]
public Task ScrubAspTextResponse()
{
    var context = new DefaultHttpContext();
    var buffer = "{\"key\":\"value\"}"u8;
    var response = context.Response;
    response.Body = new MemoryStream(buffer.ToArray());;
    response.ContentType = "application/json";
    return Verify(response)
        .ScrubAspTextResponse(_ => _.Replace("value", "replace"));
}

snippet source | anchor

Results in:

{
  StatusCode: OK,
  Headers: {
    Content-Type: application/json
  },
  Value: {
    key: replace
  }
}

snippet source | anchor

Icon

Spider designed by marialuisa iborra from The Noun Project.

About

Extends Verify to allow verification of AspNetCore bits.

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Contributors 3

  •  
  •  
  •  

Languages

0