Table of Contents

Post-Response Script

Definition .csx script containing defined test scenarios.
Naming Convention <test-case-name>-test.csx
Purpose Testing given response(s) and tear-down of data.
Restrictions .csx scripts must not contain code that accesses or processes HTTP responses outside of tp.Test methods.
Example Usage Simple Script, Manipulation with Body, Another Example, Advanced Script

Features

Testing

The main role of post-response script is to validate responses and define tests.

To learn more about how to write a test, visit this page.

Accessing Requests and Responses

  • For single requests or the most recently executed request, use tp.Request and tp.Response.
  • For multiple requests in a .http file, use tp.Requests and tp.Responses to access named requests and responses.

Working with Body Content

Both HttpRequestMessage and HttpResponseMessage objects include convenient methods for handling body content:

  • GetBody() / GetBodyAsync() - Retrieves the body as a string.
  • GetBody<TResult>() / GetBodyAsync<TResult>() - Deserializes the JSON body into an object of type TResult.
  • GetBodyAsExpando() / GetBodyAsExpandoAsync() - Retrieves the body as a case-insensitive dynamic expando object, making property access easier.
    • IMPORTANT: To use an expando object correctly, explicitly declare containing variable as dynamic.

JSON Handling

For requests that handle application/json payloads, a extension method ToExpando() can simplify access to JSON properties:

// Using case-insensitive expando object
tp.Test("Identifier should be a positive integer.", () =>
{
    // Expando object has to be marked epxlicitly as 'dynamic'
    dynamic responseBody = tp.Response.GetBodyAsExpando();
    True(responseBody.id > 0);
});

Status Code Handling

The response object includes a StatusCode() method that simplifies status code handling by returning its integer value.