"},
%q({"returnTo":{"\/categories":"\/"}}) => {"returnTo" => {"/categories" => "/"}},
%q({returnTo:{"\/categories":"\/"}}) => {"returnTo" => {"/categories" => "/"}},
%q({"return\\"To\\":":{"\/categories":"\/"}}) => {"return\"To\":" => {"/categories" => "/"}},
%q({"returnTo":{"\/categories":1}}) => {"returnTo" => {"/categories" => 1}},
%({"returnTo":[1,"a"]}) => {"returnTo" => [1, "a"]},
%({"returnTo":[1,"\\"a\\",", "b"]}) => {"returnTo" => [1, "\"a\",", "b"]},
%({"a": "'", "b": "5,000"}) => {"a" => "'", "b" => "5,000"},
%({"a": "a's, b's and c's", "b": "5,000"}) => {"a" => "a's, b's and c's", "b" => "5,000"},
%({"a": "2007-01-01"}) => {'a' => Date.new(2007, 1, 1)},
%({"first_date": "2016-01-25", "second_date": "2014-01-26"}) => {'first_date' => Date.new(2016, 1, 25), 'second_date' => Date.new(2014, 1, 26)},
%({"first_date": "2016-01-25", "non_date": "Abc", "second_date": "2014-01-26"}) => {'first_date' => Date.new(2016, 1, 25), 'non_date' => 'Abc', 'second_date' => Date.new(2014, 1, 26)},
%({"a": "2007-01-01 01:12:34 Z"}) => {'a' => Time.utc(2007, 1, 1, 1, 12, 34)},
# Handle ISO 8601 date/time format http://en.wikipedia.org/wiki/ISO_8601
%({"a": "2007-01-01T01:12:34Z"}) => {'a' => Time.utc(2007, 1, 1, 1, 12, 34)},
# no time zone
%({"a": "2007-01-01 01:12:34"}) => {'a' => "2007-01-01 01:12:34"},
%({"bio": "1985-01-29: birthdate"}) => {'bio' => '1985-01-29: birthdate'},
%({"regex": /foo.*/}) => {'regex' => /foo.*/},
%({"regex": /foo.*/i}) => {'regex' => /foo.*/i},
%({"regex": /foo.*/mix}) => {'regex' => /foo.*/mix},
%([]) => [],
%({}) => {},
%(1) => 1,
%("") => "",
%("\\"") => "\"",
%(null) => nil,
%(true) => true,
%(false) => false,
%q("http:\/\/test.host\/posts\/1") => "http://test.host/posts/1",
# \u0000 and \x00 escape sequences
%q({"foo":"bar\u0000"}) => {"foo" => "bar"},
%q({"foo":"bar\u0000baz"}) => {"foo" => "barbaz"},
%q(bar\u0000) => "bar",
%q(bar\u0000baz) => "barbaz",
%q({"foo":"bar\x00"}) => {"foo" => "bar\x00"},
%q({"foo":"bar\x00baz"}) => {"foo" => "bar\x00baz"}
}
TESTS.each do |json, expected|
it "decode json (#{json})" do
Crack::JSON.parse(json).must_equal expected
end
end
it "is not vulnerable to YAML deserialization exploits" do
class Foo; end
refute_instance_of(Foo, Crack::JSON.parse("# '---/\n--- !ruby/object:Foo\n foo: bar"))
end
it "raise error for failed decoding" do
assert_raises(Crack::ParseError) {
Crack::JSON.parse(%({: 1}))
}
end
it "be able to parse a JSON response from a Twitter search about 'firefox'" do
data = ''
File.open(File.dirname(__FILE__) + "/data/twittersearch-firefox.json", "r") { |f|
data = f.read
}
Crack::JSON.parse(data)
end
it "be able to parse a JSON response from a Twitter search about 'internet explorer'" do
data = ''
File.open(File.dirname(__FILE__) + "/data/twittersearch-ie.json", "r") { |f|
data = f.read
}
Crack::JSON.parse(data)
end
it "does not raise SystemStackError when parsing large JSON files" do
data = ''
File.open(File.dirname(__FILE__) + "/data/large_dataset.json", "r") { |f|
data = f.read
}
Crack::JSON.parse(data)
end
end
ruby-crack-0.4.4/test/parser_test.rb 0000664 0000000 0000000 00000000732 13730707631 0017431 0 ustar 00root root 0000000 0000000 require 'test_helper'
describe Crack::XML do
it "default to REXMLParser" do
Crack::XML.parser.must_equal Crack::REXMLParser
end
describe "with a custom Parser" do
class CustomParser
def self.parse(xml)
xml
end
end
before do
Crack::XML.parser = CustomParser
end
it "use the custom Parser" do
Crack::XML.parse("").must_equal ""
end
after do
Crack::XML.parser = nil
end
end
end
ruby-crack-0.4.4/test/string_test.rb 0000664 0000000 0000000 00000001741 13730707631 0017444 0 ustar 00root root 0000000 0000000 require 'test_helper'
describe Crack::Util do
describe "snake_case" do
it "lowercases one word CamelCase" do
Crack::Util.snake_case("Merb").must_equal "merb"
end
it "makes one underscore snake_case two word CamelCase" do
Crack::Util.snake_case("MerbCore").must_equal "merb_core"
end
it "handles CamelCase with more than 2 words" do
Crack::Util.snake_case("SoYouWantContributeToMerbCore").must_equal "so_you_want_contribute_to_merb_core"
end
it "handles CamelCase with more than 2 capital letter in a row" do
Crack::Util.snake_case("CNN").must_equal "cnn"
Crack::Util.snake_case("CNNNews").must_equal "cnn_news"
Crack::Util.snake_case("HeadlineCNNNews").must_equal "headline_cnn_news"
end
it "does NOT change one word lowercase" do
Crack::Util.snake_case("merb").must_equal "merb"
end
it "leaves snake_case as is" do
Crack::Util.snake_case("merb_core").must_equal "merb_core"
end
end
end
ruby-crack-0.4.4/test/test_helper.rb 0000664 0000000 0000000 00000000070 13730707631 0017407 0 ustar 00root root 0000000 0000000 require 'pp'
require 'minitest/autorun'
require 'crack'
ruby-crack-0.4.4/test/xml_test.rb 0000664 0000000 0000000 00000037017 13730707631 0016743 0 ustar 00root root 0000000 0000000 require 'test_helper'
describe Crack::XML do
it "should transform a simple tag with content" do
xml = "This is the contents"
Crack::XML.parse(xml).must_equal({ 'tag' => 'This is the contents' })
end
it "should work with cdata tags" do
xml = <<-END
END
Crack::XML.parse(xml)["tag"].strip.must_equal "text inside cdata"
end
it "should transform a simple tag with attributes" do
xml = ""
hash = { 'tag' => { 'attr1' => '1', 'attr2' => '2' } }
Crack::XML.parse(xml).must_equal hash
end
it "should transform repeating siblings into an array" do
xml =<<-XML
XML
Crack::XML.parse(xml)['opt']['user'].class.must_equal Array
hash = {
'opt' => {
'user' => [{
'login' => 'grep',
'fullname' => 'Gary R Epstein'
},{
'login' => 'stty',
'fullname' => 'Simon T Tyson'
}]
}
}
Crack::XML.parse(xml).must_equal hash
end
it "should not transform non-repeating siblings into an array" do
xml =<<-XML
XML
Crack::XML.parse(xml)['opt']['user'].class.must_equal Hash
hash = {
'opt' => {
'user' => {
'login' => 'grep',
'fullname' => 'Gary R Epstein'
}
}
}
Crack::XML.parse(xml).must_equal hash
end
describe "Parsing xml with text and attributes" do
before do
xml =<<-XML
Gary R EpsteinSimon T Tyson
XML
@data = Crack::XML.parse(xml)
end
it "correctly parse text nodes" do
@data.must_equal({
'opt' => {
'user' => [
'Gary R Epstein',
'Simon T Tyson'
]
}
})
end
it "be parse attributes for text node if present" do
@data['opt']['user'][0].attributes.must_equal({'login' => 'grep'})
end
it "default attributes to empty hash if not present" do
@data['opt']['user'][1].attributes.must_equal({})
end
it "add 'attributes' accessor methods to parsed instances of String" do
@data['opt']['user'][0].respond_to?(:attributes).must_equal(true)
@data['opt']['user'][0].respond_to?(:attributes=).must_equal(true)
end
it "not add 'attributes' accessor methods to all instances of String" do
"some-string".respond_to?(:attributes).must_equal(false)
"some-string".respond_to?(:attributes=).must_equal(false)
end
end
it "should typecast an integer" do
xml = "10"
Crack::XML.parse(xml)['tag'].must_equal 10
end
it "should typecast a true boolean" do
xml = "true"
Crack::XML.parse(xml)['tag'].must_equal(true)
end
it "should typecast a false boolean" do
["false"].each do |w|
Crack::XML.parse("#{w}")['tag'].must_equal(false)
end
end
it "should typecast a datetime" do
xml = "2007-12-31 10:32"
Crack::XML.parse(xml)['tag'].must_equal Time.parse( '2007-12-31 10:32' ).utc
end
it "should typecast a date" do
xml = "2007-12-31"
Crack::XML.parse(xml)['tag'].must_equal Date.parse('2007-12-31')
end
xml_entities = {
"<" => "<",
">" => ">",
'"' => """,
"'" => "'",
"&" => "&"
}
it "should unescape html entities" do
xml_entities.each do |k,v|
xml = "Some content #{v}"
Crack::XML.parse(xml)['tag'].must_match Regexp.new(k)
end
end
it "should unescape XML entities in attributes" do
xml_entities.each do |k,v|
xml = ""
Crack::XML.parse(xml)['tag']['attr'].must_match Regexp.new(k)
end
end
it "should undasherize keys as tags" do
xml = "Stuff"
Crack::XML.parse(xml).keys.must_include( 'tag_1' )
end
it "should undasherize keys as attributes" do
xml = ""
Crack::XML.parse(xml)['tag1'].keys.must_include( 'attr_1')
end
it "should undasherize keys as tags and attributes" do
xml = ""
Crack::XML.parse(xml).keys.must_include( 'tag_1' )
Crack::XML.parse(xml)['tag_1'].keys.must_include( 'attr_1')
end
it "should render nested content correctly" do
xml = "Tag1 Content This is strong"
Crack::XML.parse(xml)['root']['tag1'].must_equal "Tag1 Content This is strong"
end
it "should render nested content with splshould text nodes correctly" do
xml = "Tag1 ContentStuff Hi There"
Crack::XML.parse(xml)['root'].must_equal "Tag1 ContentStuff Hi There"
end
it "should ignore attributes when a child is a text node" do
xml = "Stuff"
Crack::XML.parse(xml).must_equal({ "root" => "Stuff" })
end
it "should ignore attributes when any child is a text node" do
xml = "Stuff in italics"
Crack::XML.parse(xml).must_equal({ "root" => "Stuff in italics" })
end
it "should correctly transform multiple children" do
xml = <<-XML
35Home Simpson1988-01-012000-04-28 23:01true
XML
hash = {
"user" => {
"gender" => "m",
"age" => 35,
"name" => "Home Simpson",
"dob" => Date.parse('1988-01-01'),
"joined_at" => Time.parse("2000-04-28 23:01"),
"is_cool" => true
}
}
Crack::XML.parse(xml).must_equal hash
end
it "should properly handle nil values (ActiveSupport Compatible)" do
topic_xml = <<-EOT
EOT
expected_topic_hash = {
'title' => nil,
'id' => nil,
'approved' => nil,
'written_on' => nil,
'viewed_at' => nil,
'parent_id' => nil
}
Crack::XML.parse(topic_xml)["topic"].must_equal expected_topic_hash
end
it "should handle a single record from xml (ActiveSupport Compatible)" do
topic_xml = <<-EOT
The First TopicDavid1 true 025920000002003-07-162003-07-16T09:28:00+0000david@loudthinking.com1.5135yes
EOT
expected_topic_hash = {
'title' => "The First Topic",
'author_name' => "David",
'id' => 1,
'approved' => true,
'replies_count' => 0,
'replies_close_in' => 2592000000,
'written_on' => Date.new(2003, 7, 16),
'viewed_at' => Time.utc(2003, 7, 16, 9, 28),
'author_email_address' => "david@loudthinking.com",
'parent_id' => nil,
'ad_revenue' => BigDecimal("1.50"),
'optimum_viewing_angle' => 135.0,
'resident' => 'yes',
}
Crack::XML.parse(topic_xml)["topic"].each do |k,v|
v.must_equal expected_topic_hash[k]
end
end
it "should handle multiple records (ActiveSupport Compatible)" do
topics_xml = <<-EOT
The First TopicDavid1false025920000002003-07-162003-07-16T09:28:00+0000Have a nice daydavid@loudthinking.comThe Second TopicJason1false025920000002003-07-162003-07-16T09:28:00+0000Have a nice daydavid@loudthinking.com
EOT
expected_topic_hash = {
'title' => "The First Topic",
'author_name' => "David",
'id' => 1,
'approved' => false,
'replies_count' => 0,
'replies_close_in' => 2592000000,
'written_on' => Date.new(2003, 7, 16),
'viewed_at' => Time.utc(2003, 7, 16, 9, 28),
'content' => "Have a nice day",
'author_email_address' => "david@loudthinking.com",
'parent_id' => nil
}
# puts Crack::XML.parse(topics_xml)['topics'].first.inspect
Crack::XML.parse(topics_xml)["topics"].first.each do |k,v|
v.must_equal expected_topic_hash[k]
end
end
it "should handle a single record from_xml with attributes other than type (ActiveSupport Compatible)" do
topic_xml = <<-EOT
EOT
expected_topic_hash = {
'id' => "175756086",
'owner' => "55569174@N00",
'secret' => "0279bf37a1",
'server' => "76",
'title' => "Colored Pencil PhotoBooth Fun",
'ispublic' => "1",
'isfriend' => "0",
'isfamily' => "0",
}
Crack::XML.parse(topic_xml)["rsp"]["photos"]["photo"].each do |k,v|
v.must_equal expected_topic_hash[k]
end
end
it "should handle an emtpy array (ActiveSupport Compatible)" do
blog_xml = <<-XML
XML
expected_blog_hash = {"blog" => {"posts" => []}}
Crack::XML.parse(blog_xml).must_equal expected_blog_hash
end
it "should handle empty array with whitespace from xml (ActiveSupport Compatible)" do
blog_xml = <<-XML
XML
expected_blog_hash = {"blog" => {"posts" => []}}
Crack::XML.parse(blog_xml).must_equal expected_blog_hash
end
it "should handle array with one entry from_xml (ActiveSupport Compatible)" do
blog_xml = <<-XML
a post
XML
expected_blog_hash = {"blog" => {"posts" => ["a post"]}}
Crack::XML.parse(blog_xml).must_equal expected_blog_hash
end
it "should handle array with multiple entries from xml (ActiveSupport Compatible)" do
blog_xml = <<-XML
a postanother post
XML
expected_blog_hash = {"blog" => {"posts" => ["a post", "another post"]}}
Crack::XML.parse(blog_xml).must_equal expected_blog_hash
end
it "should handle file types (ActiveSupport Compatible)" do
blog_xml = <<-XML
XML
hash = Crack::XML.parse(blog_xml)
hash.keys.must_include('blog')
hash['blog'].keys.must_include('logo')
file = hash['blog']['logo']
file.original_filename.must_equal 'logo.png'
file.content_type.must_equal 'image/png'
end
it "should handle file from xml with defaults (ActiveSupport Compatible)" do
blog_xml = <<-XML
XML
file = Crack::XML.parse(blog_xml)['blog']['logo']
file.original_filename.must_equal 'untitled'
file.content_type.must_equal 'application/octet-stream'
end
it "should handle xsd like types from xml (ActiveSupport Compatible)" do
bacon_xml = <<-EOT
0.512.50 1 2007-12-25T12:34:56+0000YmFiZS5wbmc=
EOT
expected_bacon_hash = {
'weight' => 0.5,
'chunky' => true,
'price' => BigDecimal("12.50"),
'expires_at' => Time.utc(2007,12,25,12,34,56),
'notes' => "",
'illustration' => "babe.png"
}
Crack::XML.parse(bacon_xml)["bacon"].must_equal expected_bacon_hash
end
it "should let type trickle through when unknown (ActiveSupport Compatible)" do
product_xml = <<-EOT
0.5image.gif
EOT
expected_product_hash = {
'weight' => 0.5,
'image' => {'type' => 'ProductImage', 'filename' => 'image.gif' },
}
Crack::XML.parse(product_xml)["product"].must_equal expected_product_hash
end
it "should handle unescaping from xml (ActiveResource Compatible)" do
xml_string = 'First & Last NameFirst & Last Name'
expected_hash = {
'bare_string' => 'First & Last Name',
'pre_escaped_string' => 'First & Last Name'
}
Crack::XML.parse(xml_string)['person'].must_equal expected_hash
end
it "handle an empty xml string" do
Crack::XML.parse('').must_equal({})
end
# As returned in the response body by the unfuddle XML API when creating objects
it "handle an xml string containing a single space" do
Crack::XML.parse(' ').must_equal({})
end
it "can dump parsed xml" do
xml = <<-XML
I like big butts and I cannot Lie
XML
Marshal.dump(Crack::XML.parse(xml))
end
it "properly handles a node with type == file that has children" do
# Example is an excerpt from a problematic kvm domain config file
example_xml = <<-EOT
EOT
Crack::XML.parse(example_xml).must_equal({"disk"=>{"driver"=>{"name"=>"qemu", "type"=>"raw", "cache"=>"none", "io"=>"native"}, "source"=>{"file"=>"/tmp/cdrom.iso"}, "type"=>"file", "device"=>"cdrom"}})
end
end