diff options
-rw-r--r-- | lib/puppet/network/http/handler.rb | 13 | ||||
-rwxr-xr-x | spec/unit/network/http/handler.rb | 31 |
2 files changed, 43 insertions, 1 deletions
diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb index 6f5117b16..7c7abccf5 100644 --- a/lib/puppet/network/http/handler.rb +++ b/lib/puppet/network/http/handler.rb @@ -12,7 +12,18 @@ module Puppet::Network::HTTP::Handler # Which format to use when serializing our response. Just picks # the first value in the accept header, at this point. def format_to_use(request) - accept_header(request).split(/,\s*/)[0] + unless header = accept_header(request) + raise ArgumentError, "An Accept header must be provided to pick the right format" + end + + format = nil + header.split(/,\s*/).each do |name| + next unless format = Puppet::Network::FormatHandler.format(name) + next unless format.suitable? + return name + end + + raise "No specified acceptable formats (%s) are functional on this machine" % header end def initialize_for_puppet(args = {}) diff --git a/spec/unit/network/http/handler.rb b/spec/unit/network/http/handler.rb index 6fc932091..ed0f25121 100755 --- a/spec/unit/network/http/handler.rb +++ b/spec/unit/network/http/handler.rb @@ -224,6 +224,9 @@ describe Puppet::Network::HTTP::Handler do @handler.stubs(:singular?).returns(true) @handler.stubs(:request_key).returns('key') @model_class.stubs(:find).returns @result + + @format = stub 'format', :suitable? => true + Puppet::Network::FormatHandler.stubs(:format).returns @format end it "should fail to find model if key is not specified" do @@ -246,6 +249,28 @@ describe Puppet::Network::HTTP::Handler do @handler.do_find(@request, @response) end + it "should fail if no accept header is provided" do + @handler.expects(:accept_header).with(@request).returns nil + lambda { @handler.do_find(@request, @response) }.should raise_error(ArgumentError) + end + + it "should fail if the accept header does not contain a valid format" do + @handler.expects(:accept_header).with(@request).returns "" + lambda { @handler.do_find(@request, @response) }.should raise_error(RuntimeError) + end + + it "should not use an unsuitable format" do + @handler.expects(:accept_header).with(@request).returns "foo,bar" + foo = mock 'foo', :suitable? => false + bar = mock 'bar', :suitable? => true + Puppet::Network::FormatHandler.expects(:format).with("foo").returns foo + Puppet::Network::FormatHandler.expects(:format).with("bar").returns bar + + @handler.expects(:set_content_type).with(@response, "bar") # the suitable one + + @handler.do_find(@request, @response) + end + it "should render the result using the first format specified in the accept header" do @handler.expects(:accept_header).with(@request).returns "one,two" @result.expects(:render).with("one") @@ -298,6 +323,9 @@ describe Puppet::Network::HTTP::Handler do @result = [@result1, @result2] @model_class.stubs(:render_multiple).returns "my rendered instances" @model_class.stubs(:search).returns(@result) + + @format = stub 'format', :suitable? => true + Puppet::Network::FormatHandler.stubs(:format).returns @format end it "should use a common method for determining the request parameters" do @@ -409,6 +437,9 @@ describe Puppet::Network::HTTP::Handler do @model_instance = stub('indirected model instance', :save => true) @model_class.stubs(:convert_from).returns(@model_instance) + + @format = stub 'format', :suitable? => true + Puppet::Network::FormatHandler.stubs(:format).returns @format end it "should use the 'body' hook to retrieve the body of the request" do |