Incorrect return value when using WireMock.Net with web service #397

Closed
opened 2025-12-29 15:22:46 +01:00 by adam · 2 comments
Owner

Originally created by @gpetrounrt on GitHub (Jan 26, 2022).

Clone https://github.com/gpetrounrt/WireMockWithVies and check https://github.com/gpetrounrt/WireMockWithVies/blob/main/WireMockWithVies/WireMockWithViesTest.cs
If I use "http://ec.europa.eu/taxation_customs/vies/services/checkVatService" as parameter in checkVatPortTypeClient to communicate with the corresponding WebService, I get back the expected exception message since the checkVatRequest input is invalid.
If I use http://localhost:15000/ instead as parameter in checkVatPortTypeClient, I get back a web page instead of the expected exception message.
Any idea what might be wrong in this case? Perhaps I am not using correct WireMockServerSettings? Or this might be a bug when trying to use a web service?

Originally created by @gpetrounrt on GitHub (Jan 26, 2022). Clone https://github.com/gpetrounrt/WireMockWithVies and check https://github.com/gpetrounrt/WireMockWithVies/blob/main/WireMockWithVies/WireMockWithViesTest.cs If I use "http://ec.europa.eu/taxation_customs/vies/services/checkVatService" as parameter in checkVatPortTypeClient to communicate with the corresponding WebService, I get back the expected exception message since the checkVatRequest input is invalid. If I use http://localhost:15000/ instead as parameter in checkVatPortTypeClient, I get back a web page instead of the expected exception message. Any idea what might be wrong in this case? Perhaps I am not using correct WireMockServerSettings? Or this might be a bug when trying to use a web service?
adam added the question label 2025-12-29 15:22:46 +01:00
adam closed this issue 2025-12-29 15:22:46 +01:00
Author
Owner

@StefH commented on GitHub (Jan 26, 2022):

@gpetrounrt

The code should be like:

[Fact]
public async Task WireMockWithVies_ShouldReturnFalse()
{
    var wireMockServerSettings = new WireMockServerSettings()
    {
        StartAdminInterface = true,
        ProxyAndRecordSettings = new ProxyAndRecordSettings
        {
            Url = "http://ec.europa.eu",
            SaveMapping = true,
            SaveMappingToFile = true,
            AllowAutoRedirect = true
        }
    };

    var wireMockServer = WireMockServer.Start(wireMockServerSettings);

    checkVatPortTypeClient client = new checkVatPortTypeClient(checkVatPortTypeClient.EndpointConfiguration.checkVatPort, $"{wireMockServer.Urls[0]}/taxation_customs/vies/services/checkVatService");
    checkVatRequest request = new("GR", "123456789");
    var exception = await Record.ExceptionAsync(() => client.checkVatAsync(request));

    wireMockServer.Stop();

    Assert.Equal("INVALID_INPUT", exception.Message);
}
  1. When calling using wiremock as a proxy, make sure that your client calls wiremock (and then wiremock will call the real URL)
  2. And only provide the hostname, not the full url
  3. In Unit tests, do not assign a fixed url to wiremock, you are not 100% sure that that url+port is free, better to let wiremock create a dynamic port.
@StefH commented on GitHub (Jan 26, 2022): @gpetrounrt The code should be like: ``` c# [Fact] public async Task WireMockWithVies_ShouldReturnFalse() { var wireMockServerSettings = new WireMockServerSettings() { StartAdminInterface = true, ProxyAndRecordSettings = new ProxyAndRecordSettings { Url = "http://ec.europa.eu", SaveMapping = true, SaveMappingToFile = true, AllowAutoRedirect = true } }; var wireMockServer = WireMockServer.Start(wireMockServerSettings); checkVatPortTypeClient client = new checkVatPortTypeClient(checkVatPortTypeClient.EndpointConfiguration.checkVatPort, $"{wireMockServer.Urls[0]}/taxation_customs/vies/services/checkVatService"); checkVatRequest request = new("GR", "123456789"); var exception = await Record.ExceptionAsync(() => client.checkVatAsync(request)); wireMockServer.Stop(); Assert.Equal("INVALID_INPUT", exception.Message); } ``` 1. When calling using wiremock as a proxy, make sure that your client calls wiremock (and then wiremock will call the real URL) 2. And only provide the hostname, not the full url 2. In Unit tests, do not assign a fixed url to wiremock, you are not 100% sure that that url+port is free, better to let wiremock create a dynamic port.
Author
Owner

@gpetrounrt commented on GitHub (Jan 27, 2022):

Thank you very much for the reply and the best practices information. I completely forgot that the Url should have the hostname. I have applied your suggestions and it works as expected.

@gpetrounrt commented on GitHub (Jan 27, 2022): Thank you very much for the reply and the best practices information. I completely forgot that the Url should have the hostname. I have applied your suggestions and it works as expected.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#397