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.61k stars 1.08k forks source link

When loading expectations from OpenAPI spec, for every operation mockserver only creates 1 expectation, even if there are different responses/code configured #1806

Open kapoorrohit opened 1 year ago

kapoorrohit commented 1 year ago

Describe the issue Open API spec has responses like 200, 401, 500. But when I create expectations from this Open API, it only returns first response for every operation.

MockServer version 5.15.0

  1. How you are running MockServer (i.e maven plugin, docker, etc) -> Java Client
  2. Code you used to create expectations -> Expectation[] test = mockServer.upsert( openAPIExpectation( "file:/Users/..../openapi.yaml" ) );
  3. What error you saw -> Only first response returned for each operation

Expected behaviour All responses to be returned

sann3 commented 8 months ago

Any update on this ?

IvanZhivkov-sumup commented 6 months ago

A single request can return only one response. So All response to be returned is not applicable. The mock server documentation states: if multiple response bodies are specified for an operation (i.e. different status codes) the first response body is used by default, however this can be controlled by using operationsAndResponses - link

Using operationsAndResponses you can create an expectation to return a specific response. This way you could create multiple expectations each with a different response. However each request will still get only one response, so you still have to figure out what you want to achieve.

aswini1988 commented 4 months ago

I have a similar scenario where I use the OpenAPI Specification to create mocks. https://www.mock-server.com/mock_server/using_openapi.html. Using the operationsAndResponses argument I am creating two mocks for the same operation ID but for different status codes for example operation-id is getPetByID and the status code is 200 & 400. But when I hit the endpoint to get the response from Mockserver for the 200 status code it returns a response either from 400 or 200 which is matched first. My question is how should I create mock data for these two status codes for a single operation ID so that Mockserver can return response 200 when the request is valid and 400 when the request is invalid.

var mockServerClient = require('mockserver-client').mockServerClient; mockServerClient("localhost", 1080).openAPIExpectation({ "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json", "operationsAndResponses": { "showPetById": "200" } }).then( function () { console.log("expectation created"); }, function (error) { console.log(error); } );

mockServerClient("localhost", 1080).openAPIExpectation({ "specUrlOrPayload": "https://raw.githubusercontent.com/mock-server/mockserver/master/mockserver-integration-testing/src/main/resources/org/mockserver/openapi/openapi_petstore_example.json", "operationsAndResponses": { "showPetById": "400" } }).then( function () { console.log("expectation created"); }, function (error) { console.log(error); } );