Skip to content

Commit 51da326

Browse files
committed
chore: improve checks in getCSRFToken command
1 parent 475b19a commit 51da326

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

cypress/support/commands.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,22 @@ Cypress.Commands.add('login', (username, password) => {
4242

4343
Cypress.Commands.add('getCSRFToken', () => {
4444
return cy.request('GET', 'http://localhost:8080/api/v1/repo').then((res) => {
45-
const cookies = res.headers['set-cookie'];
46-
const csrfCookie = cookies.find(c => c.startsWith('csrf='));
45+
let cookies = res.headers['set-cookie'];
46+
47+
if (typeof cookies === 'string') {
48+
cookies = [cookies];
49+
}
4750

51+
if (!cookies) {
52+
throw new Error('No cookies found in response');
53+
}
54+
55+
const csrfCookie = cookies.find(c => c.startsWith('csrf='));
4856
if (!csrfCookie) {
49-
throw new Error('No CSRF cookie found');
57+
throw new Error('No CSRF cookie found in response headers');
5058
}
5159

52-
const token = decodeURIComponent(csrfCookie.split('=')[1].split(';')[0]);
53-
return cy.wrap(token);
60+
const token = csrfCookie.split('=')[1].split(';')[0];
61+
return cy.wrap(decodeURIComponent(token));
5462
});
5563
});

0 commit comments

Comments
 (0)