Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }, () => {
Expand Down
37 changes: 37 additions & 0 deletions cypress/e2e/hides-non-requestoptions.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// loads definition for the custom "cy.api" command
/// <reference path="../../dist/types.d.ts" />

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`)
})

})
53 changes: 53 additions & 0 deletions cypress/e2e/shows-non-requestoptions.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// loads definition for the custom "cy.api" command
/// <reference path="../../dist/types.d.ts" />

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`)
})

})
32 changes: 25 additions & 7 deletions src/support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ const formatJSon = (jsonObject: object) => {

const formatRequest = (options: Partial<Cypress.RequestOptions>) => {
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
Expand All @@ -378,33 +379,50 @@ const formatRequest = (options: Partial<Cypress.RequestOptions>) => {
const hasPassword = auth?.password
const hasBearer = auth?.bearer

const genuineRequestOptions: Partial<Cypress.RequestOptions> = {
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 = (
Expand Down