2016-11-10 21:03:12 +00:00
|
|
|
import * as misc from '../misc';
|
2016-09-03 04:32:45 +00:00
|
|
|
|
|
|
|
describe('getBasicAuthHeader()', () => {
|
|
|
|
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()', () => {
|
|
|
|
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);
|
|
|
|
})
|
|
|
|
});
|
2016-09-04 21:32:36 +00:00
|
|
|
|
|
|
|
describe('generateId()', () => {
|
|
|
|
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()', () => {
|
|
|
|
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()', () => {
|
|
|
|
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-09-24 03:26:24 +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-10 21:03:12 +00:00
|
|
|
const url = misc.prepareUrlForSending('https://google.com?s=foo&s=foo %20100%');
|
2016-09-22 19:44:28 +00:00
|
|
|
expect(url).toBe('https://google.com/?s=foo&s=foo%20%20100%25');
|
|
|
|
});
|
|
|
|
});
|
2016-11-10 09:00:29 +00:00
|
|
|
|
|
|
|
describe('filterHeaders()', () => {
|
|
|
|
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'}]);
|
|
|
|
})
|
|
|
|
});
|
2016-11-10 21:03:12 +00:00
|
|
|
|
|
|
|
describe('keyedDebounce()', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
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']
|
|
|
|
}]);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('debounce()', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
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']]);
|
|
|
|
})
|
|
|
|
});
|