Skip to content

Commit

Permalink
[bidi][js] Add continueRequest and continueResponse command (#13704)
Browse files Browse the repository at this point in the history
  • Loading branch information
pujagani committed Mar 19, 2024
1 parent 5b7c95b commit 30fbca1
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 10 deletions.
@@ -0,0 +1,83 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

const { BytesValue, Header } = require('./networkTypes')

class ContinueRequestParameters {
#map = new Map()

constructor(request) {
this.#map.set('request', request)
}

body(value) {
if (!(value instanceof BytesValue)) {
throw new Error(`Value must be an instance of BytesValue. Received: '${value})'`)
}
this.#map.set('body', Object.fromEntries(value.asMap()))
return this
}

cookies(cookieHeaders) {
const cookies = []
cookieHeaders.forEach((header) => {
if (!(header instanceof Header)) {
throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`)
}
cookies.push(Object.fromEntries(header.asMap()))
})

this.#map.set('cookies', cookies)
return this
}

headers(headers) {
const headerList = []
headers.forEach((header) => {
if (!(header instanceof Header)) {
throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`)
}
headerList.push(Object.fromEntries(header.asMap()))
})

this.#map.set('headers', headerList)
return this
}

method(method) {
if (typeof method !== 'string') {
throw new Error(`Http method must be a string. Received: '${method})'`)
}
this.#map.set('method', method)
return this
}

url(url) {
if (typeof url !== 'string') {
throw new Error(`Url must be a string. Received:'${url}'`)
}

this.#map.set('url', url)
return this
}

asMap() {
return this.#map
}
}

module.exports = { ContinueRequestParameters }
@@ -0,0 +1,89 @@
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

const { BytesValue, Header } = require('./networkTypes')

