Skip to content

Commit

Permalink
test: add tests for post-request scripts and post-request scripts - I…
Browse files Browse the repository at this point in the history
…NS-3786 (#7331)

* test: add some tests for post-req script and script engine

* fix: incorrect script type
  • Loading branch information
ihexxa authored and jackkav committed May 22, 2024
1 parent 301a9d1 commit 910bf33
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 2 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: Post-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
postRequestScript: |-
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
postRequestScript: |-
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
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('Post-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
Expand Up @@ -96,3 +96,37 @@ test.describe('test hidden window handling', async () => {
await expect(statusTag).toContainText('200 OK');
});
});

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 910bf33

Please sign in to comment.