Skip to content

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:

cs
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 = await result.ReadAsTextAsync();

    // do assertions against the Output string
}
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 = await result.ReadAsTextAsync();

    // 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:

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

        _.ContentShouldContain("some snippet");

        _.ContentShouldNotContain("some warning");
    });
}
public async Task assert_on_content(IAlbaHost host)
{
    await 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:

cs
public async Task send_text(IAlbaHost host)
{
    await host.Scenario(_ =>
    {
        _.Post.Text("some text").ToUrl("/textdata");
    });
}
public async Task send_text(IAlbaHost host)
{
    await 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."