dimanche 14 juin 2020

How to send cookies through hapi when testing

I am testing an endpoint with jest using server.inject(options) from hapi.

I want to test if the user has the cookie. If it doesn't then the server should return an error. If I don't include any header I am getting Unauthorized error with Missing authentication. Which I think works because I do not provide any cookie. But I can't test if this works when I provide a cookie. I tried to add the cookie that the client used in the api call (I took it from browser) but I get invalid cookie message.

So I have no idea which cookie I should use and why that one doesn't work.

 test('should get 200', async function () {
        const options = {
            method: 'POST',
            url: '/test',
            headers: {
                cookie: 'my-cookie',
            },

        };
        const data = await server.inject(options);
        expect(data.statusCode).toBe(200);
    });

I know it might depend on my setup or the server endpoint but I would appreciate it if I could find where's the problem coming from. I played with the server configuration but it seems that it doesn't have any effect.

Aucun commentaire:

Enregistrer un commentaire