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.
Description
Currently if an API spec defines any other success (2XX) status code responses, but not specifically 200, then the mock returns
501 - Not Implementedfor 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 returning201 Createdor a DELETE request returning204 No Content.As a current workaround one can either:
responsesearch 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.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 Implementedwhen 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:
Create a file named
main.jsand add the following code: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 ImplementedExpected 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.tscurrently only checks for a 200 or "default" response as a fallback. If any other success (2XX) response is defined it is ignored and a501 - Not Implementedresponse is sent.