Skip to content

Instantly share code, notes, and snippets.

@sebadoom
Created December 11, 2014 20:31
Show Gist options
  • Save sebadoom/559ead14dd453c9c87f1 to your computer and use it in GitHub Desktop.
Save sebadoom/559ead14dd453c9c87f1 to your computer and use it in GitHub Desktop.
var should = require('should'),
userAgentFilter = require('../../lib/userAgentFilter.js');
describe("User Agent Filter logic", function(){
it("should let chrome browser pass", function(done){
var reqMock = {headers: {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1'}, url:'/session'};
var resMock = {};
userAgentFilter(reqMock, resMock, function(){
true.should.equal(true);
done();
});
});
it("should let firefox browser pass", function(done){
var reqMock = {headers: {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20120716 Firefox/15.0a2'}, url:'/session'};
var resMock = {};
userAgentFilter(reqMock, resMock, function(){
true.should.equal(true);
done();
});
});
it("should let safari browser pass", function(done){
var reqMock = {headers: {'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10'}, url:'/session'};
var resMock = {};
userAgentFilter(reqMock, resMock, function(){
true.should.equal(true);
done();
});
});
it("should let Epiphany (WebKit-based) browser pass", function(done){
var reqMock = {headers: {'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/601.1 (KHTML, like Gecko) Version/8.0 Safari/601.1 Epiphany/3.14.2'}, url:'/session'};
var resMock = {};
userAgentFilter(reqMock, resMock, function(){
true.should.equal(true);
done();
});
});
it("should send unsupported browser to device not supported", function(){
var reqMock = {headers: {'user-agent': 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US)'}, url:'/', user:{}};
var resMock = { redirect: function(path) { this.name = path; }};
userAgentFilter(reqMock, resMock);
resMock.name.indexOf("/browser-not-supported").should.be.greaterThan(-1);
});
it("enable blitz on user agents", function(done){
var reqMock = {headers: {'user-agent': 'blitz.io; [email protected]'}, url:'/session'};
var resMock = {sendfile: function(path) { this.renderedPath = path; }};
userAgentFilter(reqMock, resMock, function(){
true.should.equal(true);
done();
});
});
it("should enable chromium user agents", function(done){
var reqMock = {headers: {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.30 (KHTML, like Gecko) Ubuntu/11.04 Chromium/12.0.742.112 Chrome/12.0.742.112 Safari/534.30'}, url:'/session'};
var resMock = {sendfile: function(path) { this.renderedPath = path; }};
userAgentFilter(reqMock, resMock, function(){
true.should.equal(true);
done();
});
});
it("should work with iPhone App", function(done){
var reqMock = {headers: {'user-agent': '"Chrome/21.0.1180.89"'}, url:'/session'};
var resMock = {sendfile: function(path) { this.renderedPath = path; }};
userAgentFilter(reqMock, resMock, function(){
true.should.equal(true);
done();
});
});
it("should let iphone safari browser pass on legal pages", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/terms/privacy-policy'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let iphone safari browser pass on subscribe pages", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/subscribe/'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let iphone safari browser pass on subscribe pages", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/subscribe/owner/mural'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let iphone safari browser pass on subscribe pages", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/unsubscribe/'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let iphone safari browser pass on subscribe pages", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/unsubscribe/owner/mural'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let iphone safari browser pass on snooze pages", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/subscriptions/:owner/:mural/unsnooze'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let iphone safari browser pass on snooze pages", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/subscriptions/:owner/:mural/snooze'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let home page pass when not authenticated", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let home page pass when not authenticated", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let bye page pass", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/bye'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let about page pass", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/about'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let about page pass", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/pricing'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let help page pass", function(done){
var userAgent = 'Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3_3 like Mac OS X; ja-jp) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/help/kb/how-to-allow-murally-to-access-your-photos-in-ios'};
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let IE 11 pass", function(done){
var userAgent = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; Touch; MAARJS; rv:11.0) like Gecko';
var reqMock = { headers: { 'user-agent': userAgent}, url: '/session' };
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let API methods pass (no matter ua)", function(done){
var userAgent = 'python/mailgun';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/api/comments/webhook' };
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
it("should let API methods pass (no matter ua)", function(done){
var userAgent = 'python/mailgun';
var reqMock = { headers: { 'user-agent': userAgent }, url: '/api/rooms/count' };
userAgentFilter(reqMock, {}, function(){
true.should.equal(true);
done();
});
});
});
var passportLogoutHandler = require('./passportLogoutHandler.js'),
agent = require('useragent'),
path = require('path');
module.exports = function userAgentFilter(req, res, next) {
var userAgent = req.headers["user-agent"];
if (req.url === '/ping') return next(); // when user agent is empty let it pass
if (!userAgent) return next(); // when user agent is empty let it pass
if (req.url.match(/^\/api\/?/g)) return next();
if((req.url.match(/^\/static\/?/g)) || (req.url.match(/\.(css)/g)) || (req.url.match(/\.(js)/g))) return next(); // for the crawler
if (userAgent.indexOf('QtWeb') > -1) return next();
if (userAgent.indexOf('vComm') > -1 || userAgent.indexOf('Aula') > -1) return next();
if (userAgent.indexOf("CFNetwork") > -1) return next();
if (userAgent == "Python-urllib/2.7") return next();
if (userAgent == "Ruby") return next();
if (userAgent.indexOf('curl/') === 0) return next();
// IE11 has a sligthly different UA string
if (userAgent.indexOf('Trident/7.0;') >= 0) return next();
var ua_obj = agent.parse(userAgent);
var supportBrowser = ua_obj.family == "Chrome" ||
ua_obj.family == "Chromium" ||
ua_obj.family == "Firefox" ||
ua_obj.family == "Firefox Beta" ||
ua_obj.family == "Safari" ||
(ua_obj.family == "Epiphany" && parseInt(ua_obj.major, 10) >= 3) ||
(ua_obj.family == "IE" && parseInt(ua_obj.major, 10) >= 10);
var blitz = userAgent.indexOf("blitz.io; 66e3fd2dc17b16e6d9c86be7ed2bcbe4") >= 0;
req.isIPhone = (ua_obj.family !== 'iPad' && ua_obj.os == "iOS");
req.isIPad = (ua_obj.family === 'iPad' && ua_obj.os == "iOS");
req.isSupported = (!userAgent || supportBrowser || blitz) && !req.isIPhone && !req.isIPad;
req.isAndroid = userAgent.indexOf('Android') > -1 && userAgent.indexOf('Mobile') > -1;
req.isIE = ua_obj.family == "IE";
req.isMobile = req.isIPhone || req.isAndroid;
if (req.isSupported || req.url === '/browser-not-supported') {
return next();
}
if (!req.isIPhone && !req.isIPad) {
return res.redirect("/browser-not-supported");
}
if (req.user && req.url === '/' && req.isIPad) {
return res.redirect("/get-ipad");
}
if (req.url === '/!' && req.isIPad) {
return res.redirect("/get-ipad");
}
if (req.user && req.url === '/' && req.isIPhone) {
return res.redirect("/get-iphone");
}
if (req.url === '/!' && req.isIPhone) {
return res.redirect("/get-iphone");
}
return next();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment