From d444363a8eb4aa08caf4dca92ea857d1343f2208 Mon Sep 17 00:00:00 2001 From: Sefa Ilkimen Date: Tue, 9 Apr 2019 17:25:55 +0200 Subject: [PATCH] - fix #198: Cookie header is always passed even if there is no cookie - some cleanup --- .gitignore | 2 +- CHANGELOG.md | 1 + package.json | 2 +- scripts/build-test-app.sh | 5 ++-- scripts/setup-android-sdk.sh | 19 ------------- scripts/test-app.sh | 3 +- scripts/update-test-cert.js | 2 +- .../build.json | 0 .../config.xml | 0 .../package.json | 0 .../www/certificates/.gitkeep | 0 .../www/index.css | 0 .../www/index.html | 2 +- .../www/index.js | 0 .../{app-test-definitions.js => e2e-specs.js} | 3 +- .../helpers/apps.js | 0 .../helpers/caps.js | 0 .../helpers/logging.js | 0 .../helpers/server.js | 0 .../helpers/setup.js | 0 test/{app-mocha-specs => e2e-tooling}/test.js | 2 +- test/{js-mocha-specs.js => js-specs.js} | 27 ++++++++++++++++++ www/helpers.js | 28 +++++++++++++++++-- 23 files changed, 65 insertions(+), 31 deletions(-) delete mode 100755 scripts/setup-android-sdk.sh rename test/{app-template => e2e-app-template}/build.json (100%) rename test/{app-template => e2e-app-template}/config.xml (100%) rename test/{app-template => e2e-app-template}/package.json (100%) rename test/{app-template => e2e-app-template}/www/certificates/.gitkeep (100%) rename test/{app-template => e2e-app-template}/www/index.css (100%) rename test/{app-template => e2e-app-template}/www/index.html (93%) rename test/{app-template => e2e-app-template}/www/index.js (100%) rename test/{app-test-definitions.js => e2e-specs.js} (99%) rename test/{app-mocha-specs => e2e-tooling}/helpers/apps.js (100%) rename test/{app-mocha-specs => e2e-tooling}/helpers/caps.js (100%) rename test/{app-mocha-specs => e2e-tooling}/helpers/logging.js (100%) rename test/{app-mocha-specs => e2e-tooling}/helpers/server.js (100%) rename test/{app-mocha-specs => e2e-tooling}/helpers/setup.js (100%) rename test/{app-mocha-specs => e2e-tooling}/test.js (97%) rename test/{js-mocha-specs.js => js-specs.js} (89%) diff --git a/.gitignore b/.gitignore index 67daac4..4c4a9fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ node_modules/** -test/app-template/www/certificates/*.cer +test/e2e-app-template/www/certificates/*.cer tags .zedstate npm-debug.log diff --git a/CHANGELOG.md b/CHANGELOG.md index c1ce97c..9dcf7c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 2.0.8 +- Fixed #198: cookie header is always passed even if there is no cookie - Fixed #201: browser implementation is broken due to broken dependency - Fixed #197: iOS crashes when multiple request are done simultaneously (reverted a8e3637) - Fixed #189: error code mappings are not precise diff --git a/package.json b/package.json index 255bd57..90079ce 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "testandroid": "npm run updatecert && ./scripts/build-test-app.sh --android --emulator && ./scripts/test-app.sh --android --emulator", "testios": "npm run updatecert && ./scripts/build-test-app.sh --ios --emulator && ./scripts/test-app.sh --ios --emulator", "testapp": "npm run testandroid && npm run testios", - "testjs": "mocha ./test/js-mocha-specs.js", + "testjs": "mocha ./test/js-specs.js", "test": "npm run testjs && npm run testapp", "release": "npm run test && ./scripts/release.sh" }, diff --git a/scripts/build-test-app.sh b/scripts/build-test-app.sh index 1f56344..8e51f29 100755 --- a/scripts/build-test-app.sh +++ b/scripts/build-test-app.sh @@ -35,10 +35,11 @@ while :; do shift done +printf 'Building test app for %s\n' $PLATFORM rm -rf $ROOT/temp mkdir $ROOT/temp -cp -r $ROOT/test/app-template/. $ROOT/temp/ -cp $ROOT/test/app-test-definitions.js $ROOT/temp/www/ +cp -r $ROOT/test/e2e-app-template/. $ROOT/temp/ +cp $ROOT/test/e2e-specs.js $ROOT/temp/www/ rsync -ax --exclude node_modules --exclude scripts --exclude temp --exclude test $ROOT/. $WORKINGCOPY cd $ROOT/temp $CDV prepare diff --git a/scripts/setup-android-sdk.sh b/scripts/setup-android-sdk.sh deleted file mode 100755 index 83fe2de..0000000 --- a/scripts/setup-android-sdk.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env bash -set -e - -while getopts a:b: option; do - case "${option}" in - a) API_LEVEL=${OPTARG};; - b) BUILD_TOOLS_VERSION=${OPTARG};; - esac -done - -curl http://dl.google.com/android/android-sdk_r24.4-macosx.zip -o android-sdk-macosx.zip -tar -xvf android-sdk-macosx.zip - -echo y | ./android-sdk-macosx/tools/android update sdk --no-ui --all --filter platform-tools -echo y | ./android-sdk-macosx/tools/android update sdk --no-ui --all --filter build-tools-${BUILD_TOOLS_VERSION} -echo y | ./android-sdk-macosx/tools/android update sdk --no-ui --all --filter android-${API_LEVEL} -echo y | ./android-sdk-macosx/tools/android update sdk --no-ui --all --filter extra-android-support -echo y | ./android-sdk-macosx/tools/android update sdk --no-ui --all --filter extra-android-m2repository -echo y | ./android-sdk-macosx/tools/android update sdk --no-ui --all --filter extra-google-m2repository diff --git a/scripts/test-app.sh b/scripts/test-app.sh index 317bb03..245376b 100755 --- a/scripts/test-app.sh +++ b/scripts/test-app.sh @@ -8,6 +8,7 @@ if [ $CI == "true" ] && ([ -z $SAUCE_USERNAME ] || [ -z $SAUCE_ACCESS_KEY ]); th exit 0; fi +printf 'Running e2e tests\n' pushd $ROOT -./node_modules/.bin/mocha ./test/app-mocha-specs/test.js "$@" +./node_modules/.bin/mocha ./test/e2e-tooling/test.js "$@" popd diff --git a/scripts/update-test-cert.js b/scripts/update-test-cert.js index 864a4ff..c4df08c 100644 --- a/scripts/update-test-cert.js +++ b/scripts/update-test-cert.js @@ -3,7 +3,7 @@ const https = require('https'); const path = require('path'); const SOURCE_HOST = 'httpbin.org'; -const TARGET_PATH = path.join(__dirname, '../test/app-template/www/certificates/httpbin.org.cer'); +const TARGET_PATH = path.join(__dirname, '../test/e2e-app-template/www/certificates/httpbin.org.cer'); const getCert = hostname => new Promise((resolve, reject) => { const options = { diff --git a/test/app-template/build.json b/test/e2e-app-template/build.json similarity index 100% rename from test/app-template/build.json rename to test/e2e-app-template/build.json diff --git a/test/app-template/config.xml b/test/e2e-app-template/config.xml similarity index 100% rename from test/app-template/config.xml rename to test/e2e-app-template/config.xml diff --git a/test/app-template/package.json b/test/e2e-app-template/package.json similarity index 100% rename from test/app-template/package.json rename to test/e2e-app-template/package.json diff --git a/test/app-template/www/certificates/.gitkeep b/test/e2e-app-template/www/certificates/.gitkeep similarity index 100% rename from test/app-template/www/certificates/.gitkeep rename to test/e2e-app-template/www/certificates/.gitkeep diff --git a/test/app-template/www/index.css b/test/e2e-app-template/www/index.css similarity index 100% rename from test/app-template/www/index.css rename to test/e2e-app-template/www/index.css diff --git a/test/app-template/www/index.html b/test/e2e-app-template/www/index.html similarity index 93% rename from test/app-template/www/index.html rename to test/e2e-app-template/www/index.html index 04be6f3..3247ed6 100644 --- a/test/app-template/www/index.html +++ b/test/e2e-app-template/www/index.html @@ -15,7 +15,7 @@ - + diff --git a/test/app-template/www/index.js b/test/e2e-app-template/www/index.js similarity index 100% rename from test/app-template/www/index.js rename to test/e2e-app-template/www/index.js diff --git a/test/app-test-definitions.js b/test/e2e-specs.js similarity index 99% rename from test/app-test-definitions.js rename to test/e2e-specs.js index b791574..361793b 100644 --- a/test/app-test-definitions.js +++ b/test/e2e-specs.js @@ -383,8 +383,7 @@ const tests = [ JSON .parse(result.data.data) .headers - .Cookie - .should.be.equal(''); + .should.not.have.property('Cookie'); } }, { diff --git a/test/app-mocha-specs/helpers/apps.js b/test/e2e-tooling/helpers/apps.js similarity index 100% rename from test/app-mocha-specs/helpers/apps.js rename to test/e2e-tooling/helpers/apps.js diff --git a/test/app-mocha-specs/helpers/caps.js b/test/e2e-tooling/helpers/caps.js similarity index 100% rename from test/app-mocha-specs/helpers/caps.js rename to test/e2e-tooling/helpers/caps.js diff --git a/test/app-mocha-specs/helpers/logging.js b/test/e2e-tooling/helpers/logging.js similarity index 100% rename from test/app-mocha-specs/helpers/logging.js rename to test/e2e-tooling/helpers/logging.js diff --git a/test/app-mocha-specs/helpers/server.js b/test/e2e-tooling/helpers/server.js similarity index 100% rename from test/app-mocha-specs/helpers/server.js rename to test/e2e-tooling/helpers/server.js diff --git a/test/app-mocha-specs/helpers/setup.js b/test/e2e-tooling/helpers/setup.js similarity index 100% rename from test/app-mocha-specs/helpers/setup.js rename to test/e2e-tooling/helpers/setup.js diff --git a/test/app-mocha-specs/test.js b/test/e2e-tooling/test.js similarity index 97% rename from test/app-mocha-specs/test.js rename to test/e2e-tooling/test.js index 4056e02..91bface 100644 --- a/test/app-mocha-specs/test.js +++ b/test/e2e-tooling/test.js @@ -4,7 +4,7 @@ const wd = require('wd'); const apps = require('./helpers/apps'); const caps = Object.assign({}, require('./helpers/caps')); const serverConfig = require('./helpers/server'); -const testDefinitions = require('../app-test-definitions'); +const testDefinitions = require('../e2e-specs'); const pkgjson = require('../../package.json'); describe('Advanced HTTP', function() { diff --git a/test/js-mocha-specs.js b/test/js-specs.js similarity index 89% rename from test/js-mocha-specs.js rename to test/js-specs.js index f9ed41a..c7aa437 100644 --- a/test/js-mocha-specs.js +++ b/test/js-specs.js @@ -235,3 +235,30 @@ describe('URL util', function () { .should.equal('http://ilkimen.net/?myParam=myValue¶m1=value1#myHash'); }); }); + +describe('Common helpers', function () { + describe('mergeHeaders(globalHeaders, localHeaders)', function () { + const init = require('../www/helpers'); + init.debug = true; + + const helpers = init(null, null, null); + + it('merges empty header sets correctly', () => { + helpers.mergeHeaders({}, {}).should.eql({}); + }); + }); + + describe('getCookieHeader(url)', function () { + it('resolves cookie header correctly when no cookie is set #198', () => { + const helpers = require('../www/helpers')(null, { getCookieString: () => '' }, null); + + helpers.getCookieHeader('http://ilkimen.net').should.eql({}); + }); + + it('resolves cookie header correctly when a cookie is set', () => { + const helpers = require('../www/helpers')(null, { getCookieString: () => 'cookie=value' }, null); + + helpers.getCookieHeader('http://ilkimen.net').should.eql({ Cookie: 'cookie=value' }); + }); + }); +}) diff --git a/www/helpers.js b/www/helpers.js index 2120570..a9acc60 100644 --- a/www/helpers.js +++ b/www/helpers.js @@ -4,7 +4,7 @@ module.exports = function init(jsUtil, cookieHandler, messages) { var validClientAuthModes = ['none', 'systemstore', 'file']; var validHttpMethods = ['get', 'put', 'post', 'patch', 'head', 'delete', 'upload', 'download']; - return { + var interface = { b64EncodeUnicode: b64EncodeUnicode, checkSerializer: checkSerializer, checkSSLCertMode: checkSSLCertMode, @@ -19,6 +19,24 @@ module.exports = function init(jsUtil, cookieHandler, messages) { handleMissingOptions: handleMissingOptions }; + // expose all functions for testing purposes + if (init.debug) { + interface.mergeHeaders = mergeHeaders; + interface.checkForValidStringValue = checkForValidStringValue; + interface.checkKeyValuePairObject = checkKeyValuePairObject; + interface.checkHttpMethod = checkHttpMethod; + interface.checkTimeoutValue = checkTimeoutValue; + interface.checkHeadersObject = checkHeadersObject; + interface.checkParamsObject = checkParamsObject; + interface.resolveCookieString = resolveCookieString; + interface.createFileEntry = createFileEntry; + interface.getCookieHeader = getCookieHeader; + interface.getMatchingHostHeaders = getMatchingHostHeaders; + interface.getAllowedDataTypes = getAllowedDataTypes; + } + + return interface; + // Thanks Mozilla: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_.22Unicode_Problem.22 function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) { @@ -158,7 +176,13 @@ module.exports = function init(jsUtil, cookieHandler, messages) { } function getCookieHeader(url) { - return { Cookie: cookieHandler.getCookieString(url) }; + var cookieString = cookieHandler.getCookieString(url); + + if (cookieString.length) { + return { Cookie: cookieHandler.getCookieString(url) }; + } + + return {}; } function getMatchingHostHeaders(url, headersList) {