[Pkg-javascript-commits] [sockjs-client] 62/434: Examples/tests moved into separated directory, node code included.
Tonnerre Lombard
tonnerre-guest at moszumanska.debian.org
Wed Jan 8 00:47:01 UTC 2014
This is an automated email from the git hooks/post-receive script.
tonnerre-guest pushed a commit to branch master
in repository sockjs-client.
commit 8710bbf4e451eafa61e5f422f566f4268aad3381
Author: Marek Majkowski <majek04 at gmail.com>
Date: Wed Aug 10 14:07:49 2011 +0100
Examples/tests moved into separated directory, node code included.
---
.gitignore | 2 +-
Makefile | 27 +++++
bin/simple_http_server.js | 141 ----------------------
gen.sh | 9 --
run.sh => test.sh | 0
tests/config.js | 12 ++
tests/html/config.js | 2 +
tests/{ => html}/example-cursors.html | 0
tests/{ => html}/index.html | 0
tests/{ => html}/lib/.placeholder | 0
tests/{ => html}/smoke-reconnect.html | 0
tests/{ => html}/smoke-throughput.html | 2 +-
{tests-src => tests/html/src}/test-factory.coffee | 0
{tests-src => tests/html/src}/test-run.coffee | 0
tests/{ => html}/static/jquery.min.js | 0
tests/{ => html}/static/qunit.css | 0
tests/{ => html}/static/qunit.min.js | 0
tests/{ => html}/tests-qunit.html | 0
tests/server.js | 9 ++
tests/simple_http_server.js | 141 ++++++++++++++++++++++
tests/sockjs_test_server.js | 93 ++++++++++++++
21 files changed, 286 insertions(+), 152 deletions(-)
diff --git a/.gitignore b/.gitignore
index 2c02b79..e5f8493 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,4 @@ node_modules
sockjs.min.js
sockjs.pretty.js
sockjs.js
-tests/lib/*.js
+tests/html/lib/*.js
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..ec852a3
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,27 @@
+.PHONY: all build tests test
+
+all: sockjs.js
+
+build: sockjs.js sockjs.min.js sockjs.pretty.js
+
+sockjs.js: lib/*js
+ coffee bin/render.coffee lib/all.js > $@
+
+sockjs.min.js: lib/*js
+ coffee bin/render.coffee --minify lib/all.js > $@
+
+sockjs.pretty.js: lib/*js
+ coffee bin/render.coffee --minify --pretty lib/all.js > $@
+
+tests/html/lib/sockjs.js: sockjs.js
+ cp $< $@
+
+tests/html/lib/test-factory.js: tests/html/src/test-factory.coffee
+ coffee -o tests/html/lib/ -c --bare $<
+
+tests/html/lib/test-run.js: tests/html/src/test-run.coffee
+ coffee -o tests/html/lib/ -c --bare $<
+
+test: tests
+tests: tests/html/lib/sockjs.js tests/html/lib/test-factory.js tests/html/lib/test-run.js
+ node tests/server.js
diff --git a/bin/simple_http_server.js b/bin/simple_http_server.js
deleted file mode 100644
index e599772..0000000
--- a/bin/simple_http_server.js
+++ /dev/null
@@ -1,141 +0,0 @@
-var http = require('http');
-var url = require('url');
-var fs = require('fs');
-var path = require('path');
-var compress = require('compress');
-var crypto = require('crypto');
-var optparse = require('optparse');
-
-var mimetypes = {
- html: 'text/html',
- htm: 'text/html',
- js: 'application/javascript',
- json: 'application/json',
- css: 'text/css',
- png: 'image/png',
- jpeg: 'image/jpeg',
- jpg: 'image/jpeg',
- gif: 'image/gif'
-};
-
-var md5_hex = function (data) {
- return crypto.createHash('md5').update(data).digest('hex');
-};
-
-
-var compressable_ct = ['text', 'application'];
-
-var req_fin = function(statusCode, req, res, content, encoding) {
- var gz = '';
- if (!encoding) encoding = 'utf-8';
- if (compress && content &&
- 'accept-encoding' in req.headers &&
- req.headers['accept-encoding'].split(',').indexOf('gzip') !== -1 &&
- compressable_ct.indexOf(res.getHeader('Content-Type').split('/')[0]) !== -1) {
- var gzip = new compress.Gzip;
- gzip.init();
- content = gzip.deflate(content, encoding) + gzip.end();
- res.setHeader('Content-Encoding', 'gzip');
- gz = '(gzip)';
- encoding = 'binary';
- }
- if (content) {
- // According to: http://code.google.com/speed/page-speed/docs/caching.html
- // 'vary' effectively disables caching for IE. IE users are
- // screwed anyway.
- res.setHeader('Vary', 'Accept-Encoding');
- }
-
- if (options.cacheable) {
- var cache_for = 365 * 24 * 60 * 60; // one year.
- // See: http://code.google.com/speed/page-speed/docs/caching.html
- res.setHeader('Cache-Control', 'public, max-age=' + cache_for);
- var exp = new Date();
- exp.setTime(exp.getTime() + cache_for * 1000);
- res.setHeader('Expires', exp.toGMTString());
- };
-
- var cl = '';
- if (content) {
- cl = content.length;
- res.setHeader('Content-Length', content.length);
- } else {
- content = '';
- }
- try {
- res.writeHead(statusCode);
- res.end(content, encoding);
- } catch (x) {}
-
- console.log(req.method, req.url, statusCode, cl, gz);
-};
-
-var fs_decorator = function(req, res, cb) {
- return function (err, data) {
- if (!err) {
- try {
- cb(data);
- } catch (x) {
- err = true;
- req_fin(500, req, res);
- console.log('error', x.stack);
- }
- } else {
- // file doesn't exist or can't read it.
- req_fin(404, req, res);
- }
- };
-};
-var handler = function(req, res) {
- var filename = options.topdir + '/';
- filename += url.parse(req.url).pathname.slice(1) || 'index.html';
- var cb1, cb2;
- cb1 = function(stats) {
- var last_modified = stats.mtime.toGMTString();
- if (req.headers['if-modified-since'] === last_modified) {
- req_fin(304, req, res);
- return;
- }
- if (!options.cacheable) {
- res.setHeader('Last-Modified', last_modified);
- }
- fs.readFile(filename, fs_decorator(req, res, cb2));
- };
- cb2 = function (content) {
- var md5 = '"' + md5_hex(content) + '"';
- if (req.headers['if-none-match'] === md5) {
- req_fin(304, req, res);
- return;
- }
- var mimetype = mimetypes[path.extname(filename).slice(1)] || 'text/plain';
- mimetype += '; charset=UTF-8';
- res.setHeader('Content-Type', mimetype);
- res.setHeader('ETag', md5);
- req_fin(200, req, res, content);
- };
- fs.stat(filename, fs_decorator(req, res, cb1));
-};
-
-var switches = [
- ['-p', '--port PORT', 'Port to listen on (default: 8080)'],
- ['-h', '--host HOST', 'Ip address to bind to (default: 0.0.0.0)'],
- ['-d', '--dir DIR', 'Directory from which to serve files (default: .)']
-];
-
-var options = {port:8000, host:'0.0.0.0', topdir:'.', cacheable:false};
-var parser = new optparse.OptionParser(switches)
-parser.on('port', function(_,v) {
- options.port = Number(v);
- });
-parser.on('host', function (_,v) {
- options.host = v;
- });
-parser.on('dir', function (_,v) {
- options.topdir = v;
- });
-parser.parse(process.ARGV.slice(2));
-
-console.log(" [*] Listening on", options.host + ':' + options.port, ' serving directory:', options.topdir);
-var server = http.createServer();
-server.addListener('request', handler);
-server.listen(options.port, options.host);
diff --git a/gen.sh b/gen.sh
deleted file mode 100755
index 2231e2c..0000000
--- a/gen.sh
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh
-T0=`date +%s%0N`
-echo " [*] Generating javascript"
-coffee bin/render.coffee lib/all.js > sockjs.js && \
-coffee bin/render.coffee --minify lib/all.js > sockjs.min.js && \
-coffee bin/render.coffee --minify --pretty lib/all.js > sockjs.pretty.js && \
-T1=`date +%s%0N`
-TDMS=$(( ($T1-$T0) / 1000000))
-echo " [*] ok $TDMS ms"
diff --git a/run.sh b/test.sh
similarity index 100%
rename from run.sh
rename to test.sh
diff --git a/tests/config.js b/tests/config.js
new file mode 100644
index 0000000..90d70b7
--- /dev/null
+++ b/tests/config.js
@@ -0,0 +1,12 @@
+exports.sockjs = {
+ opts: {
+ sockjs_url: "http://127.0.0.1:8000/lib/sockjs.js"
+ },
+ port: 9999,
+ host: '0.0.0.0'
+};
+
+exports.static = {
+ port: 8000,
+ host: '0.0.0.0'
+};
diff --git a/tests/html/config.js b/tests/html/config.js
new file mode 100644
index 0000000..0601c1a
--- /dev/null
+++ b/tests/html/config.js
@@ -0,0 +1,2 @@
+// by default just change the port to 9999
+var sockjs_url = document.location.protocol + '//' + document.location.hostname + ':9999';
diff --git a/tests/example-cursors.html b/tests/html/example-cursors.html
similarity index 100%
rename from tests/example-cursors.html
rename to tests/html/example-cursors.html
diff --git a/tests/index.html b/tests/html/index.html
similarity index 100%
rename from tests/index.html
rename to tests/html/index.html
diff --git a/tests/lib/.placeholder b/tests/html/lib/.placeholder
similarity index 100%
rename from tests/lib/.placeholder
rename to tests/html/lib/.placeholder
diff --git a/tests/smoke-reconnect.html b/tests/html/smoke-reconnect.html
similarity index 100%
rename from tests/smoke-reconnect.html
rename to tests/html/smoke-reconnect.html
diff --git a/tests/smoke-throughput.html b/tests/html/smoke-throughput.html
similarity index 97%
rename from tests/smoke-throughput.html
rename to tests/html/smoke-throughput.html
index 5e73855..709ed8b 100644
--- a/tests/smoke-throughput.html
+++ b/tests/html/smoke-throughput.html
@@ -6,7 +6,7 @@
<link href="data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII=" rel="icon" type="image/x-icon" />
- <script type="text/javascript" src="sockjs.js"></script>
+ <script type="text/javascript" src="lib/sockjs.js"></script>
<script type="text/javascript" src="static/jquery.min.js"></script>
<script type="text/javascript" src="config.js"></script>
diff --git a/tests-src/test-factory.coffee b/tests/html/src/test-factory.coffee
similarity index 100%
rename from tests-src/test-factory.coffee
rename to tests/html/src/test-factory.coffee
diff --git a/tests-src/test-run.coffee b/tests/html/src/test-run.coffee
similarity index 100%
rename from tests-src/test-run.coffee
rename to tests/html/src/test-run.coffee
diff --git a/tests/static/jquery.min.js b/tests/html/static/jquery.min.js
similarity index 100%
rename from tests/static/jquery.min.js
rename to tests/html/static/jquery.min.js
diff --git a/tests/static/qunit.css b/tests/html/static/qunit.css
similarity index 100%
rename from tests/static/qunit.css
rename to tests/html/static/qunit.css
diff --git a/tests/static/qunit.min.js b/tests/html/static/qunit.min.js
similarity index 100%
rename from tests/static/qunit.min.js
rename to tests/html/static/qunit.min.js
diff --git a/tests/tests-qunit.html b/tests/html/tests-qunit.html
similarity index 100%
rename from tests/tests-qunit.html
rename to tests/html/tests-qunit.html
diff --git a/tests/server.js b/tests/server.js
new file mode 100644
index 0000000..12eafe2
--- /dev/null
+++ b/tests/server.js
@@ -0,0 +1,9 @@
+var shs = require('./simple_http_server');
+var sts = require('./sockjs_test_server');
+
+var config = require('./config');
+
+config.static.topdir = __dirname + '/html';
+
+shs.createServer(config.static);
+sts.createServer(config.sockjs);
diff --git a/tests/simple_http_server.js b/tests/simple_http_server.js
new file mode 100644
index 0000000..158b7ea
--- /dev/null
+++ b/tests/simple_http_server.js
@@ -0,0 +1,141 @@
+var http = require('http');
+var url = require('url');
+var fs = require('fs');
+var path = require('path');
+var compress = require('compress');
+var crypto = require('crypto');
+
+var mimetypes = {
+ html: 'text/html',
+ htm: 'text/html',
+ js: 'application/javascript',
+ json: 'application/json',
+ css: 'text/css',
+ png: 'image/png',
+ jpeg: 'image/jpeg',
+ jpg: 'image/jpeg',
+ gif: 'image/gif'
+};
+
+var md5_hex = function (data) {
+ return crypto.createHash('md5').update(data).digest('hex');
+};
+
+
+var compressable_ct = ['text', 'application'];
+
+var create_handler = function(options) {
+ var req_fin = function(statusCode, req, res, content, encoding) {
+ var gz = '';
+ if (!encoding) encoding = 'utf-8';
+ if (compress && content &&
+ 'accept-encoding' in req.headers &&
+ req.headers['accept-encoding'].split(',').indexOf('gzip') !== -1 &&
+ compressable_ct.indexOf(res.getHeader('Content-Type').split('/')[0]) !== -1) {
+ var gzip = new compress.Gzip;
+ gzip.init();
+ content = gzip.deflate(content, encoding) + gzip.end();
+ res.setHeader('Content-Encoding', 'gzip');
+ gz = '(gzip)';
+ encoding = 'binary';
+ }
+ if (content) {
+ // According to: http://code.google.com/speed/page-speed/docs/caching.html
+ // 'vary' effectively disables caching for IE. IE users are
+ // screwed anyway.
+ res.setHeader('Vary', 'Accept-Encoding');
+ }
+
+ if (options.cacheable) {
+ var cache_for = 365 * 24 * 60 * 60; // one year.
+ // See: http://code.google.com/speed/page-speed/docs/caching.html
+ res.setHeader('Cache-Control', 'public, max-age=' + cache_for);
+ var exp = new Date();
+ exp.setTime(exp.getTime() + cache_for * 1000);
+ res.setHeader('Expires', exp.toGMTString());
+ };
+
+ var cl = '';
+ if (content) {
+ cl = content.length;
+ res.setHeader('Content-Length', content.length);
+ } else {
+ content = '';
+ }
+ try {
+ res.writeHead(statusCode);
+ res.end(content, encoding);
+ } catch (x) {}
+
+ console.log(req.method, req.url, statusCode, cl, gz);
+ };
+
+ var fs_decorator = function(req, res, cb) {
+ return function (err, data) {
+ if (!err) {
+ try {
+ cb(data);
+ } catch (x) {
+ err = true;
+ req_fin(500, req, res);
+ console.log('error', x.stack);
+ }
+ } else {
+ // file doesn't exist or can't read it.
+ req_fin(404, req, res);
+ }
+ };
+ };
+
+ var handler = function(req, res) {
+ var filename = options.topdir + '/';
+ filename += url.parse(req.url).pathname.slice(1) || 'index.html';
+ var cb1, cb2;
+ cb1 = function(stats) {
+ var last_modified = stats.mtime.toGMTString();
+ if (req.headers['if-modified-since'] === last_modified) {
+ req_fin(304, req, res);
+ return;
+ }
+ if (!options.cacheable) {
+ res.setHeader('Last-Modified', last_modified);
+ }
+ fs.readFile(filename, fs_decorator(req, res, cb2));
+ };
+ cb2 = function (content) {
+ var md5 = '"' + md5_hex(content) + '"';
+ if (req.headers['if-none-match'] === md5) {
+ req_fin(304, req, res);
+ return;
+ }
+ var mimetype = mimetypes[path.extname(filename).slice(1)] || 'text/plain';
+ mimetype += '; charset=UTF-8';
+ res.setHeader('Content-Type', mimetype);
+ res.setHeader('ETag', md5);
+ req_fin(200, req, res, content);
+ };
+ fs.stat(filename, fs_decorator(req, res, cb1));
+ };
+ return handler;
+};
+
+var switches = [
+ ['-p', '--port PORT', 'Port to listen on (default: 8080)'],
+ ['-h', '--host HOST', 'Ip address to bind to (default: 0.0.0.0)'],
+ ['-d', '--dir DIR', 'Directory from which to serve files (default: .)']
+];
+
+exports.createServer = function(options) {
+ if (!options) options = {};
+ options.port = options.port || 8080;
+ options.host = options.host || '0.0.0.0';
+ options.topdir = options.topdir || '.';
+ options.cacheable = options.cacheable || false;
+
+ console.log(' [*] Serving directory:', JSON.stringify(options.topdir),
+ ' on:', options.host + ':' + options.port,
+ ' (cache is:', options.cacheable,')');
+ var server = http.createServer();
+ server.addListener('request', create_handler(options));
+ server.listen(options.port, options.host);
+}
diff --git a/tests/sockjs_test_server.js b/tests/sockjs_test_server.js
new file mode 100644
index 0000000..ae51bee
--- /dev/null
+++ b/tests/sockjs_test_server.js
@@ -0,0 +1,93 @@
+var http = require('http');
+var url = require('url');
+
+var sockjs = require('sockjs');
+
+
+exports.createServer = function(config) {
+ var sjs_echo = new sockjs.Server(config.opts);
+ sjs_echo.on('open', function(conn){
+ console.log(' [+] echo open ' + conn);
+ conn.on('close', function(e) {
+ console.log(' [-] echo close ' + conn, e);
+ });
+ conn.on('message', function(e) {
+ var d = JSON.stringify(e.data);
+ console.log(' [ ] echo message ' + conn,
+ d.slice(0,64)+
+ ((d.length > 64) ? '...' : ''));
+ conn.send(e.data);
+ });
+ });
+
+ var sjs_close = new sockjs.Server(config.opts);
+ sjs_close.on('open', function(conn){
+ console.log(' [+] clos open ' + conn);
+ conn.close(3000, "Go away!");
+ conn.on('close', function(e) {
+ console.log(' [-] clos close ' + conn, e);
+ });
+ });
+
+ var sjs_ticker = new sockjs.Server(config.opts);
+ sjs_ticker.on('open', function(conn){
+ console.log(' [+] ticker open ' + conn);
+ var tref;
+ var schedule = function() {
+ conn.send('tick!');
+ tref = setTimeout(schedule, 1000);
+ };
+ tref = setTimeout(schedule, 1000);
+ conn.on('close', function(e) {
+ clearTimeout(tref);
+ console.log(' [-] ticker close ' + conn, e);
+ });
+ });
+
+ var broadcast = {};
+ var sjs_broadcast = new sockjs.Server(config.opts);
+ sjs_broadcast.on('open', function(conn){
+ console.log(' [+] broadcast open ' + conn);
+ broadcast[conn.id] = conn;
+ conn.on('close', function(e) {
+ delete broadcast[conn.id];
+ console.log(' [-] broadcast close' + conn, e);
+ });
+ conn.on('message', function(e) {
+ console.log(' [-] broadcast message', e);
+ for(var id in broadcast) {
+ broadcast[id].send(e.data);
+ }
+ });
+ });
+
+
+ var default_handler = function(req, res) {
+ res.statusCode = 404;
+ if (url.parse(req.url).pathname === '/500_error') {
+ res.statusCode = 500;
+ }
+ console.log(res.statusCode, req.url);
+ if (res.writeHead) {
+ res.writeHead(res.statusCode);
+ res.end("Error");
+ } else{
+ res.end();
+ }
+ };
+
+ console.log(" [*] config:", config.opts);
+ console.log(" [*] SockJS test server listening on",
+ config.host +':'+config.port);
+ var server = http.createServer();
+ server.addListener('request', default_handler);
+ server.addListener('upgrade', default_handler);
+
+
+ sjs_echo.installHandlers(server, {prefix:'[/]echo'});
+ sjs_close.installHandlers(server, {prefix:'[/]close'});
+ sjs_ticker.installHandlers(server, {prefix:'[/]ticker'});
+ sjs_broadcast.installHandlers(server, {prefix:'[/]broadcast'});
+
+ server.listen(config.port, config.host);
+};
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git
More information about the Pkg-javascript-commits
mailing list