Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitea6f8e5

Browse files
authored
[rb] Add force encoding to remove warnings caused by json 3.0 (#16728)
* add force encoding for compatibility with json 3* fix rubocop issues
1 parentb703512 commitea6f8e5

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

‎rb/lib/selenium/webdriver/remote/http/common.rb‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Common
2828
'Accept'=>CONTENT_TYPE,
2929
'Content-Type'=>"#{CONTENT_TYPE}; charset=UTF-8"
3030
}.freeze
31+
BINARY_ENCODINGS=[Encoding::BINARY,Encoding::ASCII_8BIT].freeze
3132

3233
class <<self
3334
attr_accessor:extra_headers
@@ -55,6 +56,7 @@ def call(verb, url, command_hash)
5556
headers['Cache-Control']='no-cache'ifverb ==:get
5657

5758
ifcommand_hash
59+
command_hash=ensure_utf8_encoding(command_hash)
5860
payload=JSON.generate(command_hash)
5961
headers['Content-Length']=payload.bytesize.to_sif%i[postput].include?(verb)
6062

@@ -91,6 +93,36 @@ def request(*)
9193
raiseNotImplementedError,'subclass responsibility'
9294
end
9395

96+
defensure_utf8_encoding(obj)
97+
caseobj
98+
whenString
99+
encode_string_to_utf8(obj)
100+
whenArray
101+
obj.map{ |item|ensure_utf8_encoding(item)}
102+
whenHash
103+
obj.each_with_object({})do |(key,value),result|
104+
result[ensure_utf8_encoding(key)]=ensure_utf8_encoding(value)
105+
end
106+
else
107+
obj
108+
end
109+
end
110+
111+
defencode_string_to_utf8(str)
112+
returnstrifstr.encoding ==Encoding::UTF_8 &&str.valid_encoding?
113+
114+
ifBINARY_ENCODINGS.include?(str.encoding)
115+
result=str.dup.force_encoding(Encoding::UTF_8)
116+
returnresultifresult.valid_encoding?
117+
end
118+
119+
str.encode(Encoding::UTF_8)
120+
rescueEncodingError=>e
121+
raiseError::WebDriverError,
122+
"Unable to encode string to UTF-8:#{e.message}. " \
123+
"String encoding:#{str.encoding}, content:#{str.inspect}"
124+
end
125+
94126
defcreate_response(code,body,content_type)
95127
code=code.to_i
96128
body=body.to_s.strip

‎rb/spec/unit/selenium/webdriver/remote/http/common_spec.rb‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,79 @@ module Http
7474
.with(:post,URI.parse('http://server/session'),
7575
hash_including('User-Agent'=>'rspec/1.0 (ruby 3.2)'),'{}')
7676
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).tohave_received(:request)do |_verb,_url,_headers,payload|
87+
expect{JSON.parse(payload)}.not_toraise_error
88+
parsed=JSON.parse(payload)
89+
expect(parsed['script']).toeq('return navigator.userAgent;')
90+
expect(parsed['script'].encoding).toeq(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).tohave_received(:request)do |_verb,_url,_headers,payload|
107+
expect{JSON.parse(payload)}.not_toraise_error
108+
parsed=JSON.parse(payload)
109+
expect(parsed['outer']['inner']).toeq('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).tohave_received(:request)do |_verb,_url,_headers,payload|
121+
expect{JSON.parse(payload)}.not_toraise_error
122+
parsed=JSON.parse(payload)
123+
expect(parsed['items']).toeq(['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+
.toraise_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).tohave_received(:request)do |_verb,_url,_headers,payload|
144+
expect{JSON.parse(payload)}.not_toraise_error
145+
parsed=JSON.parse(payload)
146+
expect(parsed['script']).toeq('already utf-8')
147+
end
148+
end
149+
end
77150
end
78151
end# Http
79152
end# Remote

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp