Skip to content

Latest commit

 

History

History
191 lines (166 loc) · 5.47 KB

http-recording.md

File metadata and controls

191 lines (166 loc) · 5.47 KB

Http Recording

Http Recording allows, when a method is being tested, for any http requests made as part of that method call to be recorded and verified.

Supported in netstandard2.1 and up

Usage

Call HttpRecording.StartRecording(); before the method being tested is called.

The perform the verification as usual:

[Fact]
public async Task TestHttpRecording()
{
    HttpRecording.StartRecording();

    var sizeOfResponse = await MethodThatDoesHttpCalls();

    await Verifier.Verify(
            new
            {
                sizeOfResponse,
            })
        .ModifySerialization(settings =>
        {
            //scrub some headers that are not consistent between test runs
            settings.IgnoreMembers("traceparent", "Date");
        });
}

static async Task<int> MethodThatDoesHttpCalls()
{
    using var client = new HttpClient();

    var jsonResult = await client.GetStringAsync("https://httpbin.org/json");
    var xmlResult = await client.GetStringAsync("https://httpbin.org/xml");
    return jsonResult.Length + xmlResult.Length;
}

snippet source | anchor

The requests/response pairs will be appended to the verified file.

{
  target: {
    sizeOfResponse: 951
  },
  httpCalls: [
    {
      Uri: https://httpbin.org/json,
      RequestHeaders: {},
      ResponseHeaders: {
        Access-Control-Allow-Credentials: true,
        Access-Control-Allow-Origin: *,
        Connection: keep-alive,
        Server: gunicorn/19.9.0
      },
      ResponseContentHeaders: {
        Content-Length: 429,
        Content-Type: application/json
      },
      ResponseContentString:
{
  "slideshow": {
    "author": "Yours Truly",
    "date": "date of publication",
    "slides": [
      {
        "title": "Wake up to WonderWidgets!",
        "type": "all"
      },
      {
        "items": [
          "Why <em>WonderWidgets</em> are great",
          "Who <em>buys</em> WonderWidgets"
        ],
        "title": "Overview",
        "type": "all"
      }
    ],
    "title": "Sample Slide Show"
  }
}
    },
    {
      Uri: https://httpbin.org/xml,
      RequestHeaders: {},
      ResponseHeaders: {
        Access-Control-Allow-Credentials: true,
        Access-Control-Allow-Origin: *,
        Connection: keep-alive,
        Server: gunicorn/19.9.0
      },
      ResponseContentHeaders: {
        Content-Length: 522,
        Content-Type: application/xml
      },
      ResponseContentString:
<!--  A SAMPLE set of slides  -->
<slideshow title="Sample Slide Show" date="Date of publication" author="Yours Truly">
  <!-- TITLE SLIDE -->
  <slide type="all">
    <title>Wake up to WonderWidgets!</title>
  </slide>
  <!-- OVERVIEW -->
  <slide type="all">
    <title>Overview</title>
    <item>Why <em>WonderWidgets</em> are great</item>
    <item />
    <item>Who <em>buys</em> WonderWidgets</item>
  </slide>
</slideshow>
    }
  ]
}

snippet source | anchor

Explicit Usage

The above usage results in the http calls being automatically added snapshot file. Calls can also be explicitly read and recorded using HttpRecording.FinishRecording(). This enables:

  • Filtering what http calls are included in the snapshot.
  • Only verifying a subset of information for each http call.
  • Performing additional asserts on http calls.

For example:

[Fact]
public async Task TestHttpRecordingExplicit()
{
    HttpRecording.StartRecording();

    var sizeOfResponse = await MethodThatDoesHttpCalls();

    var httpCalls = HttpRecording.FinishRecording().ToList();

    // Ensure all calls finished in under 5 seconds
    var threshold = TimeSpan.FromSeconds(5);
    foreach (var call in httpCalls)
    {
        Assert.True(call.Duration < threshold);
    }

    await Verifier.Verify(
            new
            {
                sizeOfResponse,
                // Only use the Uri in the snapshot
                httpCalls = httpCalls.Select(_ => _.Uri)
            });
}

snippet source | anchor

Results in the following:

{
  sizeOfResponse: 951,
  httpCalls: [
    https://httpbin.org/json,
    https://httpbin.org/xml
  ]
}

snippet source | anchor