mock-server / mockserver

MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
http://mock-server.com
Apache License 2.0
4.54k stars 1.06k forks source link

Proxy as default behaviour when no expectations set #30

Closed gvermoen closed 10 years ago

gvermoen commented 10 years ago

Hi,

Thanks for this great tool, it works like a charm! We use MockServer extensively for testing our webservices with mocked back-ends and are very happy with it.

We have some struggle however because after deployment our webservices cannot easily switch between mock and real backend. So I was thinking to maybe use Apache to route between /mockserver and the backend services, but that would involve a lot of configuration for each service.

I read on the mock-server.com that MockServer also supports basic proxying functionality. So ideally I would like MockServer to act as a proxy and let it route all requests to the backend webservices, based on some default expectation. Then at some point when I upload short-living expectations, MockServer should change it behaviour from proxying to mocking, and after the count of remainingTimes has passed it should return to proxying behaviour.

Do you think that would be possible with Mock Server?

jamesdbloom commented 10 years ago

Sorry for the slow response but this week has been very busy.

I would definitely be possible to extend MockServer to support this type of functionality.

One way to achieve this with the current functionality could be to use 302 redirects.
For example:

  1. set an unlimited expectation using Times.unlimited() that returns a 302 and redirects the requester to the real endpoint which could be on another hostname / ip address.
  2. set a different short lived expectation using Times.once() or Times.exactly(int count) when ever you want a fully mocked response

I can look into adding this as a full extension, although it might be fairly substantial therefore it is likely to take at least a month or more to do it properly.

Let me know if you need more information about the 302 approach.

J

gvermoen commented 10 years ago

Hi James,

That's ok, thanks for your response. And my apologies for my late response...

I found out that a colleague of my has contacted you too. We're on a large project with multiple teams and didn't know from each other we had the same idea... ;-) So I'll just follow them with the solution they are going to. Thanks anyway for your effort and also thanks for this great tool!

regards, Gerben

PS: about the 302 redirect solution; that wouldn't work in our case because we dont use a browser to interact with MockServer. Our webservices wouldn't know how to deal with 302 codes.

2014-04-26 8:13 GMT+02:00 James D Bloom notifications@github.com:

Sorry for the slow response but this week has been very busy.

I would definitely be possible to extend MockServer to support this type of functionality.

One way to achieve this with the current functionality could be to use 302 redirects.

For example:

  1. set an unlimited expectation using Times.unlimited() that returns a 302 and redirects the requester to the real endpoint which could be on another hostname / ip address.
  2. set a different short lived expectation using Times.once() or Times.exactly(int count) when ever you want a fully mocked response

I can look into adding this as a full extension, although it might be fairly substantial therefore it is likely to take at least a month or more to do it properly.

Let me know if you need more information about the 302 approach.

J

— Reply to this email directly or view it on GitHubhttps://github.com/jamesdbloom/mockserver/issues/30#issuecomment-41461130 .

jamesdbloom commented 10 years ago

ok I'll close this as a duplicate of the other issue your college raised.

btw what HTTP client is are webservices using? As far as I'm aware most clients, (Apache HTTP, Jetty, Spring (aka Apache), Java API) all support 302 redirects and follow redirects by default unless it is disabled.

jamesbloomnektan commented 10 years ago

Please see comment from issue #32

The code implementation and testing is complete and the new API has been released in version 2.10. The only remaining work is to update the documentation.

The Java API is as follows:

mockServerClient
        .when(
                request()
                        .withPath("/some_path")
                        .withBody(new StringBody("some_request_body", Body.Type.EXACT)),
                exactly(3)
        )
        .forward(
                forward()
                        .withHost("some_host")
                        .withPort(9090)
                        .withScheme(HttpForward.Scheme.HTTPS)
        );

The scheme defaults to HTTP if not specified.

The JSON API is as follows:

{
  "httpRequest" : {
    "method" : "someMethod",
    "url" : "http://www.example.com",
    "path" : "/some_path",
    "body" : {
      "type" : "EXACT",
      "value" : "some_request_body"
    }
  },
  "httpForward" : {
    "host" : "some_host",
    "port" : 9090,
    "scheme" : "HTTPS"  },
  "times" : {
    "remainingTimes" : 3,
    "unlimited" : false
  }
}