Unable to build WireMockContainerBuilder with custom image
Describe the bug
Custom WireMock images that include their own mappings like from the guide can not be used in WireMockContainerBuilder.WithImage() leading to a InvalidOperationException on Build() because _imageOS is never set.
Expected behavior:
var container = new WireMockContainerBuilder()
.WithLinuxImage()
.WithImage("custom-wiremock:latest")
.WithAutoRemove(true)
.WithCleanUp(true)
.Build();
await container.StartAsync().ConfigureAwait(false);
var client = container.CreateClient();
// Start requesting the built-in mappings
I expect with the above code to work an not throw an exception. However after WithLinuxImage() _imageOS is still set to null because the builder objects gets recreated from scratch with only the docker config remaining.
@OliBomby Please note that this project is related to WireMock.Net , the manual you reference is for wiremock (the Java docker version). This will not work.
Please note that this project is related to WireMock.Net , the manual you reference is for wiremock (the Java docker version). This will not work.
Ah I did not realize there was a distinction in the WireMock version used in this Docker image.
I still think it should work though. It almost works just like this, if it didn't throw the InvalidOperationException the rest of my code would've worked. The REST API of the Java and .NET versions of WireMock are almost the same.
Here's a minimal working example of using the Java image in a .NET test container:
var container = new ContainerBuilder()
.WithImage("custom-wiremock:latest")
.WithPortBinding(8080, true)
.WithAutoRemove(true)
.WithCleanUp(true)
.Build();
await container.StartAsync().ConfigureAwait(false);
// Wait for WireMock to start
Thread.Sleep(5000);
var client = new HttpClient();
client.BaseAddress = new UriBuilder(Uri.UriSchemeHttp, container.Hostname, container.GetMappedPublicPorts()[8080]).Uri;
This code would be much cleaner if I could make use of the WireMockContainerBuilder and WireMockContainer classes, so that would be really nice to have.
@OliBomby I understand your question. Currently, it's indeed not possible to provide a custom WireMock.Net or wiremock-org (java) image. I can try to add that logic.
However, the mappings are not 100% the same, so using the container.CreateClient(); will not work correct for you.
The best would be that I create a new NuGet named WireMock.Org.Testcontainers which will use a default java docker image and also provide code to provide your own custom image.
I did fix it for WireMock.Net.Testcontainers --> https://github.com/wiremock/WireMock.Net/pull/1391
The WireMock.Net.Testcontainers is updated to accept a custom image and set the _imageOS.
I will publish a new NuGet soon.
However, I cannot give any guarantee that this works for the wiremock java version.
I'm afraid this implementation still won't work for any image which doesn't have wiremock.net in the name, because _imageOS will still be null in the builder object returned by WithCustomImage, because the base.WithImage method creates a new builder object and doesn't retain the value for _imagesOS. https://github.com/testcontainers/testcontainers-dotnet/blob/3f9de427b877a4fee394eab354fd98abbee06417/src/Testcontainers/Builders/ContainerBuilder%603.cs#L93-L96
https://github.com/wiremock/WireMock.Net/blob/e3e2dc7c6506e4428d55d1154e247de751c8c6cb/src/WireMock.Net.Testcontainers/WireMockContainerBuilder.cs#L281-L284
I see
https://github.com/wiremock/WireMock.Net/pull/1392