Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added express4-cookie example & test
- Loading branch information
Showing
7 changed files
with
100 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var express = require('express'); | ||
var cookieParser = require('cookie-parser'); | ||
var url = require('url'); | ||
var i18n = require('../../i18n'); | ||
|
||
i18n.configure({ | ||
locales: ['en', 'de', 'ar'], | ||
cookie: 'yourcookiename', | ||
directory: __dirname+'/locales' | ||
}); | ||
|
||
var app = express(); | ||
app.use(cookieParser()); | ||
app.use(i18n.init); | ||
|
||
app.get('/test', function (req, res) { | ||
// delay a response to simulate a long running process, | ||
// while another request comes in with altered language settings | ||
setTimeout(function () { | ||
res.send('<body>res: ' + res.__('Hello') + ' req: ' + req.__('Hello') + '</body>'); | ||
}, app.getDelay(req, res)); | ||
}); | ||
|
||
app.get('/testfail', function (req, res) { | ||
// delay a response to simulate a long running process, | ||
// while another request comes in with altered language settings | ||
setTimeout(function () { | ||
res.send('<body>' + i18n.__('Hello') + '</body>'); | ||
}, app.getDelay(req, res)); | ||
}); | ||
|
||
// simple param parsing | ||
app.getDelay = function (req, res) { | ||
return url.parse(req.url, true).query.delay || 0; | ||
}; | ||
|
||
// startup | ||
app.listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"Hello": "مرحبا" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"Hello World": "Hallo Welt", | ||
"Hello": "Hallo" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"Hello World": "Hello World", | ||
"Hello": "Hello" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require('./index'); | ||
|
||
var Browser = require('zombie'), | ||
visitLinks = require('../testlib/visitlinks'), | ||
DE = new Browser(), | ||
EN = new Browser(); | ||
AR = new Browser(); | ||
|
||
EN.setCookie({name: 'yourcookiename', domain: 'localhost', value: 'en'}); | ||
DE.setCookie({name: 'yourcookiename', domain: 'localhost', value: 'de'}); | ||
AR.setCookie({name: 'yourcookiename', domain: 'localhost', value: 'ar'}); | ||
|
||
describe('Using i18n in express 4.x with cookieParser', function () { | ||
describe('res.__() is able to handle concurrent request correctly', function () { | ||
describe('serial requests', function () { | ||
visitLinks('series', 'test', EN, 'res: Hello req: Hello', DE, 'res: Hallo req: Hallo'); | ||
}); | ||
|
||
describe('parallel requests', function () { | ||
visitLinks('parallel', 'test', EN, 'res: Hello req: Hello', DE, 'res: Hallo req: Hallo'); | ||
}); | ||
|
||
describe('serial requests AR', function () { | ||
visitLinks('series', 'test', EN, 'res: Hello req: Hello', AR, 'res: مرحبا req: مرحبا'); | ||
}); | ||
|
||
describe('parallel requests AR', function () { | ||
visitLinks('parallel', 'test', EN, 'res: Hello req: Hello', AR, 'res: مرحبا req: مرحبا'); | ||
}); | ||
|
||
}); | ||
|
||
describe('i18n.__() is NOT able to handle concurrent request correctly', function () { | ||
describe('serial requests', function () { | ||
visitLinks('series', 'testfail', EN, 'Hello', DE, 'Hello'); | ||
}); | ||
|
||
describe('parallel requests', function () { | ||
visitLinks('parallel', 'testfail', EN, 'Hello', DE, 'Hello'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters