Skip to content

Commit 7768882

Browse files
thomhurstclaude
andcommitted
fix: Use WireMock Admin API for dynamic mapping addition
- Replace file copy approach with Admin API POST request - Use temporary file to avoid complex JSON escaping issues - Update tests to verify mapping addition success via ExecResult - Ensures mappings are immediately active without reload delays This fixes the test failure where mappings added via file copy were not immediately available to WireMock. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 366a8c3 commit 7768882

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/Testcontainers.WireMock/WireMockContainer.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,22 @@ public string GetAdminUrl()
3737
/// <param name="mappingJson">The mapping JSON content.</param>
3838
/// <param name="ct">Cancellation token.</param>
3939
/// <returns>Task that completes when the mapping has been added.</returns>
40-
public async Task AddMappingFromJsonAsync(string mappingJson, CancellationToken ct = default)
40+
public async Task<ExecResult> AddMappingFromJsonAsync(string mappingJson, CancellationToken ct = default)
4141
{
42-
var mappingFilePath = $"/home/wiremock/mappings/{Guid.NewGuid():N}.json";
42+
// Write mapping to a temporary file first, then use curl to post it
43+
var tempFile = $"/tmp/mapping_{Guid.NewGuid():N}.json";
44+
await CopyAsync(Encoding.UTF8.GetBytes(mappingJson), tempFile, Unix.FileMode644, ct)
45+
.ConfigureAwait(false);
46+
47+
var result = await ExecAsync(new[] { "curl", "-X", "POST", "http://localhost:8080/__admin/mappings",
48+
"-H", "Content-Type: application/json", "-d", $"@{tempFile}" }, ct)
49+
.ConfigureAwait(false);
4350

44-
await CopyAsync(Encoding.UTF8.GetBytes(mappingJson), mappingFilePath, Unix.FileMode644, ct)
51+
// Clean up temp file
52+
await ExecAsync(new[] { "rm", tempFile }, ct)
4553
.ConfigureAwait(false);
54+
55+
return result;
4656
}
4757

4858
/// <summary>

tests/Testcontainers.WireMock.Tests/WireMockContainerTest.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ public async Task CanAddMappingAndReceiveStubResponse()
5353
}";
5454

5555
// When
56-
await _wireMockContainer.AddMappingFromJsonAsync(mappingJson)
57-
.ConfigureAwait(false);
58-
59-
// Give WireMock time to process the mapping
60-
await Task.Delay(1000)
56+
var result = await _wireMockContainer.AddMappingFromJsonAsync(mappingJson)
6157
.ConfigureAwait(false);
58+
59+
// Verify mapping was added successfully
60+
Assert.Equal(0, result.ExitCode);
6261

6362
using var httpClient = new HttpClient();
6463
httpClient.BaseAddress = new Uri(_wireMockContainer.GetBaseUrl());
@@ -89,8 +88,9 @@ public async Task CanResetMappings()
8988
}
9089
}";
9190

92-
await _wireMockContainer.AddMappingFromJsonAsync(mappingJson)
91+
var addResult = await _wireMockContainer.AddMappingFromJsonAsync(mappingJson)
9392
.ConfigureAwait(false);
93+
Assert.Equal(0, addResult.ExitCode);
9494

9595
// When
9696
var execResult = await _wireMockContainer.ResetAsync()

0 commit comments

Comments
 (0)