Skip to content

Commit

Permalink
more consistent IDNA handling of wrong input
Browse files Browse the repository at this point in the history
  • Loading branch information
jarthod committed Apr 18, 2023
1 parent 7d75d6c commit f0b98df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/addressable/idna.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class << self

# public interface implemented by all backends
def to_ascii(value)
backend.to_ascii(value)
backend.to_ascii(value) if value.is_a?(String)
rescue Error
strict_mode ? raise : value
end

def to_unicode(value)
backend.to_unicode(value)
backend.to_unicode(value) if value.is_a?(String)
rescue Error
strict_mode ? raise : value
end
Expand Down
14 changes: 14 additions & 0 deletions spec/addressable/idna_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@
"example..host"
)).to eq("example..host")
end

it "handles nil input" do
expect(Addressable::IDNA.to_ascii(nil)).to eq(nil)
expect(Addressable::IDNA.to_ascii(45)).to eq(nil)
expect(Addressable::IDNA.to_ascii([])).to eq(nil)
expect(Addressable::IDNA.to_ascii({})).to eq(nil)
end
end

shared_examples_for "converting from ASCII to unicode" do
Expand Down Expand Up @@ -256,6 +263,13 @@
"example..host"
)).to eq("example..host")
end

it "handles unexpected input as nil" do
expect(Addressable::IDNA.to_unicode(nil)).to eq(nil)
expect(Addressable::IDNA.to_unicode(45)).to eq(nil)
expect(Addressable::IDNA.to_unicode([])).to eq(nil)
expect(Addressable::IDNA.to_unicode({})).to eq(nil)
end
end

describe Addressable::IDNA, "when using the pure-Ruby implementation" do
Expand Down

0 comments on commit f0b98df

Please sign in to comment.