Working with Plain Text Requests

If you find yourself needing to test HTTP endpoints that either send text or return text, Alba has you covered with some built in helpers.

Reading the Response Text

To read the response body as text, use this syntax:

public async Task read_text(IAlbaHost host)
{
    var result = await host.Scenario(_ =>
    {
        _.Get.Url("/output");
    });

    // This deserializes the response body to the
    // designated Output type
    var outputString = result.ReadAsText();

    // do assertions against the Output string
}

snippet source | anchor

Assertions against the Response Text

You have these built in operations for asserting on the response body text:

public Task assert_on_content(IAlbaHost host)
{
    return host.Scenario(_ =>
    {
        _.ContentShouldBe("exactly this");

        _.ContentShouldContain("some snippet");

        _.ContentShouldNotContain("some warning");
    });
}

snippet source | anchor

Sending Text

Lastly, you can send text to an HTTP endpoint with this syntax:

public Task send_text(IAlbaHost host)
{
    return host.Scenario(_ =>
    {
        _.Post.Text("some text").ToUrl("/textdata");
    });
}

snippet source | anchor

Do note that this also sets the content-length header to the string length and sets the content-type header of the request to "text/plain."