How to force prism mock server to return different response during test

(Zohar Tal) #1

Hi all,

We have generated a mock server and would like to use it for component tests, i.e. the mock server is used by the “component under test”.

We’d like to make the mocked server return different responses for the same request.
For example, during the same testing run, we’d like it to return some JSON object for the first time it’s called, and for the second time we’d like for the request to fail and for the mock server to return status 400.

Can it be done with Prism mock server?

Thanks!

(Taylor Barnett) #2

Hi @zohar.tal! There isn’t really a way to have it do this in a specific ordering right now. It’s a good idea though! The one thing you could do is have multiple example requests, but you won’t be able to have a guaranteed ordering for them.

(Nicolas Tisserand) #3

Hi @zohar.tal
As @taylor said, there is no way to reply with different responses in a specific order.
But you can command the mock behaviour in adding optional query params :

  • ?__dynamic=true : then the mock creates a random response (still compliant with the contract) instead of replying the example. See here : https://docs.stoplight.io/mocking/dynamic-mocking
  • ?__code=XXX : then the mock response code is XXX (useful to test error cases)

You can mix these options together.
There are also other params. Full documentation in the helpers documentation here : https://next.stoplight.io/stoplight/prism/version%2F1.0/helpers.scenarios.yml?edit=%2Fscenarios%2Fb-mock

I’ve also seen somebody who needed to have different responses with the same request. It was a trick based on the “Content-Type” header, like “application/json-2”. The full discussion is here : https://github.com/stoplightio/prism/issues/111

Hope it’ll give you new ideas.

1 Like
(Zohar Tal) #4

Hi all

i will try the solution that you suggests & see if it give me the expected result

thanks

zohar

1 Like
(Zohar Tal) #5

Hi
I did the solution with the query parameters __code & __example at the test scenario for getting specific response status cod & body from the mock server when i send the request directly to the mock server .
how ever i have a problem , because between the consumer(my test scenarios) & the mock server (producer) i have another real service (lets call it rp_service) .
So when i send a request it goes through the rp_service that supposed to forward it & get the answer from the mock server .
how can i managed the mock server to bring me different response(status & body) in this situation ? because the service in the middle (rp_service ) do not familiar with the test query param
flow is like this:
http request: http://rp_service_ip:8080/getVc---------> mocked prism server

(Nicolas Tisserand) #6

Hi,

I think that you should focus on your rp_service that looks like a proxy.
This component MUST forward the query parameters to the Prism mock server otherwise it won’t work at all.
Could you get rid of this component and send your requests directly to the mock server ?

1 Like