mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 06:39:48 +00:00
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:
parent
aaf76f755e
commit
7f767a8341
@ -357,13 +357,13 @@ resources:
|
|||||||
settingFollowRedirects: global
|
settingFollowRedirects: global
|
||||||
preRequestScript: |-
|
preRequestScript: |-
|
||||||
// set local
|
// set local
|
||||||
pm.variables.set('varStr', 'varStr');
|
insomnia.variables.set('varStr', 'varStr');
|
||||||
pm.variables.set('varNum', 777);
|
insomnia.variables.set('varNum', 777);
|
||||||
pm.variables.set('varBool', true);
|
insomnia.variables.set('varBool', true);
|
||||||
// has
|
// has
|
||||||
pm.environment.set('varStr', pm.variables.get('varStr'));
|
insomnia.environment.set('varStr', insomnia.variables.get('varStr'));
|
||||||
pm.environment.set('varNum', pm.variables.get('varNum'));
|
insomnia.environment.set('varNum', insomnia.variables.get('varNum'));
|
||||||
pm.environment.set('varBool', pm.variables.get('varBool'));
|
insomnia.environment.set('varBool', insomnia.variables.get('varBool'));
|
||||||
body:
|
body:
|
||||||
mimeType: "application/json"
|
mimeType: "application/json"
|
||||||
text: |-
|
text: |-
|
||||||
@ -773,23 +773,23 @@ resources:
|
|||||||
settingFollowRedirects: global
|
settingFollowRedirects: global
|
||||||
preRequestScript: |-
|
preRequestScript: |-
|
||||||
insomnia.test('happy tests', () => {
|
insomnia.test('happy tests', () => {
|
||||||
pm.expect(200).to.eql(200);
|
insomnia.expect(200).to.eql(200);
|
||||||
pm.expect('uname').to.be.a('string');
|
insomnia.expect('uname').to.be.a('string');
|
||||||
pm.expect('a').to.have.lengthOf(1);
|
insomnia.expect('a').to.have.lengthOf(1);
|
||||||
pm.expect('xxx_customer_id_yyy').to.include("customer_id");
|
insomnia.expect('xxx_customer_id_yyy').to.include("customer_id");
|
||||||
pm.expect(201).to.be.oneOf([201,202]);
|
insomnia.expect(201).to.be.oneOf([201,202]);
|
||||||
pm.expect(199).to.be.below(200);
|
insomnia.expect(199).to.be.below(200);
|
||||||
// test objects
|
// test objects
|
||||||
pm.expect({a: 1, b: 2}).to.have.all.keys('a', 'b');
|
insomnia.expect({a: 1, b: 2}).to.have.all.keys('a', 'b');
|
||||||
pm.expect({a: 1, b: 2}).to.have.any.keys('a', 'b');
|
insomnia.expect({a: 1, b: 2}).to.have.any.keys('a', 'b');
|
||||||
pm.expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd');
|
insomnia.expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd');
|
||||||
pm.expect({a: 1}).to.have.property('a');
|
insomnia.expect({a: 1}).to.have.property('a');
|
||||||
pm.expect({a: 1, b: 2}).to.be.a('object')
|
insomnia.expect({a: 1, b: 2}).to.be.a('object')
|
||||||
.that.has.all.keys('a', 'b');
|
.that.has.all.keys('a', 'b');
|
||||||
});
|
});
|
||||||
insomnia.test('unhappy tests', () => {
|
insomnia.test('unhappy tests', () => {
|
||||||
pm.expect(199).to.eql(200);
|
insomnia.expect(199).to.eql(200);
|
||||||
pm.expect(199).to.be.oneOf([201,202]);
|
insomnia.expect(199).to.be.oneOf([201,202]);
|
||||||
});
|
});
|
||||||
body:
|
body:
|
||||||
mimeType: "application/json"
|
mimeType: "application/json"
|
||||||
|
@ -65,7 +65,7 @@ const runPreRequestScript = async (
|
|||||||
'eval',
|
'eval',
|
||||||
'_',
|
'_',
|
||||||
`
|
`
|
||||||
const $ = insomnia, pm = insomnia;
|
const $ = insomnia;
|
||||||
${script};
|
${script};
|
||||||
return insomnia;`
|
return insomnia;`
|
||||||
);
|
);
|
||||||
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -69,6 +69,24 @@ const mapGrantTypeToInsomniaGrantType = (grantType: string) => {
|
|||||||
return grantType;
|
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 {
|
export class ImportPostman {
|
||||||
collection;
|
collection;
|
||||||
|
|
||||||
@ -132,7 +150,7 @@ export class ImportPostman {
|
|||||||
(Array.isArray(scriptOrRows.exec) ? scriptOrRows.exec.join('\n') : scriptOrRows.exec) :
|
(Array.isArray(scriptOrRows.exec) ? scriptOrRows.exec.join('\n') : scriptOrRows.exec) :
|
||||||
'';
|
'';
|
||||||
|
|
||||||
return scriptContent;
|
return translateHandlersInScript(scriptContent);
|
||||||
};
|
};
|
||||||
|
|
||||||
importRequestItem = (
|
importRequestItem = (
|
||||||
|
Loading…
Reference in New Issue
Block a user