fix: persist script's changes on environment and baseEnvironment (#7306)

* fix: persist script's changes on environment and baseEnvironment

* fix: avoid persisting effect when no environment is activated

* fix: comparing mutated env with baseEnvironment

* Update packages/insomnia/src/ui/routes/request.tsx

---------

Co-authored-by: Jack Kavanagh <jackkav@gmail.com>
This commit is contained in:
Hexxa
2024-04-24 18:04:37 +08:00
committed by GitHub
parent 719e3ca692
commit 8e00a2e27e
3 changed files with 75 additions and 0 deletions

View File

@@ -1047,3 +1047,33 @@ resources:
"asyncTaskDone": {{ _.asyncTaskDone}}
}
_type: request
- _id: req_89dade2ee9ee42fbb22d588783a9df15
parentId: fld_01de564274824ecaad272330339ea6b2
modified: 1636707449231
created: 1636141014552
url: http://127.0.0.1:4010/echo
name: persist environment
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: |-
insomnia.environment.set('__fromScript', 'environment');
insomnia.baseEnvironment.set('__fromScript1', 'baseEnvironment');
insomnia.collectionVariables.set('__fromScript2', 'collection');
body:
mimeType: "application/json"
text: |-
{}
_type: request

View File

@@ -408,6 +408,30 @@ test.describe('pre-request features tests', async () => {
await expect(responsePane).toContainText('✓ happy tests'); // original proxy
await expect(responsePane).toContainText('✕ unhappy tests: AssertionError: expected 199 to deeply equal 200'); // updated proxy
});
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 environment').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
'__fromScript1': 'baseEnvironment',
'__fromScript2': 'collection',
});
});
});
test.describe('unhappy paths', async () => {

View File

@@ -401,6 +401,27 @@ export const sendAction: ActionFunction = async ({ request, params }) => {
{ cookies: mutatedContext.cookieJar.cookies },
);
}
// when base environment is activated, `mutatedContext.environment` points to it
const isActiveEnvironmentBase = mutatedContext.environment?._id === baseEnvironment._id;
const hasEnvironmentAndIsNotBase = mutatedContext.environment && !isActiveEnvironmentBase;
if (hasEnvironmentAndIsNotBase) {
await models.environment.update(
environment,
{
data: mutatedContext.environment.data,
dataPropertyOrder: mutatedContext.environment.dataPropertyOrder,
}
);
}
if (mutatedContext.baseEnvironment) {
await models.environment.update(
baseEnvironment,
{
data: mutatedContext.baseEnvironment.data,
dataPropertyOrder: mutatedContext.baseEnvironment.dataPropertyOrder,
}
);
}
}
const renderedResult = await tryToInterpolateRequest(