From 7bc41cefa0115067a2e9aab3dbd1924667c46dfe Mon Sep 17 00:00:00 2001 From: Luke Kanies Date: Sat, 14 Feb 2009 17:35:34 -0600 Subject: Adding clarity to query string handling in REST calls We previously only handled simple strings as values, but we know handle true and false as booleans, we URI-escape all strings, and we can yaml-encode and then escape arrays of strings. This could get abused a bit, in that we're just yaml-dumping anything that's an array, but it should be pretty safe. Mmmm, should. Signed-off-by: Luke Kanies --- spec/unit/network/http/mongrel/rest.rb | 34 ++++++++++++++++++++++++++++++---- spec/unit/network/http/webrick/rest.rb | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 60 insertions(+), 8 deletions(-) (limited to 'spec/unit/network') diff --git a/spec/unit/network/http/mongrel/rest.rb b/spec/unit/network/http/mongrel/rest.rb index 9dbc1c9ab..e72d4f540 100755 --- a/spec/unit/network/http/mongrel/rest.rb +++ b/spec/unit/network/http/mongrel/rest.rb @@ -97,16 +97,42 @@ describe "Puppet::Network::HTTP::MongrelREST" do end end - describe "and determining the request parameters", :shared => true do + describe "and determining the request parameters" do before do @request.stubs(:params).returns({}) end - it "should include the HTTP request parameters" do + it "should include the HTTP request parameters, with the keys as symbols" do @request.expects(:params).returns('QUERY_STRING' => 'foo=baz&bar=xyzzy') result = @handler.params(@request) - result["foo"].should == "baz" - result["bar"].should == "xyzzy" + result[:foo].should == "baz" + result[:bar].should == "xyzzy" + end + + it "should URI-decode the HTTP parameters" do + encoding = URI.escape("foo bar") + @request.expects(:params).returns('QUERY_STRING' => "foo=#{encoding}") + result = @handler.params(@request) + result[:foo].should == "foo bar" + end + + it "should convert the string 'true' to the boolean" do + @request.expects(:params).returns('QUERY_STRING' => 'foo=true') + result = @handler.params(@request) + result[:foo].should be_true + end + + it "should convert the string 'false' to the boolean" do + @request.expects(:params).returns('QUERY_STRING' => 'foo=false') + result = @handler.params(@request) + result[:foo].should be_false + end + + it "should YAML-load and URI-decode values that are YAML-encoded" do + escaping = URI.escape(YAML.dump(%w{one two})) + @request.expects(:params).returns('QUERY_STRING' => "foo=#{escaping}") + result = @handler.params(@request) + result[:foo].should == %w{one two} end it "should pass the client's ip address to model find" do diff --git a/spec/unit/network/http/webrick/rest.rb b/spec/unit/network/http/webrick/rest.rb index 1f4ccbe29..b32cf49c5 100755 --- a/spec/unit/network/http/webrick/rest.rb +++ b/spec/unit/network/http/webrick/rest.rb @@ -83,11 +83,37 @@ describe Puppet::Network::HTTP::WEBrickREST do end describe "and determining the request parameters" do - it "should include the HTTP request parameters" do - @request.stubs(:query).returns(:foo => :baz, :bar => :xyzzy) + it "should include the HTTP request parameters, with the keys as symbols" do + @request.stubs(:query).returns("foo" => "baz", "bar" => "xyzzy") result = @handler.params(@request) - result[:foo].should == :baz - result[:bar].should == :xyzzy + result[:foo].should == "baz" + result[:bar].should == "xyzzy" + end + + it "should URI-decode the HTTP parameters" do + encoding = URI.escape("foo bar") + @request.expects(:query).returns('foo' => encoding) + result = @handler.params(@request) + result[:foo].should == "foo bar" + end + + it "should convert the string 'true' to the boolean" do + @request.expects(:query).returns('foo' => "true") + result = @handler.params(@request) + result[:foo].should be_true + end + + it "should convert the string 'false' to the boolean" do + @request.expects(:query).returns('foo' => "false") + result = @handler.params(@request) + result[:foo].should be_false + end + + it "should YAML-load and URI-decode values that are YAML-encoded" do + escaping = URI.escape(YAML.dump(%w{one two})) + @request.expects(:query).returns('foo' => escaping) + result = @handler.params(@request) + result[:foo].should == %w{one two} end it "should pass the client's ip address to model find" do -- cgit