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.36k stars 199 forks source link

Is it possible to use WireMock as a proxy server for HTTPS requests? #1007

Closed robvangeloven closed 8 months ago

robvangeloven commented 8 months ago

So I have a use case where I'm stuck with a hardcoded URL embedded in an external SDK. There is no way for me to change that behaviour and I do need to verify the output somehow for my current use case.

I though to myself: what if I make WireMock the default proxy server? And that does work, but only for HTTP calls. HTTPS calls get a

System.Net.Http.HttpRequestException : The SSL connection could not be established, see inner exception.
    ---- System.Security.Authentication.AuthenticationException : Cannot determine the frame size or a corrupted frame was received.

However, if I remove the WireMock-as-proxy setting and make the call directly it works just fine. Only I have trouble figuring out why this is.

var wireMockServer = WireMockServer.Start(new WireMockServerSettings
  {
      Urls = new[] { "http://localhost:9091", "https://localhost:9443" },
      UseSSL = true,
  });

  // If I remove the DefaultProxy setting everything works just fine
  HttpClient.DefaultProxy = new WebProxy
  {
      Address = new Uri(wireMockServer.Url!),
      BypassProxyOnLocal = false,
  };

  wireMockServer
      .Given(Request.Create())
      .RespondWith(
          Response.Create()
          .WithStatusCode(200)
          .WithBody(@"{ ""msg"": ""Hello world!"" }"));

  var result = await new HttpClient().PostAsync(wireMockServer.Urls[1], null);
PG-RichT commented 8 months ago

@robvangeloven I'd be interested to see a response to this as I find myself in the same position. This wouldn't happen to be the Adyen SDK by any chance?

robvangeloven commented 8 months ago

@robvangeloven I'd be interested to see a response to this as I find myself in the same position. This wouldn't happen to be the Adyen SDK by any chance?

Buckaroo, so same difference really 😅

StefH commented 8 months ago

When calling the https endpoint from WireMock.Net via Postman (SSL certificate verification= true), does that work ?

And maybe these pages can help you?

robvangeloven commented 8 months ago

When calling the https endpoint from WireMock.Net via Postman (SSL certificate verification= true), does that work ?

And maybe these pages can help you?

I haven't tested it via Postman, mostly because the code works just fine if I don't point the default httpclient proxy to the WireMock instance. If WireMock is configured to be the default proxy though (no other changes). I get weird SSL errors.

See also the code I linked: if I remove the

HttpClient.DefaultProxy = new WebProxy
  {
      Address = new Uri(wireMockServer.Url!),
      BypassProxyOnLocal = false,
  };

Everything works just fine. With it: SSL errors. HTTP calls work fine in both cases by the way.

StefH commented 8 months ago

@robvangeloven I'm able to reproduce your issue (https://github.com/WireMock-Net/WireMock.Net/pull/1010), however I do not have any idea on the root cause.

StefH commented 8 months ago

@robvangeloven / @PG-RichT

After some investigating I now understand what you are trying to to.

However, configuring WireMock.Net to act as a proxy-server is possible. For this purpose you should use a different solution / NuGet.

StefH commented 8 months ago

https://github.com/WireMock-Net/WireMock.Net/pull/1010

robvangeloven commented 8 months ago

@PG-RichT / anyone reading this: the FryProxy or BenderProxy could help as a MITM proxy your solution