Skip to content

Commit

Permalink
fix(webdriverio): fix mock filtering (#13668)
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann authored Sep 28, 2024
1 parent b793d5d commit 7471ddb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
10 changes: 5 additions & 5 deletions packages/webdriverio/src/utils/interception/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ export default class WebDriverInterception {
#matchesFilterOptions<T extends local.NetworkBeforeRequestSentParameters | local.NetworkResponseCompletedParameters> (request: T) {
let isRequestMatching = true

if (this.#filterOptions.method) {
if (isRequestMatching && this.#filterOptions.method) {
isRequestMatching = typeof this.#filterOptions.method === 'function'
? this.#filterOptions.method(request.request.method)
: this.#filterOptions.method === request.request.method
: this.#filterOptions.method.toLowerCase() === request.request.method.toLowerCase()
}

if (this.#filterOptions.requestHeaders) {
if (isRequestMatching && this.#filterOptions.requestHeaders) {
isRequestMatching = typeof this.#filterOptions.requestHeaders === 'function'
? this.#filterOptions.requestHeaders(request.request.headers.reduce((acc, { name, value }) => {
acc[name] = value.type === 'string' ? value.value : Buffer.from(value.value, 'base64').toString()
Expand All @@ -240,7 +240,7 @@ export default class WebDriverInterception {
})
}

if (this.#filterOptions.responseHeaders && 'response' in request) {
if (isRequestMatching && this.#filterOptions.responseHeaders && 'response' in request) {
isRequestMatching = typeof this.#filterOptions.responseHeaders === 'function'
? this.#filterOptions.responseHeaders(request.response.headers.reduce((acc, { name, value }) => {
acc[name] = value.type === 'string' ? value.value : Buffer.from(value.value, 'base64').toString()
Expand All @@ -258,7 +258,7 @@ export default class WebDriverInterception {
})
}

if (this.#filterOptions.statusCode && 'response' in request) {
if (isRequestMatching && this.#filterOptions.statusCode && 'response' in request) {
isRequestMatching = typeof this.#filterOptions.statusCode === 'function'
? this.#filterOptions.statusCode(request.response.status)
: this.#filterOptions.statusCode === request.response.status
Expand Down
68 changes: 68 additions & 0 deletions packages/webdriverio/tests/utils/interception/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,72 @@ describe('WebDriverInterception', () => {
expect(browser.networkContinueRequest).toHaveBeenCalledWith({ request: 123 })
expect(browser.networkFailRequest).toHaveBeenCalledTimes(0)
})

it('handleResponseStarted', async () => {
const browser: any = new EventEmitter()
browser.sessionSubscribe = vi.fn().mockReturnValue(Promise.resolve())
browser.networkProvideResponse = vi.fn().mockReturnValue(Promise.resolve())
browser.networkAddIntercept = vi.fn().mockReturnValue(Promise.resolve({ intercept: '123' }))
const mock = await WebDriverInterception.initiate('http://foobar.com:1234/foo/bar.html?foo=bar', {
method: 'get',
requestHeaders: { foo: 'bar' },
responseHeaders: { bar: 'foo' },
statusCode: 200
}, browser)
browser.emit('network.responseStarted', {
request: {
url: 'http://foobar.com:1234/foo/bar.html?foo=bar',
method: 'GET',
headers: [{ name: 'foo', value: { type: 'string', value: 'bar' } }]
}
})
expect(browser.networkProvideResponse).toHaveBeenCalledTimes(0)

browser.emit('network.responseStarted', {
isBlocked: true,
request: {
url: 'http://foobar.com:1234/foo/bar.html?foo=bar',
method: 'GET',
request: 123,
headers: [{ name: 'foo', value: { type: 'string', value: 'bar' } }]
}
})
expect(browser.networkProvideResponse).toHaveBeenCalledTimes(1)
expect(browser.networkProvideResponse).toHaveBeenCalledWith({
request: 123,
})
vi.mocked(browser.networkProvideResponse).mockClear()

mock.respondOnce({ foo: 'bar' })
browser.emit('network.responseStarted', {
isBlocked: true,
request: {
url: 'http://foobar.com:1234/foo/bar.html?foo=bar',
method: 'GET',
request: 123,
headers: [{ name: 'foo', value: { type: 'string', value: 'bar' } }]
}
})
expect(browser.networkProvideResponse).toHaveBeenCalledTimes(1)
expect(browser.networkProvideResponse).toHaveBeenCalledWith({
request: 123,
body: {
type: 'string',
value: JSON.stringify({ foo: 'bar' })
}
})
browser.emit('network.responseStarted', {
isBlocked: true,
request: {
url: 'http://foobar.com:1234/foo/bar.html?foo=bar',
method: 'GET',
request: 123,
headers: [{ name: 'foo', value: { type: 'string', value: 'bar' } }]
}
})
expect(browser.networkProvideResponse).toHaveBeenCalledTimes(2)
expect(browser.networkProvideResponse).toHaveBeenCalledWith({
request: 123
})
})
})

0 comments on commit 7471ddb

Please sign in to comment.