Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request.json() crashes application with node integration #6

Open
alexandermukhin opened this issue Feb 16, 2024 · 2 comments
Open

request.json() crashes application with node integration #6

alexandermukhin opened this issue Feb 16, 2024 · 2 comments

Comments

@alexandermukhin
Copy link

Hello, thanks for the plugin!
There is an error that crashes an app when parsing body in handlers request.json()

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at parseJSONFromBytes (node:internal/deps/undici/undici:4553:19)
    at successSteps (node:internal/deps/undici/undici:4527:27)
    at fullyReadBody (node:internal/deps/undici/undici:1307:9)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async specConsumeBody (node:internal/deps/undici/undici:4536:7)

Reproduce:

handler

  http.post("api/post/json", async ({ request }) => {
    const body = await request.json();

    return HttpResponse.json(body);
  }),

index.html

<!doctype html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>vite-plugin-msw</title>
  </head>
  <style>
    html,
    body {
      margin: 0;
      padding: 0;
    }

    html {
      font-family: sans-serif;
    }

    body {
      padding: 1.5rem;
    }

    pre {
      background-color: #eee;
      padding: 1.5rem;
    }
  </style>
  <body>
    <h1>Server response:</h1>
    <pre id="app"></pre>
    <button id="post-json-btn">Send POST JSON Request</button>
    <button id="post-fd-btn">Send POST Form data Request</button>
    <script type="module" src="/main.ts"></script>
  </body>
</html>

main.js

const jsonBtn = document.getElementById("post-json-btn");

jsonBtn.addEventListener("click", () => {
  const body = JSON.stringify({ test: 1 });

  fetch("/api/post/json", {
    method: "POST",
    headers: {
      "Content-Type": "application/json;charset=utf-8",
    },
    body,
  })
    .then((res) => res.json())
    .then((json) => {
      const $app = document.getElementById("app");
      if ($app) {
        $app.textContent = JSON.stringify(json, null, 2);
      }
    });
});
@AABorisov
Copy link

AABorisov commented Mar 15, 2024

I resolved body in createNodeMiddleware:

      let requestBody = {};
      if (!["GET", "HEAD"].includes(req.method)) {
        const body = Readable.toWeb(req)
        requestBody = {
          body,
          duplex: 'half',
        }
      }
      const mockedRequest = new Request(new URL(req.url, serverOrigin), {
        method: req.method,
        headers: new Headers(sanitizeHeaders(req.headers)),
        credentials: "omit",
        ...requestBody,
      });

@K-ETFreeman
Copy link

K-ETFreeman commented Nov 18, 2024

Faced this issue too, would be nice to see fix implemented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants