Skip to content

balanikas/FluentAssertions.Http

Repository files navigation

FluentAssertions.Http

Http extensions for FluentAssertions

build and test CodeQL

Nuget Nuget (with prereleases) Nuget GitHub stars

API Docs

https://balanikas.github.io/FluentAssertions.Http/api/index.html

Usage

Given a response from an http request
    var response = await _factory.CreateClient().GetAsync("/api/customers");
and an expectation
    var expected = new CustomerModel();

Headers

    //assert content headers
    response.Should().HaveContentHeader("X-Custom-Header");
    response.Should().HaveContentHeaderValue(HttpResponseHeader.ContentType, "application/json; charset=utf-8");
    response.Should().HaveContentHeaderValues(HttpResponseHeader.Allow, new[] { "GET", "PUT" });
    
    //assert response headers
    response.Should().HaveResponseHeader("X-Custom-Header");
    response.Should().HaveResponseHeaderValue(HttpResponseHeader.AcceptRanges, "range1");
    response.Should().HaveResponseHeaderValues(HttpResponseHeader.AcceptRanges, new []{"range1","range2"});

Content

    //assert string content
    response.Should().HaveContentMatching(x => x.StartsWith("hello"));
    response.Should().HaveContent("hello world");
    
    //assert typed content
    response.Should().HaveContentMatching<CustomerModel>(x => x.Name == "Alex" && x.Addresses.Count == 2);
    response.Should().HaveContent<CustomerModel>(expected);
    response.Should().HaveContent<CustomerModel>(expected, o => o.Excluding(x => x.Id)); 

Combine

    response.Should()
        .HaveResponseHeader("X-Custom-Header")
        .And
        .HaveContent(expected, options => options.Excluding(x => x.Id));