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

466 lines
12 KiB
TypeScript
Raw Normal View History

2018-06-25 17:42:50 +00:00
import { globalBeforeEach } from '../../__jest__/before-each';
import { CONTENT_TYPE_GRAPHQL } from '../../common/constants';
2021-07-22 23:04:56 +00:00
import * as models from '../index';
import { newBodyGraphQL } from '../request';
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,
settingFollowRedirects: 'global',
2016-11-10 17:33:28 +00:00
});
});
});
2019-05-08 17:59:16 +00:00
describe('create()', () => {
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);
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,
settingFollowRedirects: 'global',
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);
expect(() =>
models.request.create({
name: 'Test Request',
}),
).toThrow('New Requests missing `parentId`');
2016-11-10 17:33:28 +00:00
});
});
2019-05-08 17:59:16 +00:00
describe('updateMimeType()', () => {
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: [
{
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([
{
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: [],
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 = {
headers: [
{
name: 'content-type',
value: 'application/x-www-form-urlencoded',
},
],
body: 'foo=bar&baz={{ hello }}',
};
const expected = {
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 = {
headers: [
{
name: 'content-type',
value: 'application/x-www-form-urlencoded',
},
],
body: '{"foo": "bar"}',
};
const expected = {
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 = {
headers: [
{
name: 'content-type',
value: contentType,
},
],
body: '',
};
const expected = {
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', () => {
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,
body: {
mimeType: '',
text: 'hello world!',
},
settingStoreCookies: true,
settingSendCookies: true,
settingDisableRenderRequestBody: false,
settingEncodeUrl: true,
settingRebuildPath: true,
settingFollowRedirects: 'global',
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);
});
});
describe('newBodyGraphQL()', () => {
it('strips \\\\n characters', () => {
const input =
'{"query": "query getCustomer() {\\\\n id\\\\n name\\\\n email\\\\n __typename\\\\n }\\\\n"}';
const expectedTextOutput = '{"query": "query getCustomer() { id name email __typename }"}';
const actualOutput = newBodyGraphQL(input);
expect(actualOutput).toEqual({
mimeType: CONTENT_TYPE_GRAPHQL,
text: expectedTextOutput,
});
});
it('does nothing to empty string', () => {
const input = '';
const expectedTextOutput = '';
const actualOutput = newBodyGraphQL(input);
expect(actualOutput).toEqual({
mimeType: CONTENT_TYPE_GRAPHQL,
text: expectedTextOutput,
});
});
it('does nothing to object that has no \\\\n characters', () => {
const input = '{ "foo": "bar" }';
const expectedTextOutput = '{ "foo": "bar" }';
const actualOutput = newBodyGraphQL(input);
expect(actualOutput).toEqual({
mimeType: CONTENT_TYPE_GRAPHQL,
text: expectedTextOutput,
});
});
});