Skip to content

Conversation

haga-rak
Copy link
Owner

@haga-rak haga-rak commented Apr 14, 2025

This pull request introduces two new actions: TransformResponseBodyAction and TransformRequestBodyAction. These actions offer a simpler and more intuitive alternative to ISubstitution, enabling conditional modifications on request or response bodies.

You can leverage this new feature to inspect and conditionally edit request or response bodies dynamically.

Usage

Access to this feature is possible:

  • Directly via the corresponding actions: TransformResponseBodyAction and TransformRequestBodyAction.
  • Or by utilizing the extension methods provided by TransformActionExtensions, as shown in the example below:

Example: Uppercasing any response body text

var fluxzySetting = FluxzySetting.CreateLocalRandomPort();
fluxzySetting.ConfigureRule().WhenAny()
    .TransformResponse(
        (_, originalContent) => Task.FromResult(originalContent.ToUpperInvariant()));

Another example that returns the SHA1 body hash instead of the original content

var setting = FluxzySetting.CreateLocalRandomPort();
setting.ConfigureRule().WhenAny().TransformRequest(TransformationFunction);

async Task<BodyContent?> TransformationFunction(TransformContext context, IBodyReader originalContent)
{
    // context.Exchange : information about the ongoing exchange
    // context.Connection : information about the connection, could be null with a request body since the remote connection is not yet established
    
    var contentAsString = await originalContent.ConsumeAsString(); // consume content a string 

    // Alternatively you can consume the content in other formats:
    //
    // var contentAsByteArray = await originalContent.ConsumeAsBytes(); // consume content as a byte array
    // var contentAsStream = originalContent.ConsumeAsStream(); // consume content as a stream
    //
    // You can return null to dismiss any read/modification. However, it will work only if the sream has not been consumed yet. 
    
    var hash = System.Security.Cryptography.SHA256
                     .HashData(Encoding.UTF8.GetBytes(contentAsString));
    var hashAsString = System.Convert.ToHexString(hash);

    return new BodyContent(hashAsString);
}

Implementation Details

In all cases, the caller must supply a function that accepts the original content along with any contextual settings, returning the transformed content as a string, byte[], or Stream.

Under the hood, this functionality utilizes IStreamSubstitution, which automatically manages size control headers (such as Content-Length, chunked transfer encoding). Additionally, encoded content (gzip, deflate, etc.) is preprocessed transparently by Fluxzy.

Memory Considerations

Note that transforming responses into byte arrays or strings involves loading the entire response content into memory.

@haga-rak haga-rak added new-action New fluxzy action added enhancement New feature or request labels Apr 14, 2025
@haga-rak haga-rak changed the title Add TransformRequestBodyAction and TransformResponseBodyAction Add TransformRequestBodyAction and TransformResponseBodyAction to inspect/replace quickly HTTP bodies Apr 14, 2025
@haga-rak haga-rak merged commit 2425ac2 into main Apr 14, 2025
3 checks passed
@haga-rak haga-rak linked an issue Apr 14, 2025 that may be closed by this pull request
@Mrgaton
Copy link

Mrgaton commented May 27, 2025

it would be great when you transform response to have acess to the request content

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new-action New fluxzy action added
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Real-time Internet Traffic Logging Without Writing to Disk
2 participants