Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
parse (setCookieStr = '', domain) {
// parse
var cookies = cookieParser.parse(cookieParser.splitCookiesString(setCookieStr))
// 转换为 Cookie 对象
return cookies.map((item) => {
if (!item.domain) item.domain = domain
return new Cookie(item)
})
}
it('should return a user object', async function () {
let res = await request(app).get('/login');
let setCookieHeader = res.headers['set-cookie'];
if (setCookieHeader.length === 1) {
setCookieHeader = setCookieParser.splitCookiesString(setCookieHeader[0]);
}
const cookies = setCookieParser.parse(setCookieHeader, { map: true });
const cookie = Object.values(cookies).map(c => c.name + '=' + c.value).join('; ');
const xsrfToken = cookies['XSRF-TOKEN'].value;
res = await request(app).post('/api/login')
.set('X-XSRF-TOKEN', xsrfToken)
.set('Cookie', cookie)
.send({ userName: 'administrator', password: 'urungi' })
.expect(200);
expect(res.body).toHaveProperty('user');
expect(res.body.user).toHaveProperty('roles');
expect(res.body.user.roles).toContain('ADMIN');
});
});
async function login (app, username = 'administrator', password = 'urungi') {
const res = await request(app).get('/login');
let setCookieHeader = res.headers['set-cookie'];
if (setCookieHeader.length === 1) {
setCookieHeader = setCookieParser.splitCookiesString(setCookieHeader[0]);
}
const cookies = setCookieParser.parse(setCookieHeader, { map: true });
const cookie = Object.values(cookies).map(c => c.name + '=' + c.value).join('; ');
const xsrfToken = cookies['XSRF-TOKEN'].value;
const headers = {
'X-XSRF-TOKEN': xsrfToken,
Cookie: cookie,
};
await request(app).post('/api/login')
.set(headers)
.send({ userName: username, password: password });
return headers;
}