Skip to content

creating a mockrequest with a header from the options does not allow getting the header according in the standard way. #316

@fvanderwielen

Description

@fvanderwielen

When creating a mockrequest the following way:

        const { req } = createMocks({
            method: "POST",
            body: "someBody",
            headers: { "Referer" : "someReferer" },
        });
        const response = await handlerUnderTest(req);

the request handler under test cannot use Headers.get from the standard. This is needed when for example mocking NextRequest, because it is heavily based upon that Web Request API standard, it just extends it a little.

This happens because inside createRequest the headers object gets passed directly into the mock requestobject here.

There is a workaround by monkeypatching the getter onto the mockRequest after creation:

        const { req } = createMocks({
            method: "POST",
            body: "someBody",
        });
        req.headers.get = () => "somereferer";

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions