Skip to content

Commit

Permalink
feat(driver): add filtering API for proxy logs
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig committed Jun 22, 2022
1 parent 7422394 commit 8fe8670
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
19 changes: 19 additions & 0 deletions cli/types/cypress.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,25 @@ declare namespace Cypress {
defaults(options: Partial<KeyboardDefaultsOptions>): void
}

ProxyLogging: {
/**
* Reset the filter rules for proxy logs to the default (all XHRs and fetches are logged)
*/
filter(): void
/**
* Set custom filtering rules for proxy logs.
* @param filterFn a function that returns true if the request should be logged and false otherwise
*/
filter(filterFn: (req: {
requestId: string
method: HttpMethod
url: string
headers: { [key: string]: string | string[] }
resourceType: 'document' | 'fetch' | 'xhr' | 'websocket' | 'stylesheet' | 'script' | 'image' | 'font' | 'cspviolationreport' | 'ping' | 'manifest' | 'other'
originalResourceType: string | undefined
}) => boolean): void
}

/**
* @see https://on.cypress.io/api/api-server
*/
Expand Down
18 changes: 14 additions & 4 deletions packages/driver/src/cypress/proxy-logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ function getRequestLogConfig (req: Omit<ProxyRequest, 'log'>): Partial<Cypress.I
}
}

function shouldLog (preRequest: BrowserPreRequest) {
return ['xhr', 'fetch'].includes(preRequest.resourceType)
}

class ProxyRequest {
log?: Cypress.Log
preRequest: BrowserPreRequest
Expand Down Expand Up @@ -251,10 +247,15 @@ type UnmatchedXhrLog = {
stack?: string
}

function defaultFilterFn (preRequest: BrowserPreRequest) {
return ['xhr', 'fetch'].includes(preRequest.resourceType)
}

export default class ProxyLogging {
unloggedPreRequests: Array<BrowserPreRequest> = []
unmatchedXhrLogs: Array<UnmatchedXhrLog> = []
proxyRequests: Array<ProxyRequest> = []
filterFn: (preRequest: BrowserPreRequest) => boolean = defaultFilterFn

constructor (private Cypress: Cypress.Cypress) {
Cypress.on('request:event', (eventName, data) => {
Expand Down Expand Up @@ -282,6 +283,15 @@ export default class ProxyLogging {
})
}

filter (filterFn?: typeof this.filterFn): void {
if (filterFn !== undefined && typeof filterFn !== 'function') {
// TODO: move to error_messages once API is finalized
throw new Error(`ProxyLogging.filter() accepts a function as the first argument, but a ${typeof filterFn} was passed.`)
}

this.filterFn = filterFn || defaultFilterFn
}

/**
* The `cy.route()` XHR stub functions will log before a proxy log is received, so this queues an XHR log to be overridden by a proxy log later.
*/
Expand Down

5 comments on commit 8fe8670

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8fe8670 Jun 22, 2022

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.3.0/linux-x64/issue-9358-ignore-http-logs-8fe867036211afdf4d64117735c2bee46bd0b719/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8fe8670 Jun 22, 2022

Choose a reason for hiding this comment

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

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.3.0/linux-arm64/issue-9358-ignore-http-logs-8fe867036211afdf4d64117735c2bee46bd0b719/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8fe8670 Jun 22, 2022

Choose a reason for hiding this comment

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

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.3.0/darwin-arm64/issue-9358-ignore-http-logs-8fe867036211afdf4d64117735c2bee46bd0b719/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8fe8670 Jun 22, 2022

Choose a reason for hiding this comment

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

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.3.0/win32-x64/issue-9358-ignore-http-logs-8fe867036211afdf4d64117735c2bee46bd0b719/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 8fe8670 Jun 22, 2022

Choose a reason for hiding this comment

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

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.3.0/darwin-x64/issue-9358-ignore-http-logs-8fe867036211afdf4d64117735c2bee46bd0b719/cypress.tgz

Please sign in to comment.