Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/rb/spec/unit/selenium/webdriver/remote/http/common_spec.rb
4101 views
1
# frozen_string_literal: true
2
3
# Licensed to the Software Freedom Conservancy (SFC) under one
4
# or more contributor license agreements. See the NOTICE file
5
# distributed with this work for additional information
6
# regarding copyright ownership. The SFC licenses this file
7
# to you under the Apache License, Version 2.0 (the
8
# "License"); you may not use this file except in compliance
9
# with the License. You may obtain a copy of the License at
10
#
11
# http://www.apache.org/licenses/LICENSE-2.0
12
#
13
# Unless required by applicable law or agreed to in writing,
14
# software distributed under the License is distributed on an
15
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
# KIND, either express or implied. See the License for the
17
# specific language governing permissions and limitations
18
# under the License.
19
20
require File.expand_path('../../spec_helper', __dir__)
21
22
module Selenium
23
module WebDriver
24
module Remote
25
module Http
26
describe Common do
27
subject(:common) do
28
common = described_class.new
29
common.server_url = URI.parse('http://server')
30
allow(common).to receive(:request)
31
32
common
33
end
34
35
after do
36
described_class.extra_headers = nil
37
described_class.user_agent = nil
38
end
39
40
it 'sends non-empty body header for POST requests without command data' do
41
common.call(:post, 'clear', nil)
42
43
expect(common).to have_received(:request)
44
.with(:post, URI.parse('http://server/clear'),
45
hash_including('Content-Length' => '2'), '{}')
46
end
47
48
it 'sends a standard User-Agent by default' do
49
user_agent_regexp = %r{\Aselenium/#{WebDriver::VERSION} \(ruby #{Platform.os}\)\z}
50
51
common.call(:post, 'session', nil)
52
53
expect(common).to have_received(:request)
54
.with(:post, URI.parse('http://server/session'),
55
hash_including('User-Agent' => a_string_matching(user_agent_regexp)), '{}')
56
end
57
58
it 'allows registering extra headers' do
59
described_class.extra_headers = {'Foo' => 'bar'}
60
61
common.call(:post, 'session', nil)
62
63
expect(common).to have_received(:request)
64
.with(:post, URI.parse('http://server/session'),
65
hash_including('Foo' => 'bar'), '{}')
66
end
67
68
it 'allows overriding default User-Agent' do
69
described_class.user_agent = 'rspec/1.0 (ruby 3.2)'
70
71
common.call(:post, 'session', nil)
72
73
expect(common).to have_received(:request)
74
.with(:post, URI.parse('http://server/session'),
75
hash_including('User-Agent' => 'rspec/1.0 (ruby 3.2)'), '{}')
76
end
77
78
context 'when encoding strings to UTF-8' do
79
it 'converts binary-encoded strings that are valid UTF-8' do
80
binary_string = +'return navigator.userAgent;'
81
binary_string.force_encoding(Encoding::BINARY)
82
command_hash = {script: binary_string, args: []}
83
84
common.call(:post, 'execute', command_hash)
85
86
expect(common).to have_received(:request) do |_verb, _url, _headers, payload|
87
expect { JSON.parse(payload) }.not_to raise_error
88
parsed = JSON.parse(payload)
89
expect(parsed['script']).to eq('return navigator.userAgent;')
90
expect(parsed['script'].encoding).to eq(Encoding::UTF_8)
91
end
92
end
93
94
it 'converts binary-encoded strings in nested hashes' do
95
binary_string = +'test value'
96
binary_string.force_encoding(Encoding::BINARY)
97
command_hash = {
98
outer: {
99
inner: binary_string,
100
another: 'utf8 string'
101
}
102
}
103
104
common.call(:post, 'test', command_hash)
105
106
expect(common).to have_received(:request) do |_verb, _url, _headers, payload|
107
expect { JSON.parse(payload) }.not_to raise_error
108
parsed = JSON.parse(payload)
109
expect(parsed['outer']['inner']).to eq('test value')
110
end
111
end
112
113
it 'converts binary-encoded strings in arrays' do
114
binary_string = +'array item'
115
binary_string.force_encoding(Encoding::BINARY)
116
command_hash = {items: [binary_string, 'utf8 item']}
117
118
common.call(:post, 'test', command_hash)
119
120
expect(common).to have_received(:request) do |_verb, _url, _headers, payload|
121
expect { JSON.parse(payload) }.not_to raise_error
122
parsed = JSON.parse(payload)
123
expect(parsed['items']).to eq(['array item', 'utf8 item'])
124
end
125
end
126
127
it 'raises error for invalid byte sequences' do
128
# Create an invalid UTF-8 byte sequence
129
invalid_string = +"\xFF\xFE"
130
invalid_string.force_encoding(Encoding::BINARY)
131
command_hash = {script: invalid_string}
132
133
expect { common.call(:post, 'execute', command_hash) }
134
.to raise_error(WebDriver::Error::WebDriverError, /Unable to encode string to UTF-8/)
135
end
136
137
it 'handles already UTF-8 encoded strings' do
138
utf8_string = 'already utf-8'
139
command_hash = {script: utf8_string}
140
141
common.call(:post, 'execute', command_hash)
142
143
expect(common).to have_received(:request) do |_verb, _url, _headers, payload|
144
expect { JSON.parse(payload) }.not_to raise_error
145
parsed = JSON.parse(payload)
146
expect(parsed['script']).to eq('already utf-8')
147
end
148
end
149
end
150
end
151
end # Http
152
end # Remote
153
end # WebDriver
154
end # Selenium
155
156