diff --git a/README.md b/README.md index 46a1742..58404b0 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,9 @@ This will add a new command `cy.api` for making API requests. |---------|---------------|-------------| | CYPRESS_API_MESSAGES | true | Show and make call to api server logs | | CYPRESS_API_SHOW_CREDENTIALS | false | Show authentication password | +| CYPRESS_API_SHOW_GENUINE_REQUEST_OPTIONS_ONLY | false | Show only genuinely sent request options | -By default `cy.api` print response in the browser. To have the same behaviour as `cy.request` and use `cy.visit` normally, you need to desactivate `apiDisplayRequest` : +By default `cy.api` print response in the browser. To have the same behaviour as `cy.request` and use `cy.visit` normally, you need to deactivate `apiDisplayRequest` : ```js it('my test without displaying request', { apiDisplayRequest: false }, () => { diff --git a/cypress/e2e/hides-non-requestoptions.cy.ts b/cypress/e2e/hides-non-requestoptions.cy.ts new file mode 100644 index 0000000..de06956 --- /dev/null +++ b/cypress/e2e/hides-non-requestoptions.cy.ts @@ -0,0 +1,37 @@ +// loads definition for the custom "cy.api" command +/// + +describe('hide non RequestOptions', () => { + + it("hides non RequestOptions if env var `API_SHOW_GENUINE_REQUEST_OPTIONS_ONLY` is set to true", { + env: { + API_SHOW_GENUINE_REQUEST_OPTIONS_ONLY: true, + }, + }, () => { + cy.api({ + url: '/', + log: true, + failOnStatusCode: true, + retryOnStatusCodeFailure: true, + retryOnNetworkFailure: true, + }) + cy.get('.cy-api > div > .hljs').last() + .should("not.contain.text", `"log": true`) + .should("not.contain.text", `"failOnStatusCode": true`) + .should("not.contain.text", `"retryOnStatusCodeFailure": true`) + .should("not.contain.text", `"retryOnNetworkFailure": true`) + cy.api({ + url: '/unknown-route', + log: false, + failOnStatusCode: false, + retryOnStatusCodeFailure: false, + retryOnNetworkFailure: false, + }) + cy.get('.cy-api > div > .hljs').last() + .should("not.contain.text", `"log": false`) + .should("not.contain.text", `"failOnStatusCode": false`) + .should("not.contain.text", `"retryOnStatusCodeFailure": false`) + .should("not.contain.text", `"retryOnNetworkFailure": false`) + }) + +}) diff --git a/cypress/e2e/shows-non-requestoptions.cy.ts b/cypress/e2e/shows-non-requestoptions.cy.ts new file mode 100644 index 0000000..2bb46a0 --- /dev/null +++ b/cypress/e2e/shows-non-requestoptions.cy.ts @@ -0,0 +1,53 @@ +// loads definition for the custom "cy.api" command +/// + +describe('show non RequestOptions', () => { + + it("shows non RequestOptions if env var `API_SHOW_GENUINE_REQUEST_OPTIONS_ONLY` is not set", () => { + cy.api({ + url: '/', + log: false, + failOnStatusCode: false, + retryOnStatusCodeFailure: false, + retryOnNetworkFailure: false, + }) + cy.get('.cy-api > div > .hljs') + .should("contain.text", `"log": false`) + .should("contain.text", `"failOnStatusCode": false`) + .should("contain.text", `"retryOnStatusCodeFailure": false`) + .should("contain.text", `"retryOnNetworkFailure": false`) + }) + + it("shows non RequestOptions if env var `API_SHOW_GENUINE_REQUEST_OPTIONS_ONLY` is set to false", { + env: { + API_SHOW_GENUINE_REQUEST_OPTIONS_ONLY: false, + }, + }, () => { + cy.api({ + url: '/unknown-route', + log: false, + failOnStatusCode: false, + retryOnStatusCodeFailure: false, + retryOnNetworkFailure: false, + }) + cy.get('.cy-api > div > .hljs') + .should("contain.text", `"log": false`) + .should("contain.text", `"failOnStatusCode": false`) + .should("contain.text", `"retryOnStatusCodeFailure": false`) + .should("contain.text", `"retryOnNetworkFailure": false`) + + cy.api({ + url: '/', + log: true, + failOnStatusCode: true, + retryOnStatusCodeFailure: true, + retryOnNetworkFailure: true, + }) + cy.get('.cy-api > div > .hljs').last() + .should("contain.text", `"log": true`) + .and("contain.text", `"failOnStatusCode": true`) + .and("contain.text", `"retryOnStatusCodeFailure": true`) + .and("contain.text", `"retryOnNetworkFailure": true`) + }) + +}) diff --git a/src/support.ts b/src/support.ts index 6959cb8..5eb6810 100644 --- a/src/support.ts +++ b/src/support.ts @@ -370,6 +370,7 @@ const formatJSon = (jsonObject: object) => { const formatRequest = (options: Partial) => { const showCredentials = Cypress.env('API_SHOW_CREDENTIALS') + const showGenuineRequestOptionsOnly = Cypress.env('API_SHOW_GENUINE_REQUEST_OPTIONS_ONLY') const auth = options?.auth as { username?: string password?: string @@ -378,33 +379,50 @@ const formatRequest = (options: Partial) => { const hasPassword = auth?.password const hasBearer = auth?.bearer + const genuineRequestOptions: Partial = { + auth: options?.auth || {}, + body: options?.body, + encoding: options?.encoding, + followRedirect: options?.followRedirect, + form: options?.form, + gzip: options?.gzip, + headers: options?.headers || {}, + method: options?.method, + qs: options?.qs || {}, + url: options?.url, + } + + const updatedOptions = showGenuineRequestOptionsOnly + ? genuineRequestOptions + : { ...options } + if (!showCredentials && hasPassword && hasBearer) { return formatJSon({ - ...options, + ...updatedOptions, auth: { - ...options.auth, + ...updatedOptions.auth, bearer: '*****', password: '*****', }, }) } else if (!showCredentials && hasPassword) { return formatJSon({ - ...options, + ...updatedOptions, auth: { - ...options.auth, + ...updatedOptions.auth, password: '*****', }, }) } else if (!showCredentials && hasBearer) { return formatJSon({ - ...options, + ...updatedOptions, auth: { - ...options.auth, + ...updatedOptions.auth, bearer: '*****', }, }) } - return formatJSon(options) + return formatJSon(updatedOptions) } const formatResponse = (