Skip to content

Accept all success (2XX) status codes rather than just 200 #86

Description

@cekstedt

Description

Currently if an API spec defines any other success (2XX) status code responses, but not specifically 200, then the mock returns 501 - Not Implemented for every call. It is common practice for certain REST methods to provide more specific status codes on success, like a POST request creating a resource returning 201 Created or a DELETE request returning 204 No Content.

As a current workaround one can either:

  • Change every affected fetch call to use the additional response search parameter (eg: await fetch('/user?response=201')). This solution conflicts with MSW's philosophy of accessing your mocked API just as you would without a mock in the way.
  • Change every affected endpoint to return a 200 status code. This solution removes semantic meaning from your response codes and conflicts with REST best practices.

I am not certain that this qualifies as a "bug" since it is mentioned in the documentation (bullet pt. 3), but the current behavior of returning 501 - Not Implemented when there are success responses defined in the spec feels counter-intuitive and counter-productive.

Reproduction steps

Create a new node.js project and install msw & source:

$ npm init -y
$ npm i msw @msw/source

Create a file named main.js and add the following code:

import { fromOpenApi } from "@msw/source/open-api";
import { setupServer } from "msw/node";

function createSpec(statusCode) {
  return {
    basePath: "https://localhost",
    paths: {
      "/user": {
        post: {
          responses: {
            [statusCode]: {
              content: {
                "application/json": {
                  example: {
                    id: "abc-123",
                  },
                },
              },
            },
          },
        },
      },
    },
  };
}

const options = {
  method: "POST",
  body: JSON.stringify({ id: "def-456" }),
  headers: {
    "Content-Type": "application/json",
  },
};

for (const statusCode of ["200", "201"]) {
  const handlers = await fromOpenApi(createSpec(statusCode));
  const server = setupServer(...handlers);
  server.listen();
  const response = await fetch("https://localhost/user", options);
  const body = await response.text();
  console.log(
    `When spec defines a ${statusCode} code response the mock returns ${response.status} - ${body}`,
  );
  server.resetHandlers();
  server.close();
}

Lastly, run main.js.

$ node main.js
When spec defines a 200 code response the mock returns 200 - {"id":"abc-123"}
When spec defines a 201 code response the mock returns 501 - Not Implemented

Expected behavior

I created a fork that demonstrates the behavior that I would expect, namely returning any defined success (2XX) status code in the mocked response, rather than just looking for 200.

Initial assessment

File src/open-api/utils/open-api-utils.ts currently only checks for a 200 or "default" response as a fallback. If any other success (2XX) response is defined it is ignored and a 501 - Not Implemented response is sent.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions