When several test drivers use the same REST API, each can accumulate the same request-building and response-handling code. That duplication makes drivers harder to read and maintain. An infrastructure layer puts shared technical details in helper methods, leaving each driver responsible for the details of its particular action. section 15.3
Keep the responsibilities distinct:
- Step definitions express testing decisions; test drivers handle the automation needed for an action. section 15.0
- Infrastructure helpers handle shared details needed by drivers, such as sending REST requests and reading results. section 15.3
- Drivers retain action-specific details such as the path, HTTP method, and payload. section 15.3
Placing an order
An order driver initially builds a POST request, adds bearer-token authentication, sends it, and handles the response itself. After the shared plumbing moves into a REST API helper, its place-order action supplies the order path, request payload, and expected Created status. A login driver uses the same helper for its own request. section 15.3
Listing 15.9 Helper methods defined in the RestApiContext class
*** Support/RestApiContext.cs ***
public record VoidReturn; #A
public class RestApiContext(AppHostingContext appHostingContext)
{
public string BearerToken { get; set; } #B
...
public async Task<TResult> GetRequest<TResult>( #C
string path, object? payload = null)
{
var response = await SendRequest( #D
HttpMethod.Get, path, payload);
response.EnsureSuccessStatusCode(); #E
return await ReadResult<TResult>(response);
}
public async Task<TResult> ProcessRequest<TResult>( #F
string actionName, HttpMethod method, string path,
object? payload = null,
HttpStatusCode successStatusCode = HttpStatusCode.OK)
{
var response = await SendRequest( #D
method, path, payload);
if (response.StatusCode != successStatusCode) #G
{
string errorMessage = await ReadErrorMessage(response);
throw new TestActionFailedException( #H
$"{actionName} failed with status code {response.StatusCode}. " +
$"Error message: '{errorMessage}'");
}
return typeof(TResult) == typeof(VoidReturn) ?
default! :
await ReadResult<TResult>(response); #I
}
private async Task<HttpResponseMessage> SendRequest( #J
HttpMethod method, string path, object? payload = null)
{
var request = new HttpRequestMessage(method, path);
if (!string.IsNullOrEmpty(BearerToken)) #K
{
request.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", BearerToken);
}
if (payload != null) #L
{
request.Content = JsonContent.Create(payload);
}
var httpClient = appHostingContext.AppHost.CreateClient();
return await httpClient.SendAsync(request); #M
}
private async Task<TResult> ReadResult<TResult>( #N
HttpResponseMessage response)
{
return await response.Content.ReadFromJsonAsync<TResult>()
?? throw new InvalidOperationException("No result payload found");
}
private async Task<string> ReadErrorMessage( #O
HttpResponseMessage response)
{
var errorMessage =
(await response.Content.ReadFromJsonAsync<ErrorResponse>())?.Error;
return string.IsNullOrEmpty(errorMessage) ? "n/a" : errorMessage;
}
}The helper-method listing shows how request sending and result handling can be gathered in one place for drivers to call. section 15.3
Make these helpers fit the application rather than trying to cover every possible use of REST. Here, error responses have a particular structure and authentication uses a bearer header; a different application may need different helpers. section 15.3


