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.57k stars 1.07k forks source link

How to get access to Http request data #203

Closed manish-panwar closed 8 years ago

manish-panwar commented 8 years ago

Hello,

I am using maven plugin to start the server and given below is how ExpectationInitializer is implemented.

@Override
public void initializeExpectations(MockServerClient mockServerClient) {
    // Allow any request and respond 200
    mockServerClient.when(request()).respond(handleRequest());
}

private HttpResponse handleRequest() {
    return HttpResponse.response().withStatusCode(200);
}

We have 1000+ scenarios to automate, and we want mock server to respond specific response body & headers based on header value coming from client request(basically client will pass on a header which can be used to determine a file name which contains the expected response that to be returned).

We don't want to have 100s of condition while we can have a generic implementation which can generate the response body based on header key.

So far I could see that HttpRequest object contains only the matcher data, not the actual HTTP request body/headers.

Thanks, Manish

manish-panwar commented 8 years ago

This is the response returned by Mock Server.

    {
      "statusCode" : 200,
    }

This is the request content/header I need access to. I see this in logs, but can't access it in HttpRequest object.

    {
      "method" : "GET",
      "path" : "/Microsoft",
      "headers" : [ {
        "name" : "Host",
        "values" : [ "localhost:8083" ]
      }, {
        "name" : "User-Agent",
        "values" : [ "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36" ]
      }, {
        "name" : "Postman-Token",
        "values" : [ "a27341d9-cdef-e8cf-db99-71ca5d36f082" ]
      }, {
        "name" : "Cache-Control",
        "values" : [ "no-cache" ]
      }, {
        "name" : "expected-response-file",
        "values" : [ "expected-response-file" ]
      }, {
        "name" : "aw-tenant-code",
        "values" : [ "1H2BA4AAAAG5A6UAADQA" ]
      }, {
        "name" : "Content-Type",
        "values" : [ "application/json" ]
      }, {
        "name" : "Accept",
        "values" : [ "*/*" ]
      }, {
        "name" : "Accept-Encoding",
        "values" : [ "gzip, deflate, sdch" ]
      }, {
        "name" : "Accept-Language",
        "values" : [ "en-US,en;q=0.8" ]
      }, {
        "name" : "Content-Length",
        "values" : [ "0" ]
      } ],
      "keepAlive" : true,
      "secure" : false
    }
jamesdbloom commented 8 years ago

I think all you need to implement is an instance of org.mockserver.mock.action.ExpectationCallback, this will allow you to dynamically create a response based on data in the request and therefore have a single expectation and instance of org.mockserver.mock.action.ExpectationCallback that supports all 100s different header-file combinations. For more details see: http://www.mock-server.com/mock_server/creating_expectations.html#callback

I'm going to close this ticket as I think this give you everything you need, please feel free to re-open if you have further queries.