Path: blob/trunk/rb/spec/unit/selenium/webdriver/bidi/intercepted_response_spec.rb
4079 views
# frozen_string_literal: true12# Licensed to the Software Freedom Conservancy (SFC) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The SFC licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.1819require File.expand_path('../spec_helper', __dir__)20require File.expand_path('../../../../../lib/selenium/webdriver/bidi/network/cookies', __dir__)21require File.expand_path('../../../../../lib/selenium/webdriver/bidi/network/headers', __dir__)22require File.expand_path('../../../../../lib/selenium/webdriver/bidi/network/credentials', __dir__)23require File.expand_path('../../../../../lib/selenium/webdriver/bidi/network/intercepted_response', __dir__)2425module Selenium26module WebDriver27class BiDi28describe InterceptedResponse do29let(:mock_network) { instance_double(Network) }30let(:response_id) { 'resp-456' }31let(:intercepted_response) { described_class.new(mock_network, {'request' => response_id}) }32let(:mock_headers_data) { [{name: 'Content-Type', value: 'application/json'}] }33let(:mock_cookies_data) { [{name: 'session', value: {type: 'string', value: 'abc'}}] }34let(:mock_body_string) { '{"message": "Access Denied"}' }3536before do37allow(intercepted_response).to receive(:id).and_return(response_id)38end3940describe 'Initialization and Default State' do41it 'has an empty hash as headers by default' do42expect(intercepted_response.headers).to be_empty43end4445it 'has an empty hash as cookies by default' do46expect(intercepted_response.cookies).to be_empty47end48end4950describe '#continue' do51before do52allow(mock_network).to receive(:continue_response)53end5455it 'sends nil headers and cookies when not explicitly set' do56expected_payload = {id: response_id, cookies: nil, headers: nil, credentials: nil, reason: nil, status: nil}57intercepted_response.continue5859expect(mock_network).to have_received(:continue_response).with(expected_payload)60end6162it 'sends headers payload and uses default nil cookies/credentials' do63intercepted_response.headers = mock_headers_data64reason = 'Custom Reason'65status = 2016667expected_payload = {68id: response_id,69cookies: nil,70headers: Headers.new(mock_headers_data).as_json,71credentials: nil,72reason: reason,73status: status74}75intercepted_response.reason = reason76intercepted_response.status = status77intercepted_response.continue7879expect(mock_network).to have_received(:continue_response).with(expected_payload)80end8182it 'sends full custom payload when all fields are set' do83credentials = Credentials.new(username: 'user', password: 'pass')8485intercepted_response.headers = mock_headers_data86intercepted_response.cookies = mock_cookies_data87intercepted_response.credentials(username: 'user', password: 'pass')88intercepted_response.reason = 'Test Reason'89intercepted_response.status = 4049091expected_payload = {92id: response_id,93cookies: Cookies.new(mock_cookies_data).as_json,94headers: Headers.new(mock_headers_data).as_json,95credentials: credentials.as_json,96reason: 'Test Reason',97status: 40498}99100intercepted_response.continue101102expect(mock_network).to have_received(:continue_response).with(expected_payload)103end104end105106describe '#provide_response' do107before do108allow(mock_network).to receive(:provide_response)109end110111it 'sends nil headers, cookies, and body when not explicitly set' do112expected_payload = {id: response_id, cookies: nil, headers: nil, body: nil, reason: nil, status: nil}113intercepted_response.provide_response114115expect(mock_network).to have_received(:provide_response).with(expected_payload)116end117118it 'sends body payload and uses default [] cookies/headers' do119intercepted_response.body = mock_body_string120reason = 'Provided Success'121status = 200122123expected_payload = {124id: response_id,125cookies: nil,126headers: nil,127body: {type: 'string', value: mock_body_string.to_json},128reason: reason,129status: status130}131intercepted_response.reason = reason132intercepted_response.status = status133intercepted_response.provide_response134135expect(mock_network).to have_received(:provide_response).with(expected_payload)136end137138it 'sends full custom payload when all fields are set' do139intercepted_response.headers = mock_headers_data140intercepted_response.cookies = mock_cookies_data141intercepted_response.body = mock_body_string142intercepted_response.reason = 'Forbidden'143intercepted_response.status = 403144145expected_payload = {146id: response_id,147cookies: Cookies.new(mock_cookies_data).as_json,148headers: Headers.new(mock_headers_data).as_json,149body: {type: 'string', value: mock_body_string.to_json},150reason: 'Forbidden',151status: 403152}153154intercepted_response.provide_response155156expect(mock_network).to have_received(:provide_response).with(expected_payload)157end158end159160describe 'setters/getters' do161it '#credentials' do162credentials = intercepted_response.credentials(username: 'u', password: 'p')163expect(credentials).to be_a(Credentials)164expect(intercepted_response.credentials).to be(credentials)165end166167it '#headers=' do168intercepted_response.headers = mock_headers_data169expect(intercepted_response.headers).to be_a(Headers)170end171172it '#cookies=' do173intercepted_response.cookies = mock_cookies_data174first_cookies = intercepted_response.cookies175intercepted_response.cookies = [{name: 'c2', value: 'v2'}]176second_cookies = intercepted_response.cookies177expect(first_cookies).to be(second_cookies)178end179180it '#body=' do181data = {key: 'value', number: 123}182intercepted_response.body = data183expected_body = {184type: 'string',185value: data.to_json186}187expect(intercepted_response.body).to eq(expected_body)188end189end190end191end192end193end194195196