class ContinueResponseParameters {
#map = new Map()

constructor(request) {
this.#map.set('request', request)
}

cookies(cookieHeaders) {
const cookies = []
cookieHeaders.forEach((header) => {
if (!(header instanceof Header)) {
throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`)
}
cookies.push(Object.fromEntries(header.asMap()))
})

this.#map.set('cookies', cookies)
return this
}

credentials(username, password) {
if (typeof username !== 'string') {
throw new Error(`Username must be a string. Received:'${username}'`)
}

if (typeof password !== 'string') {
throw new Error(`Password must be a string. Received:'${password}'`)
}

this.#map.set('credentials', { type: 'password', username: username, password: password })

return this
}

headers(headers) {
const headerList = []
headers.forEach((header) => {
if (!(header instanceof Header)) {
throw new Error(`Header must be an instance of Header. Received:'${header}'`)
}
headerList.push(Object.fromEntries(header.asMap()))
})

this.#map.set('headers', headerList)
return this
}

reasonPhrase(reasonPhrase) {
if (typeof reasonPhrase !== 'string') {
throw new Error(`Reason phrase must be a string. Received: '${reasonPhrase})'`)
}
this.#map.set('reasonPhrase', reasonPhrase)
return this
}

statusCode(statusCode) {
if (!Number.isInteger(statusCode)) {
throw new Error(`Status must be an integer. Received:'${statusCode}'`)
}

this.#map.set('statusCode', statusCode)
return this
}

asMap() {
return this.#map
}
}

module.exports = { ContinueResponseParameters }
30 changes: 29 additions & 1 deletion javascript/node/selenium-webdriver/bidi/network.js
Expand Up @@ -17,6 +17,8 @@

const { BeforeRequestSent, ResponseStarted, FetchError } = require('./networkTypes')
const { AddInterceptParameters } = require('./addInterceptParameters')
const { ContinueResponseParameters } = require('./continueResponseParameters')
const { ContinueRequestParameters } = require('./continueRequestParameters')

class Network {
constructor(driver, browsingContextIds) {
Expand Down Expand Up @@ -98,7 +100,7 @@ class Network {

async addIntercept(params) {
if (!(params instanceof AddInterceptParameters)) {
throw new Error(`Params must be an instance of AddInterceptParamenters. Received:'${params}'`)
throw new Error(`Params must be an instance of AddInterceptParameters. Received:'${params}'`)
}

const command = {
Expand Down Expand Up @@ -168,6 +170,32 @@ class Network {
await this.bidi.send(command)
}

async continueRequest(params) {
if (!(params instanceof ContinueRequestParameters)) {
throw new Error(`Params must be an instance of ContinueRequestParameters. Received:'${params}'`)
}

const command = {
method: 'network.continueRequest',
params: Object.fromEntries(params.asMap()),
}

let response = await this.bidi.send(command)
}

async continueResponse(params) {
if (!(params instanceof ContinueResponseParameters)) {
throw new Error(`Params must be an instance of ContinueResponseParameters. Received:'${params}'`)
}

const command = {
method: 'network.continueResponse',
params: Object.fromEntries(params.asMap()),
}

await this.bidi.send(command)
}

async close() {
await this.bidi.unsubscribe(
'network.beforeRequestSent',
Expand Down
15 changes: 6 additions & 9 deletions javascript/node/selenium-webdriver/bidi/networkTypes.js
Expand Up @@ -59,10 +59,12 @@ class BytesValue {
}

class Header {
constructor(name, value, binaryValue) {
constructor(name, value) {
this._name = name
if (!(value instanceof BytesValue)) {
throw new Error(`Value must be an instance of BytesValue. Received:'${value}'`)
}
this._value = value
this._binaryValue = binaryValue
}

get name() {
Expand All @@ -72,10 +74,6 @@ class Header {
get value() {
return this._value
}

get binaryValue() {
return this._binaryValue
}
}

class Cookie {
Expand Down Expand Up @@ -222,9 +220,8 @@ class RequestData {
headers.forEach((header) => {
let name = header.name
let value = 'value' in header ? header.value : null
let binaryValue = 'binaryValue' in header ? header.binaryValue : null

this._headers.push(new Header(name, value, binaryValue))
this._headers.push(new Header(name, new BytesValue(value.type, value.value)))
})

this._cookies = []
Expand Down Expand Up @@ -488,4 +485,4 @@ class ResponseStarted extends BaseParameters {
}
}

module.exports = { BytesValue, Cookie, SameSite, BeforeRequestSent, ResponseStarted, FetchError }
module.exports = { Header, BytesValue, Cookie, SameSite, BeforeRequestSent, ResponseStarted, FetchError }
Expand Up @@ -25,6 +25,8 @@ const Network = require('../../bidi/network')
const { AddInterceptParameters } = require('../../bidi/addInterceptParameters')
const { InterceptPhase } = require('../../bidi/interceptPhase')
const { until } = require('../../index')
const { ContinueRequestParameters } = require('../../bidi/continueRequestParameters')
const { ContinueResponseParameters } = require('../../bidi/continueResponseParameters')

suite(
function (env) {
Expand Down Expand Up @@ -115,6 +117,36 @@ suite(
assert.strictEqual(e.name, 'TimeoutError')
}
})

xit('can continue request', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))

let counter = 0

await network.beforeRequestSent(async (event) => {
await network.continueRequest(new ContinueRequestParameters(event.request.request))
counter = counter + 1
})

await driver.get(Pages.logEntryAdded)

assert.strictEqual(counter, 1)
})

xit('can continue response', async function () {
await network.addIntercept(new AddInterceptParameters(InterceptPhase.RESPONSE_STARTED))

let counter = 0

await network.responseStarted(async (event) => {
await network.continueResponse(new ContinueResponseParameters(event.request.request))
counter = counter + 1
})

await driver.get(Pages.logEntryAdded)

assert.strictEqual(counter, 1)
})
})
},
{ browsers: [Browser.FIREFOX] },
Expand Down

0 comments on commit 30fbca1

Please sign in to comment.