Path: blob/trunk/javascript/selenium-webdriver/bidi/continueResponseParameters.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617const { Header } = require('./networkTypes')1819/**20* Represents the parameters for a continue response.21* Described in https://w3c.github.io/webdriver-bidi/#command-network-continueResponse.22*/23class ContinueResponseParameters {24#map = new Map()2526constructor(request) {27this.#map.set('request', request)28}2930/**31* Sets the cookies for the response.32*33* @param {Header[]} cookieHeaders - The array of cookie headers.34* @returns {ContinueResponseParameters} - The current instance of the ContinueResponseParameters for chaining.35* @throws {Error} - If the cookieHeader is not an instance of Header.36*/37cookies(cookieHeaders) {38const cookies = []39cookieHeaders.forEach((header) => {40if (!(header instanceof Header)) {41throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`)42}43cookies.push(Object.fromEntries(header.asMap()))44})4546this.#map.set('cookies', cookies)47return this48}4950/**51* Sets the credentials for authentication.52*53* @param {string} username - The username for authentication.54* @param {string} password - The password for authentication.55* @returns {ContinueResponseParameters} The current instance of the ContinueResponseParameters for chaining.56* @throws {Error} If username or password is not a string.57*/58credentials(username, password) {59if (typeof username !== 'string') {60throw new Error(`Username must be a string. Received:'${username}'`)61}6263if (typeof password !== 'string') {64throw new Error(`Password must be a string. Received:'${password}'`)65}6667this.#map.set('credentials', { type: 'password', username: username, password: password })6869return this70}7172/**73* Sets the headers for the response.74*75* @param {Header[]} headers - An array of Header objects representing the headers.76* @returns {ContinueResponseParameters} - The current instance of the ContinueResponseParameters for chaining.77* @throws {Error} - If the header value is not an instance of Header.78*/79headers(headers) {80const headerList = []81headers.forEach((header) => {82if (!(header instanceof Header)) {83throw new Error(`Header value must be an instance of Header. Received:'${header}'`)84}85headerList.push(Object.fromEntries(header.asMap()))86})8788this.#map.set('headers', headerList)89return this90}9192/**93* Sets the reason phrase for the response.94*95* @param {string} reasonPhrase - The reason phrase for the response.96* @returns {ContinueResponseParameters} - The current instance of the ContinueResponseParameters for chaining.97* @throws {Error} - If the reason phrase is not a string.98*/99reasonPhrase(reasonPhrase) {100if (typeof reasonPhrase !== 'string') {101throw new Error(`Reason phrase must be a string. Received: '${reasonPhrase})'`)102}103this.#map.set('reasonPhrase', reasonPhrase)104return this105}106107/**108* Sets the status code for the response.109*110* @param {number} statusCode - The status code to set.111* @returns {ContinueResponseParameters} - The current instance of the ContinueResponseParameters for chaining.112* @throws {Error} - If the `statusCode` parameter is not an integer.113*/114statusCode(statusCode) {115if (!Number.isInteger(statusCode)) {116throw new Error(`Status must be an integer. Received:'${statusCode}'`)117}118119this.#map.set('statusCode', statusCode)120return this121}122123asMap() {124return this.#map125}126}127128module.exports = { ContinueResponseParameters }129130131