question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Error] Headers x-msw-request-id forbidden at dispatchError / Buffer.js

See original GitHub issue

Environment

Name Version
msw 0.28.2
node 12.18.0
OS Mac OS Catalina 10.15.5

Request handlers


import { setupServer } from 'msw/node'
import { rest } from 'msw'

const handlers = [
  rest.get(SOME_API, (req, res, ctx) => {
      return res(ctx.json(''));
    },
  ),
];

const server = setupServer(...handlers);

beforeAll(() => server.listen());
afterEach(() => server.resetHandlers());
afterAll(() => server.close());

test('some test..', async () => {
  server.use(
    rest.get('/api/1/..', (req, res, ctx) => {
      return res(ctx.json({...}));
    }),

    rest.get('/api/2/..', (req, res, ctx) => {
      return res(ctx.json({...}));
    }),
  );

  render(<Component />);
  await waitFor(() => screen.getByText(/test test/i));

});

test('some test2..', async () => {
  server.use(
    rest.get('/api/1/..', (req, res, ctx) => {
      return res(ctx.json({...}));
    }),

    rest.get('/api/2/..', (req, res, ctx) => {
      return res(ctx.json({...}));
    }),
  );

  render(<Component />);
  await waitFor(() => screen.getByText(/test test2/i));

});

...

test('some test8..', async () => {
  server.use(
    rest.get('/api/1/..', (req, res, ctx) => {
      return res(ctx.json({...}));
    }),

    rest.get('/api/2/..', (req, res, ctx) => {
      return res(ctx.json({...}));
    }),
  );

  render(<Component />);
  await waitFor(() => screen.getByText(/test test8/i));

});


Actual request

I think it is not important.

Current behavior

Error: Headers x-msw-request-id forbidden
        at dispatchError (/Users/.../node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:62:19)
        at validCORSPreflightHeaders (/Users/.../node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:99:5)
        at Request.<anonymous> (/Users/.../node_modules/jsdom/lib/jsdom/living/xhr/xhr-utils.js:372:12)
        at Request.emit (events.js:315:20)
        at Request.onRequestResponse (/Users/.../node_modules/request/request.js:1059:10)
        at ClientRequestOverride.emit (events.js:315:20)
        at ClientRequest.<anonymous> (/Users/.../node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/createClientRequestOverride.ts:324:14)
        at ClientRequest.emit (events.js:327:22)
        at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:596:27)
        at HTTPParser.parserOnHeadersComplete (_http_common.js:119:17) undefined

buffer.js:210
throw new ERR_INVALID_ARG_TYPE('target', ['Buffer', 'Uint8Array'], target);
^

TypeError [ERR_INVALID_ARG_TYPE]: The "target" argument must be an instance of Buffer or Uint8Array. Received null
at _copy (buffer.js:210:11)
at Buffer.copy (buffer.js:768:12)
at Request.<anonymous> (/Users/.../node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js:864:11)
at Request.emit (events.js:315:20)
at Gunzip.<anonymous> (/Users/.../node_modules/request/request.js:1073:12)
at Gunzip.emit (events.js:315:20)
at addChunk (_stream_readable.js:295:12)
at readableAddChunk (_stream_readable.js:271:9)
at Gunzip.Readable.push (_stream_readable.js:212:10)
at Gunzip.Transform.push (_stream_transform.js:152:32) {
code: 'ERR_INVALID_ARG_TYPE'
}

Expected behavior

Just All PASS!!

Comment

I wrote many test code. but only when i use many msw api in one test file, this error was happen. (Actually it is hard to say many because it happen with 4~8 test function in one file)

when i used 0.21.2 version, I only got error ‘buffer.js:210’. After update 0.28.2 version, I got additional error ‘Headers x-msw-request-id forbidden’.

The bigger problem is sometimes the test passes, even though most time it fails.
The only one solution what i found is, divide each test function in a different test file, like component1.test.tsx, component2.test.tsx, … component6.test.tsx.
Then the result is all passed.

I guess the cause is maybe promise or memory leak problem.

Could i get some help? 🙏

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kettanaitocommented, May 10, 2021

Check your request URL, it looks like your actual request URL has an invalid protocol (http::// instead of http://):

• POST https:://some_exception_handler_url_api/

I’m closing this, as there hasn’t been any reproduction scenario provided. My reply above still stands: most likely the request handler you wrote cannot capture the request you make (very likely a URL mismatch). Feel free to post the link to the reproduction repository so we could take this further. Thanks.

0reactions
kettanaitocommented, Apr 29, 2021

The warning you’ve posted above means that your request isn’t intercepted by MSW. This is likely due to different request URLs in your handler and in the actual request. Please compare those carefully. Once again, you’ve stripped the actual/expected URLs from the info, so there isn’t much I can help with.

Since your request gets performed as-is (to the actual endpoint), that endpoint is likely to error on the presence of the x-msw-request-id header. That’s not exactly an issue, as your setup doesn’t satisfy the intention.

Yeah, a reproduction repository would be best. It’s okay if it takes time. It saves much more for the maintainers to get to the root cause and fix it. Thanks for understanding that.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Error: Headers Authorization forbidden" in CORS · Issue #2408
Hey, I have a test running with jsdom, in which i'm sending a cross-origin request with an Authorization header (i'm using nock to...
Read more >
Node.Js error "No 'Access-Control-Allow-Origin' header is ...
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. I am implementing each the same way ......
Read more >
open-draft - Bountysource
If MSW output a warning/error when a network request was made to an unmocked ... [Error] Headers x-msw-request-id forbidden at dispatchError / Buffer.js...
Read more >
Errors | Node.js v19.3.0 Documentation
When encountering this error, a possible alternative to creating a Buffer instance ... HTTP/1 connection specific headers are forbidden to be used in...
Read more >
Fetch Standard
The Fetch Standard also defines the fetch() JavaScript API, which exposes ... A forbidden response-header name is a header name that is a ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found