Skip to content

feat: enable insomnia.sendRequest in pre-request scripting - INS-3379#7147

Merged
ihexxa merged 10 commits intodevelopfrom
pre-script-send-req-2
Mar 15, 2024
Merged

feat: enable insomnia.sendRequest in pre-request scripting - INS-3379#7147
ihexxa merged 10 commits intodevelopfrom
pre-script-send-req-2

Conversation

@ihexxa
Copy link
Contributor

@ihexxa ihexxa commented Mar 6, 2024

Please review this PR (#7145) firstly as current one is based on it

Changes:

  • enabled insomnia.sendRequest
  • added tests

How to test

Run this script and check if it runs well, basically it sends several requests with different content types:

            const rawReq = {
                url: 'https://httpbin.org/anything',
                method: 'POST',
                header: {
                    'Content-Type': 'text/plain',
                },
                body: {
                    mode: 'raw',
                    raw: 'rawContent',
                },
            };
            const urlencodedReq = {
                url: 'https://httpbin.org/anything',
                method: 'POST',
                header: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: {
                    mode: 'urlencoded',
                    urlencoded: [
                        { key: 'k1', value: 'v1' },
                        { key: 'k2', value: 'v2' },
                    ],
                },
            };
            const gqlReq = {
                url: 'https://httpbin.org/anything',
                method: 'POST',
                header: {
                    'Content-Type': 'application/graphql',
                },
                body: {
                    mode: 'graphql',
                    graphql: {
                        query: 'query',
                        operationName: 'operation',
                        variables: 'var',
                    },
                },
            };
            const fileReq = {
                url: 'https://httpbin.org/anything',
                method: 'POST',
                header: {
                    'Content-Type': 'application/octet-stream',
                },
                body: {
                    mode: 'file',
                    file: "${getFixturePath('files/rawfile.txt')}",
                },
            };
            const formdataReq = {
                url: 'https://httpbin.org/anything',
                method: 'POST',
                header: {
                    // 'Content-Type': 'multipart/form-data',
                },
                body: {
                    mode: 'formdata',
                    formdata: [
                        { key: 'k1', type: 'text', value: 'v1' },
                        { key: 'k2', type: 'file', value: "${getFixturePath('files/rawfile.txt')}" },
                    ],
                },
            };
            const promises = [rawReq, urlencodedReq, gqlReq, fileReq, formdataReq].map(req => {
                return new Promise((resolve, reject) => {
                    insomnia.sendRequest(
                        req,
                        (err, resp) => {
                            if (err != null) {
                                reject(err);
                            } else {
                                resolve(resp);
                            }
                        }
                    );
                });
            });
            // send request
            const resps = await Promise.all(promises);
            // set envs
            insomnia.environment.set('rawBody', resps[0].body);
            insomnia.environment.set('urlencodedBody', resps[1].body);
            insomnia.environment.set('gqlBody', resps[2].body);
            insomnia.environment.set('fileBody', resps[3].body);
            insomnia.environment.set('formdataBody', resps[4].body);

@ihexxa ihexxa self-assigned this Mar 6, 2024
@ihexxa ihexxa changed the title feat: enable insomnia.sendRequest in pre-request scripting feat: enable insomnia.sendRequest in pre-request scripting - INS-3379 Mar 6, 2024
@ihexxa ihexxa force-pushed the pre-script-send-req-2 branch from c0dbb3f to 919765d Compare March 12, 2024 02:45
@jackkav jackkav force-pushed the pre-script-send-req-2 branch from 1ff7c34 to 91ffe8d Compare March 14, 2024 09:46
Copy link
Contributor

@jackkav jackkav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create tickets for the todos in here so we can split the work if necessary.

@ihexxa
Copy link
Contributor Author

ihexxa commented Mar 15, 2024

@ihexxa ihexxa merged commit 7bd2441 into develop Mar 15, 2024
@ihexxa ihexxa deleted the pre-script-send-req-2 branch March 15, 2024 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants