Skip to content

Commit

Permalink
test: add some tests for post-req script and script engine
Browse files Browse the repository at this point in the history
  • Loading branch information
ihexxa committed Apr 29, 2024
1 parent ab97c3b commit 76ba136
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 3 deletions.
109 changes: 109 additions & 0 deletions packages/insomnia-smoke-test/fixtures/post-request-collection.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
_type: export
__export_format: 4
__export_date: 2024-02-13T07:27:17.322Z
__export_source: insomnia.desktop.app:v8.6.1
resources:
- _id: wrk_6b9b8455fd784462ae19cd51d7156f86
parentId: null
modified: 1707808692801
created: 1707808692801
name: Pre-request Scripts
description: ""
scope: collection
_type: workspace
- _id: req_244fe815da6c4342a17f0cfd98cf648c
parentId: wrk_6b9b8455fd784462ae19cd51d7156f86
modified: 1707809218855
created: 1707808697304
url: http://127.0.0.1:4010/echo
name: tests with expect and test
description: ""
method: POST
preRequestScript: |-
insomnia.test('happy tests', () => {
insomnia.expect(200).to.eql(200);
insomnia.expect('uname').to.be.a('string');
insomnia.expect('a').to.have.lengthOf(1);
insomnia.expect('xxx_customer_id_yyy').to.include("customer_id");
insomnia.expect(201).to.be.oneOf([201,202]);
insomnia.expect(199).to.be.below(200);
// test objects
insomnia.expect({a: 1, b: 2}).to.have.all.keys('a', 'b');
insomnia.expect({a: 1, b: 2}).to.have.any.keys('a', 'b');
insomnia.expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd');
insomnia.expect({a: 1}).to.have.property('a');
insomnia.expect({a: 1, b: 2}).to.be.a('object')
.that.has.all.keys('a', 'b');
});
insomnia.test('unhappy tests', () => {
insomnia.expect(199).to.eql(200);
insomnia.expect(199).to.be.oneOf([201,202]);
});
body:
mimeType: "application/json"
text: |-
{}
parameters: []
headers:
- name: 'Content-Type'
value: 'application/json'
authentication: {}
metaSortKey: -1707809028499
isPrivate: false
pathParameters: []
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
_type: request
- _id: req_244fe815da6c4342a17f0cfd98cf6401
parentId: wrk_6b9b8455fd784462ae19cd51d7156f86
modified: 1707809218855
created: 1707808697304
url: http://127.0.0.1:4010/echo
name: persist environments
description: ""
method: POST
preRequestScript: |-
insomnia.environment.set('__fromPostScript', 'environment');
insomnia.baseEnvironment.set('__fromPostScript1', 'baseEnvironment');
insomnia.collectionVariables.set('__fromPostScript2', 'collection');
body:
mimeType: "application/json"
text: |-
{}
parameters: []
headers:
- name: 'Content-Type'
value: 'application/json'
authentication: {}
metaSortKey: -1707809028499
isPrivate: false
pathParameters: []
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
_type: request
- _id: env_f9ef1d097c5e00986051fcb4f7a921eea1a86916
parentId: wrk_6b9b8455fd784462ae19cd51d7156f86
modified: 1707808692805
created: 1707808692805
name: Base Environment
data: {}
dataPropertyOrder: null
color: null
isPrivate: false
metaSortKey: 1707808692805
_type: environment
- _id: jar_f9ef1d097c5e00986051fcb4f7a921eea1a86916
parentId: wrk_6b9b8455fd784462ae19cd51d7156f86
modified: 1707808692807
created: 1707808692807
name: Default Jar
cookies: []
_type: cookie_jar
56 changes: 56 additions & 0 deletions packages/insomnia-smoke-test/fixtures/pre-request-collection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1114,3 +1114,59 @@ resources:
text: |-
{}
_type: request
- _id: req_89dade2ee9ee42fbb22d588783a9df17
parentId: fld_01de564274824ecaad272330339ea6b2
modified: 1636707449231
created: 1636141014552
url: http://127.0.0.1:4010/echo
name: infinite loop
description: ""
method: POST
parameters: []
headers:
- name: 'Content-Type'
value: 'application/json'
authentication: {}
metaSortKey: -1636141014553
isPrivate: false
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
preRequestScript: |-
while(true) {}
body:
mimeType: "application/json"
text: |-
{}
_type: request
- _id: req_89dade2ee9ee42fbb22d588783a9df18
parentId: fld_01de564274824ecaad272330339ea6b2
modified: 1636707449231
created: 1636141014552
url: http://127.0.0.1:4010/echo
name: simple log
description: ""
method: POST
parameters: []
headers:
- name: 'Content-Type'
value: 'application/json'
authentication: {}
metaSortKey: -1636141014553
isPrivate: false
settingStoreCookies: true
settingSendCookies: true
settingDisableRenderRequestBody: false
settingEncodeUrl: true
settingRebuildPath: true
settingFollowRedirects: global
preRequestScript: |-
console.log('back to normal');
body:
mimeType: "application/json"
text: |-
{}
_type: request
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expect } from '@playwright/test';

