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

Question: proxy passthrough when no match? #148

Closed
josdeweger-zz opened this issue May 30, 2018 · 16 comments
Closed

Question: proxy passthrough when no match? #148

josdeweger-zz opened this issue May 30, 2018 · 16 comments

Comments

@josdeweger-zz
Copy link

josdeweger-zz commented May 30, 2018

Is there a way to configure the proxy so that when there is no match, the request is passed through to the endpoint and passed back via the proxy to the client?

I have the following setup:

_server = FluentMockServer.Start(new FluentMockServerSettings
    {
        Urls = new[] { "http://localhost:9443" },
        StartAdminInterface = true,
        ReadStaticMappings = false
    });

_server
   .Given(Request.Create()
       .WithPath("/*"))
   .AtPriority(1000)
   .RespondWith(Response.Create()
       .WithProxy("my.url.com")
    );

//then some request response matches with priority lower than 1000

When I hit an exact match, I get the expected response back. But when there is no match, I get nothing back and the request times out. In the _server.LogEntriesChanged event handler I see the response from my actual endpoint returning properly, but it is not passed through to the caller?

@StefH
Copy link
Collaborator

StefH commented Jun 3, 2018

Your setup looks fine.

What happens if you just define the proxy and send a request ?

See also this example
https://github.com/WireMock-Net/WireMock.Net/blob/master/examples/WireMock.Net.ConsoleApplication/MainApp.cs#L89

@josdeweger-zz
Copy link
Author

Makes no difference. The url I proxy to is also in my hosts file. Could that be an issue?

@StefH
Copy link
Collaborator

StefH commented Jun 5, 2018

What happens if you just try http://www.google.com ?

@josdeweger-zz
Copy link
Author

josdeweger-zz commented Jun 5, 2018

Using http://www.google.com seems to work just fine.

When using my own url, I'm getting the following response displayed using the LogEntriesChanged event (removed some of the sensitive content and changed the url and path, but the idea is the same):

 {
    "Guid": "c9cc3288-102e-42fa-87f9-87b693c32c3d",
    "RequestMessage": {
      "ClientIP": "127.0.0.1",
      "Url": "http://my-url.com/with/some/path",
      "DateTime": "2018-06-05T20:43:05.2735592+02:00",
      "Path": "/with/some/path",
      "PathSegments": [
        "with",
        "some",
        "path"
      ],
      "Method": "post",
      "Headers": {
        "Connection": [
          "keep-alive"
        ],
        "Content-Type": [
          "text/xml"
        ],
        "Accept": [
          "*/*"
        ],
        "Accept-Encoding": [
          "gzip, deflate"
        ],
        "Host": [
          "my-url.com"
        ],
        "User-Agent": [
          "PostmanRuntime/7.1.5"
        ],
        "Content-Length": [
          "876"
        ]
      },
      "Cookies": null,
      "Query": null,
      "RawQuery": "",
      "Body": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\r\n  ... the soap request body here ... </soap:Envelope>",
      "BodyAsJson": null,
      "BodyAsBytes": null,
      "Host": "my-url.com",
      "Protocol": "http",
      "Port": 80,
      "Origin": "http://my-url.com:80",
      "BodyEncoding": {
        "BodyName": "utf-8",
        "EncodingName": "Unicode (UTF-8)",
        "HeaderName": "utf-8",
        "WebName": "utf-8",
        "WindowsCodePage": 1200,
        "IsBrowserDisplay": true,
        "IsBrowserSave": true,
        "IsMailNewsDisplay": true,
        "IsMailNewsSave": true,
        "IsSingleByte": false,
        "EncoderFallback": {
          "DefaultString": "?",
          "MaxCharCount": 1
        },
        "DecoderFallback": {
          "DefaultString": "?",
          "MaxCharCount": 1
        },
        "IsReadOnly": true,
        "CodePage": 65001
      }
    },
    "ResponseMessage": {
      "Headers": {
        "Content-Type": [
          "text/xml; charset=UTF-8"
        ],
        "Content-Language": [
          "nl-NL"
        ],
        "Connection": [
          "Keep-Alive"
        ],
        "Date": [
          "Tue, 05 Jun 2018 18:43:05 GMT"
        ],
        "Keep-Alive": [
          "timeout=10, max=100"
        ],
        "Transfer-Encoding": [
          "chunked"
        ],
        "X-Powered-By": [
          "Servlet/3.0",
          "Servlet/3.0"
        ],
        "X-Pad": [
          "avoid browser bug"
        ]
      },
      "StatusCode": 200,
      "BodyOriginal": null,
      "BodyDestination": null,
      "Body": "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">... some soap response body here ...</soap:Envelope>",
      "BodyAsJson": null,
      "BodyAsJsonIndented": null,
      "BodyAsBytes": null,
      "BodyAsFile": null,
      "BodyAsFileIsCached": null,
      "BodyEncoding": {
        "BodyName": "utf-8",
        "EncodingName": "Unicode (UTF-8)",
        "HeaderName": "utf-8",
        "WebName": "utf-8",
        "WindowsCodePage": 1200,
        "IsBrowserDisplay": true,
        "IsBrowserSave": true,
        "IsMailNewsDisplay": true,
        "IsMailNewsSave": true,
        "IsSingleByte": false,
        "EncoderFallback": {
          "DefaultString": "?",
          "MaxCharCount": 1
        },
        "DecoderFallback": {
          "DefaultString": "?",
          "MaxCharCount": 1
        },
        "IsReadOnly": true,
        "CodePage": 65001
      }
    },
    "RequestMatchResult": {
      "TotalScore": 2.0,
      "TotalNumber": 2,
      "IsPerfectMatch": true,
      "AverageTotalScore": 1.0,
      "MatchDetails": [
        {
          "Key": "WireMock.Matchers.Request.RequestMessageMethodMatcher, WireMock.Net, Version=1.0.3.20, Culture=neutral, PublicKeyToken=null",
          "Value": 1.0
        },
        {
          "Key": "WireMock.Matchers.Request.RequestMessagePathMatcher, WireMock.Net, Version=1.0.3.20, Culture=neutral, PublicKeyToken=null",
          "Value": 1.0
        }
      ]
    },
    "MappingGuid": "6a9079dd-e61f-445d-950e-dc44431c970c",
    "MappingTitle": null
  }

