From 80f7baa6048384a33b7b4e3d586d3a1e95c39914 Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 5 Feb 2017 16:45:11 +0100 Subject: [PATCH] writing test for scriptLocation function, fixing non-removed query separator bug --- js/privatebin.js | 3 ++- js/test.js | 52 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/js/privatebin.js b/js/privatebin.js index e8d8acb..708b185 100644 --- a/js/privatebin.js +++ b/js/privatebin.js @@ -259,7 +259,8 @@ jQuery.PrivateBin = function($, sjcl, Base64, RawDeflate) { var scriptLocation = window.location.href.substring( 0, window.location.href.length - window.location.search.length - window.location.hash.length - ), hashIndex = scriptLocation.indexOf('#'); + ), + hashIndex = scriptLocation.indexOf('?'); if (hashIndex !== -1) { scriptLocation = scriptLocation.substring(0, hashIndex); diff --git a/js/test.js b/js/test.js index b979534..58a7fdb 100644 --- a/js/test.js +++ b/js/test.js @@ -1,22 +1,20 @@ 'use strict'; -var jsc = require('jsverify'); - -before(function () { - this.jsdom = require('jsdom-global')(); - global.$ = global.jQuery = require('./jquery-3.1.1'); - global.sjcl = require('./sjcl-1.0.6'); - global.Base64 = require('./base64-2.1.9'); - global.RawDeflate = require('./rawdeflate-0.5'); - require('./rawinflate-0.3'); - require('./privatebin'); -}) - -after(function () { - this.jsdom(); -}) +var jsc = require('jsverify'), + jsdom = require('jsdom-global'), + cleanup = jsdom(); +global.$ = global.jQuery = require('./jquery-3.1.1'); +global.sjcl = require('./sjcl-1.0.6'); +global.Base64 = require('./base64-2.1.9'); +global.RawDeflate = require('./rawdeflate-0.5'); +require('./rawinflate-0.3'); +require('./privatebin'); describe('helper', function () { describe('secondsToHuman', function () { + after(function () { + cleanup(); + }); + jsc.property('returns an array with a number and a word', 'integer', function (number) { var result = $.PrivateBin.helper.secondsToHuman(number); return Array.isArray(result) && @@ -56,5 +54,29 @@ describe('helper', function () { return $.PrivateBin.helper.secondsToHuman(number)[1] === 'month'; }); }); + + describe('scriptLocation', function () { + after(function () { + cleanup(); + }); + + var a2zString = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'], + alnumString = a2zString.concat(['0','1','2','3','4','5','6','7','8','9']), + queryString = alnumString.concat(['+','%','&','.','*','-','_']); + jsc.property( + 'returns the URL without query & fragment', + jsc.nearray(jsc.elements(a2zString)), + jsc.nearray(jsc.elements(a2zString)), + jsc.array(jsc.elements(queryString)), + 'string', + function (schema, address, query, fragment) { + var expected = schema.join('') + '://' + address.join('') + '/', + clean = jsdom('', {url: expected + '?' + query.join('') + '#' + fragment}), + result = $.PrivateBin.helper.scriptLocation(); + clean(); + return expected === result; + } + ); + }); });