eugef / node-mocks-http

Mock 'http' objects for testing Express routing functions
Other
753 stars 133 forks source link

How to set cookies to mock request object? #165

Closed amitguptagwl closed 6 years ago

amitguptagwl commented 6 years ago

I've tried 3 ways and only 1 works out of them.

       var request  = httpMocks.createRequest({
            url: '/test/val',
            cookies: ["alpha=gamma","alpha=beta"],//doesn't work
            headers: {
                "cookie" : "foo=bar; Expires=Tue, 15 Mar 2016 21:47:38 GMT;",
                "header2" : "val2"
            }
        });

        request._setCookiesVariable("alpha","gamma");//doesn't work
        request._setCookiesVariable("alpha","beta");//doesn't work

So which is the proper way of setting the cookie to the mocked request?

eugef commented 6 years ago

This works for me:

const req = httpMocks.createRequest({
            cookies: {
                'alpha': 'gamma',
                'beta': 'lambda'
            }
        });