But in Postman and SoapUI I still get no response back

@StefH
Copy link
Collaborator

StefH commented Jun 5, 2018

The LogRequest seems ok.

Just a quick question: is your wiremock and that my.url.com just running on http or so you want to proxy from https to http, or from http to https? Maybe that could an issue?

In case of https, there could be an issue with self-signed certificates, these are default blocked by postman.

@josdeweger-zz
Copy link
Author

Before replying, let me say: thanks for your quick reply, love this library and your support. Keep up the good work!

To reply to your question: it's http only, and I tried both Postman and SoapUI. If I have the opportunity I will ask a colleague to pull down my branch and test it on another machine.

@StefH
Copy link
Collaborator

StefH commented Jun 6, 2018

You're welcome!

Which framework do you use to run wiremock?

  • .net 4.5.2 or higher
  • .net core 1.x
  • .net core 2.x

@josdeweger-zz
Copy link
Author

Im currently on .net core 2.0, but I also tried .net 4.5.2. Same problem unfortunately

@StefH
Copy link
Collaborator

StefH commented Jun 29, 2018

@josdeweger Any update on this issue? Did you get it working? Or still an issue?

Note that if proxying to google.com works, the issue must be in your network or backend server.

@josdeweger-zz
Copy link
Author

I recently asked a colleague to try the same setup to rule out the problem only occurs on my machine, but unfortunately the response is also not returned on his machine.

Let me try and see if I can spot differences in request/response using Fiddler comparing with and without Wiremock

@josdeweger-zz
Copy link
Author

josdeweger-zz commented Jul 17, 2018

Hi @StefH,

I gave it another try, created a new Console application using .net framework 4.7, using WireMock.Net.Standalone 1.0.4.5:

