eugef / node-mocks-http

Mock 'http' objects for testing Express,js, Next.js and Koa routing functions
Other
755 stars 134 forks source link

request.getHeader() missing? #240

Closed rhazegh closed 3 years ago

rhazegh commented 3 years ago

Thank you for this great library. I am using this to test my Nextjs API routes. It has been working so far until I decided to send some http headers in the request.

In the test file:

const { req, res } = createMocks({
  method: "GET",
  headers: { language: "en" },
});

When I try to retrieve the headers in the API end point:

export const handler = async (req, res) => {
  // ...
  const { language } = req.headers;     // <-- This works
  const lang2 = req.getHeader("language");  // <-- This throws an error: TypeError: req.getHeader is not a function
  // ...
}

According to Nodejs documentation https://nodejs.org/api/http.html#http_request_getheader_name, it seems that request.getHeader(name) should be available.

Is this something that can be added to node-mocks-http?

eugef commented 3 years ago

Hi @rhazegh you are right, req.getHeader() should be also available.

Here is the PR with a fix https://github.com/howardabrams/node-mocks-http/pull/241

rhazegh commented 3 years ago

@eugef Fantastic. Thanks for the quick fix.

BTW, I found out that Nextjs's request object for API routes is an instance of Node's http.IncomingMessage which supports request.headers so I ended up not using request.getHeader(). Thanks again.

eugef commented 3 years ago

you are welcome