Skip to main content

Definition

Assembly: CloudNimble.Breakdance.DotHttp.dll Namespace: CloudNimble.Breakdance.DotHttp Inheritance: System.Object

Syntax

CloudNimble.Breakdance.DotHttp.DotHttpAssertions

Summary

Provides smart assertion helpers for HTTP responses that go beyond simple status code checking.

Remarks

Detects common API error patterns like 200 OK responses that contain error payloads.

Examples

var response = await httpClient.SendAsync(request);

// Validate response meets common API expectations
await DotHttpAssertions.AssertValidResponseAsync(response);

// Or with custom options
await DotHttpAssertions.AssertValidResponseAsync(response,
    checkStatusCode: true,
    checkContentType: true,
    checkBodyForErrors: true);

Methods

AssertBodyContainsAsync

Validates that the response body contains the specified text.

Syntax

public static System.Threading.Tasks.Task AssertBodyContainsAsync(System.Net.Http.HttpResponseMessage response, string expectedText, int maxBodyPreviewLength = 500)

Parameters

NameTypeDescription
responseSystem.Net.Http.HttpResponseMessageThe HTTP response.
expectedTextstringThe text that should be present in the body.
maxBodyPreviewLengthintMaximum length of body content to include in error messages.

Returns

Type: System.Threading.Tasks.Task

Examples

await DotHttpAssertions.AssertBodyContainsAsync(response, "\"success\":true");

AssertContentType

Validates that the response Content-Type matches the expected value.

Syntax

public static void AssertContentType(System.Net.Http.HttpResponseMessage response, string expectedContentType)

Parameters

NameTypeDescription
responseSystem.Net.Http.HttpResponseMessageThe HTTP response.
expectedContentTypestringThe expected Content-Type (e.g., “application/json”).

Examples

DotHttpAssertions.AssertContentType(response, "application/json");

AssertHeader

Validates that the response contains a specific header.

Syntax

public static void AssertHeader(System.Net.Http.HttpResponseMessage response, string headerName, string expectedValue = null)

Parameters

NameTypeDescription
responseSystem.Net.Http.HttpResponseMessageThe HTTP response.
headerNamestringThe expected header name.
expectedValuestringOptional expected header value.

Examples

DotHttpAssertions.AssertHeader(response, "X-Request-Id");
DotHttpAssertions.AssertHeader(response, "Cache-Control", "no-store");

AssertNoErrorsInBodyAsync

Validates that the response body does not contain error patterns.

Syntax

public static System.Threading.Tasks.Task AssertNoErrorsInBodyAsync(System.Net.Http.HttpResponseMessage response, int maxBodyPreviewLength = 500)

Parameters

NameTypeDescription
responseSystem.Net.Http.HttpResponseMessageThe HTTP response.
maxBodyPreviewLengthintMaximum length of body content to include in error messages.

Returns

Type: System.Threading.Tasks.Task

Examples

await DotHttpAssertions.AssertNoErrorsInBodyAsync(response);

AssertStatusCodeAsync

Validates that the response status code matches the expected value.

Syntax

public static System.Threading.Tasks.Task AssertStatusCodeAsync(System.Net.Http.HttpResponseMessage response, int expectedStatusCode, int maxBodyPreviewLength = 500)

Parameters

NameTypeDescription
responseSystem.Net.Http.HttpResponseMessageThe HTTP response.
expectedStatusCodeintThe expected status code.
maxBodyPreviewLengthintMaximum length of body content to include in error messages.

Returns

Type: System.Threading.Tasks.Task

Examples

await DotHttpAssertions.AssertStatusCodeAsync(response, 201); // Created
await DotHttpAssertions.AssertStatusCodeAsync(response, 204); // No Content

AssertValidResponseAsync

Validates that the response meets common API contract expectations.

Syntax

public static System.Threading.Tasks.Task AssertValidResponseAsync(System.Net.Http.HttpResponseMessage response, bool checkStatusCode = true, bool checkContentType = true, bool checkBodyForErrors = true, bool logResponseOnFailure = true, int maxBodyPreviewLength = 500)

Parameters

NameTypeDescription
responseSystem.Net.Http.HttpResponseMessageThe HTTP response to validate.
checkStatusCodeboolWhether to check the status code for success. Default is true.
checkContentTypeboolWhether to verify Content-Type is present when body exists. Default is true.
checkBodyForErrorsboolWhether to check for error patterns in the response body. Default is true.
logResponseOnFailureboolWhether to include the response body in failure messages. Default is true.
maxBodyPreviewLengthintMaximum length of body content to include in error messages. Default is 500.

Returns

Type: System.Threading.Tasks.Task A task that completes when validation is done.

Exceptions

ExceptionDescription
DotHttpAssertionExceptionThrown when an assertion fails.

Examples

var response = await httpClient.SendAsync(request);
await DotHttpAssertions.AssertValidResponseAsync(response);