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

[Question] Override openAPI spec expectation #1642

Closed raminqaf closed 1 year ago

raminqaf commented 1 year ago

Hello, you mentioned in this issue how to override expectations. Currently, I am creating the expectations through the OpenAPI specs. I am using the petsotre.yaml file, and it works perfectly fine. Take look at this path:

  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Pet'
              examples:
                Crumble:
                  value:
                    id: 2
                    name: Crumble
                    tag: dog
# rest of the yaml...

I am trying to upsert expectations for this path with certain path parameters. Take a look at this code snippet:

    final ClientAndServer mockServer = startClientAndServer(1080);

    mockServer.upsert(
        OpenAPIExpectation
            .openAPIExpectation(this.openApiPath)
            .withOperationsAndResponses(this.operations)
    );

    mockServer.upsert(Expectation
        .when(
            request()
                .withPath("/v1/pets/{petId}")
                .withPathParameters(
                    param("petId", "bad-cat")
                )
        ).thenRespond(
            response()
                .withStatusCode(400)
        )
    );

    mockServer.upsert(Expectation
        .when(
            request()
                .withPath("/v1/pets/{petId}")
                .withPathParameters(
                    param("petId", "dead-cat")
                )
        ).thenRespond(
            response()
                .withStatusCode(500)
        )
    );

What I am trying to do here is that I want to get certain responses with predefined parameters for the defined path pets/{petId}. So if I call GET /v1/pets/dead-cat I expect to get 500, but I receive a 200 OK. As if the upserted expectations never trigger, only the openAPI spec expectation works. Is there a solution to this problem?

raminqaf commented 1 year ago

Okay, I found the solution to the problem! I just had to order the expectations. So the correct way of defining the would be:

    final ClientAndServer mockServer = startClientAndServer(1080);

    mockServer.upsert(Expectation
        .when(
            request()
                .withPath("/v1/pets/{petId}")
                .withPathParameters(
                    param("petId", "bad-cat")
                )
        ).thenRespond(
            response()
                .withStatusCode(400)
        )
    );

    mockServer.upsert(Expectation
        .when(
            request()
                .withPath("/v1/pets/{petId}")
                .withPathParameters(
                    param("petId", "dead-cat")
                )
        ).thenRespond(
            response()
                .withStatusCode(500)
        )
    );

    mockServer.upsert(
        OpenAPIExpectation
            .openAPIExpectation(this.openApiPath)
            .withOperationsAndResponses(this.operations)
    );