insomnia/packages/insomnia-app/app/models/__tests__/request.test.js

337 lines
9.2 KiB
JavaScript
Raw Normal View History

2016-12-01 18:48:49 +00:00
import * as models from '../index';
2018-06-25 17:42:50 +00:00
import { globalBeforeEach } from '../../__jest__/before-each';
2016-11-10 17:33:28 +00:00
describe('init()', () => {
2017-07-20 03:36:44 +00:00
beforeEach(globalBeforeEach);
2016-11-10 17:33:28 +00:00
it('contains all required fields', async () => {
Date.now = jest.fn().mockReturnValue(1478795580200);
2017-07-20 03:36:44 +00:00
expect(models.request.init()).toEqual({
2018-03-06 03:45:40 +00:00
isPrivate: false,
2016-11-10 17:33:28 +00:00
authentication: {},
body: {},
2016-11-10 17:33:28 +00:00
headers: [],
metaSortKey: -1478795580200,
method: 'GET',
name: 'New Request',
description: '',
2016-11-10 17:33:28 +00:00
parameters: [],
url: '',
settingStoreCookies: true,
settingSendCookies: true,
settingDisableRenderRequestBody: false,
settingEncodeUrl: true,
settingRebuildPath: true,
2016-11-10 17:33:28 +00:00
});
});
});
describe('create()', async () => {
2017-07-20 03:36:44 +00:00
beforeEach(globalBeforeEach);
2016-11-10 17:33:28 +00:00
it('creates a valid request', async () => {
Date.now = jest.fn().mockReturnValue(1478795580200);
2016-11-10 17:33:28 +00:00
2017-12-12 19:26:27 +00:00
const request = await models.request.create({
name: 'Test Request',
parentId: 'fld_124',
description: 'A test Request',
2017-12-12 19:26:27 +00:00
});
2016-11-10 17:33:28 +00:00
const expected = {
_id: 'req_cc1dd2ca4275747aa88199e8efd42403',
2018-03-06 03:45:40 +00:00
isPrivate: false,
2016-11-10 17:33:28 +00:00
created: 1478795580200,
modified: 1478795580200,
parentId: 'fld_124',
type: 'Request',
authentication: {},
description: 'A test Request',
body: {},
2016-11-10 17:33:28 +00:00
headers: [],
metaSortKey: -1478795580200,
method: 'GET',
name: 'Test Request',
parameters: [],
url: '',
settingStoreCookies: true,
settingSendCookies: true,
settingDisableRenderRequestBody: false,
settingEncodeUrl: true,
settingRebuildPath: true,
2016-11-10 17:33:28 +00:00
};
expect(request).toEqual(expected);
2017-07-20 03:36:44 +00:00
expect(await models.request.getById(expected._id)).toEqual(expected);
2016-11-10 17:33:28 +00:00
});
it('fails when missing parentId', async () => {
Date.now = jest.fn().mockReturnValue(1478795580200);
2018-06-25 17:42:50 +00:00
expect(() => models.request.create({ name: 'Test Request' })).toThrow(
'New Requests missing `parentId`',
2018-06-25 17:42:50 +00:00
);
2016-11-10 17:33:28 +00:00
});
});
describe('updateMimeType()', async () => {
2017-07-20 03:36:44 +00:00
beforeEach(globalBeforeEach);
2016-11-10 17:33:28 +00:00
it('adds header when does not exist', async () => {
2018-06-25 17:42:50 +00:00
const request = await models.request.create({
name: 'My Request',
parentId: 'fld_1',
2018-06-25 17:42:50 +00:00
});
2016-11-10 17:33:28 +00:00
expect(request).not.toBeNull();
2018-10-17 16:42:33 +00:00
const newRequest = await models.request.updateMimeType(request, 'text/html');
expect(newRequest.headers).toEqual([{ name: 'Content-Type', value: 'text/html' }]);
2016-11-10 17:33:28 +00:00
});
it('replaces header when exists', async () => {
2017-07-20 03:36:44 +00:00
const request = await models.request.create({
2016-11-10 17:33:28 +00:00
name: 'My Request',
parentId: 'fld_1',
headers: [
2018-06-25 17:42:50 +00:00
{ name: 'content-tYPE', value: 'application/json' },
{ name: 'foo', value: 'bar' },
{ bad: true },
null,
],
2016-11-10 17:33:28 +00:00
});
expect(request).not.toBeNull();
2018-10-17 16:42:33 +00:00
const newRequest = await models.request.updateMimeType(request, 'text/html');
2016-11-10 17:33:28 +00:00
expect(newRequest.headers).toEqual([
2018-06-25 17:42:50 +00:00
{ name: 'content-tYPE', value: 'text/html' },
{ name: 'foo', value: 'bar' },
{ bad: true },
null,
2016-11-10 17:33:28 +00:00
]);
});
it('replaces header when exists', async () => {
2017-07-20 03:36:44 +00:00
const request = await models.request.create({
2016-11-10 17:33:28 +00:00
name: 'My Request',
parentId: 'fld_1',
headers: [{ name: 'content-tYPE', value: 'application/json' }],
2016-11-10 17:33:28 +00:00
});
expect(request).not.toBeNull();
2018-10-17 16:42:33 +00:00
const newRequest = await models.request.updateMimeType(request, 'text/html');
expect(newRequest.headers).toEqual([{ name: 'content-tYPE', value: 'text/html' }]);
2016-11-10 17:33:28 +00:00
});
it('removes existing content-type when set to null (i.e. no body)', async () => {
2017-07-20 03:36:44 +00:00
const request = await models.request.create({
2016-11-10 17:33:28 +00:00
name: 'My Request',
parentId: 'fld_1',
headers: [{ name: 'content-tYPE', value: 'application/json' }],
2016-11-10 17:33:28 +00:00
});
expect(request).not.toBeNull();
2017-07-20 03:36:44 +00:00
const newRequest = await models.request.updateMimeType(request, null);
expect(newRequest.body).toEqual({});
expect(newRequest.headers).toEqual([]);
2016-11-10 17:33:28 +00:00
});
it('uses saved body when provided', async () => {
const request = await models.request.create({
name: 'My Request',
parentId: 'fld_1',
body: {
text: 'My Data',
},
});
expect(request).not.toBeNull();
2018-10-17 16:42:33 +00:00
const newRequest = await models.request.updateMimeType(request, 'application/json', false, {
text: 'Saved Data',
2018-10-17 16:42:33 +00:00
});
expect(newRequest.body.text).toEqual('Saved Data');
});
it('uses existing body when saved body not provided', async () => {
const request = await models.request.create({
name: 'My Request',
parentId: 'fld_1',
body: {
text: 'My Data',
},
});
expect(request).not.toBeNull();
2018-10-17 16:42:33 +00:00
const newRequest = await models.request.updateMimeType(request, 'application/json', false, {});
expect(newRequest.body.text).toEqual('My Data');
});
2016-11-10 17:33:28 +00:00
});
describe('migrate()', () => {
2017-07-20 03:36:44 +00:00
beforeEach(globalBeforeEach);
it('migrates basic case', () => {
const original = {
headers: [],
body: 'hello world!',
};
const expected = {
headers: [],
2018-06-25 17:42:50 +00:00
body: { mimeType: '', text: 'hello world!' },
url: '',
};
2017-07-20 03:36:44 +00:00
expect(models.request.migrate(original)).toEqual(expected);
});
it('migrates form-urlencoded', () => {
const original = {
2018-10-17 16:42:33 +00:00
headers: [{ name: 'content-type', value: 'application/x-www-form-urlencoded' }],
body: 'foo=bar&baz={{ hello }}',
};
const expected = {
2018-10-17 16:42:33 +00:00
headers: [{ name: 'content-type', value: 'application/x-www-form-urlencoded' }],
body: {
mimeType: 'application/x-www-form-urlencoded',
params: [{ name: 'foo', value: 'bar' }, { name: 'baz', value: '{{ hello }}' }],
2016-12-08 23:29:45 +00:00
},
url: '',
};
2017-07-20 03:36:44 +00:00
expect(models.request.migrate(original)).toEqual(expected);
});
it('migrates form-urlencoded with charset', () => {
const original = {
2018-06-25 17:42:50 +00:00
headers: [
{
name: 'content-type',
value: 'application/x-www-form-urlencoded; charset=utf-8',
},
2018-06-25 17:42:50 +00:00
],
body: 'foo=bar&baz={{ hello }}',
};
const expected = {
2018-06-25 17:42:50 +00:00
headers: [
{
name: 'content-type',
value: 'application/x-www-form-urlencoded; charset=utf-8',
},
2018-06-25 17:42:50 +00:00
],
body: {
mimeType: 'application/x-www-form-urlencoded',
params: [{ name: 'foo', value: 'bar' }, { name: 'baz', value: '{{ hello }}' }],
2016-12-08 23:29:45 +00:00
},
url: '',
};
2017-07-20 03:36:44 +00:00
expect(models.request.migrate(original)).toEqual(expected);
});
it('migrates form-urlencoded malformed', () => {
const original = {
2018-10-17 16:42:33 +00:00
headers: [{ name: 'content-type', value: 'application/x-www-form-urlencoded' }],
body: '{"foo": "bar"}',
};
const expected = {
2018-10-17 16:42:33 +00:00
headers: [{ name: 'content-type', value: 'application/x-www-form-urlencoded' }],
body: {
mimeType: 'application/x-www-form-urlencoded',
params: [{ name: '{"foo": "bar"}', value: '' }],
2016-12-08 23:29:45 +00:00
},
url: '',
};
2017-07-20 03:36:44 +00:00
expect(models.request.migrate(original)).toEqual(expected);
});
it('migrates mime-type', () => {
const contentToMimeMap = {
'application/json; charset=utf-8': 'application/json',
'text/plain': 'text/plain',
malformed: 'malformed',
};
for (const contentType of Object.keys(contentToMimeMap)) {
const original = {
2018-06-25 17:42:50 +00:00
headers: [{ name: 'content-type', value: contentType }],
body: '',
};
const expected = {
2018-06-25 17:42:50 +00:00
headers: [{ name: 'content-type', value: contentType }],
body: { mimeType: contentToMimeMap[contentType], text: '' },
url: '',
};
2017-07-20 03:36:44 +00:00
expect(models.request.migrate(original)).toEqual(expected);
}
});
it('skips migrate for schema 1', () => {
const original = {
body: { mimeType: 'text/plain', text: 'foo' },
};
2017-07-20 03:36:44 +00:00
expect(models.request.migrate(original)).toBe(original);
});
2016-12-01 18:48:49 +00:00
it('migrates with weird data', () => {
2018-06-25 17:42:50 +00:00
const newBody = { body: { mimeType: '', text: 'foo bar!' } };
const stringBody = { body: 'foo bar!' };
const nullBody = { body: null };
2016-12-01 18:48:49 +00:00
const noBody = {};
const expected = {
body: {
2016-12-01 19:13:01 +00:00
mimeType: '',
text: 'foo bar!',
2016-12-08 23:29:45 +00:00
},
url: '',
};
2016-12-01 18:48:49 +00:00
const expected2 = {
2016-12-08 23:29:45 +00:00
body: {},
url: '',
2016-12-01 18:48:49 +00:00
};
2017-07-20 03:36:44 +00:00
expect(models.request.migrate(newBody)).toEqual(expected);
expect(models.request.migrate(stringBody)).toEqual(expected);
expect(models.request.migrate(nullBody)).toEqual(expected2);
expect(models.request.migrate(noBody)).toEqual(expected2);
2016-12-01 18:48:49 +00:00
});
2017-09-13 06:11:49 +00:00
it('migrates from initModel()', async () => {
Date.now = jest.fn().mockReturnValue(1478795580200);
2016-12-01 18:48:49 +00:00
const original = {
_id: 'req_123',
headers: [],
body: 'hello world!',
2016-12-01 18:48:49 +00:00
};
const expected = {
_id: 'req_123',
2018-03-06 03:45:40 +00:00
isPrivate: false,
2016-12-01 18:48:49 +00:00
type: 'Request',
url: '',
created: 1478795580200,
modified: 1478795580200,
metaSortKey: -1478795580200,
name: 'New Request',
description: '',
2016-12-01 18:48:49 +00:00
method: 'GET',
headers: [],
authentication: {},
parameters: [],
parentId: null,
2018-06-25 17:42:50 +00:00
body: { mimeType: '', text: 'hello world!' },
settingStoreCookies: true,
settingSendCookies: true,
settingDisableRenderRequestBody: false,
settingEncodeUrl: true,
settingRebuildPath: true,
2016-12-01 18:48:49 +00:00
};
2017-09-13 06:11:49 +00:00
const migrated = await models.initModel(models.request.type, original);
2016-12-01 18:48:49 +00:00
expect(migrated).toEqual(expected);
});
});