Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/selenium-webdriver/bidi/continueResponseParameters.js
2884 views
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 { Header } = require('./networkTypes')
19
20
/**
21
* Represents the parameters for a continue response.
22
* Described in https://w3c.github.io/webdriver-bidi/#command-network-continueResponse.
23
*/
24
class ContinueResponseParameters {
25
#map = new Map()
26
27
constructor(request) {
28
this.#map.set('request', request)
29
}
30
31
/**
32
* Sets the cookies for the response.
33
*
34
* @param {Header[]} cookieHeaders - The array of cookie headers.
35
* @returns {ContinueResponseParameters} - The current instance of the ContinueResponseParameters for chaining.
36
* @throws {Error} - If the cookieHeader is not an instance of Header.
37
*/
38
cookies(cookieHeaders) {
39
const cookies = []
40
cookieHeaders.forEach((header) => {
41
if (!(header instanceof Header)) {
42
throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`)
43
}
44
cookies.push(Object.fromEntries(header.asMap()))
45
})
46
47
this.#map.set('cookies', cookies)
48
return this
49
}
50
51
/**
52
* Sets the credentials for authentication.
53
*
54
* @param {string} username - The username for authentication.
55
* @param {string} password - The password for authentication.
56
* @returns {ContinueResponseParameters} The current instance of the ContinueResponseParameters for chaining.
57
* @throws {Error} If username or password is not a string.
58
*/
59
credentials(username, password) {
60
if (typeof username !== 'string') {
61
throw new Error(`Username must be a string. Received:'${username}'`)
62
}
63
64
if (typeof password !== 'string') {
65
throw new Error(`Password must be a string. Received:'${password}'`)
66
}
67
68
this.#map.set('credentials', { type: 'password', username: username, password: password })
69
70
return this
71
}
72
73
/**
74
* Sets the headers for the response.
75
*
76
* @param {Header[]} headers - An array of Header objects representing the headers.
77
* @returns {ContinueResponseParameters} - The current instance of the ContinueResponseParameters for chaining.
78
* @throws {Error} - If the header value is not an instance of Header.
79
*/
80
headers(headers) {
81
const headerList = []
82
headers.forEach((header) => {
83
if (!(header instanceof Header)) {
84
throw new Error(`Header value must be an instance of Header. Received:'${header}'`)
85
}
86
headerList.push(Object.fromEntries(header.asMap()))
87
})
88
89
this.#map.set('headers', headerList)
90
return this
91
}
92
93
/**
94
* Sets the reason phrase for the response.
95
*
96
* @param {string} reasonPhrase - The reason phrase for the response.
97
* @returns {ContinueResponseParameters} - The current instance of the ContinueResponseParameters for chaining.
98
* @throws {Error} - If the reason phrase is not a string.
99
*/
100
reasonPhrase(reasonPhrase) {
101
if (typeof reasonPhrase !== 'string') {
102
throw new Error(`Reason phrase must be a string. Received: '${reasonPhrase})'`)
103
}
104
this.#map.set('reasonPhrase', reasonPhrase)
105
return this
106
}
107
108
/**
109
* Sets the status code for the response.
110
*
111
* @param {number} statusCode - The status code to set.
112
* @returns {ContinueResponseParameters} - The current instance of the ContinueResponseParameters for chaining.
113
* @throws {Error} - If the `statusCode` parameter is not an integer.
114
*/
115
statusCode(statusCode) {
116
if (!Number.isInteger(statusCode)) {
117
throw new Error(`Status must be an integer. Received:'${statusCode}'`)
118
}
119
120
this.#map.set('statusCode', statusCode)
121
return this
122
}
123
124
asMap() {
125
return this.#map
126
}
127
}
128
129
module.exports = { ContinueResponseParameters }
130
131