WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.35k stars 197 forks source link

Is there a way to record API calls in the file format required for WireMock Testcontainers? #1094

Closed asos-alexhaigh closed 2 months ago

asos-alexhaigh commented 2 months ago

One feature of WireMock that I have always appreciated was the ability to generate the JSON files for Mocks by recording requests and responses from the real API. It removes human error and allows us to have full confidence that we are testing with the correct data.

I've just started using the WireMock test containers in a new project and it seems that it requires JSON in a different format which has required me to manually adjust the files to get it to work.

Is there a way to record files so I don't have to do this manual step?

This is how I am loading the mapping files

    private const string PATH_TO_MAPPINGS = @"my/path/toMappings";

    private readonly WireMockContainer _api= new WireMockContainerBuilder()
        .WithAutoRemove(autoRemove: true)
        .WithMappings(PATH_TO_MAPPINGS)
        .WithWatchStaticMappings(true)
        .WithCleanUp(cleanUp: true)
        .Build();

    public async Task StartAsync() => await _api.StartAsync();

An example of a recorded JSON file. This will not work when provided to a testcontainer.

{
  "id": "3ba83902-9666-4b24-8626-860396c0d14f",
  "name": "rest_v4_users",
  "request": {
    "url": "/rest/v4/users/?clientUserId=doesnotexist",
    "method": "GET"
  },
  "response": {
    "status": 204,
    "headers": {
      "Strict-Transport-Security": "max-age=15780000; includeSubDomains",
      "X-Rate-Limit": "7500",
      "X-Rate-Limit-Remaining": "7491",
      "X-Rate-Limit-Reset": "1714150451",
      "Set-Cookie": [ "JSESSIONIDSSO=123; Path=/; Secure; HttpOnly; SameSite=Lax", "JSESSIONIDSSO=123; Path=/; Secure; HttpOnly; SameSite=Lax", "TS01aeada2=123; Path=/; Secure; HTTPOnly" ],
      "X-ContextId": "123",
      "X-Server": "8.51",
      "Date": "Fri, 26 Apr 2024 16:47:07 GMT"
    }
  },
  "uuid": "3ba83902-9666-4b24-8626-860396c0d14f",
  "persistent": true,
  "insertionIndex": 14
}

My manual migration from to the above, to make it work with the test container.

{
  "Guid": "3ba83902-9666-4b24-8626-860396c0d14f",
  "UpdatedAt": "2024-04-26T17:21:13.6373664Z",
  "Request": {
    "Path": {
      "Matchers": [
        {
          "Name": "WildcardMatcher",
          "Pattern": "/v4/users",
          "IgnoreCase": true
        }
      ]
    },
    "Methods": [
      "GET"
    ],
    "Params": [
      {
        "Name": "clientUserId",
        "Matchers": [
          {
            "Name": "ExactMatcher",
            "Pattern": "doesnotexist"
          }
        ]
      }
    ]
  },
  "Response": {
    "StatusCode": 204
  }
}
StefH commented 2 months ago

The recorded JSON file seems to be from WireMock (Java version)

The manual migration JSON is the one that this project (WireMock.Net for .NET) uses.

Note that there should be some support in this project to support JSON files from WireMock.org (Java), bit I'm not sure if that still works. See also https://github.com/WireMock-Net/WireMock.Net/wiki/WireMock.Org