insomnia/app/common/__tests__/misc.test.js

240 lines
6.9 KiB
JavaScript
Raw Normal View History

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';
describe('getBasicAuthHeader()', () => {
2017-07-20 03:36:44 +00:00
beforeEach(globalBeforeEach);
it('succeed with username and password', () => {
2016-11-10 21:03:12 +00:00
const header = misc.getBasicAuthHeader('user', 'password');
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');
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', '');
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);
expect(header).toEqual({
name: 'Authorization',
value: 'Basic dXNlcjo='
});
});
});
describe('hasAuthHeader()', () => {
2017-07-20 03:36:44 +00:00
beforeEach(globalBeforeEach);
it('finds valid header', () => {
2016-11-10 21:03:12 +00:00
const yes = misc.hasAuthHeader([
{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([
{name: 'foo', value: 'bar'},
{name: 'AuthOrizAtiOn', value: 'foo'}
]);
expect(yes).toEqual(true);
});
});
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');
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
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();
Sync Proof of Concept (#33) * Maybe working POC * Change to use remote url * Other URL too * Some logic * Got the push part working * Made some updates * Fix * Update * Add status code check * Stuff * Implemented new sync api * A bit more robust * Debounce changes * Change timeout * Some fixes * Remove .less * Better error handling * Fix base url * Support for created vs updated docs * Try silent * Silence removal too * Small fix after merge * Fix test * Stuff * Implement key generation algorithm * Tidy * stuff * A bunch of stuff for the new API * Integrated the session stuff * Stuff * Just started on encryption * Lots of updates to encryption * Finished createResourceGroup function * Full encryption/decryption working (I think) * Encrypt localstorage with sessionID * Some more * Some extra checks * Now uses separate DB. Still needs to be simplified a LOT * Fix deletion bug * Fixed unicode bug with encryption * Simplified and working * A bunch of polish * Some stuff * Removed some workspace meta properties * Migrated a few more meta properties * Small changes * Fix body scrolling and url cursor jumping * Removed duplication of webpack port * Remove workspaces reduces * Some small fixes * Added sync modal and opt-in setting * Good start to sync flow * Refactored modal footer css * Update sync status * Sync logger * A bit better logging * Fixed a bunch of sync-related bugs * Fixed signup form button * Gravatar component * Split sync modal into tabs * Tidying * Some more error handling * start sending 'user agent * Login/signup error handling * Use real UUIDs * Fixed tests * Remove unused function * Some extra checks * Moved cloud sync setting to about page * Some small changes * Some things
2016-10-21 17:20:36 +00:00
expect(id).toMatch(/^[a-z0-9]{32}$/);
2016-09-04 21:32:36 +00:00
});
});
describe('setDefaultProtocol()', () => {
2017-07-20 03:36:44 +00:00
beforeEach(globalBeforeEach);
it('no-ops on empty url', () => {
const url = misc.setDefaultProtocol('');
expect(url).toBe('');
});
it('correctly sets protocol for empty', () => {
2016-11-10 21:03:12 +00:00
const url = misc.setDefaultProtocol('google.com');
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');
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');
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');
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
});
it('doesn\'t decode ignored characters', () => {
// Encoded should skip raw versions of @ ; ,
const url = misc.prepareUrlForSending('https://google.com/@;,&^+');
expect(url).toBe('https://google.com/@;,%26%5E+');
// 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');
});
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'}]);
});
2016-11-10 09:00:29 +00:00
});
2016-11-10 21:03:12 +00:00
describe('keyedDebounce()', () => {
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']
}]);
});
2016-11-10 21:03:12 +00:00
});
describe('debounce()', () => {
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']]);
});
2016-11-10 21:03:12 +00:00
});