Skip to content

Commit 630ce85

Browse files
committed
[test] add a test case to test document.cookies for 'http', 'file' and 'app' protocol. See issue nwjs#1130
1 parent b871dfa commit 630ce85

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script>
5+
var gui = require('nw.gui');
6+
function getCookie(c_name)
7+
{
8+
var c_value = document.cookie;
9+
var c_start = c_value.indexOf(" " + c_name + "=");
10+
if (c_start == -1)
11+
{
12+
c_start = c_value.indexOf(c_name + "=");
13+
}
14+
if (c_start == -1)
15+
{
16+
c_value = null;
17+
}
18+
else
19+
{
20+
c_start = c_value.indexOf("=", c_start) + 1;
21+
var c_end = c_value.indexOf(";", c_start);
22+
if (c_end == -1)
23+
{
24+
c_end = c_value.length;
25+
}
26+
c_value = unescape(c_value.substring(c_start,c_end));
27+
}
28+
return c_value;
29+
}
30+
31+
function setCookie(c_name,value,exdays)
32+
{
33+
var exdate=new Date();
34+
exdate.setDate(exdate.getDate() + exdays);
35+
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
36+
document.cookie=c_name + "=" + c_value;
37+
}
38+
39+
function checkCookie()
40+
{
41+
var username=123;
42+
setCookie("username",username,365);
43+
username=getCookie("username");
44+
var client = require('../../../nw_test_app').createClient({
45+
argv: gui.App.argv,
46+
data: username
47+
});
48+
}
49+
</script>
50+
</head>
51+
<body onload="checkCookie()">
52+
</body>
53+
</html>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "nw",
3+
"main": "app://whatever/index.html",
4+
"window": {
5+
"show": false
6+
}
7+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script>
5+
var gui = require('nw.gui');
6+
function getCookie(c_name)
7+
{
8+
var c_value = document.cookie;
9+
var c_start = c_value.indexOf(" " + c_name + "=");
10+
if (c_start == -1)
11+
{
12+
c_start = c_value.indexOf(c_name + "=");
13+
}
14+
if (c_start == -1)
15+
{
16+
c_value = null;
17+
}
18+
else
19+
{
20+
c_start = c_value.indexOf("=", c_start) + 1;
21+
var c_end = c_value.indexOf(";", c_start);
22+
if (c_end == -1)
23+
{
24+
c_end = c_value.length;
25+
}
26+
c_value = unescape(c_value.substring(c_start,c_end));
27+
}
28+
return c_value;
29+
}
30+
31+
function setCookie(c_name,value,exdays)
32+
{
33+
var exdate=new Date();
34+
exdate.setDate(exdate.getDate() + exdays);
35+
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
36+
document.cookie=c_name + "=" + c_value;
37+
}
38+
39+
function checkCookie()
40+
{
41+
var username=123;
42+
setCookie("username",username,365);
43+
username=getCookie("username");
44+
var client = require('../../../nw_test_app').createClient({
45+
argv: gui.App.argv,
46+
data: username
47+
});
48+
}
49+
</script>
50+
</head>
51+
<body onload="checkCookie()">
52+
</body>
53+
</html>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "nw",
3+
"main": "index.html",
4+
"window": {
5+
"show": false
6+
}
7+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
var path = require('path');
2+
var app_test = require('./nw_test_app');
3+
var assert = require('assert');
4+
var gui = require('nw.gui');
5+
var results = new Array();
6+
describe('document.cookies', function() {
7+
8+
describe('http', function() {
9+
before(function(done) {
10+
this.timeout(0);
11+
var url = "http://127.0.0.1:8123/document_cookies.html";
12+
var win = gui.Window.open(url);
13+
14+
setTimeout(function() {
15+
results.push(win.window.msg.textContent);
16+
done();
17+
win.window.close();
18+
}, 1000);
19+
});
20+
it('should be set', function() {
21+
assert.equal(results[0], '123');
22+
});
23+
});
24+
25+
describe('file', function() {
26+
before(function(done) {
27+
this.timeout(0);
28+
var child = app_test.createChildProcess({
29+
execPath: process.execPath,
30+
appPath: path.join(global.tests_dir, 'document_cookies', 'file'),
31+
end: function(data, app) {
32+
results.push(data);
33+
app.kill();
34+
done();
35+
}
36+
});
37+
setTimeout(done, 3000);
38+
});
39+
it ('should be set', function() {
40+
assert.equal(results[1], '123');
41+
});
42+
});
43+
44+
describe('app', function() {
45+
before(function(done) {
46+
this.timeout(0);
47+
var child = app_test.createChildProcess({
48+
execPath: process.execPath,
49+
appPath: path.join(global.tests_dir, 'document_cookies', 'app'),
50+
end: function(data, app) {
51+
results.push(data);
52+
app.kill();
53+
done();
54+
}
55+
});
56+
setTimeout(done, 3000);
57+
});
58+
it ('should be set', function() {
59+
assert.equal(results[2], '123');
60+
});
61+
});
62+
63+
});

tests/server/document_cookies.html

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script>
5+
function getCookie(c_name)
6+
{
7+
var c_value = document.cookie;
8+
var c_start = c_value.indexOf(" " + c_name + "=");
9+
if (c_start == -1)
10+
{
11+
c_start = c_value.indexOf(c_name + "=");
12+
}
13+
if (c_start == -1)
14+
{
15+
c_value = null;
16+
}
17+
else
18+
{
19+
c_start = c_value.indexOf("=", c_start) + 1;
20+
var c_end = c_value.indexOf(";", c_start);
21+
if (c_end == -1)
22+
{
23+
c_end = c_value.length;
24+
}
25+
c_value = unescape(c_value.substring(c_start,c_end));
26+
}
27+
return c_value;
28+
}
29+
30+
function setCookie(c_name,value,exdays)
31+
{
32+
var exdate=new Date();
33+
exdate.setDate(exdate.getDate() + exdays);
34+
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
35+
document.cookie=c_name + "=" + c_value;
36+
}
37+
38+
function checkCookie()
39+
{
40+
var username=123;
41+
setCookie("username",username,365);
42+
username=getCookie("username");
43+
document.getElementById('msg').innerHTML = username;
44+
<!-- (username!=null && username!="") -->
45+
}
46+
</script>
47+
</head>
48+
<body onload="checkCookie()">
49+
<span>document.cookies test </span>
50+
<span id="msg" style="color:Green"></span>
51+
</body>
52+
</html>

0 commit comments

Comments
 (0)