static void Main(string[] args)
        {
            int port = 8501;

            var settings = new FluentMockServerSettings
            {
                Urls = new[] { $"http://+:{port}" },
                StartAdminInterface = true
            };

            var server = StandAloneApp.Start(settings);

            Console.WriteLine("FluentMockServer running at {0}", port);

            server
                .Given(Request.Create()
                    .WithPath("/*"))
                .RespondWith(Response.Create()
                    .WithProxy("http://my.url.com/")
                );

            Console.WriteLine("Press any key to stop the server");
            Console.ReadKey();

            Console.WriteLine("Displaying all requests");
            Console.WriteLine(JsonConvert.SerializeObject(server.LogEntries, Formatting.Indented));

            Console.WriteLine("Press any key to quit");
            Console.ReadKey();
        }

I get a response now, with the following error:

{
    "Status": "{\"ClassName\":\"System.ArgumentException\",\"Message\":\"The 'Keep-Alive' header must be modified using the appropriate property or method.\",\"Data\":null,\"InnerException\":null,\"HelpURL\":null,\"StackTraceString\":\"   at System.Net.WebHeaderCollection.ThrowOnRestrictedHeader(String headerName)\\r\\n   at System.Net.WebHeaderCollection.Set(String name, String value)\\r\\n   at Microsoft.Owin.Host.HttpListener.RequestProcessing.ResponseHeadersDictionary.Set(String header, String value)\\r\\n   at Microsoft.Owin.Host.HttpListener.RequestProcessing.HeadersDictionaryBase.Set(String key, String[] value)\\r\\n   at Microsoft.Owin.Host.HttpListener.RequestProcessing.HeadersDictionaryBase.set_Item(String key, String[] value)\\r\\n   at Microsoft.Owin.Infrastructure.OwinHelpers.SetHeaderUnmodified(IDictionary`2 headers, String key, String[] values)\\r\\n   at Microsoft.Owin.Infrastructure.OwinHelpers.AppendHeaderUnmodified(IDictionary`2 headers, String key, String[] values)\\r\\n   at Microsoft.Owin.HeaderDictionary.AppendValues(String key, String[] values)\\r\\n   at WireMock.Owin.OwinResponseMapper.SetResponseHeaders(ResponseMessage responseMessage, IOwinResponse response)\\r\\n   at WireMock.Owin.OwinResponseMapper.<MapAsync>d__3.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)\\r\\n   at WireMock.Owin.WireMockMiddleware.<Invoke>d__5.MoveNext()\\r\\n--- End of stack trace from previous location where exception was thrown ---\\r\\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\\r\\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\\r\\n   at WireMock.Owin.GlobalExceptionMiddleware.<Invoke>d__3.MoveNext()\",\"RemoteStackTraceString\":null,\"RemoteStackIndex\":0,\"ExceptionMethod\":\"8\\nThrowOnRestrictedHeader\\nSystem, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089\\nSystem.Net.WebHeaderCollection\\nVoid ThrowOnRestrictedHeader(System.String)\",\"HResult\":-2147024809,\"Source\":\"System\",\"WatsonBuckets\":null,\"ParamName\":\"name\"}"
}

Looks very similar to #70 ?

@StefH
Copy link
Collaborator

StefH commented Jul 17, 2018

Mmm.

Maybe this link has a solution.
https://stackoverflow.com/questions/239725/cannot-set-some-http-headers-when-using-system-net-webrequest

I will take a look and see if this error can be simulated.

@StefH
Copy link
Collaborator

StefH commented Jul 18, 2018

I can reproduce with NET 4.5.x project, I get same exception. When using a dotnetcore project, it just works so it seems ?

I will investigate a solution.

@StefH
Copy link
Collaborator

StefH commented Jul 19, 2018

See linked changeset f1896ef for some details on how this will be fixed.

Please take a look and comment.

@josdeweger-zz
Copy link
Author

Great, thanks for the quick reply! I commented on the commit

@StefH
Copy link
Collaborator

StefH commented Jul 19, 2018

Closing.

New NuGet with this solution will be added in some time.

@StefH StefH closed this as completed Jul 19, 2018
StefH added a commit that referenced this issue Jul 19, 2018
* WebHeaderCollection.IsRestricted (#148)

* Update dependencies

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

No branches or pull requests

2 participants