Skip to content

Commit

Permalink
Fix other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed Sep 26, 2017
1 parent 5f7e4de commit 5e61577
Showing 1 changed file with 22 additions and 46 deletions.
68 changes: 22 additions & 46 deletions test/escaper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,167 +14,143 @@ const internals = {};

// Test shortcuts

const lab = exports.lab = Lab.script();
const describe = lab.experiment;
const it = lab.test;
const { describe, it } = exports.lab = Lab.script();
const expect = Code.expect;


describe('escapeJavaScript()', () => {

it('encodes / characters', (done) => {
it('encodes / characters', async () => {

const encoded = Hoek.escapeJavaScript('<script>alert(1)</script>');
expect(encoded).to.equal('\\x3cscript\\x3ealert\\x281\\x29\\x3c\\x2fscript\\x3e');
done();
});

it('encodes \' characters', (done) => {
it('encodes \' characters', async () => {

const encoded = Hoek.escapeJavaScript('something(\'param\')');
expect(encoded).to.equal('something\\x28\\x27param\\x27\\x29');
done();
});

it('encodes large unicode characters with the correct padding', (done) => {
it('encodes large unicode characters with the correct padding', async () => {

const encoded = Hoek.escapeJavaScript(String.fromCharCode(500) + String.fromCharCode(1000));
expect(encoded).to.equal('\\u0500\\u1000');
done();
});

it('doesn\'t throw an exception when passed null', (done) => {
it('doesn\'t throw an exception when passed null', async () => {

const encoded = Hoek.escapeJavaScript(null);
expect(encoded).to.equal('');
done();
});
});

describe('escapeHtml()', () => {

it('encodes / characters', (done) => {
it('encodes / characters', async () => {

const encoded = Hoek.escapeHtml('<script>alert(1)</script>');
expect(encoded).to.equal('&lt;script&gt;alert&#x28;1&#x29;&lt;&#x2f;script&gt;');
done();
});

it('encodes < and > as named characters', (done) => {
it('encodes < and > as named characters', async () => {

const encoded = Hoek.escapeHtml('<script><>');
expect(encoded).to.equal('&lt;script&gt;&lt;&gt;');
done();
});

it('encodes large unicode characters', (done) => {
it('encodes large unicode characters', async () => {

const encoded = Hoek.escapeHtml(String.fromCharCode(500) + String.fromCharCode(1000));
expect(encoded).to.equal('&#500;&#1000;');
done();
});

it('doesn\'t throw an exception when passed null', (done) => {
it('doesn\'t throw an exception when passed null', async () => {

const encoded = Hoek.escapeHtml(null);
expect(encoded).to.equal('');
done();
});

it('encodes {} characters', (done) => {
it('encodes {} characters', async () => {

const encoded = Hoek.escapeHtml('{}');
expect(encoded).to.equal('&#x7b;&#x7d;');
done();
});
});

describe('escapeJson()', () => {

it('encodes < and > as unicode escaped equivalents', (done) => {
it('encodes < and > as unicode escaped equivalents', async () => {

const encoded = Hoek.escapeJson('<script><>');
expect(encoded).to.equal('\\u003cscript\\u003e\\u003c\\u003e');
done();
});

it('doesn\'t encode \0 as hex escaped equivalent', (done) => {
it('doesn\'t encode \0 as hex escaped equivalent', async () => {

const encoded = Hoek.escapeJson('\0');
expect(encoded).to.equal('\0');
done();
});

it('encodes & (ampersand) as unicode escaped equivalent', (done) => {
it('encodes & (ampersand) as unicode escaped equivalent', async () => {

const encoded = Hoek.escapeJson('&&');
expect(encoded).to.equal('\\u0026\\u0026');
done();
});

it('encodes line seperator as unicode escaped equivalent', (done) => {
it('encodes line seperator as unicode escaped equivalent', async () => {

const lineSeparator = String.fromCharCode(0x2028);
const encoded = Hoek.escapeJson(lineSeparator);
expect(encoded).to.equal('\\u2028');
done();
});

it('encodes paragraph seperator as unicode escaped equivalent', (done) => {
it('encodes paragraph seperator as unicode escaped equivalent', async () => {

const paragraphSeparator = String.fromCharCode(0x2029);
const encoded = Hoek.escapeJson(paragraphSeparator);
expect(encoded).to.equal('\\u2029');
done();
});

it('doesn\'t encode U+13F0 Cherokee Letter Ye as unicode escaped equivalent', (done) => {
it('doesn\'t encode U+13F0 Cherokee Letter Ye as unicode escaped equivalent', async () => {

const encoded = Hoek.escapeJson('Ᏸ');
expect(encoded).to.equal('Ᏸ');
done();
});

it('doesn\'t encode U+1F4A9 PILE OF POO as unicode escaped equivalent', (done) => {
it('doesn\'t encode U+1F4A9 PILE OF POO as unicode escaped equivalent', async () => {

const encoded = Hoek.escapeJson('💩');
expect(encoded).to.equal('💩');
done();
});

it('doesn\'t encode U+1D306 TETRAGRAM FOR CENTRE as unicode escaped equivalent', (done) => {
it('doesn\'t encode U+1D306 TETRAGRAM FOR CENTRE as unicode escaped equivalent', async () => {

const encoded = Hoek.escapeJson('𝌆');
expect(encoded).to.equal('𝌆');
done();
});

it('doesn\'t encode \\ (backslash)', (done) => {
it('doesn\'t encode \\ (backslash)', async () => {

const encoded = Hoek.escapeJson('\\');
expect(encoded).to.equal('\\');
done();
});

it('doesn\'t throw an exception when passed null', (done) => {
it('doesn\'t throw an exception when passed null', async () => {

const encoded = Hoek.escapeJson(null);
expect(encoded).to.equal('');
done();
});

it('doesn\'t encode {} characters', (done) => {
it('doesn\'t encode {} characters', async () => {

const encoded = Hoek.escapeJson('{}');
expect(encoded).to.equal('{}');
done();
});

it('doesn\'t encode / (slash) character', (done) => {
it('doesn\'t encode / (slash) character', async () => {

const encoded = Hoek.escapeJson('<script>alert(1)</script>');
expect(encoded).to.equal('\\u003cscript\\u003ealert(1)\\u003c/script\\u003e');
done();
});

});

0 comments on commit 5e61577

Please sign in to comment.