diff options
author | Stas Vilchik <vilchiks@gmail.com> | 2015-06-17 10:38:21 +0200 |
---|---|---|
committer | Stas Vilchik <vilchiks@gmail.com> | 2015-06-17 12:15:09 +0200 |
commit | 8ed69055f07420f46393c536d1b2671f0f135dd1 (patch) | |
tree | 4419ed24847f8db6bd3f25428aa42e63fa3c2758 /server/sonar-web/src/test | |
parent | ab60a7acc96eb219991bea1427b672f5785d46e7 (diff) | |
download | sonarqube-8ed69055f07420f46393c536d1b2671f0f135dd1.tar.gz sonarqube-8ed69055f07420f46393c536d1b2671f0f135dd1.zip |
refactor server for web tests
Diffstat (limited to 'server/sonar-web/src/test')
-rw-r--r-- | server/sonar-web/src/test/server-coverage.js | 93 | ||||
-rw-r--r-- | server/sonar-web/src/test/server-lib/api.js | 26 | ||||
-rw-r--r-- | server/sonar-web/src/test/server-lib/assets.js | 13 | ||||
-rw-r--r-- | server/sonar-web/src/test/server-lib/coverage.js | 27 | ||||
-rw-r--r-- | server/sonar-web/src/test/server-lib/mocks.js | 50 | ||||
-rw-r--r-- | server/sonar-web/src/test/server-lib/pages.js | 16 | ||||
-rw-r--r-- | server/sonar-web/src/test/server.js | 71 |
7 files changed, 159 insertions, 137 deletions
diff --git a/server/sonar-web/src/test/server-coverage.js b/server/sonar-web/src/test/server-coverage.js index 3fd4400cea3..e7b68fd9f0a 100644 --- a/server/sonar-web/src/test/server-coverage.js +++ b/server/sonar-web/src/test/server-coverage.js @@ -1,85 +1,22 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -var express = require('express'), - path = require('path'), +/* jshint node: true */ +var express = require('express'); - im = require('istanbul-middleware'), - url = require('url'), - JS_RE = /\.js$/, - THIRD_PARTY_RE = /\/third-party\//, - TEMPLATES_RE = /\/templates.js/; +var api = require('./server-lib/api'), + assets = require('./server-lib/assets'), + coverage = require('./server-lib/coverage'), + mocks = require('./server-lib/mocks'), + pages = require('./server-lib/pages'); -var app = express(); +var app = express(), + port = process.env.PORT || 3000; - -// Views -app.set('views', __dirname + '/views'); -app.set('view engine', 'jade'); - - -// Coverage -var staticPath = path.join(__dirname, '../../build'); -im.hookLoader(staticPath); -app.use(im.createClientHandler(staticPath, { - matcher: function (req) { - var parsed = url.parse(req.url); - return parsed.pathname && parsed.pathname.match(JS_RE) && - !parsed.pathname.match(THIRD_PARTY_RE) && - !parsed.pathname.match(TEMPLATES_RE); - } -})); -app.use('/coverage', im.createHandler()); - - -// Static -app.use('/js', express.static(path.join(staticPath, 'js'))); -app.use('/css', express.static(path.join(staticPath, 'css'))); - - -// API -app.get('/api/l10n/index', function (req, res) { - res.setHeader('Content-Type', 'application/json'); - res.end('{}'); -}); -app.get('/api/generic/long', function (req, res) { - setTimeout(function() { - res.setHeader('Content-Type', 'application/json'); - res.end('{}'); - }, 2000); -}); -app.get('/api/generic/failed', function (req, res) { - res.setHeader('Content-Type', 'application/json'); - res.status('400'); - res.end('{"errors":[{"msg":"Error Message"}]}'); -}); - - -// Pages -app.get('/pages/:page', function (req, res) { - res.render(req.param('page')); -}); - - -// Get the port from environment variables -var port = process.env.PORT || 8000; +app.use(api()); +app.use(coverage()); +app.use(assets()); +app.use(pages()); +app.use(mocks()); app.listen(port); console.log('Server running on port %d', port); + diff --git a/server/sonar-web/src/test/server-lib/api.js b/server/sonar-web/src/test/server-lib/api.js new file mode 100644 index 00000000000..8b861e66820 --- /dev/null +++ b/server/sonar-web/src/test/server-lib/api.js @@ -0,0 +1,26 @@ +/* jshint node: true */ +var express = require('express'); + +module.exports = function () { + var app = express(); + + app.get('/api/l10n/index', function (req, res) { + res.setHeader('Content-Type', 'application/json'); + res.end('{}'); + }); + + app.get('/api/generic/long', function (req, res) { + setTimeout(function () { + res.setHeader('Content-Type', 'application/json'); + res.end('{}'); + }, 2000); + }); + + app.get('/api/generic/failed', function (req, res) { + res.setHeader('Content-Type', 'application/json'); + res.status('400'); + res.end('{"errors":[{"msg":"Error Message"}]}'); + }); + + return app; +}; diff --git a/server/sonar-web/src/test/server-lib/assets.js b/server/sonar-web/src/test/server-lib/assets.js new file mode 100644 index 00000000000..4864da2076a --- /dev/null +++ b/server/sonar-web/src/test/server-lib/assets.js @@ -0,0 +1,13 @@ +/* jshint node: true */ +var express = require('express'), + path = require('path'); + +module.exports = function () { + var app = express(), + staticPath = path.join(__dirname, '../../../build'); + + app.use('/js', express.static(path.join(staticPath, 'js'))); + app.use('/css', express.static(path.join(staticPath, 'css'))); + + return app; +}; diff --git a/server/sonar-web/src/test/server-lib/coverage.js b/server/sonar-web/src/test/server-lib/coverage.js new file mode 100644 index 00000000000..615fb4444b4 --- /dev/null +++ b/server/sonar-web/src/test/server-lib/coverage.js @@ -0,0 +1,27 @@ +/* jshint node: true */ +var express = require('express'), + path = require('path'), + im = require('istanbul-middleware'), + url = require('url'), + JS_RE = /\.js$/, + THIRD_PARTY_RE = /\/third-party\//, + TEMPLATES_RE = /\/templates.js/; + +module.exports = function () { + var app = express(); + + var staticPath = path.join(__dirname, '../../../build'); + im.hookLoader(staticPath); + app.use(im.createClientHandler(staticPath, { + matcher: function (req) { + var parsed = url.parse(req.url); + return parsed.pathname && + parsed.pathname.match(JS_RE) && + !parsed.pathname.match(THIRD_PARTY_RE) && + !parsed.pathname.match(TEMPLATES_RE); + } + })); + app.use('/coverage', im.createHandler()); + + return app; +}; diff --git a/server/sonar-web/src/test/server-lib/mocks.js b/server/sonar-web/src/test/server-lib/mocks.js new file mode 100644 index 00000000000..41e578cf99d --- /dev/null +++ b/server/sonar-web/src/test/server-lib/mocks.js @@ -0,0 +1,50 @@ +/* jshint node: true */ +var express = require('express'), + bodyParser = require('body-parser'); + +module.exports = function () { + var app = express(), + mocks = []; + + app.use(bodyParser.json({ limit: '100mb' })); + + app.post('/mock', function (req, res) { + var url = req.body.url; + + mocks = mocks.filter(function (mock) { + return mock.url !== url; + }); + mocks.push(req.body); + + res.status('204'); + res.end('{}'); + }); + + app.post('/unmock', function (req, res) { + var url = req.body.url; + + mocks = mocks.filter(function (mock) { + return mock.url !== url; + }); + + res.status('204'); + res.end('{}'); + }); + + app.get('/api/*', function (req, res) { + var mock; + mocks.forEach(function (m) { + mock = m.url === req.url && m; + }); + if (mock) { + res.status('200'); + res.setHeader('Content-Type', 'application/json'); + res.end(mock.response); + } else { + res.status('404'); + res.end(); + } + }); + + return app; +}; diff --git a/server/sonar-web/src/test/server-lib/pages.js b/server/sonar-web/src/test/server-lib/pages.js new file mode 100644 index 00000000000..fe2e2110bee --- /dev/null +++ b/server/sonar-web/src/test/server-lib/pages.js @@ -0,0 +1,16 @@ +/* jshint node: true */ +var express = require('express'), + path = require('path'); + +module.exports = function () { + var app = express(); + + app.set('views', path.join(__dirname, '../views')); + app.set('view engine', 'jade'); + + app.get('/pages/:page', function (req, res) { + res.render(req.param('page')); + }); + + return app; +}; diff --git a/server/sonar-web/src/test/server.js b/server/sonar-web/src/test/server.js index 01f952d0dd0..18c9b804c4d 100644 --- a/server/sonar-web/src/test/server.js +++ b/server/sonar-web/src/test/server.js @@ -1,65 +1,18 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -var express = require('express'), - path = require('path'); +/* jshint node: true */ +var express = require('express'); -var app = express(); +var api = require('./server-lib/api'), + assets = require('./server-lib/assets'), + mocks = require('./server-lib/mocks'), + pages = require('./server-lib/pages'); +var app = express(), + port = process.env.PORT || 3000; -// Views -app.set('views', __dirname + '/views'); -app.set('view engine', 'jade'); - - -// Static -var staticPath = path.join(__dirname, '../../build'); -app.use('/js', express.static(path.join(staticPath, 'js'))); -app.use('/css', express.static(path.join(staticPath, 'css'))); - - -// API -app.get('/api/l10n/index', function (req, res) { - res.setHeader('Content-Type', 'application/json'); - res.end('{}'); -}); -app.get('/api/generic/long', function (req, res) { - setTimeout(function() { - res.setHeader('Content-Type', 'application/json'); - res.end('{}'); - }, 2000); -}); -app.get('/api/generic/failed', function (req, res) { - res.setHeader('Content-Type', 'application/json'); - res.status('400'); - res.end('{"errors":[{"msg":"Error Message"}]}'); -}); - - -// Pages -app.get('/pages/:page', function (req, res) { - res.render(req.param('page')); -}); - - -// Get the port from environment variables -var port = process.env.PORT || 8000; +app.use(api()); +app.use(assets()); +app.use(pages()); +app.use(mocks()); app.listen(port); |