How to use mockservers with a middleware service?

Hi everyone!

I am looking for a help as I am trying to provide different responses on same mock server calls. I tried with x-mock-response-name, x-mock-response-id and x-mock-match-request-headers but none of them are working on my system because I have not a classic “client-server” communication.

Let me explain my setup:

I have a client (let’s say local cURL or Postman), a middleware (an API I am developping) and a server (which I can’t edit at all). The server is the part I want to mock.

When the client requests the middleware, the middleware will trigger 2 different requests to the server (for instance, 1 for auth and 1 for the business logic) by transfering a white list of HTTP Headers I can control to the server.
What I want to do is:

  • when the client requests the middleware with some custom header “Custom-Header: A”, the server (mocked) gives the responses R1 for the 1st request and R2_A for the 2nd request.
  • when the client requests the middleware with some custom header “Custom-Header: B”, the server (mocked) gives the responses R1 for the 1st request and R2_B for the 2nd request.

My problem with x-mock-response-name and x-mock-response-id is that, even if I can set R2_A or R2_B, on client headers (so that the middleware will transfer them to the server for the second request), Postman gives me an error because when the 1st request is sent, only R1 is known so I have the mockResponseNotFoundError error.

I tried with x-mock-match-request-headers, but that’s basically the same issue: if on client side I am requesting the middleware with headers related to R1 and R2_A for instance, the 1st request is not having a matching headers as the response would only have the R1 header but not the R2_A.

Of course, even if I can control the white list of headers the middleware could transfer to the server, I cannot choose which headers are transfered for each request. The middle ware can only transfer to the server all the headers given that are matiching the whiltelist.

Is there any way to use something similar than x-mock-response-name, but if there are no responses matching with the given name, the default algorithm to select the example is executed rather than throwing an error? To me, this would be the easiest way to solve my situation.

Thank you a lot and please, do not hesitate to ask questions if I was unclear in my issue presentation :slight_smile:

In order to better explain the architecture I have, here is a little schema:

What I want to mock are the servers on the Server side here. Here is my workflow:

  1. The client (i.e. a curl request, Postman, …) calls my API with some data in the request body.
  2. (a, b and c). My API is calling some servers with some data in the request body. Some of those data are inferred thanks to the data sent by the client, and other are generated by the API (like a timestamp for instance). As I expect the servers on the Server side are mocked, I get a fake response for each of those 3 requests.
  3. My API builds and sends a response to the client thanks to the data fetched by the requests done on step 2.

Nothing too much complicated, eh? :laughing:

My current problem is that I can’t choose which fake answer each mock servers must return on steps 2a., 2b. and 2c.

Something else that is important is that we must consider that giving my example, requests 2b. and 2c. are calling the exact same URL with the exact same HTTP Method: only the body differs.

I tried with x-mock-response-name and x-mock-response-id: I can ask the client to send those headers as my API can only transfer some headers to the server side, but can’t generate nor infer some. The problem is that if I give the response-name or the response-id of the mock response for the request 2a., I will have a Postman error when the requests 2b. and 2c. will be sent (mockResponseNotFoundError).

I have the same problem with x-mock-match-request-headers, which I detailled in my previous post.

I can’t use x-mock-response-code as some requests (for instance 2b. and 2c.) will have the same
expected response code so this header won’t filter the unexpected examples.

I can’t use x-mock-match-request-body as my API is sending some newly generated data to the server each time (like a current_timestamp value, for instance), so even with the exact same request body sent by the client in step 1., the requests bodies for 2a., 2b., and 2c. will differ at every calls, so that the example can’t be found.

So, my question here is: is there any way to achieve this?
Like I said, if Postman could simply follow its default algorithm instead of throwing an error if the x-mock-response-name doesn’t match, I could find a solution to this. In the meantime, I’m lost.

Thank you for your help!