Permalink
Browse files

added example: express-cookie, tested concurrency (#4)

  • Loading branch information...
1 parent 51b1e6e commit e11043bea15618fd555bfab7c1a61c635c9e1126 @mashpie committed Mar 17, 2013
Showing with 113 additions and 3 deletions.
  1. +6 −1 Makefile
  2. +42 −0 examples/express-cookies.js
  3. +60 −0 examples/express-cookies.test.js
  4. +5 −2 package.json
View
@@ -9,4 +9,9 @@ coverage: lib-cov
lib-cov:
@jscoverage i18n.js i18n-cov.js
-.PHONY: test
+examples:
+ @NODE_ENV=test mocha --reporter $(REPORTER) examples/*.test.js
+
+all: test examples
+
+.PHONY: test examples
@@ -0,0 +1,42 @@
+var express = require('express'),
+ i18n = process.env.EXPRESS_COV ? require('../i18n-cov') : require('../i18n'),
+ app = module.exports = express();
+
+i18n.configure({
+ locales: ['en', 'de'],
+ cookie: 'yourcookiename',
+ directory: './locales',
+});
+
+app.configure(function () {
+ app.use(express.cookieParser());
+ app.use(i18n.init);
+ app.use(function (req, res, next) {
+ res.locals.__ = res.__ = function () {
+ return i18n.__.apply(req, arguments);
+ };
+ res.locals.__n = res.__n = function () {
+ return i18n.__n.apply(req, arguments);
+ };
+ next();
+ });
+ return app.use(app.router);
+});
+
+app.get('/test', function (req, res) {
+ var _ref = 0,
+ delay = (_ref = req.query.delay) != null ? _ref : 0;
+ return setTimeout(function () {
+ res.send('<body>' + res.__('Hello') + '</body>');
+ }, delay);
+});
+
+app.get('/testfail', function (req, res) {
+ var _ref = 0,
+ delay = (_ref = req.query.delay) != null ? _ref : 0;
+ return setTimeout(function () {
+ res.send('<body>' + i18n.__('Hello') + '</body>');
+ }, delay);
+});
+
+app.listen(3000);
@@ -0,0 +1,60 @@
+require('./express-cookies');
+
+var should = require('should'),
+ Browser = require('zombie'),
+ async = require('async'),
+ DE = new Browser(),
+ EN = new Browser();
+EN.cookies('localhost', '/').set("yourcookiename", "en");
+DE.cookies('localhost', '/').set("yourcookiename", "de");
+
+visitLinks = function (asyncMethod, url, textEN, textDE) {
+ return it('should show '+textEN+' in EN and '+textDE+' in DE', function (done) {
+ return async[asyncMethod]([
+
+ function (cb) {
+
+ return EN.visit('http://localhost:3000/'+url+'/?delay=1000', function () {
+ EN.text('body').should.equal(textEN);
+ return cb();
+ });
+
+ },
+
+ function (cb) {
+ return setTimeout(function () {
+
+ return DE.visit('http://localhost:3000/'+url+'/', function () {
+ DE.text('body').should.equal(textDE);
+ return cb();
+ });
+
+ }, 200);
+ }
+
+ ],
+
+ done);
+
+ });
+};
+
+describe('res.__() is able to handle concurrent request correctly', function(){
+ describe('serial requests return different languages', function () {
+ return visitLinks('series', 'test', 'Hello', 'Hallo');
+ });
+
+ describe('parallel requests return different languages', function () {
+ return visitLinks('series', 'test', 'Hello', 'Hallo');
+ });
+})
+
+describe('i18n.__() is NOT able to handle concurrent request correctly', function(){
+ describe('serial requests return different languages', function () {
+ return visitLinks('series', 'testfail', 'Hello', 'Hello');
+ });
+
+ describe('parallel requests return different languages', function () {
+ return visitLinks('series', 'testfail', 'Hello', 'Hello');
+ });
+})
View
@@ -18,12 +18,15 @@
},
"devDependencies": {
"mocha": ">=1.8.1",
- "should": ">=1.2.1"
+ "should": ">=1.2.1",
+ "async": "*",
+ "zombie": "*",
+ "express": "*"
},
"engines": {
"node": ">=0.4.0"
},
"scripts": {
- "test": "mocha -R spec"
+ "test": "make all"
}
}

0 comments on commit e11043b

Please sign in to comment.