Skip to content

Commit

Permalink
Nokogiri::Schema{,new,read_memory}() support argument forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
openbl committed Nov 14, 2024
1 parent c3ae13d commit 1e2632e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/nokogiri/xml/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class << self
# - +parse_options+ (Nokogiri::XML::ParseOptions)
# [Returns] Nokogiri::XML::Schema
#
def Schema(input, parse_options = ParseOptions::DEFAULT_SCHEMA)
Schema.new(input, parse_options)
def Schema(...)
Schema.new(...)
end
end

Expand Down Expand Up @@ -67,8 +67,8 @@ class Schema
#
# [Returns] Nokogiri::XML::Schema
#
def self.new(input, parse_options = ParseOptions::DEFAULT_SCHEMA)
read_memory(input, parse_options)
def self.new(...)
read_memory(...)
end

# :call-seq:
Expand All @@ -86,7 +86,7 @@ def self.new(input, parse_options = ParseOptions::DEFAULT_SCHEMA)
# Defaults to Nokogiri::XML::ParseOptions::DEFAULT_SCHEMA
#
# [Returns] Nokogiri::XML::Schema
def self.read_memory(input, parse_options = ParseOptions::DEFAULT_SCHEMA)
def self.read_memory(input, parse_options_ = ParseOptions::DEFAULT_SCHEMA, parse_options: parse_options_)
from_document(Nokogiri::XML::Document.parse(input), parse_options)
end

Expand Down
30 changes: 30 additions & 0 deletions test/xml/test_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,16 @@ class TestNokogiriXMLSchema < Nokogiri::TestCase
assert_equal(1, errors.grep(%r{failed to load.*http://localhost:8000/making-a-request}).length)
end

it "XML::Schema parsing attempts to access external DTDs with kwargs" do
doc = Nokogiri::XML::Schema.new(schema, parse_options: Nokogiri::XML::ParseOptions.new.nononet)
errors = doc.errors.map(&:to_s)
assert_empty(
errors.grep(/Attempt to load network entity/),
"Should not see xmlIO.c:xmlNoNetExternalEntityLoader() raising XML_IO_NETWORK_ATTEMPT",
)
assert_equal(1, errors.grep(%r{failed to load.*http://localhost:8000/making-a-request}).length)
end

it "XML::Schema parsing of memory attempts to access external DTDs" do
doc = Nokogiri::XML::Schema.read_memory(schema, Nokogiri::XML::ParseOptions.new.nononet)
errors = doc.errors.map(&:to_s)
Expand All @@ -407,6 +417,16 @@ class TestNokogiriXMLSchema < Nokogiri::TestCase
)
assert_equal(1, errors.grep(%r{failed to load.*http://localhost:8000/making-a-request}).length)
end

it "XML::Schema parsing of memory attempts to access external DTDs with kwargs" do
doc = Nokogiri::XML::Schema.read_memory(schema, parse_options: Nokogiri::XML::ParseOptions.new.nononet)
errors = doc.errors.map(&:to_s)
assert_empty(
errors.grep(/ERROR: Attempt to load network entity/),
"Should not see xmlIO.c:xmlNoNetExternalEntityLoader() raising XML_IO_NETWORK_ATTEMPT",
)
assert_equal(1, errors.grep(%r{failed to load.*http://localhost:8000/making-a-request}).length)
end
end
end

Expand All @@ -429,10 +449,20 @@ class TestNokogiriXMLSchema < Nokogiri::TestCase
assert_equal 0, doc.errors.map(&:to_s).grep(/WARNING: Attempt to load network entity/).length
end

it "XML::Schema parsing attempts to access external DTDs with kwargs" do
doc = Nokogiri::XML::Schema.new(schema, parse_options: Nokogiri::XML::ParseOptions.new.nononet)
assert_equal 0, doc.errors.map(&:to_s).grep(/WARNING: Attempt to load network entity/).length
end

it "XML::Schema parsing of memory attempts to access external DTDs" do
doc = Nokogiri::XML::Schema.read_memory(schema, Nokogiri::XML::ParseOptions.new.nononet)
assert_equal 0, doc.errors.map(&:to_s).grep(/WARNING: Attempt to load network entity/).length
end

it "XML::Schema parsing of memory attempts to access external DTDs with kwargs" do
doc = Nokogiri::XML::Schema.read_memory(schema, parse_options: :XML::ParseOptions.new.nononet)
assert_equal 0, doc.errors.map(&:to_s).grep(/WARNING: Attempt to load network entity/).length
end
end
end
end
Expand Down

0 comments on commit 1e2632e

Please sign in to comment.