summary refs log tree commit
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2009-04-28 00:20:38 -0500
committerJoshua Peek <josh@joshpeek.com>2009-04-28 00:20:38 -0500
commit4c6efcad787683fb9e93ac490628a33f86b7c9f9 (patch)
treea2f49b6e44e25c1750642e9a3d05fcf2601a2a51
parent884770528a349eec8d9d4f91bf252e0d53e35641 (diff)
downloadrack-4c6efcad787683fb9e93ac490628a33f86b7c9f9.tar.gz
Fix brittle query string generation tests
-rw-r--r--test/spec_rack_mock.rb6
-rw-r--r--test/spec_rack_utils.rb62
2 files changed, 28 insertions, 40 deletions
diff --git a/test/spec_rack_mock.rb b/test/spec_rack_mock.rb
index f26f52f1..bf09c2e0 100644
--- a/test/spec_rack_mock.rb
+++ b/test/spec_rack_mock.rb
@@ -132,7 +132,8 @@ context "Rack::MockRequest" do
     res = Rack::MockRequest.new(app).get("/foo?baz=2", :params => {:foo => {:bar => "1"}})
     env = YAML.load(res.body)
     env["REQUEST_METHOD"].should.equal "GET"
-    env["QUERY_STRING"].should.equal "foo[bar]=1&baz=2"
+    env["QUERY_STRING"].should.match "baz=2"
+    env["QUERY_STRING"].should.match "foo[bar]=1"
     env["PATH_INFO"].should.equal "/foo"
     env["mock.postdata"].should.equal ""
   end
@@ -141,7 +142,8 @@ context "Rack::MockRequest" do
     res = Rack::MockRequest.new(app).get("/foo?baz=2", :params => "foo[bar]=1")
     env = YAML.load(res.body)
     env["REQUEST_METHOD"].should.equal "GET"
-    env["QUERY_STRING"].should.equal "foo[bar]=1&baz=2"
+    env["QUERY_STRING"].should.match "baz=2"
+    env["QUERY_STRING"].should.match "foo[bar]=1"
     env["PATH_INFO"].should.equal "/foo"
     env["mock.postdata"].should.equal ""
   end
diff --git a/test/spec_rack_utils.rb b/test/spec_rack_utils.rb
index 13103983..5abeaf42 100644
--- a/test/spec_rack_utils.rb
+++ b/test/spec_rack_utils.rb
@@ -123,10 +123,6 @@ context "Rack::Utils" do
 
     Rack::Utils.build_nested_query("foo" => "1", "bar" => "2").
       should.equal "foo=1&bar=2"
-    Rack::Utils.build_nested_query("foo" => nil, "bar" => "").
-      should.equal "foo&bar="
-    Rack::Utils.build_nested_query("foo" => "bar", "baz" => "").
-      should.equal "foo=bar&baz="
     Rack::Utils.build_nested_query("my weird field" => "q1!2\"'w$5&7/z8)?").
       should.equal "my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F"
 
@@ -137,40 +133,30 @@ context "Rack::Utils" do
     Rack::Utils.build_nested_query("foo" => ["bar"]).
       should.equal "foo[]=bar"
 
-    Rack::Utils.build_nested_query("foo" => ["1", "2"]).
-      should.equal "foo[]=1&foo[]=2"
-    Rack::Utils.build_nested_query("foo" => "bar", "baz" => ["1", "2", "3"]).
-      should.equal "foo=bar&baz[]=1&baz[]=2&baz[]=3"
-    Rack::Utils.build_nested_query("foo" => ["bar"], "baz" => ["1", "2", "3"]).
-      should.equal "foo[]=bar&baz[]=1&baz[]=2&baz[]=3"
-
-    Rack::Utils.build_nested_query("foo" => ["1", "2"]).
-      should.equal "foo[]=1&foo[]=2"
-    Rack::Utils.build_nested_query("foo" => "bar", "baz" => ["1", "2", "3"]).
-      should.equal "foo=bar&baz[]=1&baz[]=2&baz[]=3"
-    Rack::Utils.build_nested_query("x" => {"y" => {"z" => "1"}}).
-      should.equal "x[y][z]=1"
-    Rack::Utils.build_nested_query("x" => {"y" => {"z" => ["1"]}}).
-      should.equal "x[y][z][]=1"
-    Rack::Utils.build_nested_query("x" => {"y" => {"z" => ["1", "2"]}}).
-      should.equal "x[y][z][]=1&x[y][z][]=2"
-
-    Rack::Utils.build_nested_query("x" => {"y" => [{"z" => "1"}]}).
-      should.equal "x[y][][z]=1"
-    Rack::Utils.build_nested_query("x" => {"y" => [{"z" => ["1"]}]}).
-      should.equal "x[y][][z][]=1"
-    Rack::Utils.build_nested_query("x" => {"y" => [{"z" => "1", "w" => "2"}]}).
-      should.equal "x[y][][z]=1&x[y][][w]=2"
-
-    Rack::Utils.build_nested_query("x" => {"y" => [{"v" => {"w" => "1"}}]}).
-      should.equal "x[y][][v][w]=1"
-    Rack::Utils.build_nested_query("x" => {"y" => [{"z" => "1", "v" => {"w" => "2"}}]}).
-      should.equal "x[y][][z]=1&x[y][][v][w]=2"
-
-    Rack::Utils.build_nested_query("x" => {"y" => [{"z" => "1"}, {"z" => "2"}]}).
-      should.equal "x[y][][z]=1&x[y][][z]=2"
-    Rack::Utils.build_nested_query("x" => {"y" => [{"z" => "1", "w" => "a"}, {"z" => "2", "w" => "3"}]}).
-      should.equal "x[y][][z]=1&x[y][][w]=a&x[y][][z]=2&x[y][][w]=3"
+    # The ordering of the output query string is unpredictable with 1.8's
+    # unordered hash. Test that build_nested_query performs the inverse
+    # function of parse_nested_query.
+    [{"foo" => nil, "bar" => ""},
+     {"foo" => "bar", "baz" => ""},
+     {"foo" => ["1", "2"]},
+     {"foo" => "bar", "baz" => ["1", "2", "3"]},
+     {"foo" => ["bar"], "baz" => ["1", "2", "3"]},
+     {"foo" => ["1", "2"]},
+     {"foo" => "bar", "baz" => ["1", "2", "3"]},
+     {"x" => {"y" => {"z" => "1"}}},
+     {"x" => {"y" => {"z" => ["1"]}}},
+     {"x" => {"y" => {"z" => ["1", "2"]}}},
+     {"x" => {"y" => [{"z" => "1"}]}},
+     {"x" => {"y" => [{"z" => ["1"]}]}},
+     {"x" => {"y" => [{"z" => "1", "w" => "2"}]}},
+     {"x" => {"y" => [{"v" => {"w" => "1"}}]}},
+     {"x" => {"y" => [{"z" => "1", "v" => {"w" => "2"}}]}},
+     {"x" => {"y" => [{"z" => "1"}, {"z" => "2"}]}},
+     {"x" => {"y" => [{"z" => "1", "w" => "a"}, {"z" => "2", "w" => "3"}]}}
+    ].each { |params|
+      qs = Rack::Utils.build_nested_query(params)
+      Rack::Utils.parse_nested_query(qs).should.equal params
+    }
 
     lambda { Rack::Utils.build_nested_query("foo=bar") }.
       should.raise(ArgumentError).