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