From 977cf2bef2103df3d2d3b99d56576ef36375a9ff Mon Sep 17 00:00:00 2001 From: George He Date: Fri, 2 Feb 2024 11:23:00 +0800 Subject: [PATCH] test: bring test cases back --- packages/insomnia-smoke-test/server/index.ts | 7 +- .../tests/smoke/pre-request-script-ui.test.ts | 237 +++++++++--------- .../sdk-objects/send-req.ts | 2 +- 3 files changed, 121 insertions(+), 125 deletions(-) diff --git a/packages/insomnia-smoke-test/server/index.ts b/packages/insomnia-smoke-test/server/index.ts index 73c11715c9..14ed86eee6 100644 --- a/packages/insomnia-smoke-test/server/index.ts +++ b/packages/insomnia-smoke-test/server/index.ts @@ -28,14 +28,17 @@ app.get('/pets/:id', (req, res) => { res.status(200).send({ id: req.params.id }); }); -app.get('/echo', jsonParser, async (req, res) => { +async function echoHandler(req: any, res: any) { res.status(200).send({ method: req.method, headers: req.headers, data: req.body, cookies: req.cookies, }); -}); +}; + +app.get('/echo', jsonParser, echoHandler); +app.post('/echo', jsonParser, echoHandler); app.get('/builds/check/*', (_req, res) => { res.status(200).send({ diff --git a/packages/insomnia-smoke-test/tests/smoke/pre-request-script-ui.test.ts b/packages/insomnia-smoke-test/tests/smoke/pre-request-script-ui.test.ts index 06a3b9e592..954500c65a 100644 --- a/packages/insomnia-smoke-test/tests/smoke/pre-request-script-ui.test.ts +++ b/packages/insomnia-smoke-test/tests/smoke/pre-request-script-ui.test.ts @@ -111,20 +111,32 @@ test.describe('pre-request UI tests', async () => { name: 'sendRequest API Request URL mode', preReqScript: ` const postRequest = { - url: 'https://postman-echo.com/post', + url: 'https://httpbin.org/anything', method: 'POST', header: { - 'Content-Type': 'application/json', - 'X-Foo': 'bar' + 'Content-Type': 'application/json', + 'X-Foo': 'bar', + 'Cookie': 'cookie1=value1; cookie2=value2;', }, body: { - mode: 'raw', - raw: JSON.stringify({ key: 'this is json' }) + mode: 'raw', + raw: JSON.stringify({ key: 'this is json' }) } }; - pm.sendRequest(postRequest, (error, response) => { - console.log(error ? error : response.json()); + // send request + const resp = await new Promise((resolve, reject) => { + insomnia.sendRequest( + postRequest, + (err, resp) => { + if (err != null) { + reject(err); + } else { + resolve(resp); + } + } + ); }); + // set envs insomnia.environment.set('body', resp.body); // insomnia.environment.set('stream', resp.stream); insomnia.environment.set('cookies', JSON.stringify(resp.cookies.filter(_ => true, {}))); @@ -150,121 +162,102 @@ test.describe('pre-request UI tests', async () => { expect(bodyJson.data.status).toEqual('OK'); }, }, - // { - // name: 'require url module', - // preReqScript: ` - // const URL = require('url'); - // const url = new URL('https://user:pwd@insomnia.com:6666/p1?q1=a&q2=b#hashcontent'); - // insomnia.environment.set('hash', "#hashcontent"); - // insomnia.environment.set('host', "insomnia.com:6666"); - // insomnia.environment.set('hostname', "insomnia.com"); - // insomnia.environment.set('href', "https://user:pwd@insomnia.com:6666/p1?q1=a&q2=b#hashcontent"); - // insomnia.environment.set('origin', "https://insomnia.com:6666"); - // insomnia.environment.set('password', "pwd"); - // insomnia.environment.set('pathname', "/p1"); - // insomnia.environment.set('port', "6666"); - // insomnia.environment.set('protocol', "https:"); - // insomnia.environment.set('search', "?q1=a&q2=b"); - // insomnia.environment.set('username', "user"); - // insomnia.environment.set('seachParam', url.searchParams.toString()); - // `, - // body: `{ - // "hash": "{{ _.hash }}", - // "host": "{{ _.host }}", - // "hostname": "{{ _.hostname }}", - // "href": "{{ _.href }}", - // "origin": "{{ _.origin }}", - // "password": "{{ _.password }}", - // "pathname": "{{ _.pathname }}", - // "port": "{{ _.port }}", - // "protocol": "{{ _.protocol }}", - // "search": "{{ _.search }}", - // "username": "{{ _.username }}", - // "seachParam": "{{ _.seachParam }}" - // }`, - // expectedResponse: { - // method: 'GET', - // headers: { - // host: '127.0.0.1:4010', - // 'user-agent': 'insomnia/8.5.1', - // 'content-type': 'application/json', - // 'accept': '*/*', - // 'content-length': '532', - // }, - // data: { - // hash: '#hashcontent', - // host: 'insomnia.com:6666', - // hostname: 'insomnia.com', - // href: 'https://user:pwd@insomnia.com:6666/p1?q1=a&q2=b#hashcontent', - // origin: 'https://insomnia.com:6666', - // password: 'pwd', - // pathname: '/p1', - // port: '6666', - // protocol: 'https:', - // search: '?q1=a&q2=b', - // username: 'user', - // seachParam: 'q1=a&q2=b', - // }, - // cookies: {}, - // }, - // }, - // { - // name: 'require uuid module', - // preReqScript: ` - // const uuid = require('uuid'); - // insomnia.environment.set('uuid', uuid.NIL); - // `, - // body: `{ - // "uuid": "{{ _.uuid}}" - // }`, - // expectedResponse: { - // method: 'GET', - // headers: { - // host: '127.0.0.1:4010', - // 'user-agent': 'insomnia/8.5.1', - // 'content-type': 'application/json', - // 'accept': '*/*', - // 'content-length': '70', - // }, - // data: { - // uuid: '00000000-0000-0000-0000-000000000000', - // }, - // cookies: {}, - // }, - // }, - // { - // name: 'require collection url object ', - // preReqScript: ` - // const insoCollection = require('insomnia-collection'); - // const Url = require('postman-collection').Url; - // const { QueryParam, Variable } = require('postman-collection'); - // const url = new Url({ - // auth: { - // username: 'usernameValue', - // password: 'passwordValue', - // }, - // hash: 'hashValue', - // host: ['hostValue', 'com'], - // path: ['pathLevel1', 'pathLevel2'], - // port: '777', - // protocol: 'https:', - // query: [ - // new QueryParam({ key: 'key1', value: 'value1' }), - // new QueryParam({ key: 'key2', value: 'value2' }), - // ], - // variables: [ - // new Variable({ key: 'varKey', value: 'varValue' }), - // ], - // }); - // insomnia.environment.set('url', url.toString()); - // `, - // body: `{ - // "url": "{{ _.url }}" - // }`, - // expectedBody: { - // url: 'https://usernameValue:passwordValue@hostvalue.com:777/pathLevel1/pathLevel2?key1=value1&key2=value2#hashValue', - // }, - // }, + { + name: 'require the url module', + preReqScript: ` + const { URL } = require('url'); + const url = new URL('https://user:pwd@insomnia.com:6666/p1?q1=a&q2=b#hashcontent'); + insomnia.environment.set('hash', "#hashcontent"); + insomnia.environment.set('host', "insomnia.com:6666"); + insomnia.environment.set('hostname', "insomnia.com"); + insomnia.environment.set('href', "https://user:pwd@insomnia.com:6666/p1?q1=a&q2=b#hashcontent"); + insomnia.environment.set('origin', "https://insomnia.com:6666"); + insomnia.environment.set('password', "pwd"); + insomnia.environment.set('pathname', "/p1"); + insomnia.environment.set('port', "6666"); + insomnia.environment.set('protocol', "https:"); + insomnia.environment.set('search', "?q1=a&q2=b"); + insomnia.environment.set('username', "user"); + insomnia.environment.set('seachParam', url.searchParams.toString()); + `, + body: `{ + "hash": "{{ _.hash }}", + "host": "{{ _.host }}", + "hostname": "{{ _.hostname }}", + "href": "{{ _.href }}", + "origin": "{{ _.origin }}", + "password": "{{ _.password }}", + "pathname": "{{ _.pathname }}", + "port": "{{ _.port }}", + "protocol": "{{ _.protocol }}", + "search": "{{ _.search }}", + "username": "{{ _.username }}", + "seachParam": "{{ _.seachParam }}" + }`, + customVerify: (bodyJson: any) => { + expect(bodyJson.data).toEqual({ + hash: '#hashcontent', + host: 'insomnia.com:6666', + hostname: 'insomnia.com', + href: 'https://user:pwd@insomnia.com:6666/p1?q1=a&q2=b#hashcontent', + origin: 'https://insomnia.com:6666', + password: 'pwd', + pathname: '/p1', + port: '6666', + protocol: 'https:', + search: '?q1=a&q2=b', + username: 'user', + seachParam: 'q1=a&q2=b', + }); + }, + }, + { + name: 'require the uuid module', + preReqScript: ` + const uuid = require('uuid'); + insomnia.environment.set('uuid', uuid.NIL); + `, + body: `{ + "uuid": "{{ _.uuid}}" + }`, + expectedBody: { + uuid: '00000000-0000-0000-0000-000000000000' + }, + }, + { + name: 'require the collection url object ', + preReqScript: ` + const insoCollection = require('insomnia-collection'); + const Url = require('postman-collection').Url; + const { QueryParam, Variable } = require('postman-collection'); + // create an Url + const url = new Url({ + auth: { + username: 'usernameValue', + password: 'passwordValue', + }, + hash: 'hashValue', + host: ['hostValue', 'com'], + path: ['pathLevel1', 'pathLevel2'], + port: '777', + protocol: 'https:', + query: [ + new QueryParam({ key: 'key1', value: 'value1' }), + new QueryParam({ key: 'key2', value: 'value2' }), + ], + variables: [ + new Variable({ key: 'varKey', value: 'varValue' }), + ], + }); + insomnia.environment.set('url', url.toString()); + `, + body: `{ + "url": "{{ _.url }}" + }`, + expectedBody: { + url: 'https://usernameValue:passwordValue@hostvalue.com:777/pathLevel1/pathLevel2?key1=value1&key2=value2#hashValue', + }, + }, ]; for (let i = 0; i < testCases.length; i++) { diff --git a/packages/insomnia/src/renderers/hidden-browser-window/sdk-objects/send-req.ts b/packages/insomnia/src/renderers/hidden-browser-window/sdk-objects/send-req.ts index da2ec1ddcf..fa0214f896 100644 --- a/packages/insomnia/src/renderers/hidden-browser-window/sdk-objects/send-req.ts +++ b/packages/insomnia/src/renderers/hidden-browser-window/sdk-objects/send-req.ts @@ -381,7 +381,7 @@ function fromRequestToCurlOptions(req: string | Request | RequestOptions, settin body: { mimeType: undefined, method: finalReq.method, - text: finalReq.body, + text: finalReq.body?.toString(), }, // TODO: use value from headers authentication: transformAuthentication(finalReq.auth), settingFollowRedirects: settings.followRedirects ? 'on' : 'off',