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

Support Optionally Mocking Response or Forward Request #32

Closed jamesdbloom closed 10 years ago

jamesdbloom commented 10 years ago

Request from Wilfred van der Deijl:

"Hi James. We've been using MockServer at a large client in the Netherlands for mocking (soap) web services when unit testing compound services that are supposed to invoke other services. This works great and we love the mechanism of selecting certain requests eligible for mocking. Developers run with a local MockServer, but testers want to use the central Service Bus that points to a single central MockServer installation. The configure this with a number of REST calls prior to performing their (soapUI) tests. This works fine but we want to take the next step in testing web userinterface that invoke (soap) services. Sometimes testers want to run the UI against "real" services and sometimes against MockServer. We're looking for a way to change this dynamically and preferably per request. I've been thinking about setting up MockProxy and have it forward certain requests to MockServer and certain requests to the "real" server. We could use the already great system of selecting requests. The only thing is that MockProxy doesn't support forwarding/rewriting requests to other origin servers than what was requested by the client. I hope I could explain what we're looking for. Is this something that is on your roadmap or are you willing to accept this as a contribution from our side?"

Response:

"Hi Wilfred, I'm happy to look into this as there is a proxy in the MockServer that is currently used to record requests while making requests to a real endpoint. I'm fairly sure I can connect the two parts together. To be honest I'm just had a quick look and it looks like a fairly straight forward addition if done in the correct way. I'll see what I can do I might be able to have this completed and released by the end of the weekend. If not mid next week should be doable."

jamesdbloom commented 10 years ago

code implemented but still need to complete:

This should be completed be released by Sunday 18th.

jamesdbloom commented 10 years ago

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
  }
}
jamesdbloom commented 10 years ago

closing issue and opening another issue for documentation