feat: translate pm object handler into insomnia object instead of supporting alias - INS-3702 (#7253)

* feat: translate pm object handler into insomnia object instead of supporting alias

* chore: remove comment

* simplify implementation

---------

Co-authored-by: jackkav <jackkav@gmail.com>
This commit is contained in:
Hexxa 2024-04-09 21:13:51 +08:00 committed by GitHub
parent aaf76f755e
commit 7f767a8341
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 85 additions and 21 deletions

View File

@ -357,13 +357,13 @@ resources:
settingFollowRedirects: global
preRequestScript: |-
// set local
pm.variables.set('varStr', 'varStr');
pm.variables.set('varNum', 777);
pm.variables.set('varBool', true);
insomnia.variables.set('varStr', 'varStr');
insomnia.variables.set('varNum', 777);
insomnia.variables.set('varBool', true);
// has
pm.environment.set('varStr', pm.variables.get('varStr'));
pm.environment.set('varNum', pm.variables.get('varNum'));
pm.environment.set('varBool', pm.variables.get('varBool'));
insomnia.environment.set('varStr', insomnia.variables.get('varStr'));
insomnia.environment.set('varNum', insomnia.variables.get('varNum'));
insomnia.environment.set('varBool', insomnia.variables.get('varBool'));
body:
mimeType: "application/json"
text: |-
@ -773,23 +773,23 @@ resources:
settingFollowRedirects: global
preRequestScript: |-
insomnia.test('happy tests', () => {
pm.expect(200).to.eql(200);
pm.expect('uname').to.be.a('string');
pm.expect('a').to.have.lengthOf(1);
pm.expect('xxx_customer_id_yyy').to.include("customer_id");
pm.expect(201).to.be.oneOf([201,202]);
pm.expect(199).to.be.below(200);
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
pm.expect({a: 1, b: 2}).to.have.all.keys('a', 'b');
pm.expect({a: 1, b: 2}).to.have.any.keys('a', 'b');
pm.expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd');
pm.expect({a: 1}).to.have.property('a');
pm.expect({a: 1, b: 2}).to.be.a('object')
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', () => {
pm.expect(199).to.eql(200);
pm.expect(199).to.be.oneOf([201,202]);
insomnia.expect(199).to.eql(200);
insomnia.expect(199).to.be.oneOf([201,202]);
});
body:
mimeType: "application/json"

View File

@ -65,7 +65,7 @@ const runPreRequestScript = async (
'eval',
'_',
`
const $ = insomnia, pm = insomnia;
const $ = insomnia;
${script};
return insomnia;`
);

View File

@ -0,0 +1,46 @@
import { describe, expect, it } from '@jest/globals';
import { translateHandlersInScript } from '../postman';
describe('test translateHandlersInScript', () => {
[
{
script: "pm.environment.set('', '')",
expected: "insomnia.environment.set('', '')",
},
{
script: 'ipm.fn()',
expected: 'ipm.fn()',
},
{
script: 'h5pm.fn()',
expected: 'h5pm.fn()',
},
{
script: '$pm.fn()',
expected: '$pm.fn()',
},
{
script: '_pm.fn()',
expected: '_pm.fn()',
},
{
script: 'call(pm.environment.get("hehe"))',
expected: 'call(insomnia.environment.get("hehe"))',
},
{
script: `
console.log(pm.variables.get('score'), pm.variables.get('score2'));
console.log(pm.collectionVariables.get('score'));
console.log(pm.environment.get('score'));`,
expected: `
console.log(insomnia.variables.get('score'), insomnia.variables.get('score2'));
console.log(insomnia.collectionVariables.get('score'));
console.log(insomnia.environment.get('score'));`,
},
].forEach(testCase => {
it(`translate: ${testCase.script}`, () => {
expect(translateHandlersInScript(testCase.script)).toBe(testCase.expected);
});
});
});

View File

@ -69,6 +69,24 @@ const mapGrantTypeToInsomniaGrantType = (grantType: string) => {
return grantType;
};
export function translateHandlersInScript(scriptContent: string): string {
let translated = scriptContent;
// Replace pm.* with insomnia.*
// This is a simple implementation that only replaces the first instance of pm.* in the script
let offset = 0;
for (let i = 0; i < scriptContent.length - 2; i++) {
const isPM = scriptContent.slice(i, i + 3) === 'pm.';
const isPrevCharacterAlphaNumeric = i - 1 >= 0 && /[0-9a-zA-Z\_\$]/.test(scriptContent[i - 1]);
if (isPM && !isPrevCharacterAlphaNumeric) {
translated = translated.slice(0, i + offset) + 'insomnia.' + translated.slice(i + 3 + offset);
offset += 6;
}
}
return translated;
}
export class ImportPostman {
collection;
@ -132,7 +150,7 @@ export class ImportPostman {
(Array.isArray(scriptOrRows.exec) ? scriptOrRows.exec.join('\n') : scriptOrRows.exec) :
'';
return scriptContent;
return translateHandlersInScript(scriptContent);
};
importRequestItem = (