HttpRequestSpy 0.0.20

Suggested Alternatives

HttpRequest.Spy

Additional Details

HttpRequestSpy has been renamed to HttpRequest.Spy

There is a newer version of this package available.
See the version list below for details.
dotnet add package HttpRequestSpy --version 0.0.20                
NuGet\Install-Package HttpRequestSpy -Version 0.0.20                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HttpRequestSpy" Version="0.0.20" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HttpRequestSpy --version 0.0.20                
#r "nuget: HttpRequestSpy, 0.0.20"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HttpRequestSpy as a Cake Addin
#addin nuget:?package=HttpRequestSpy&version=0.0.20

// Install HttpRequestSpy as a Cake Tool
#tool nuget:?package=HttpRequestSpy&version=0.0.20                

HttpRequestSpy

HttpRequestSpy is a tool aiming to test outgoing HttpRequest sent via an HttpClient.

It compares all sent http requests to an expected request definition and provides a user friendly error message when no recorded httprequest matches the expected one.

This tool is useful when you want to test your outgoing http requests but you don't want to mock the http client.

Installation

dotnet add package HttpRequestSpy

Usage

    [Fact]
    public async Task HttpRequest_should_be_sent()
    {
        // Arrange
        var spy = new HttpRequestSpy();
        using var httpClient = new HttpClient(new SpyHttpMessageHandler(spy));

        var instance = new TypedHttpClient(httpClient);

        // Act
        await instance.MakeHttpRequest();

        // Assert
        spy.HasRecordedRequests(1);

        spy.AGetRequestTo("/some/ressource")
           .WithQuery(new {
               id = "12"
           })
           .OccuredOnce();
    }


    public class TypedHttpClient(HttpClient httpClient) {
        public async Task<HttpResponseMessage> MakeHttpRequest() {
            return await httpClient.GetAsync("/some/ressource?id=12");
        }
    }

Features

Fluent API

The HttpRequestSpy provides a fluent API to help you define your expected http request.

    spy.AGetRequestTo("/some/ressource")
       .WithQuery(new {
           id = "12"
       })
       .OccuredOnce();

Occurrences

You can define the number of occurences of your expected request.

    spy.AGetRequestTo("/some/ressource")
       .OccuredOnce();

    spy.AGetRequestTo("/some/ressource")
       .OccuredTwice();

    spy.AGetRequestTo("/some/ressource")
       .Occured(3);
    
    spy.AGetRequestTo("/some/ressource")
       .NeverOccured();

Number of overall recorded requests

You can define the number of overall recorded requests.

    spy.HasRecordedRequests(5);
       .NeverOccured();

This codes checks that 5 requests have been recorded regardless of the content of the requests.

Absolute path and relative url

You can define the absolute path or the relative url of your expected request.

    spy.AGetRequestTo("/some/ressource")
       .OccuredOnce();
    spy.AGetRequestTo("https://myapi.com/some/ressource")
       .OccuredOnce();

GET, POST, PUT, DELETE, PATCH http verbs

You can define the http verb of your expected request.

    spy.AGetRequestTo("/some/ressource")
       .OccuredOnce();
    
    spy.APostRequestTo("/some/ressource")
       .OccuredOnce();
    
    spy.APutRequestTo("/some/ressource")
       .OccuredOnce();
    
    spy.APatchRequestTo("/some/ressource")
       .OccuredOnce();
    
    spy.ADeleteRequestTo("/some/ressource")
       .OccuredOnce();

Query parameters

You can define the query parameters of your expected request as an anonymous object.

    spy.AGetRequestTo("/some/ressource")
       .WithQuery(new {
           id = "12",
           date = "2024-01-05"
       })
        /// Or
       .WithQuery("id=12&date=2024-01-05")
        /// Or
       .WithQueryParam("id", "12")
       .WithQueryParam("date", "2024-01-05")
        
       .OccuredOnce();

Body / Payload

Only for POST, PUT, PATCH requests

You can define the body of your expected request as an anonymous object or target a specific property.

Check Json payload

See HttpRequestSpyWhenJsonPayloadShould.cs file for all use cases.

    spy.APostRequestTo("/some/ressource")       
       .WithJsonPayload(new
                    {
                        prop1 = 1,
                        prop2 = "value",
                        subProp = new
                        {
                            subProp1 = "subValue"
                        }
                    })
        
        // Or
        .WithJsonPayloadProperty("prop1") // Will check that the property exists
        .WithJsonPayloadProperty("prop1", 1)
        .WithJsonPayloadProperty("prop2", "value")
        .WithJsonPayloadProperty("subProp", new
                        {
                            subProp1 = "subValue"
                        })
        
        
       .OccuredOnce();
Json payload matching JsonSchema

Using JsonSchema, you can check that the payload matches a specific schema.

    spy.APostRequestTo("/some/ressource")       
        .WithPayloadMatchingJsonSchema("path/to/schema.json")
       .OccuredOnce();
Check Xml payload

See HttpRequestSpyWhenXmlPayloadShould.cs file for all use cases.

    spy.APostRequestTo("/some/ressource")
       .WithXmlPayload(new
                    {
                        prop1 = 1,
                        prop2 = "value",
                        subProp = new
                        {
                            subProp1 = "subValue"
                        }
                    })
        
        // Or
        .WithXmlPayloadProperty("/_x003F_anonymous_x003F_/prop1") // Will check that the property exists
        .WithXmlPayloadProperty("/_x003F_anonymous_x003F_/prop1", 1)
        .WithXmlPayloadProperty("/_x003F_anonymous_x003F_/prop2", "value")
        .WithXmlPayloadProperty("/_x003F_anonymous_x003F_/subProp", new
                        {
                            subProp1 = "subValue"
                        })
        // Or  :
        .WithXmlPayloadProperty("/_x003F_anonymous_x003F_/subProp/subProp1", "subValue")
        
        
       .OccuredOnce();
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.24 145 1/5/2024 0.0.24 is deprecated.
0.0.23 131 1/5/2024 0.0.23 is deprecated.
0.0.20 136 1/5/2024 0.0.20 is deprecated.