liveBook · Manning

Infrastructure layer

As explained in Effective Behavior-Driven Development

The automation-solution layer containing helper methods for interacting with underlying software infrastructure.

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

How Effective Behavior-Driven Development covers it

How deep it goes
Works through REST helper methods and a driver refactoring, while deferring generic methods and classes to language documentation.
Examples
Contrasts an order driver containing REST plumbing with order and login drivers calling shared helper methods.
What it assumes
Assumes familiarity with REST requests and driver classes; leaves generic methods and classes to programming-language documentation.
What you can do after
Build it from scratch by extracting shared REST plumbing into application-specific helpers used by test drivers.
How it connects
Connects test drivers and step definitions through a separation of testing, automation, and infrastructure responsibilities.

Also explained in

Compare how 3 books explain infrastructure layer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Effective Behavior-Driven Development. The section links point to the parts of the book it draws on.