-
-
Notifications
You must be signed in to change notification settings - Fork 388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add specs for CGI.escapeURIComponent
#1080
Merged
andrykonchin
merged 7 commits into
ruby:master
from
lxxxvi:add-specs-for-CGI-escapeURIComponent
Oct 30, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cc2162c
add specs for CGI.escapeURIComponent
lxxxvi 5e78549
Update library/cgi/escapeURIComponent_spec.rb
lxxxvi bf4bb06
improve spec descriptions
lxxxvi 3b3319f
move version guard to top-level
lxxxvi cbb32d4
assert error message, not only type
lxxxvi 32ac6d8
add spec with implicit type conversion
lxxxvi 16648f4
Polish a bit specs
andrykonchin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
require_relative '../../spec_helper' | ||
require 'cgi' | ||
|
||
ruby_version_is "3.2" do | ||
describe "CGI.escapeURIComponent" do | ||
it "escapes whitespace" do | ||
string = "&<>\" \xE3\x82\x86\xE3\x82\x93\xE3\x82\x86\xE3\x82\x93" | ||
CGI.escapeURIComponent(string).should == '%26%3C%3E%22%20%E3%82%86%E3%82%93%E3%82%86%E3%82%93' | ||
end | ||
|
||
it "does not escape with unreserved characters" do | ||
string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~" | ||
CGI.escapeURIComponent(string).should == "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~" | ||
end | ||
|
||
it "supports String with invalid encoding" do | ||
string = "\xC0\<\<".force_encoding("UTF-8") | ||
CGI.escapeURIComponent(string).should == "%C0%3C%3C" | ||
end | ||
|
||
it "processes String bytes one by one, not characters" do | ||
CGI.escapeURIComponent("β").should == "%CE%B2" # "β" bytes representation is CE B2 | ||
end | ||
|
||
it "raises a TypeError with nil" do | ||
-> { | ||
CGI.escapeURIComponent(nil) | ||
}.should raise_error(TypeError, 'no implicit conversion of nil into String') | ||
end | ||
|
||
it "encodes empty string" do | ||
CGI.escapeURIComponent("").should == "" | ||
end | ||
|
||
it "encodes single whitespace" do | ||
CGI.escapeURIComponent(" ").should == "%20" | ||
end | ||
|
||
it "encodes double whitespace" do | ||
CGI.escapeURIComponent(" ").should == "%20%20" | ||
end | ||
|
||
it "preserves encoding" do | ||
string = "whatever".encode("ASCII-8BIT") | ||
CGI.escapeURIComponent(string).encoding.should == Encoding::ASCII_8BIT | ||
end | ||
|
||
it "uses implicit type conversion to String" do | ||
object = Object.new | ||
def object.to_str | ||
"a b" | ||
end | ||
|
||
CGI.escapeURIComponent(object).should == "a%20b" | ||
end | ||
end | ||
end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: we could add some other cases:
#to_str
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit uncertain here. I added a spec for the implicit type conversion, but I'm not sure if I understood it correctly what you suggested. Please let me know if I misunderstood.
I could use a pointer regarding the ascii-not-compatible string idea... how could I approach this? Thanks for your help, I appreciate it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding implicit conversion - you are correct. Actually you may look for similar test cases, e.g. here:
spec/core/basicobject/instance_eval_spec.rb
Lines 287 to 301 in db5f026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant that most of the test cases use ASCII characters. But the method actually handles multibyte encodings, e.g. UTF-8/UTF-16/UTF-32 and just converts their bytes one by one (ignoring "characters" that could contain several bytes):