Path: blob/trunk/rb/spec/unit/selenium/webdriver/remote/http/common_spec.rb
4101 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__)2021module Selenium22module WebDriver23module Remote24module Http25describe Common do26subject(:common) do27common = described_class.new28common.server_url = URI.parse('http://server')29allow(common).to receive(:request)3031common32end3334after do35described_class.extra_headers = nil36described_class.user_agent = nil37end3839it 'sends non-empty body header for POST requests without command data' do40common.call(:post, 'clear', nil)4142expect(common).to have_received(:request)43.with(:post, URI.parse('http://server/clear'),44hash_including('Content-Length' => '2'), '{}')45end4647it 'sends a standard User-Agent by default' do48user_agent_regexp = %r{\Aselenium/#{WebDriver::VERSION} \(ruby #{Platform.os}\)\z}4950common.call(:post, 'session', nil)5152expect(common).to have_received(:request)53.with(:post, URI.parse('http://server/session'),54hash_including('User-Agent' => a_string_matching(user_agent_regexp)), '{}')55end5657it 'allows registering extra headers' do58described_class.extra_headers = {'Foo' => 'bar'}5960common.call(:post, 'session', nil)6162expect(common).to have_received(:request)63.with(:post, URI.parse('http://server/session'),64hash_including('Foo' => 'bar'), '{}')65end6667it 'allows overriding default User-Agent' do68described_class.user_agent = 'rspec/1.0 (ruby 3.2)'6970common.call(:post, 'session', nil)7172expect(common).to have_received(:request)73.with(:post, URI.parse('http://server/session'),74hash_including('User-Agent' => 'rspec/1.0 (ruby 3.2)'), '{}')75end7677context 'when encoding strings to UTF-8' do78it 'converts binary-encoded strings that are valid UTF-8' do79binary_string = +'return navigator.userAgent;'80binary_string.force_encoding(Encoding::BINARY)81command_hash = {script: binary_string, args: []}8283common.call(:post, 'execute', command_hash)8485expect(common).to have_received(:request) do |_verb, _url, _headers, payload|86expect { JSON.parse(payload) }.not_to raise_error87parsed = JSON.parse(payload)88expect(parsed['script']).to eq('return navigator.userAgent;')89expect(parsed['script'].encoding).to eq(Encoding::UTF_8)90end91end9293it 'converts binary-encoded strings in nested hashes' do94binary_string = +'test value'95binary_string.force_encoding(Encoding::BINARY)96command_hash = {97outer: {98inner: binary_string,99another: 'utf8 string'100}101}102103common.call(:post, 'test', command_hash)104105expect(common).to have_received(:request) do |_verb, _url, _headers, payload|106expect { JSON.parse(payload) }.not_to raise_error107parsed = JSON.parse(payload)108expect(parsed['outer']['inner']).to eq('test value')109end110end111112it 'converts binary-encoded strings in arrays' do113binary_string = +'array item'114binary_string.force_encoding(Encoding::BINARY)115command_hash = {items: [binary_string, 'utf8 item']}116117common.call(:post, 'test', command_hash)118119expect(common).to have_received(:request) do |_verb, _url, _headers, payload|120expect { JSON.parse(payload) }.not_to raise_error121parsed = JSON.parse(payload)122expect(parsed['items']).to eq(['array item', 'utf8 item'])123end124end125126it 'raises error for invalid byte sequences' do127# Create an invalid UTF-8 byte sequence128invalid_string = +"\xFF\xFE"129invalid_string.force_encoding(Encoding::BINARY)130command_hash = {script: invalid_string}131132expect { common.call(:post, 'execute', command_hash) }133.to raise_error(WebDriver::Error::WebDriverError, /Unable to encode string to UTF-8/)134end135136it 'handles already UTF-8 encoded strings' do137utf8_string = 'already utf-8'138command_hash = {script: utf8_string}139140common.call(:post, 'execute', command_hash)141142expect(common).to have_received(:request) do |_verb, _url, _headers, payload|143expect { JSON.parse(payload) }.not_to raise_error144parsed = JSON.parse(payload)145expect(parsed['script']).to eq('already utf-8')146end147end148end149end150end # Http151end # Remote152end # WebDriver153end # Selenium154155156