diff options
-rw-r--r-- | lib/puppet/indirector/rest.rb | 5 | ||||
-rw-r--r-- | lib/puppet/network/http/handler.rb | 4 | ||||
-rwxr-xr-x | spec/integration/indirector/rest.rb | 9 | ||||
-rwxr-xr-x | spec/unit/indirector/rest.rb | 6 | ||||
-rwxr-xr-x | spec/unit/network/http/handler.rb | 14 |
5 files changed, 35 insertions, 3 deletions
diff --git a/lib/puppet/indirector/rest.rb b/lib/puppet/indirector/rest.rb index e59b332c0..a2767d05b 100644 --- a/lib/puppet/indirector/rest.rb +++ b/lib/puppet/indirector/rest.rb @@ -46,7 +46,10 @@ class Puppet::Indirector::REST < Puppet::Indirector::Terminus else path = "/#{indirection.name}s" end - deserialize(network.get(path, headers), true) + unless result = deserialize(network.get(path, headers), true) + return [] + end + return result end def destroy(request) diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb index 0069933bd..291481acd 100644 --- a/lib/puppet/network/http/handler.rb +++ b/lib/puppet/network/http/handler.rb @@ -82,8 +82,10 @@ module Puppet::Network::HTTP::Handler def do_search(request, response) args = params(request) result = model.search(args) + if result.nil? or (result.is_a?(Array) and result.empty?) + return do_exception(response, "Could not find instances in %s with '%s'" % [model.name, args.inspect], 404) + end - # LAK:FAIL This doesn't work. format = format_to_use(request) set_content_type(response, format) diff --git a/spec/integration/indirector/rest.rb b/spec/integration/indirector/rest.rb index 86d4d4291..b307e3cab 100755 --- a/spec/integration/indirector/rest.rb +++ b/spec/integration/indirector/rest.rb @@ -138,6 +138,9 @@ describe Puppet::Indirector::REST do @model_instances = [ Puppet::TestIndirectedFoo.new(23), Puppet::TestIndirectedFoo.new(24) ] @mock_model.stubs(:search).returns @model_instances + # Force yaml, because otherwise our mocks can't work correctly + Puppet::TestIndirectedFoo.stubs(:supported_formats).returns %w{yaml} + @mock_model.stubs(:render_multiple).returns @model_instances.to_yaml end @@ -354,6 +357,10 @@ describe Puppet::Indirector::REST do describe "when matching model instances can be found" do before :each do @model_instances = [ Puppet::TestIndirectedFoo.new(23), Puppet::TestIndirectedFoo.new(24) ] + + # Force yaml, because otherwise our mocks can't work correctly + Puppet::TestIndirectedFoo.stubs(:supported_formats).returns %w{yaml} + @mock_model.stubs(:search).returns @model_instances @mock_model.stubs(:render_multiple).returns @model_instances.to_yaml end @@ -390,7 +397,7 @@ describe Puppet::Indirector::REST do end it "should return nil" do - Puppet::TestIndirectedFoo.search('bar').should be_nil + Puppet::TestIndirectedFoo.search('bar').should == [] end end diff --git a/spec/unit/indirector/rest.rb b/spec/unit/indirector/rest.rb index 801e8888e..28ceb693a 100755 --- a/spec/unit/indirector/rest.rb +++ b/spec/unit/indirector/rest.rb @@ -187,6 +187,12 @@ describe Puppet::Indirector::REST do @searcher.search(@request) end + it "should return an empty array if serialization returns nil" do + @model.stubs(:convert_from_multiple).returns nil + + @searcher.search(@request).should == [] + end + it "should generate an error when result data deserializes fails" do @searcher.expects(:deserialize).raises(ArgumentError) lambda { @searcher.search(@request) }.should raise_error(ArgumentError) diff --git a/spec/unit/network/http/handler.rb b/spec/unit/network/http/handler.rb index 1ed816d97..816c0ea2e 100755 --- a/spec/unit/network/http/handler.rb +++ b/spec/unit/network/http/handler.rb @@ -330,6 +330,20 @@ describe Puppet::Network::HTTP::Handler do @handler.expects(:set_response).with { |response, data| data == "my rendered instances" } @handler.do_search(@request, @response) end + + it "should return a 404 when searching returns an empty array" do + @model_class.stubs(:name).returns "my name" + @handler.expects(:set_response).with { |response, body, status| status == 404 } + @model_class.stubs(:search).returns([]) + @handler.do_search(@request, @response) + end + + it "should return a 404 when searching returns nil" do + @model_class.stubs(:name).returns "my name" + @handler.expects(:set_response).with { |response, body, status| status == 404 } + @model_class.stubs(:search).returns([]) + @handler.do_search(@request, @response) + end end describe "when destroying a model instance" do |