Table of Contents

Mock response

Fluxzy provides various way to mock a response.

MockedResponseAction is the starting point for mocking a response body and keeping control on low level settings.

However, the ConfigureRule chain has several fluent mocking methods that can be used to mock a response quickly. These mocking methods starts with the word Reply....

using Fluxzy;
using Fluxzy.Rules.Actions;
using Fluxzy.Rules.Actions.HighLevelActions;
using Fluxzy.Rules.Filters;
using Fluxzy.Rules.Filters.RequestFilters;

var fluxzySetting = FluxzySetting.CreateDefault();

fluxzySetting.ConfigureRule()
    .WhenUriMatch("https://server.com/api/animal/dogs", StringSelectorOperation.StartsWith)
    .ReplyFile("/path/to/dog.jpeg", 200, "image/jpeg")
    .WhenUriMatch("https://server.com/api/animal/cats", StringSelectorOperation.StartsWith)
    .ReplyJson("""
                   [
                     {
                       "name": "opale"
                     }
                   ]
                   """)
    .WhenAll(new PostFilter(), new HostFilter("www.google.com"))
    .Forward("https://www.example.com"); 

// Create a new proxy instance 
await using var proxy = new Proxy(fluxzySetting);

Console.WriteLine("Press any key to exit");
Console.ReadKey();

Mocked response doesn't not create a connection to the expected host. It's a simple substitution of the response body. If you're mocked response depends on the actual server response, you should use a IStreamSubstitution instead.