mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-06-19 00:00:08 +08:00
- improved test app
- defined new tests
This commit is contained in:
@@ -9,7 +9,8 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1 id="descriptionLbl">Advanced HTTP test suite</h1>
|
||||
<textarea rows="20" cols="50" id="resultTextarea">Click next to run first test.</textarea>
|
||||
<textarea rows="16" cols="50" id="expectedTextarea">Click next to run first test.</textarea>
|
||||
<textarea rows="16" cols="50" id="resultTextarea"></textarea>
|
||||
<button id="nextBtn">Run next test</button>
|
||||
|
||||
<script type="text/javascript" src="cordova.js"></script>
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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 };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user