import { loadFixture } from '../../playwright/paths';
import { test } from '../../playwright/test';;

test.describe('post-request features tests', async () => {
test.slow(process.platform === 'darwin' || process.platform === 'win32', 'Slow app start on these platforms');

test.beforeEach(async ({ app, page }) => {
const text = await loadFixture('post-request-collection.yaml');
await app.evaluate(async ({ clipboard }, text) => clipboard.writeText(text), text);

await page.getByRole('button', { name: 'Create in project' }).click();
await page.getByRole('menuitemradio', { name: 'Import' }).click();
await page.locator('[data-test-id="import-from-clipboard"]').click();
await page.getByRole('button', { name: 'Scan' }).click();
await page.getByRole('dialog').getByRole('button', { name: 'Import' }).click();

await page.getByLabel('Pre-request Scripts').click();
});

test('insomnia.test and insomnia.expect can work together', async ({ page }) => {
const responsePane = page.getByTestId('response-pane');

await page.getByLabel('Request Collection').getByTestId('tests with expect and test').press('Enter');

// send
await page.getByTestId('request-pane').getByRole('button', { name: 'Send' }).click();

// verify
await page.getByRole('tab', { name: 'Timeline' }).click();

await expect(responsePane).toContainText('✓ happy tests');
await expect(responsePane).toContainText('✕ unhappy tests: AssertionError: expected 199 to deeply equal 200');
});

test('environment and baseEnvironment can be persisted', async ({ page }) => {
const statusTag = page.locator('[data-testid="response-status-tag"]:visible');
await page.getByLabel('Request Collection').getByTestId('persist environments').press('Enter');

// send
await page.getByTestId('request-pane').getByRole('button', { name: 'Send' }).click();

// verify response
await page.waitForSelector('[data-testid="response-status-tag"]:visible');
await expect(statusTag).toContainText('200 OK');

// verify persisted environment
await page.getByLabel('Manage Environments').click();
const responseBody = page.getByRole('dialog').getByTestId('CodeEditor').locator('.CodeMirror-line');
const rows = await responseBody.allInnerTexts();
const bodyJson = JSON.parse(rows.join(' '));

expect(bodyJson).toEqual({
// no environment is selected so the environment value is not persisted
'__fromPostScript1': 'baseEnvironment',
'__fromPostScript2': 'collection',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ test.describe('pre-request features tests', async () => {
// verify
await page.getByRole('tab', { name: 'Timeline' }).click();

await expect(responsePane).toContainText('✓ happy tests'); // original proxy
await expect(responsePane).toContainText('✕ unhappy tests: AssertionError: expected 199 to deeply equal 200'); // updated proxy
await expect(responsePane).toContainText('✓ happy tests');
await expect(responsePane).toContainText('✕ unhappy tests: AssertionError: expected 199 to deeply equal 200');
});

test('environment and baseEnvironment can be persisted', async ({ page }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect } from '@playwright/test';

import { loadFixture } from '../../playwright/paths';
import { test } from '../../playwright/test';

Expand Down Expand Up @@ -55,6 +57,40 @@ test('handle hidden browser window getting closed', async ({ app, page }) => {
const hiddenWindow = windows[1];
hiddenWindow.close();
await page.getByRole('button', { name: 'Send' }).click();
// as the hidden window is restarted, it should not show "Timeout: Hidden browser window is not responding"
// as the hidden window is restarted, it should not show "Timeout: Hidden browser window is not responding", which is tested below
await page.getByText('Timeout: Pre-request script took too long').click();
});

test('window should be restarted if it hangs', async ({ app, page }) => {
test.slow(process.platform === 'darwin' || process.platform === 'win32', 'Slow app start on these platforms');

// load collection
const text = await loadFixture('pre-request-collection.yaml');
await app.evaluate(async ({ clipboard }, text) => clipboard.writeText(text), text);

await page.getByRole('button', { name: 'Create in project' }).click();
await page.getByRole('menuitemradio', { name: 'Import' }).click();
await page.locator('[data-test-id="import-from-clipboard"]').click();
await page.getByRole('button', { name: 'Scan' }).click();
await page.getByRole('dialog').getByRole('button', { name: 'Import' }).click();

// update timeout
await page.getByTestId('settings-button').click();
await page.getByLabel('Request timeout (ms)').fill('100');
await page.getByRole('button', { name: '' }).click();

// send the request with infinite loop script
await page.getByText('Pre-request Scripts').click();
await page.getByLabel('Request Collection').getByTestId('infinite loop').press('Enter');
await page.getByTestId('request-pane').getByRole('button', { name: 'Send' }).click();
await page.getByText('Timeout: Hidden browser window is not responding').click();

// send the another script with normal script
await page.getByLabel('Request Collection').getByTestId('simple log').press('Enter');
await page.getByTestId('request-pane').getByRole('button', { name: 'Send' }).click();

// it should still work
const statusTag = page.locator('[data-testid="response-status-tag"]:visible');
await page.waitForSelector('[data-testid="response-status-tag"]:visible');
await expect(statusTag).toContainText('200 OK');
});

0 comments on commit 76ba136

Please sign in to comment.