Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
asos-alexhaigh opened this issue Apr 30, 2024 · 1 comment
Labels

Comments

@asos-alexhaigh
Copy link

asos-alexhaigh commented Apr 30, 2024

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
Copy link
Collaborator

StefH commented Apr 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants