Skip to content

Commit 30fbca1

Browse files
authored
[bidi][js] Add continueRequest and continueResponse command (#13704)
1 parent 5b7c95b commit 30fbca1

File tree

5 files changed

+239
-10
lines changed

5 files changed

+239
-10
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
const { BytesValue, Header } = require('./networkTypes')
19+
20+
class ContinueRequestParameters {
21+
#map = new Map()
22+
23+
constructor(request) {
24+
this.#map.set('request', request)
25+
}
26+
27+
body(value) {
28+
if (!(value instanceof BytesValue)) {
29+
throw new Error(`Value must be an instance of BytesValue. Received: '${value})'`)
30+
}
31+
this.#map.set('body', Object.fromEntries(value.asMap()))
32+
return this
33+
}
34+
35+
cookies(cookieHeaders) {
36+
const cookies = []
37+
cookieHeaders.forEach((header) => {
38+
if (!(header instanceof Header)) {
39+
throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`)
40+
}
41+
cookies.push(Object.fromEntries(header.asMap()))
42+
})
43+
44+
this.#map.set('cookies', cookies)
45+
return this
46+
}
47+
48+
headers(headers) {
49+
const headerList = []
50+
headers.forEach((header) => {
51+
if (!(header instanceof Header)) {
52+
throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`)
53+
}
54+
headerList.push(Object.fromEntries(header.asMap()))
55+
})
56+
57+
this.#map.set('headers', headerList)
58+
return this
59+
}
60+
61+
method(method) {
62+
if (typeof method !== 'string') {
63+
throw new Error(`Http method must be a string. Received: '${method})'`)
64+
}
65+
this.#map.set('method', method)
66+
return this
67+
}
68+
69+
url(url) {
70+
if (typeof url !== 'string') {
71+
throw new Error(`Url must be a string. Received:'${url}'`)
72+
}
73+
74+
this.#map.set('url', url)
75+
return this
76+
}
77+
78+
asMap() {
79+
return this.#map
80+
}
81+
}
82+
83+
module.exports = { ContinueRequestParameters }
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
const { BytesValue, Header } = require('./networkTypes')
19+
20+
class ContinueResponseParameters {
21+
#map = new Map()
22+
23+
constructor(request) {
24+
this.#map.set('request', request)
25+
}
26+
27+
cookies(cookieHeaders) {
28+
const cookies = []
29+
cookieHeaders.forEach((header) => {
30+
if (!(header instanceof Header)) {
31+
throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`)
32+
}
33+
cookies.push(Object.fromEntries(header.asMap()))
34+
})
35+
36+
this.#map.set('cookies', cookies)
37+
return this
38+
}
39+
40+
credentials(username, password) {
41+
if (typeof username !== 'string') {
42+
throw new Error(`Username must be a string. Received:'${username}'`)
43+
}
44+
45+
if (typeof password !== 'string') {
46+
throw new Error(`Password must be a string. Received:'${password}'`)
47+
}
48+
49+
this.#map.set('credentials', { type: 'password', username: username, password: password })
50+
51+
return this
52+
}
53+
54+
headers(headers) {
55+
const headerList = []
56+
headers.forEach((header) => {
57+
if (!(header instanceof Header)) {
58+
throw new Error(`Header must be an instance of Header. Received:'${header}'`)
59+
}
60+
headerList.push(Object.fromEntries(header.asMap()))
61+
})
62+
63+
this.#map.set('headers', headerList)
64+
return this
65+
}
66+
67+
reasonPhrase(reasonPhrase) {
68+
if (typeof reasonPhrase !== 'string') {
69+
throw new Error(`Reason phrase must be a string. Received: '${reasonPhrase})'`)
70+
}
71+
this.#map.set('reasonPhrase', reasonPhrase)
72+
return this
73+
}
74+
75+
statusCode(statusCode) {
76+
if (!Number.isInteger(statusCode)) {
77+
throw new Error(`Status must be an integer. Received:'${statusCode}'`)
78+
}
79+
80+
this.#map.set('statusCode', statusCode)
81+
return this
82+
}
83+
84+
asMap() {
85+
return this.#map
86+
}
87+
}
88+
89+
module.exports = { ContinueResponseParameters }

javascript/node/selenium-webdriver/bidi/network.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
const { BeforeRequestSent, ResponseStarted, FetchError } = require('./networkTypes')
1919
const { AddInterceptParameters } = require('./addInterceptParameters')
20+
const { ContinueResponseParameters } = require('./continueResponseParameters')
21+
const { ContinueRequestParameters } = require('./continueRequestParameters')
2022

2123
class Network {
2224
constructor(driver, browsingContextIds) {
@@ -98,7 +100,7 @@ class Network {
98100

99101
async addIntercept(params) {
100102
if (!(params instanceof AddInterceptParameters)) {
101-
throw new Error(`Params must be an instance of AddInterceptParamenters. Received:'${params}'`)
103+
throw new Error(`Params must be an instance of AddInterceptParameters. Received:'${params}'`)
102104
}
103105

104106
const command = {
@@ -168,6 +170,32 @@ class Network {
168170
await this.bidi.send(command)
169171
}
170172

173+
async continueRequest(params) {
174+
if (!(params instanceof ContinueRequestParameters)) {
175+
throw new Error(`Params must be an instance of ContinueRequestParameters. Received:'${params}'`)
176+
}
177+
178+
const command = {
179+
method: 'network.continueRequest',
180+
params: Object.fromEntries(params.asMap()),
181+
}
182+
183+
let response = await this.bidi.send(command)
184+
}
185+
186+
async continueResponse(params) {
187+
if (!(params instanceof ContinueResponseParameters)) {
188+
throw new Error(`Params must be an instance of ContinueResponseParameters. Received:'${params}'`)
189+
}
190+
191+
const command = {
192+
method: 'network.continueResponse',
193+
params: Object.fromEntries(params.asMap()),
194+
}
195+
196+
await this.bidi.send(command)
197+
}
198+
171199
async close() {
172200
await this.bidi.unsubscribe(
173201
'network.beforeRequestSent',

javascript/node/selenium-webdriver/bidi/networkTypes.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ class BytesValue {
5959
}
6060

6161
class Header {
62-
constructor(name, value, binaryValue) {
62+
constructor(name, value) {
6363
this._name = name
64+
if (!(value instanceof BytesValue)) {
65+
throw new Error(`Value must be an instance of BytesValue. Received:'${value}'`)
66+
}
6467
this._value = value
65-
this._binaryValue = binaryValue
6668
}
6769

6870
get name() {
@@ -72,10 +74,6 @@ class Header {
7274
get value() {
7375
return this._value
7476
}
75-
76-
get binaryValue() {
77-
return this._binaryValue
78-
}
7977
}
8078

8179
class Cookie {
@@ -222,9 +220,8 @@ class RequestData {
222220
headers.forEach((header) => {
223221
let name = header.name
224222
let value = 'value' in header ? header.value : null
225-
let binaryValue = 'binaryValue' in header ? header.binaryValue : null
226223

227-
this._headers.push(new Header(name, value, binaryValue))
224+
this._headers.push(new Header(name, new BytesValue(value.type, value.value)))
228225
})
229226

230227
this._cookies = []
@@ -488,4 +485,4 @@ class ResponseStarted extends BaseParameters {
488485
}
489486
}
490487

491-
module.exports = { BytesValue, Cookie, SameSite, BeforeRequestSent, ResponseStarted, FetchError }
488+
module.exports = { Header, BytesValue, Cookie, SameSite, BeforeRequestSent, ResponseStarted, FetchError }

javascript/node/selenium-webdriver/test/bidi/network_commands_test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const Network = require('../../bidi/network')
2525
const { AddInterceptParameters } = require('../../bidi/addInterceptParameters')
2626
const { InterceptPhase } = require('../../bidi/interceptPhase')
2727
const { until } = require('../../index')
28+
const { ContinueRequestParameters } = require('../../bidi/continueRequestParameters')
29+
const { ContinueResponseParameters } = require('../../bidi/continueResponseParameters')
2830

2931
suite(
3032
function (env) {
@@ -115,6 +117,36 @@ suite(
115117
assert.strictEqual(e.name, 'TimeoutError')
116118
}
117119
})
120+
121+
xit('can continue request', async function () {
122+
await network.addIntercept(new AddInterceptParameters(InterceptPhase.BEFORE_REQUEST_SENT))
123+
124+
let counter = 0
125+
126+
await network.beforeRequestSent(async (event) => {
127+
await network.continueRequest(new ContinueRequestParameters(event.request.request))
128+
counter = counter + 1
129+
})
130+
131+
await driver.get(Pages.logEntryAdded)
132+
133+
assert.strictEqual(counter, 1)
134+
})
135+
136+
xit('can continue response', async function () {
137+
await network.addIntercept(new AddInterceptParameters(InterceptPhase.RESPONSE_STARTED))
138+
139+
let counter = 0
140+
141+
await network.responseStarted(async (event) => {
142+
await network.continueResponse(new ContinueResponseParameters(event.request.request))
143+
counter = counter + 1
144+
})
145+
146+
await driver.get(Pages.logEntryAdded)
147+
148+
assert.strictEqual(counter, 1)
149+
})
118150
})
119151
},
120152
{ browsers: [Browser.FIREFOX] },

0 commit comments

Comments
 (0)