From d435d256b384aa357892bf14b4d2ca7a92159f6d Mon Sep 17 00:00:00 2001 From: Filipe Freire Date: Tue, 2 Jul 2024 10:05:03 +0100 Subject: [PATCH] doc: after-response scripting [INS-4085] (#217) * doc: after-response scripting [INS-4085] * comment out docker builds --- .github/workflows/docker.yml | 17 ++--- docs/_data/main-nav.yaml | 6 +- docs/insomnia/after-response-script.md | 93 ++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 docs/insomnia/after-response-script.md diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 643df78..6e7a7bb 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,14 +1,15 @@ name: Docker on: - schedule: - - cron: '45 19 * * *' - push: - branches: [ "main" ] - # Publish semver tags as releases. - tags: [ 'v*.*.*' ] - pull_request: - branches: [ "main" ] + workflow_dispatch: + # schedule: + # - cron: '45 19 * * *' + # push: + # branches: [ "main" ] + # # Publish semver tags as releases. + # tags: [ 'v*.*.*' ] + # pull_request: + # branches: [ "main" ] env: # Use docker.io for Docker Hub if empty diff --git a/docs/_data/main-nav.yaml b/docs/_data/main-nav.yaml index 1910b91..cd1f2f8 100644 --- a/docs/_data/main-nav.yaml +++ b/docs/_data/main-nav.yaml @@ -202,11 +202,13 @@ toc: url: /inso-cli/provenance/verify-binary-provenance - title: Continuous Integration url: /inso-cli/continuous-integration - - title: Pre-request Script - collapse-id: pre-request-script + - title: Pre-request and After-Response Scripts + collapse-id: pre-request-after-response-scripts items: - title: Insomnia Pre-request Script Overview url: /insomnia/pre-request-script + - title: Insomnia After-Response Script Overview + url: /insomnia/after-response-script - title: API Mocking collapse-id: api-mocking items: diff --git a/docs/insomnia/after-response-script.md b/docs/insomnia/after-response-script.md new file mode 100644 index 0000000..35c688c --- /dev/null +++ b/docs/insomnia/after-response-script.md @@ -0,0 +1,93 @@ +--- +layout: article-detail +title: Insomnia After-Response Script Overview +category: "Requests and Responses" +category-url: requests-and-responses +--- + +Starting with the [release of Insomnia 9.3.0](https://konghq.com/blog/product-releases/insomnia-9-3-ga), the concept of after-response scripts (also known as post-request scripts or test scripts in other applications) has been introduced. These scripts are executed following the completion of a request. + +In after-response scripts, you can perform similar scripting activities as you would in pre-request scripts. For more details on pre-request scripts, see the documentation [here](https://docs.insomnia.rest/insomnia/pre-request-script). + +These scripts can be utilized for: + +- Performing tests and assertions on the response. +- Storing certain parts of the response into environment variables. +- Any other script you would apply in [pre-request scripts](https://docs.insomnia.rest/insomnia/pre-request-script). + +For tests and assertions you can use `insomnia.test` and `insomnia.expect` functions: + +```js +insomnia.test('Check if status is 200', () => { + insomnia.expect(insomnia.response.status).to.eql(200); +}); +``` + +Here are some examples `insomnia.expect` calls you can make: + +```js +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); + +// Testing objects +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.an('object').that.has.all.keys('a', 'b'); +``` + +You can also access various response attributes: + +```js +const status = insomnia.response.status; +const responseTime = insomnia.response.responseTime; +const jsonBody = insomnia.response.json(); +const textBody = insomnia.response.text(); +const header = insomnia.response.headers.find(header => header.key === 'Content-Type'); +const cookies = insomnia.response.cookies.toObject(); +``` + +Example of storing response body parts into the environment: + +```js +insomnia.environment.set("whole_response", insomnia.response.json()); + +// Storing a specific field +const jsonBody = insomnia.response.json(); +insomnia.environment.set("specific_field", jsonBody.specific_field); +``` + +Similar to pre-request scripts, after-response scripts allow for actions such as importing libraries, setting delays, and sending requests at the end of the script. + +```js +const atob = require('atob'); + +await new Promise((resolve) => setTimeout(resolve, 1000)); + +const resp = await new Promise((resolve, reject) => { + insomnia.sendRequest( + 'https://mock.insomnia.rest', + (err, resp) => { + err ? reject(err) : resolve(resp); + } + ); +}); +``` + +## Migrating from Postman post-request scripts + +After-response scripts exported from Postman should also work when imported into Insomnia. + +There are some differences to be aware about: + +- Top level awaits are allowed. +- Global environment `insomnia.globals` and iteration data `insomnia.iterationData` are not supported yet. +- `CollectionVariables` is mapped to `baseEnvironment` in Insomnia. +- Deprecated `postman` interfaces are not supported yet, such as `postman.setEnvironmentVariable`. + +If you notice any incompatibility issues, please report these by create a [new issue on Github](https://github.com/kong/insomnia/issues).