2016-11-10 21:03:12 +00:00
|
|
|
import * as misc from '../misc';
|
2017-07-20 03:36:44 +00:00
|
|
|
import {globalBeforeEach} from '../../__jest__/before-each';
|
2016-09-03 04:32:45 +00:00
|
|
|
|
|
|
|
describe('getBasicAuthHeader()', () => {
|
2017-07-20 03:36:44 +00:00
|
|
|
beforeEach(globalBeforeEach);
|
2016-09-03 04:32:45 +00:00
|
|
|
it('succeed with username and password', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const header = misc.getBasicAuthHeader('user', 'password');
|
2016-09-03 04:32:45 +00:00
|
|
|
expect(header).toEqual({
|
|
|
|
name: 'Authorization',
|
|
|
|
value: 'Basic dXNlcjpwYXNzd29yZA=='
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('succeed with no username', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const header = misc.getBasicAuthHeader(null, 'password');
|
2016-09-03 04:32:45 +00:00
|
|
|
expect(header).toEqual({
|
|
|
|
name: 'Authorization',
|
|
|
|
value: 'Basic OnBhc3N3b3Jk'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('succeed with username and empty password', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const header = misc.getBasicAuthHeader('user', '');
|
2016-09-03 04:32:45 +00:00
|
|
|
expect(header).toEqual({
|
|
|
|
name: 'Authorization',
|
|
|
|
value: 'Basic dXNlcjo='
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('succeed with username and null password', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const header = misc.getBasicAuthHeader('user', null);
|
2016-09-03 04:32:45 +00:00
|
|
|
expect(header).toEqual({
|
|
|
|
name: 'Authorization',
|
|
|
|
value: 'Basic dXNlcjo='
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('hasAuthHeader()', () => {
|
2017-07-20 03:36:44 +00:00
|
|
|
beforeEach(globalBeforeEach);
|
2016-09-03 04:32:45 +00:00
|
|
|
it('finds valid header', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const yes = misc.hasAuthHeader([
|
2016-09-03 04:32:45 +00:00
|
|
|
{name: 'foo', value: 'bar'},
|
|
|
|
{name: 'authorization', value: 'foo'}
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(yes).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('finds valid header case insensitive', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const yes = misc.hasAuthHeader([
|
2016-09-03 04:32:45 +00:00
|
|
|
{name: 'foo', value: 'bar'},
|
|
|
|
{name: 'AuthOrizAtiOn', value: 'foo'}
|
|
|
|
]);
|
|
|
|
|
|
|
|
expect(yes).toEqual(true);
|
2017-03-03 20:09:08 +00:00
|
|
|
});
|
2016-09-03 04:32:45 +00:00
|
|
|
});
|
2016-09-04 21:32:36 +00:00
|
|
|
|
|
|
|
describe('generateId()', () => {
|
2017-07-20 03:36:44 +00:00
|
|
|
beforeEach(globalBeforeEach);
|
2016-09-04 21:32:36 +00:00
|
|
|
it('generates a valid ID', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const id = misc.generateId('foo');
|
2016-10-21 17:20:36 +00:00
|
|
|
expect(id).toMatch(/^foo_[a-z0-9]{32}$/);
|
2016-09-04 21:32:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('generates without prefix', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const id = misc.generateId();
|
2016-10-21 17:20:36 +00:00
|
|
|
expect(id).toMatch(/^[a-z0-9]{32}$/);
|
2016-09-04 21:32:36 +00:00
|
|
|
});
|
|
|
|
});
|
2016-09-12 20:04:15 +00:00
|
|
|
|
|
|
|
describe('setDefaultProtocol()', () => {
|
2017-07-20 03:36:44 +00:00
|
|
|
beforeEach(globalBeforeEach);
|
2017-03-06 03:00:43 +00:00
|
|
|
it('no-ops on empty url', () => {
|
|
|
|
const url = misc.setDefaultProtocol('');
|
|
|
|
expect(url).toBe('');
|
|
|
|
});
|
|
|
|
|
2016-09-12 20:04:15 +00:00
|
|
|
it('correctly sets protocol for empty', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const url = misc.setDefaultProtocol('google.com');
|
2016-09-12 20:04:15 +00:00
|
|
|
expect(url).toBe('http://google.com');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not set for valid url', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const url = misc.setDefaultProtocol('https://google.com');
|
2016-09-12 20:04:15 +00:00
|
|
|
expect(url).toBe('https://google.com');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not set for valid url', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const url = misc.setDefaultProtocol('http://google.com');
|
2016-09-12 20:04:15 +00:00
|
|
|
expect(url).toBe('http://google.com');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not set for invalid url', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const url = misc.setDefaultProtocol('httbad://google.com');
|
2016-09-12 20:04:15 +00:00
|
|
|
expect(url).toBe('httbad://google.com');
|
|
|
|
});
|
|
|
|
});
|
2016-09-22 19:44:28 +00:00
|
|
|
|
|
|
|
describe('prepareUrlForSending()', () => {
|
2017-07-20 03:36:44 +00:00
|
|
|
beforeEach(globalBeforeEach);
|
2016-09-22 19:44:28 +00:00
|
|
|
it('does not touch normal url', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const url = misc.prepareUrlForSending('http://google.com');
|
2016-09-22 19:44:28 +00:00
|
|
|
expect(url).toBe('http://google.com/');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works with no protocol', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const url = misc.prepareUrlForSending('google.com');
|
2016-09-22 19:44:28 +00:00
|
|
|
expect(url).toBe('http://google.com/');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('encodes pathname', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const url = misc.prepareUrlForSending('https://google.com/foo bar/100%/foo');
|
2016-09-22 19:44:28 +00:00
|
|
|
expect(url).toBe('https://google.com/foo%20bar/100%25/foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('encodes pathname mixed encoding', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const url = misc.prepareUrlForSending('https://google.com/foo bar baz%20qux/100%/foo%25');
|
2016-10-11 16:57:06 +00:00
|
|
|
expect(url).toBe('https://google.com/foo%20bar%20baz%20qux/100%25/foo%25');
|
2016-09-22 19:44:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('leaves already encoded pathname', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const url = misc.prepareUrlForSending('https://google.com/foo%20bar%20baz/100%25/foo');
|
2016-09-22 19:44:28 +00:00
|
|
|
expect(url).toBe('https://google.com/foo%20bar%20baz/100%25/foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('encodes querystring', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const url = misc.prepareUrlForSending('https://google.com?s=foo bar 100%&hi');
|
2016-11-17 21:36:55 +00:00
|
|
|
expect(url).toBe('https://google.com/?s=foo%20bar%20100%25&hi');
|
2016-09-22 19:44:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('encodes querystring with mixed spaces', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
const url = misc.prepareUrlForSending('https://google.com?s=foo %20100%');
|
2016-09-22 19:44:28 +00:00
|
|
|
expect(url).toBe('https://google.com/?s=foo%20%20100%25');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('encodes querystring with repeated keys', () => {
|
2016-11-17 21:22:19 +00:00
|
|
|
const url = misc.prepareUrlForSending('https://google.com/;@,!?s=foo,;@-!&s=foo %20100%');
|
2016-11-17 21:55:48 +00:00
|
|
|
expect(url).toBe('https://google.com/;@,!?s=foo,%3B%40-!&s=foo%20%20100%25');
|
2016-09-22 19:44:28 +00:00
|
|
|
});
|
2016-11-30 23:11:36 +00:00
|
|
|
|
|
|
|
it('doesn\'t decode ignored characters', () => {
|
|
|
|
// Encoded should skip raw versions of @ ; ,
|
2017-01-13 00:45:15 +00:00
|
|
|
const url = misc.prepareUrlForSending('https://google.com/@;,&^+');
|
|
|
|
expect(url).toBe('https://google.com/@;,%26%5E+');
|
2016-11-30 23:11:36 +00:00
|
|
|
|
|
|
|
// Encoded should skip encoded versions of @ ; ,
|
|
|
|
const url2 = misc.prepareUrlForSending('https://google.com/%40%3B%2C%26%5E');
|
|
|
|
expect(url2).toBe('https://google.com/%40%3B%2C%26%5E');
|
|
|
|
});
|
2017-03-29 23:09:28 +00:00
|
|
|
|
|
|
|
it('doesn\'t encode if last param set', () => {
|
|
|
|
const url = misc.prepareUrlForSending('https://google.com/%%?foo=%%', false);
|
|
|
|
expect(url).toBe('https://google.com/%%?foo=%%');
|
|
|
|
});
|
2016-09-22 19:44:28 +00:00
|
|
|
});
|
2016-11-10 09:00:29 +00:00
|
|
|
|
|
|
|
describe('filterHeaders()', () => {
|
2017-07-20 03:36:44 +00:00
|
|
|
beforeEach(globalBeforeEach);
|
2016-11-10 09:00:29 +00:00
|
|
|
it('handles bad headers', () => {
|
2016-11-10 21:03:12 +00:00
|
|
|
expect(misc.filterHeaders(null, null)).toEqual([]);
|
|
|
|
expect(misc.filterHeaders([], null)).toEqual([]);
|
|
|
|
expect(misc.filterHeaders(['bad'], null)).toEqual([]);
|
|
|
|
expect(misc.filterHeaders(['bad'], 'good')).toEqual([]);
|
|
|
|
expect(misc.filterHeaders(null, 'good')).toEqual([]);
|
|
|
|
expect(misc.filterHeaders([{name: 'good', value: 'valid'}], null)).toEqual([]);
|
|
|
|
expect(misc.filterHeaders([{name: 'good', value: 'valid'}], 'good'))
|
2016-11-10 09:00:29 +00:00
|
|
|
.toEqual([{name: 'good', value: 'valid'}]);
|
2017-03-03 20:09:08 +00:00
|
|
|
});
|
2016-11-10 09:00:29 +00:00
|
|
|
});
|
2016-11-10 21:03:12 +00:00
|
|
|
|
|
|
|
describe('keyedDebounce()', () => {
|
2017-07-20 01:55:40 +00:00
|
|
|
beforeEach(async () => {
|
2017-07-20 03:36:44 +00:00
|
|
|
await globalBeforeEach();
|
2016-11-10 21:03:12 +00:00
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
// There has to be a better way to reset this...
|
|
|
|
setTimeout.mock.calls = [];
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => jest.clearAllTimers());
|
|
|
|
|
|
|
|
it('debounces correctly', () => {
|
|
|
|
const resultsList = [];
|
|
|
|
const fn = misc.keyedDebounce(results => {
|
|
|
|
resultsList.push(results);
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
fn('foo', 'bar');
|
|
|
|
fn('baz', 'bar');
|
|
|
|
fn('foo', 'bar2');
|
|
|
|
fn('foo', 'bar3');
|
|
|
|
fn('multi', 'foo', 'bar', 'baz');
|
|
|
|
|
|
|
|
expect(setTimeout.mock.calls.length).toBe(5);
|
|
|
|
expect(resultsList).toEqual([]);
|
|
|
|
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
expect(resultsList).toEqual([{
|
|
|
|
foo: ['bar3'],
|
|
|
|
baz: ['bar'],
|
|
|
|
multi: ['foo', 'bar', 'baz']
|
|
|
|
}]);
|
2017-03-03 20:09:08 +00:00
|
|
|
});
|
2016-11-10 21:03:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('debounce()', () => {
|
2017-07-20 01:55:40 +00:00
|
|
|
beforeEach(async () => {
|
2017-07-20 03:36:44 +00:00
|
|
|
await globalBeforeEach();
|
2016-11-10 21:03:12 +00:00
|
|
|
jest.useFakeTimers();
|
|
|
|
|
|
|
|
// There has to be a better way to reset this...
|
|
|
|
setTimeout.mock.calls = [];
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => jest.clearAllTimers());
|
|
|
|
|
|
|
|
it('debounces correctly', () => {
|
|
|
|
const resultList = [];
|
|
|
|
const fn = misc.debounce((...args) => {
|
|
|
|
resultList.push(args);
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
fn('foo');
|
|
|
|
fn('foo');
|
|
|
|
fn('multi', 'foo', 'bar', 'baz');
|
|
|
|
fn('baz', 'bar');
|
|
|
|
fn('foo', 'bar3');
|
|
|
|
|
|
|
|
expect(setTimeout.mock.calls.length).toBe(5);
|
|
|
|
expect(resultList).toEqual([]);
|
|
|
|
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
expect(resultList).toEqual([['foo', 'bar3']]);
|
2017-03-03 20:09:08 +00:00
|
|
|
});
|
2016-11-10 21:03:12 +00:00
|
|
|
});
|