require 'test/unit' require 'glue/uri' class Dummy # :nodoc: all attr_accessor :test1, :test2 end class TC_Uri < Test::Unit::TestCase # :nodoc: all include Glue def test_query_string_to_hash # bad query string assert_equal(0, UriUtils.query_string_to_hash("").length) assert_equal(0, UriUtils.query_string_to_hash(nil).length) # single valued parameters = UriUtils.query_string_to_hash("koko=2&lala=3") assert_equal("2", parameters["koko"]) assert_equal("3", parameters["lala"]) parameters = UriUtils.query_string_to_hash("koko=2;lala=3") assert_equal("2", parameters["koko"]) assert_equal("3", parameters["lala"]) # multivalued parameters = UriUtils.query_string_to_hash("koko=2;lala=3&koko=5") assert_equal(2, parameters["koko"].length) assert_equal("2", parameters["koko"][0]) assert_equal("3", parameters["lala"]) assert_equal("5", parameters["koko"][1]) # non existing value assert_equal(nil, parameters["non-existing-value"]) end def test_hash_to_query_string hash = { "koko" => 1, "lala" => 2} qs = "koko=1;lala=2" assert_equal(qs, UriUtils.hash_to_query_string(hash)) assert_equal(nil, UriUtils.hash_to_query_string(nil)) # bug: dont encode complex objects hash = { "a" => 2, "b" => 3, "c" => Dummy.new } qs = "a=2;b=3" assert_equal(qs, UriUtils.hash_to_query_string(hash)) end def test_get_query_string uri = "people/gmosx.sx?koko=1;lala=2" assert_equal("koko=1;lala=2", UriUtils.get_query_string(uri)) uri = "http://www.navel.gr/people/gmosx.sx?koko=1&lala=2" assert_equal("koko=1&lala=2", UriUtils.get_query_string(uri)) uri = "http://www.navel.gr:8080/people/gmosx.sx?koko=1;lala=2" assert_equal("koko=1;lala=2", UriUtils.get_query_string(uri)) end def test_chomp_query_string uri = "people/gmosx.sx" assert_equal("people/gmosx.sx", UriUtils.chomp_query_string(uri)) uri = "people/gmosx.sx?koko=1;lala=2" assert_equal("people/gmosx.sx", UriUtils.chomp_query_string(uri)) uri = "http://www.navel.gr/people/gmosx.sx?koko=1&lala=2" assert_equal("http://www.navel.gr/people/gmosx.sx", UriUtils.chomp_query_string(uri)) uri = "http://www.navel.gr:8080/people/gmosx.sx?koko=1;lala=2" assert_equal("http://www.navel.gr:8080/people/gmosx.sx", UriUtils.chomp_query_string(uri)) assert_equal(nil, UriUtils.chomp_query_string(nil)) end def test_update_query_string uri = "ko/index.sx?koko=1" hash = {"lala" => 2, "kaka" => 3} assert_equal("ko/index.sx?koko=1;lala=2;kaka=3", UriUtils.update_query_string(uri, hash)) uri = "http://www.navel.gr:8080/ko/index.sx?koko=1" hash = {"lala" => 2, "kaka" => 3} assert_equal("http://www.navel.gr:8080/ko/index.sx?koko=1;lala=2;kaka=3", UriUtils.update_query_string(uri, hash)) uri = "http://www.navel.gr" hash = {"lala" => 2, "kaka" => 3} assert_equal("http://www.navel.gr?lala=2;kaka=3", UriUtils.update_query_string(uri, hash)) # bug: no ? when passed an empty hash uri = "http://www.navel.gr" assert_equal("http://www.navel.gr", UriUtils.update_query_string(uri, {})) end end