eugef / node-mocks-http

Mock 'http' objects for testing Express routing functions
Other
755 stars 134 forks source link

request.is() function returns null #151

Closed wizardnet972 closed 4 years ago

wizardnet972 commented 6 years ago

The post request is not valid.

 const req1 = httpMocks.createRequest({
                url: '/',
                method: 'POST',
                body: {
                    name: 'test',
                },
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            });

            console.log('rr', req1.is('application/x-www-form-urlencoded')); // return null
wizardnet972 commented 6 years ago

Found why. I should add transfer-encoding.

like this:

    const req1 = httpMocks.createRequest({
                url: '/',
                method: 'POST',
                body: {
                    name: 'test',          
                },
                headers: {
                    'content-type': 'application/x-www-form-urlencoded',
                    'transfer-encoding': 'chunked'
                },
jaqua commented 6 years ago

Your are right, but why do we have to set transfer-encoding?

cringblom commented 6 years ago

This is because of the type-is dependancy which is used by the req.is() function From the docs:

null will be returned if the request does not have a body.

To determine if the request has a body it checks for the headers: transfer-encoding or content-length.

Neither of these seem to be set by httpMocks.createRequest({...})

github-actions[bot] commented 4 years ago

Stale issue message