spec: Group session.cookies tests

This commit is contained in:
Cheng Zhao 2016-01-13 17:19:53 +08:00
parent 60d44b3b04
commit 63c646242a

View file

@ -28,80 +28,51 @@ describe('session module', function() {
afterEach(function() { afterEach(function() {
return w.destroy(); return w.destroy();
}); });
it('should get cookies', function(done) {
var server; describe('session.cookies', function() {
server = http.createServer(function(req, res) { it('should get cookies', function(done) {
res.setHeader('Set-Cookie', ['0=0']); var server;
res.end('finished'); server = http.createServer(function(req, res) {
return server.close(); res.setHeader('Set-Cookie', ['0=0']);
}); res.end('finished');
return server.listen(0, '127.0.0.1', function() { return server.close();
var port; });
port = server.address().port; return server.listen(0, '127.0.0.1', function() {
w.loadURL(url + ":" + port); var port;
return w.webContents.on('did-finish-load', function() { port = server.address().port;
return w.webContents.session.cookies.get({ w.loadURL(url + ":" + port);
url: url return w.webContents.on('did-finish-load', function() {
}, function(error, list) { return w.webContents.session.cookies.get({
var cookie, i, len; url: url
if (error) { }, function(error, list) {
return done(error); var cookie, i, len;
} if (error) {
for (i = 0, len = list.length; i < len; i++) { return done(error);
cookie = list[i]; }
if (cookie.name === '0') { for (i = 0, len = list.length; i < len; i++) {
if (cookie.value === '0') { cookie = list[i];
return done(); if (cookie.name === '0') {
} else { if (cookie.value === '0') {
return done("cookie value is " + cookie.value + " while expecting 0"); return done();
} else {
return done("cookie value is " + cookie.value + " while expecting 0");
}
} }
} }
} return done('Can not find cookie');
return done('Can not find cookie'); });
}); });
}); });
}); });
}); it('should over-write the existent cookie', function(done) {
it('should over-write the existent cookie', function(done) { return session.defaultSession.cookies.set({
return session.defaultSession.cookies.set({ url: url,
url: url, name: '1',
name: '1', value: '1'
value: '1' }, function(error) {
}, function(error) {
if (error) {
return done(error);
}
return session.defaultSession.cookies.get({
url: url
}, function(error, list) {
var cookie, i, len;
if (error) { if (error) {
return done(error); return done(error);
} }
for (i = 0, len = list.length; i < len; i++) {
cookie = list[i];
if (cookie.name === '1') {
if (cookie.value === '1') {
return done();
} else {
return done("cookie value is " + cookie.value + " while expecting 1");
}
}
}
return done('Can not find cookie');
});
});
});
it('should remove cookies', function(done) {
return session.defaultSession.cookies.set({
url: url,
name: '2',
value: '2'
}, function(error) {
if (error) {
return done(error);
}
return session.defaultSession.cookies.remove(url, '2', function() {
return session.defaultSession.cookies.get({ return session.defaultSession.cookies.get({
url: url url: url
}, function(error, list) { }, function(error, list) {
@ -111,15 +82,48 @@ describe('session module', function() {
} }
for (i = 0, len = list.length; i < len; i++) { for (i = 0, len = list.length; i < len; i++) {
cookie = list[i]; cookie = list[i];
if (cookie.name === '2') { if (cookie.name === '1') {
return done('Cookie not deleted'); if (cookie.value === '1') {
return done();
} else {
return done("cookie value is " + cookie.value + " while expecting 1");
}
} }
} }
return done(); return done('Can not find cookie');
});
});
});
it('should remove cookies', function(done) {
return session.defaultSession.cookies.set({
url: url,
name: '2',
value: '2'
}, function(error) {
if (error) {
return done(error);
}
return session.defaultSession.cookies.remove(url, '2', function() {
return session.defaultSession.cookies.get({
url: url
}, function(error, list) {
var cookie, i, len;
if (error) {
return done(error);
}
for (i = 0, len = list.length; i < len; i++) {
cookie = list[i];
if (cookie.name === '2') {
return done('Cookie not deleted');
}
}
return done();
});
}); });
}); });
}); });
}); });
describe('session.clearStorageData(options)', function() { describe('session.clearStorageData(options)', function() {
fixtures = path.resolve(__dirname, 'fixtures'); fixtures = path.resolve(__dirname, 'fixtures');
return it('clears localstorage data', function(done) { return it('clears localstorage data', function(done) {