Post-Response Script
Definition | .csx script to be executed after all HTTP requests within test case. |
Naming Convention | <test-case-name>-test.csx |
Purpose | Testing given response(s) and tear-down of data. |
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
andtp.Response
. - For multiple requests in a
.http
file, usetp.Requests
andtp.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 astring
.GetBody<TResult>()
/GetBodyAsync<TResult>()
- Deserializes the JSON body into an object of typeTResult
.GetBodyAsExpando()
/GetBodyAsExpandoAsync()
- Retrieves the body as a case-insensitivedynamic
expando object, making property access easier.- IMPORTANT: To use an expando object correctly, explicitly declare containing variable as
dynamic
.
- IMPORTANT: To use an expando object correctly, explicitly declare containing variable as
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.