stoplightio / prism

Turn any OpenAPI2/3 and Postman Collection file into an API server with mocking, transformations and validations.
https://stoplight.io/open-source/prism
Apache License 2.0
4.2k stars 344 forks source link

When dynamic responses are enabled, a response with no body and status code 404 returns a 200 #1836

Closed pytlesk4 closed 2 years ago

pytlesk4 commented 3 years ago

Summary

When dynamic mocking is enabled on the server, the bellow test should pass without disabling dynamic mocking. __code doesn't seem to be respected.

describe.each([[oas2File], [oas3File]])('server %s', file => {
  let server: ThenArg<ReturnType<typeof instantiatePrism>>;

  beforeEach(async () => {
    server = await instantiatePrism(resolve(__dirname, 'fixtures', file), {
      mock: { dynamic: true },
    });
  });

  // .... other tests ....
  it('will return requested response using the __code property', async () => {
    const response = await makeRequest('/pets/123?__code=404');

    expect(response.status).toBe(404);
    return expect(response.text()).resolves.toBe('');
  });

  // .... other tests ....
});

Questions

chohmann commented 2 years ago

@brendarearden we think this is already supported looking at the docs. Can you see if you can reproduce? If not, we can close this issue.

brendarearden commented 2 years ago

I was unable to reproduce this issue so it appears to have been resolved. I am able to get a 404 response with the following steps:

  1. Mock the server in dynamic mode
    prism mock -d https://raw.githack.com/OAI/OpenAPI-Specification/master/examples/v3.0/petstore-expanded.yaml
  2. From the cli run a curl command to the mock server, setting the response code to 404
    curl -v http://127.0.0.1:4010/pets/123 -H "Prefer: code=404"

    or

    curl -v http://127.0.0.1:4010/pets/123\?__code\=404  
  3. See the status code is returned as 404
    • From the mock server logs
      ...
      [8:17:16 AM] ›     [NEGOTIATOR] ℹ  info      Unable to find a 404 response definition
      [8:17:16 AM] ›     [NEGOTIATOR] ✔  success   Found a compatible content for */*
      [8:17:16 AM] ›     [NEGOTIATOR] ✔  success   Responding with the requested status code 404
    • The output from the curl command
    • Trying 127.0.0.1:4010...
    • Connected to 127.0.0.1 (127.0.0.1) port 4010 (#0)

      GET /pets/123?__code=404 HTTP/1.1 Host: 127.0.0.1:4010 User-Agent: curl/7.79.1 Accept: /

    • Mark bundle as not supporting multiuse < HTTP/1.1 404 Not Found < Access-Control-Allow-Origin: < Access-Control-Allow-Headers: < Access-Control-Allow-Credentials: true < Access-Control-Expose-Headers: * < Content-type: application/json < Content-Length: 44 < Date: Fri, 01 Apr 2022 15:19:39 GMT < Connection: keep-alive < Keep-Alive: timeout=5 <
    • Connection #0 to host 127.0.0.1 left intact {"code":-1804477846,"message":"dolor minim"}%