Path: blob/trunk/javascript/selenium-webdriver/bidi/provideResponseParameters.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 { BytesValue, Header } = require('./networkTypes')1819/**20* Represents parameters for providingResponse command.21* Described in https://w3c.github.io/webdriver-bidi/#command-network-provideResponse.22* @class23*/24class ProvideResponseParameters {25#map = new Map()2627constructor(request) {28this.#map.set('request', request)29}3031/**32* Sets the body value for the response parameters.33*34* @param {BytesValue} value - The value to set as the body. Must be an instance of BytesValue.35* @returns {ProvideResponseParameters} - Returns the ProvideResponseParameters object for chaining.36* @throws {Error} - Throws an error if the value is not an instance of BytesValue.37*/38body(value) {39if (!(value instanceof BytesValue)) {40throw new Error(`Value must be an instance of BytesValue. Received: ${typeof value} with value: ${value}`)41}42this.#map.set('body', Object.fromEntries(value.asMap()))43return this44}4546/**47* Sets the cookie headers for the response.48*49* @param {Header[]} cookieHeaders - An array of cookie headers.50* @returns {ProvideResponseParameters} - Returns the ProvideResponseParameters object for chaining.51* @throws {Error} - Throws an error if a cookie header is not an instance of Header.52*/53cookies(cookieHeaders) {54const cookies = []55cookieHeaders.forEach((header) => {56if (!(header instanceof Header)) {57throw new Error(`CookieHeader must be an instance of Header. Received:'${header}'`)58}59cookies.push(Object.fromEntries(header.asMap()))60})6162this.#map.set('cookies', cookies)63return this64}6566/**67* Sets the headers for the response.68*69* @param {Header[]} headers - The headers to be set.70* @returns {ProvideResponseParameters} - Returns the ProvideResponseParameters object for chaining.71* @throws {Error} - If the provided header is not an instance of Header.72*/73headers(headers) {74const headerList = []75headers.forEach((header) => {76if (!(header instanceof Header)) {77throw new Error(`Header must be an instance of Header. Received:'${header}'`)78}79headerList.push(Object.fromEntries(header.asMap()))80})8182this.#map.set('headers', headerList)83return this84}8586/**87* Sets the reason phrase for the response.88*89* @param {string} reasonPhrase - The reason phrase to set.90* @returns {ProvideResponseParameters} - Returns the ProvideResponseParameters object for chaining.91* @throws {Error} - If the reason phrase is not a string.92*/93reasonPhrase(reasonPhrase) {94if (typeof reasonPhrase !== 'string') {95throw new Error(`Reason phrase must be a string. Received: '${reasonPhrase})'`)96}97this.#map.set('reasonPhrase', reasonPhrase)98return this99}100101/**102* Sets the status code for the response.103*104* @param {number} statusCode - The status code to set.105* @returns {ProvideResponseParameters} - Returns the ProvideResponseParameters object for chaining.106* @throws {Error} - If the status code is not an integer.107*/108statusCode(statusCode) {109if (!Number.isInteger(statusCode)) {110throw new Error(`Status must be an integer. Received:'${statusCode}'`)111}112113this.#map.set('statusCode', statusCode)114return this115}116117asMap() {118return this.#map119}120}121122module.exports = { ProvideResponseParameters }123124125