From 7037fd2fa7ee3ee27a7d2bfcd8cb7068db629cc8 Mon Sep 17 00:00:00 2001 From: Sefa Ilkimen Date: Fri, 13 Oct 2017 16:30:48 +0200 Subject: [PATCH] - improved test app - defined new tests --- test/app-template/www/index.html | 3 +- test/app-template/www/js/index.js | 40 ++++++++++++++++++------ test/mocha-specs/test.js | 12 ++++--- test/test-definitions.js | 52 +++++++++++++++++++++++++++---- 4 files changed, 85 insertions(+), 22 deletions(-) diff --git a/test/app-template/www/index.html b/test/app-template/www/index.html index 4c33ca9..ff105fe 100644 --- a/test/app-template/www/index.html +++ b/test/app-template/www/index.html @@ -9,7 +9,8 @@

Advanced HTTP test suite

- + + diff --git a/test/app-template/www/js/index.js b/test/app-template/www/js/index.js index 9c8d0df..f001589 100644 --- a/test/app-template/www/js/index.js +++ b/test/app-template/www/js/index.js @@ -5,35 +5,53 @@ const app = { document.getElementById('nextBtn').addEventListener('click', app.onNextBtnClick); }, - print: function(prefix, content) { - const text = '\n' + prefix + ': ' + JSON.stringify(content); + printResult: function(prefix, content) { + const text = prefix + ': ' + JSON.stringify(content); document.getElementById('resultTextarea').value += text; }, reject: function(content) { - app.print('result - rejected', content); + app.printResult('result - rejected', content); }, resolve: function(content) { - app.print('result - resolved', content); + app.printResult('result - resolved', content); }, runTest: function(index) { const testDefinition = tests[index]; const titleText = app.testIndex + ': ' + testDefinition.description; - const resultText = 'expectation - ' + testDefinition.expected; + const expectedText = 'expected - ' + testDefinition.expected; - document.getElementById('resultTextarea').value = resultText; + document.getElementById('expectedTextarea').value = expectedText; + document.getElementById('resultTextarea').value = ''; document.getElementById('descriptionLbl').innerText = titleText; - testDefinition.func(index); + testDefinition.func(app.resolve, app.reject); + }, + + onBeforeTest: function(testIndex, cb) { + if (hooks && hooks.onBeforeEachTest) { + return hooks.onBeforeEachTest(function() { + const testDefinition = tests[testIndex]; + + if (testDefinition.before) { + testDefinition.before(cb); + } else { + cb(); + } + }); + } else { + cb(); + } }, onFinishedAllTests: function() { const titleText = 'No more tests'; - const resultText = 'You have run all available tests.'; + const expectedText = 'You have run all available tests.'; - document.getElementById('resultTextarea').value = resultText; + document.getElementById('expectedTextarea').value = expectedText; + document.getElementById('resultTextarea').value = ''; document.getElementById('descriptionLbl').innerText = titleText; }, @@ -41,7 +59,9 @@ const app = { app.testIndex += 1; if (app.testIndex < tests.length) { - app.runTest(app.testIndex); + app.onBeforeTest(app.testIndex, function() { + app.runTest(app.testIndex); + }); } else { app.onFinishedAllTests(); } diff --git a/test/mocha-specs/test.js b/test/mocha-specs/test.js index 36dbf31..1c291bc 100644 --- a/test/mocha-specs/test.js +++ b/test/mocha-specs/test.js @@ -5,6 +5,7 @@ const apps = require('./helpers/apps'); const caps = Object.assign({}, require('./helpers/caps')); const serverConfig = require('./helpers/server'); const testDefinitions = require('../test-definitions'); +const pkgjson = require('../../package.json'); describe('Advanced HTTP', function() { let driver; @@ -18,6 +19,7 @@ describe('Advanced HTTP', function() { const desiredCaps = caps[(isAndroid ? 'android' : 'ios') + (isDevice ? 'Device' : 'Emulator')]; const desiredApp = apps[(isAndroid ? 'android' : 'ios') + appName]; + desiredCaps.name = pkgjson.name; desiredCaps.app = desiredApp; return desiredCaps; @@ -26,14 +28,14 @@ describe('Advanced HTTP', function() { const validateTestIndex = number => driver .elementById('descriptionLbl') .text() - .then(text => parseInt(text.match(/(\d):/)[1], 10)) - .should.eventually.become(number); + .then(text => parseInt(text.match(/(\d+):/)[1], 10)) + .should.eventually.become(number, 'Test index is not matching!'); const validateTestTitle = testTitle => driver .elementById('descriptionLbl') .text() - .then(text => text.match(/\d:\ (.*)/)[1]) - .should.eventually.become(testTitle); + .then(text => text.match(/\d+:\ (.*)/)[1]) + .should.eventually.become(testTitle, 'Test description is not matching!'); const validateResult = text => driver .elementById('resultTextarea') @@ -60,7 +62,7 @@ describe('Advanced HTTP', function() { } })); - testDefinitions.forEach((definition, index) => { + testDefinitions.tests.forEach((definition, index) => { it(definition.description, function() { return clickNext() .then(() => validateTestIndex(index)) diff --git a/test/test-definitions.js b/test/test-definitions.js index 7ec72b1..f52259e 100644 --- a/test/test-definitions.js +++ b/test/test-definitions.js @@ -1,27 +1,67 @@ +const hooks = { + onBeforeEachTest: function(done) { + cordova.plugin.http.acceptAllCerts(false, done, done); + } +}; + +const helpers = { + acceptAllCerts: function(done) { cordova.plugin.http.acceptAllCerts(true, done, done); } +}; + const tests = [ { description: 'should reject self signed cert (GET)', expected: 'rejected: {"status":-1,"error":"cancelled"}', - func: function() { cordova.plugin.http.get('https://self-signed.badssl.com/', {}, {}, app.resolve, app.reject); } + func: function(resolve, reject) { cordova.plugin.http.get('https://self-signed.badssl.com/', {}, {}, resolve, reject); } },{ description: 'should reject self signed cert (PUT)', expected: 'rejected: {"status":-1,"error":"cancelled"}', - func: function() { cordova.plugin.http.put('https://self-signed.badssl.com/', { test: 'testString' }, {}, app.resolve, app.reject); } + func: function(resolve, reject) { cordova.plugin.http.put('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); } },{ description: 'should reject self signed cert (POST)', expected: 'rejected: {"status":-1,"error":"cancelled"}', - func: function() { cordova.plugin.http.post('https://self-signed.badssl.com/', { test: 'testString' }, {}, app.resolve, app.reject); } + func: function(resolve, reject) { cordova.plugin.http.post('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); } },{ description: 'should reject self signed cert (PATCH)', expected: 'rejected: {"status":-1,"error":"cancelled"}', - func: function() { cordova.plugin.http.patch('https://self-signed.badssl.com/', { test: 'testString' }, {}, app.resolve, app.reject); } + func: function(resolve, reject) { cordova.plugin.http.patch('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); } },{ description: 'should reject self signed cert (DELETE)', expected: 'rejected: {"status":-1,"error":"cancelled"}', - func: function() { cordova.plugin.http.delete('https://self-signed.badssl.com/', {}, {}, app.resolve, app.reject); } + func: function(resolve, reject) { cordova.plugin.http.delete('https://self-signed.badssl.com/', {}, {}, resolve, reject); } + },{ + description: 'should accept bad cert (GET)', + expected: 'resolved: {\"status\":200,', + before: helpers.acceptAllCerts, + func: function(resolve, reject) { cordova.plugin.http.get('https://self-signed.badssl.com/', {}, {}, resolve, reject); } + },{ + description: 'should accept bad cert (PUT)', + expected: 'rejected: {\"status\":405,', // will be rejected because PUT is not allowed + before: helpers.acceptAllCerts, + func: function(resolve, reject) { cordova.plugin.http.put('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); } + },{ + description: 'should accept bad cert (POST)', + expected: 'rejected: {\"status\":405,', // will be rejected because POST is not allowed + before: helpers.acceptAllCerts, + func: function(resolve, reject) { cordova.plugin.http.post('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); } + },{ + description: 'should accept bad cert (PATCH)', + expected: 'rejected: {\"status\":405,', // will be rejected because PATCH is not allowed + before: helpers.acceptAllCerts, + func: function(resolve, reject) { cordova.plugin.http.patch('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); } + },{ + description: 'should accept bad cert (DELETE)', + expected: 'rejected: {\"status\":405,', // will be rejected because DELETE is not allowed + before: helpers.acceptAllCerts, + func: function(resolve, reject) { cordova.plugin.http.delete('https://self-signed.badssl.com/', {}, {}, resolve, reject); } + },{ + description: 'should fetch data from http://google.com/ (GET)', + expected: 'resolved: {\"status\":200,', + before: helpers.acceptAllCerts, + func: function(resolve, reject) { cordova.plugin.http.get('http://google.com/', {}, {}, resolve, reject); } } ]; if (module && module.exports) { - module.exports = tests; + module.exports = { tests: tests, hooks: hooks }; }