Skip to content

Commit 79afd02

Browse files
committed
Merge pull request nwjs#944 from kingFighter/server
Add test case for App.clearCache etc.
2 parents ccc942e + 3083c0f commit 79afd02

File tree

8 files changed

+115
-31
lines changed

8 files changed

+115
-31
lines changed

tests/automatic_tests/app/index.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Test Case for 'App.clearCache'</title>
6+
</head>
7+
<body onload='fun()'>
8+
<p id="wait" style="font-size:1.5em">Please wait to be closed.</p>
9+
<img src='http://127.0.0.1:8123/img.jpg'/>
10+
</body>
11+
</html>
12+
<script>
13+
function fun() {
14+
var gui = require('nw.gui');
15+
16+
if (self.name == '')
17+
self.name = 0; //initial
18+
else
19+
self.name++;//1:304,2:200;
20+
21+
if (self.name == 2)
22+
gui.App.clearCache();
23+
else if (self.name == 3) {
24+
var client = require('../../nw_test_app').createClient({
25+
argv: gui.App.argv,
26+
data: {}
27+
});
28+
}
29+
location.reload();
30+
}
31+
</script>
32+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
var gui = require('nw.gui');
2+
var assert = require('assert');
3+
var app_test = require('./nw_test_app');
4+
5+
describe('gui.App', function() {
6+
before(function(done) {
7+
this.timeout(0);
8+
var child = app_test.createChildProcess({
9+
execPath: process.execPath,
10+
appPath: path.join(global.tests_dir, 'app'),
11+
end: function(data, app) {
12+
app.kill();
13+
done();
14+
}
15+
});
16+
});
17+
18+
describe('manifest', function() {
19+
it('`gui.App.manifest` should equle to value of package.json', function() {
20+
assert.equal(gui.App.manifest.name, 'nw-tests');
21+
});
22+
23+
it('have window', function() {
24+
assert.equal(typeof gui.App.manifest.window, 'object');
25+
});
26+
27+
it('have main', function() {
28+
assert.equal(typeof gui.App.manifest.main, 'string');
29+
assert.equal(gui.App.manifest.main, 'index.html');
30+
});
31+
32+
it('have dependencies', function() {
33+
assert.equal(typeof gui.App.manifest.dependencies, 'object');
34+
});
35+
});
36+
37+
describe('clearCache()', function(done) {
38+
it('should clear the HTTP cache in memory and the one on disk', function() {
39+
var res_save = global.local_server.res_save;
40+
41+
assert.equal(res_save[1].status, 304);
42+
assert.equal(res_save[1].pathname, 'img.jpg');
43+
assert.equal(res_save[2].status, 200);
44+
assert.equal(res_save[2].pathname, 'img.jpg');
45+
});
46+
});
47+
})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "nw-gui.App-test",
3+
"main": "index.html"
4+
}

tests/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
var gui = require('nw.gui');
3333
var path = require('path');
3434
var program = require('commander');
35-
var local_server = require('./server/server');
36-
35+
global.local_server = require('./server/server');
36+
var local_server = global.local_server;
37+
3738
//socket server
3839
var net = require('net');
3940
global.server = net.createServer();

tests/server/config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
exports.Expires = {
2+
fileMatch: /^(.gif|.png|.jpg|.js|.css)$/ig,
3+
maxAge: 606024365
4+
};

tests/server/img.jpg

5.83 KB
Loading

tests/server/server.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ var http = require("http"),
22
url = require("url"),
33
path = require("path"),
44
fs = require("fs"),
5+
config = require('./config');
56
ports = new Array(8123,8124),
67
servers = new Array(ports.length);
78

9+
var res_save = new Array();
10+
811
types = {
912
".css": "text/css",
1013
".gif": "image/gif",
@@ -28,7 +31,7 @@ types = {
2831

2932
function request_listener(req, res) {
3033
var pathname=__dirname+url.parse(req.url).pathname;
31-
34+
console.log("Receive request");
3235
if (path.extname(pathname)=="") {
3336
pathname+="/";
3437
}
@@ -39,11 +42,26 @@ function request_listener(req, res) {
3942

4043
path.exists(pathname,function(exists){
4144
if(exists){
42-
var content_type = types[path.extname(pathname)] || "text/plain";
43-
res.writeHead(200, {"Content-Type": content_type});
45+
var ext = path.extname(pathname);
46+
var content_type = types[ext] || "text/plain";
4447

45-
fs.readFile(pathname,function (err,data){
46-
res.end(data);
48+
res.setHeader("Content-Type", content_type);
49+
50+
fs.stat(pathname, function (err, stat) {
51+
var last_modified = stat.mtime.toUTCString();
52+
var if_modified_since = "If-Modified-Since".toLowerCase();
53+
res.setHeader("Last-Modified", last_modified);
54+
55+
if (req.headers[if_modified_since] && last_modified == req.headers[if_modified_since]) {
56+
res_save.push({"status": 304, 'pathname':req.url.slice(1)});
57+
res.writeHead(304, "Not Modified");
58+
res.end();
59+
} else {
60+
res_save.push({"status": 200, 'pathname':req.url.slice(1)});
61+
fs.readFile(pathname,function (err,data){
62+
res.end(data);
63+
});
64+
}
4765
});
4866
} else {
4967
res.writeHead(404, {"Content-Type": "text/html"});
@@ -65,3 +83,5 @@ for (var i = 0; i < ports.length; i++) {
6583
servers[i].on('error', error_handler);
6684
console.log("Server running at http://127.0.0.1:" + ports[i]);
6785
}
86+
87+
exports.res_save = res_save;

tests/tests/App/App.js

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)