diff --git a/package.json b/package.json index 0156ece..ca1ba25 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "node-sass": "^3.3.3", "portfinder": "^0.4.0", "protractor": "git+https://github.com/jGleitz/protractor.git#browserstack", + "protractor-http-mock": "^0.1.18", "q": "^1.4.1", "sass-lint": "^1.3.2", "serve-static": "^1.10.0", diff --git a/test/behaviour/apimocker.js b/test/behaviour/apimocker.js deleted file mode 100644 index 6077ce5..0000000 --- a/test/behaviour/apimocker.js +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Simple mocker to mock the REST API - */ - -var path = require('path'); -var mocks = {}; -var extend = require('extend'); - -module.exports = { - - /** - * A middleware for connect. - */ - middleware: function(req, res, next) { - var uri = path.normalize(req.url); - var verb = req.method; - if (mocks[uri] && mocks[uri][verb]) { - var data = mocks[uri][verb]; - res.end(data.content); - res.statusCode = data.status; - if (!mocks[uri][verb].persist) { - delete mocks[uri][verb]; - } - } else { - next(); - } - }, - - /** - * Mocks the response for a request to the API at the specified endpoint. Unless options.persist is set to true, the mocked value will only be responded once. - * - * @param {string} endpoint The endpoint to mock. - * @param {object|string} object The object to return at the endpoint. If a string is passed, it will be responded unchanged, anything else will be given to JSON#stringify. - * @param {object} options Options. Possible values and defaults: - * { - * verb: 'GET', // The http verb that is mocked and this endpoint. - * status: 200, // The response's http status - * persist: false // If true, this mock is persistent. - * } - * @return {[type]} [description] - */ - mockAPI: function(endpoint, object, options) { - var uri = path.normalize(path.join('/api', endpoint)); - var defaultOptions = { - verb: 'GET', - status: 200, - persist: false - }; - mocks[uri] = mocks[uri] || {}; - var data = extend({}, defaultOptions, options, { - content: typeof object === 'string' ? object : JSON.stringify(object) - }); - mocks[uri][data.verb] = data; - } -}; \ No newline at end of file diff --git a/test/behaviour/example.js b/test/behaviour/example.js index 3dbcedf..f5fc25a 100644 --- a/test/behaviour/example.js +++ b/test/behaviour/example.js @@ -5,7 +5,7 @@ require('./asserters')(); -var server = require('./apimocker'); +var mock = require('./http-mock'); describe('Home', function() { this.timeout(1 * 60 * 1000); @@ -23,25 +23,13 @@ describe('List', function() { var locationlist = by.repeater('location in ctrl.locations'); it('should have two entries: "Sandsäcke füllen" and "Transport"', function() { + mock(['locations']); + browser.getPart('list'); - expect(element.all(locationlist).count()).to.eventually.equal(2); + expect(element.all(locationlist).count()).to.eventually.equal(3); expect(element(locationlist.column('location.name').row(0)).getInnerHtml()).to.eventually.contain('Sandsäcke füllen'); expect(element(locationlist.column('location.name').row(1)).getInnerHtml()).to.eventually.contain('Transport'); - }); - - // Example for mocking - it('should load another entry', function() { - server.mockAPI('locations', [{ - id: 1, - name: 'Mocked Location', - latitude: 49.1232, - longitude: 8, - location: 'Schlossplatz' - }]); - - browser.navigate().refresh(); - - expect(element(locationlist.column('location.name').row(0)).getInnerHtml()).to.eventually.contain('Mocked Location'); + expect(element(locationlist.column('location.name').row(2)).getInnerHtml()).to.eventually.contain('Test 3'); }); }); \ No newline at end of file diff --git a/test/behaviour/http-mock.conf.js b/test/behaviour/http-mock.conf.js new file mode 100644 index 0000000..37e4cff --- /dev/null +++ b/test/behaviour/http-mock.conf.js @@ -0,0 +1,6 @@ +exports.config = { + mocks: { + dir: 'mocks', + default: ['default'] + } +}; \ No newline at end of file diff --git a/test/behaviour/http-mock.js b/test/behaviour/http-mock.js new file mode 100644 index 0000000..81d0c35 --- /dev/null +++ b/test/behaviour/http-mock.js @@ -0,0 +1,8 @@ + +var mock = require('protractor-http-mock'); +mock.config = { + rootDirectory: __dirname, // default value: process.cwd() + protractorConfig: 'http-mock.conf.js' +}; + +module.exports = mock; \ No newline at end of file diff --git a/test/behaviour/mocks/default.js b/test/behaviour/mocks/default.js new file mode 100644 index 0000000..190e2f6 --- /dev/null +++ b/test/behaviour/mocks/default.js @@ -0,0 +1,11 @@ +module.exports = { + request: { + path: '/default', + method: 'GET' + }, + response: { + data: { + name: "i'm always loaded" + } + } +}; \ No newline at end of file diff --git a/test/behaviour/mocks/locations.js b/test/behaviour/mocks/locations.js new file mode 100644 index 0000000..a32ea94 --- /dev/null +++ b/test/behaviour/mocks/locations.js @@ -0,0 +1,33 @@ +module.exports = { + request: { + path: '/locations', + method: 'GET' + }, + response: { + data: [{ + id: 1, + name: 'Sandsäcke füllen', + latitude: 49.1232, + longitude: 8, + location: 'Schlossplatz' + }, { + id: 2, + name: 'Transport', + latitude: 48.1232, + longitude: 8, + location: 'Rheinbad', + options: { + title: 'Transport im Rheinbad' + } + }, { + id: 2, + name: 'Test 3', + latitude: 48.1232, + longitude: 8, + location: 'Rheinbad', + options: { + title: 'Transport im Rheinbad' + } + }] + } +}; \ No newline at end of file diff --git a/test/behaviour/setup.js b/test/behaviour/setup.js index 152bc52..6a7bf69 100644 --- a/test/behaviour/setup.js +++ b/test/behaviour/setup.js @@ -2,7 +2,6 @@ * Sets up the test environment */ -var mocker = require('./apimocker'); var portfinder = require('portfinder'); var Server = require('../../tasks/server'); @@ -30,8 +29,7 @@ before('Expose globals', function() { }; }); -before('Set up the JSON server and mocker', function() { +before('Set up the HTTP server', function() { var server = new Server(); - server.use(mocker.middleware); server.listen(port); }); \ No newline at end of file