diff options
-rw-r--r-- | lib/puppet/indirector/rest.rb | 18 | ||||
-rw-r--r-- | lib/puppet/network/http/handler.rb | 82 | ||||
-rwxr-xr-x | spec/unit/indirector/rest.rb | 65 | ||||
-rwxr-xr-x | spec/unit/network/http/handler.rb | 255 |
4 files changed, 124 insertions, 296 deletions
diff --git a/lib/puppet/indirector/rest.rb b/lib/puppet/indirector/rest.rb index 6429b22b7..38ecdb758 100644 --- a/lib/puppet/indirector/rest.rb +++ b/lib/puppet/indirector/rest.rb @@ -2,9 +2,11 @@ require 'net/http' require 'uri' require 'puppet/network/http_pool' +require 'puppet/network/http/handler' # Access objects via REST class Puppet::Indirector::REST < Puppet::Indirector::Terminus + include Puppet::Network::HTTP::Handler class << self attr_reader :server_setting, :port_setting @@ -62,16 +64,14 @@ class Puppet::Indirector::REST < Puppet::Indirector::Terminus end def find(request) - deserialize network(request).get("/#{environment}/#{indirection.name}/#{request.escaped_key}#{request.query_string}", headers) + p model + p indirection + p indirection.model + deserialize network(request).get(indirection2uri(request), headers) end def search(request) - if request.key - path = "/#{environment}/#{indirection.name}s/#{request.escaped_key}#{request.query_string}" - else - path = "/#{environment}/#{indirection.name}s#{request.query_string}" - end - unless result = deserialize(network(request).get(path, headers), true) + unless result = deserialize(network(request).get(indirection2uri(request), headers), true) return [] end return result @@ -79,12 +79,12 @@ class Puppet::Indirector::REST < Puppet::Indirector::Terminus def destroy(request) raise ArgumentError, "DELETE does not accept options" unless request.options.empty? - deserialize network(request).delete("/#{environment}/#{indirection.name}/#{request.escaped_key}", headers) + deserialize network(request).delete(indirection2uri(request), headers) end def save(request) raise ArgumentError, "PUT does not accept options" unless request.options.empty? - deserialize network(request).put("/#{environment}/#{indirection.name}/", request.instance.render, headers) + deserialize network(request).put(indirection2uri(request), request.instance.render, headers) end private diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb index 1e5281695..610aa0a3f 100644 --- a/lib/puppet/network/http/handler.rb +++ b/lib/puppet/network/http/handler.rb @@ -50,11 +50,9 @@ module Puppet::Network::HTTP::Handler # handle an HTTP request def process(request, response) - return do_find(request, response) if get?(request) and singular?(request) - return do_search(request, response) if get?(request) and plural?(request) - return do_destroy(request, response) if delete?(request) and singular?(request) - return do_save(request, response) if put?(request) and singular?(request) - raise ArgumentError, "Did not understand HTTP #{http_method(request)} request for '#{path(request)}'" + indirection_request = uri2indirection(path(request), params(request), http_method(request)) + + send("do_%s" % indirection_request.method, indirection_request, request, response) rescue Exception => e return do_exception(response, e) end @@ -65,20 +63,12 @@ module Puppet::Network::HTTP::Handler raise ArgumentError, "The environment must be purely alphanumeric, not '%s'" % environment unless environment =~ /^\w+$/ raise ArgumentError, "The indirection name must be purely alphanumeric, not '%s'" % indirection unless indirection =~ /^\w+$/ - plurality = (indirection == handler.to_s + "s") ? :plural : :singular - - unless METHOD_MAP[http_method] - raise ArgumentError, "No support for http method %s" % http_method - end - - unless method = METHOD_MAP[http_method][plurality] - raise ArgumentError, "No support for plural %s operations" % http_method - end - - indirection.sub!(/s$/, '') if plurality == :plural + method = indirection_method(http_method, indirection) params[:environment] = environment + raise ArgumentError, "No request key specified in %s" % uri if key == "" or key.nil? + key = URI.unescape(key) Puppet::Indirector::Request.new(indirection, method, key, params) @@ -89,14 +79,24 @@ module Puppet::Network::HTTP::Handler "/#{request.environment.to_s}/#{indirection}/#{request.escaped_key}#{request.query_string}" end - # Are we interacting with a singular instance? - def singular?(request) - %r{/#{handler.to_s}$}.match(path(request)) + def indirection_method(http_method, indirection) + unless METHOD_MAP[http_method] + raise ArgumentError, "No support for http method %s" % http_method + end + + unless method = METHOD_MAP[http_method][plurality(indirection)] + raise ArgumentError, "No support for plural %s operations" % http_method + end + + return method end - # Are we interacting with multiple instances? - def plural?(request) - %r{/#{handler.to_s}s$}.match(path(request)) + def plurality(indirection) + result = (indirection == handler.to_s + "s") ? :plural : :singular + + indirection.sub!(/s$/, '') if result + + result end # Set the response up, with the body and status. @@ -119,12 +119,9 @@ module Puppet::Network::HTTP::Handler end # Execute our find. - def do_find(request, response) - key = request_key(request) || raise(ArgumentError, "Could not locate lookup key in request path [#{path(request)}]") - key = URI.unescape(key) - args = params(request) - unless result = model.find(key, args) - return do_exception(response, "Could not find %s %s" % [model.name, key], 404) + def do_find(indirection_request, request, response) + unless result = model.find(indirection_request.key, indirection_request.options) + return do_exception(response, "Could not find %s %s" % [model.name, indirection_request.key], 404) end # The encoding of the result must include the format to use, @@ -137,16 +134,11 @@ module Puppet::Network::HTTP::Handler end # Execute our search. - def do_search(request, response) - args = params(request) - if key = request_key(request) - key = URI.unescape(key) - result = model.search(key, args) - else - result = model.search(args) - end + def do_search(indirection_request, request, response) + result = model.search(indirection_request.key, indirection_request.options) + 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) + return do_exception(response, "Could not find instances in %s with '%s'" % [model.name, indirection_request.options.inspect], 404) end format = format_to_use(request) @@ -156,11 +148,8 @@ module Puppet::Network::HTTP::Handler end # Execute our destroy. - def do_destroy(request, response) - key = request_key(request) || raise(ArgumentError, "Could not locate lookup key in request path [#{path(request)}]") - key = URI.unescape(key) - args = params(request) - result = model.destroy(key, args) + def do_destroy(indirection_request, request, response) + result = model.destroy(indirection_request.key, indirection_request.options) set_content_type(response, "yaml") @@ -168,15 +157,14 @@ module Puppet::Network::HTTP::Handler end # Execute our save. - def do_save(request, response) + def do_save(indirection_request, request, response) data = body(request).to_s raise ArgumentError, "No data to save" if !data or data.empty? - args = params(request) format = format_to_use(request) obj = model.convert_from(format_to_use(request), data) - result = save_object(obj, args) + result = save_object(indirection_request, obj) set_content_type(response, "yaml") @@ -187,8 +175,8 @@ module Puppet::Network::HTTP::Handler # LAK:NOTE This has to be here for testing; it's a stub-point so # we keep infinite recursion from happening. - def save_object(object, args) - object.save(args) + def save_object(ind_request, object) + object.save(ind_request.options) end def find_model_for_handler(handler) diff --git a/spec/unit/indirector/rest.rb b/spec/unit/indirector/rest.rb index 361412a54..9305a7109 100755 --- a/spec/unit/indirector/rest.rb +++ b/spec/unit/indirector/rest.rb @@ -43,6 +43,11 @@ describe Puppet::Indirector::REST do @response.stubs(:[]).with('content-type').returns "text/plain" @searcher = @rest_class.new + @searcher.stubs(:model).returns @model + end + + it "should include the Http Handler module" do + Puppet::Indirector::REST.ancestors.should be_include(Puppet::Network::HTTP::Handler) end it "should have a method for specifying what setting a subclass should use to retrieve its server" do @@ -152,7 +157,7 @@ describe Puppet::Indirector::REST do @searcher.stubs(:network).returns(@connection) # neuter the network connection # Use a key with spaces, so we can test escaping - @request = stub 'request', :escaped_key => 'foo', :query_string => "" + @request = Puppet::Indirector::Request.new(:foo, :find, "foo bar") end it "should call the GET http method on a network connection" do @@ -168,15 +173,9 @@ describe Puppet::Indirector::REST do @searcher.find(@request).should == 'myobject' end - it "should use the environment, indirection name, and escaped request key to create the path" do - should_path = "/%s/%s/%s" % [Puppet::Node::Environment.new, @indirection.name.to_s, "foo"] - @connection.expects(:get).with { |path, args| path == should_path }.returns(@response) - @searcher.find(@request) - end - - it "should include the query string" do - @request.expects(:query_string).with().returns "?my_string" - @connection.expects(:get).with { |path, args| path.include?("?my_string") }.returns(@response) + it "should use the URI generated by the Handler module" do + @searcher.expects(:indirection2uri).with(@request).returns "/my/uri" + @connection.expects(:get).with { |path, args| path == "/my/uri" }.returns(@response) @searcher.find(@request) end @@ -205,7 +204,7 @@ describe Puppet::Indirector::REST do @model.stubs(:convert_from_multiple) - @request = stub 'request', :escaped_key => 'foo', :query_string => "", :key => "bar" + @request = Puppet::Indirector::Request.new(:foo, :search, "foo bar") end it "should call the GET http method on a network connection" do @@ -221,22 +220,9 @@ describe Puppet::Indirector::REST do @searcher.search(@request).should == 'myobject' end - it "should use the environment and the plural indirection name as the path if there is no request key" do - should_path = "/%s/%ss" % [Puppet::Node::Environment.new, @indirection.name.to_s] - @request.stubs(:key).returns nil - @connection.expects(:get).with { |path, args| path == should_path }.returns(@response) - @searcher.search(@request) - end - - it "should use the envrironment, the plural indirection name, and the escaped request key to create the path if the request key is set" do - should_path = "/%s/%ss/%s" % [Puppet::Node::Environment.new, @indirection.name.to_s, "foo"] - @connection.expects(:get).with { |path, args| path == should_path }.returns(@response) - @searcher.search(@request) - end - - it "should include the query string" do - @request.expects(:query_string).with().returns "?my_string" - @connection.expects(:get).with { |path, args| path.include?("?my_string") }.returns(@response) + it "should use the URI generated by the Handler module" do + @searcher.expects(:indirection2uri).with(@request).returns "/mys/uri" + @connection.expects(:get).with { |path, args| path == "/mys/uri" }.returns(@response) @searcher.search(@request) end @@ -264,7 +250,7 @@ describe Puppet::Indirector::REST do @connection = stub('mock http connection', :delete => @response) @searcher.stubs(:network).returns(@connection) # neuter the network connection - @request = stub 'request', :escaped_key => 'foo', :query_string => "", :options => {} + @request = Puppet::Indirector::Request.new(:foo, :destroy, "foo bar") end it "should call the DELETE http method on a network connection" do @@ -286,14 +272,13 @@ describe Puppet::Indirector::REST do @searcher.destroy(@request).should == 'myobject' end - it "should use the environment, the indirection name, and the escaped request key to create the path" do - should_path = "/%s/%s/%s" % [Puppet::Node::Environment.new, @indirection.name.to_s, "foo"] - @connection.expects(:delete).with { |path, args| path == should_path }.returns(@response) + it "should use the URI generated by the Handler module" do + @searcher.expects(:indirection2uri).with(@request).returns "/my/uri" + @connection.expects(:delete).with { |path, args| path == "/my/uri" }.returns(@response) @searcher.destroy(@request) end it "should not include the query string" do - @request.expects(:query_string).never @connection.stubs(:delete).returns @response @searcher.destroy(@request) end @@ -322,7 +307,8 @@ describe Puppet::Indirector::REST do @searcher.stubs(:network).returns(@connection) # neuter the network connection @instance = stub 'instance', :render => "mydata" - @request = stub 'request', :instance => @instance, :query_string => "", :options => {} + @request = Puppet::Indirector::Request.new(:foo, :save, "foo bar") + @request.instance = @instance end it "should call the PUT http method on a network connection" do @@ -337,16 +323,9 @@ describe Puppet::Indirector::REST do lambda { @searcher.save(@request) }.should raise_error(ArgumentError) end - it "should use the environment and the indirection name as the path for the request" do - path = "/%s/%s/" % [Puppet::Node::Environment.new, @indirection.name] - @connection.expects(:put).with { |path, data, args| path == path }.returns @response - - @searcher.save(@request) - end - - it "should not include the query string" do - @request.expects(:query_string).never - @connection.stubs(:put).returns @response + it "should use the URI generated by the Handler module" do + @searcher.expects(:indirection2uri).with(@request).returns "/my/uri" + @connection.expects(:put).with { |path, args| path == "/my/uri" }.returns(@response) @searcher.save(@request) end diff --git a/spec/unit/network/http/handler.rb b/spec/unit/network/http/handler.rb index fc73cc131..aa3e13df7 100755 --- a/spec/unit/network/http/handler.rb +++ b/spec/unit/network/http/handler.rb @@ -58,6 +58,11 @@ describe Puppet::Network::HTTP::Handler do @handler.uri2indirection("GET", "/env/foo/bee/baz/bomb", {}).key.should == "bee/baz/bomb" end + it "should fail if no indirection key is specified" do + lambda { @handler.uri2indirection("GET", "/env/foo/", {}) }.should raise_error(ArgumentError) + lambda { @handler.uri2indirection("GET", "/env/foo", {}) }.should raise_error(ArgumentError) + end + it "should choose 'find' as the indirection method if the http method is a GET and the indirection name is singular" do @handler.uri2indirection("GET", "/env/foo/bar", {}).method.should == :find end @@ -168,151 +173,43 @@ describe Puppet::Network::HTTP::Handler do @handler.stubs(:accept_header ).returns "format_one,format_two" @handler.stubs(:set_content_type).returns "my_result" @handler.stubs(:set_response ).returns "my_result" - @handler.stubs(:path ).returns "/my_handler" - @handler.stubs(:request_key ).returns "my_result" + @handler.stubs(:path ).returns "/my_handler/my_result" + @handler.stubs(:http_method ).returns("GET") @handler.stubs(:params ).returns({}) @handler.stubs(:content_type ).returns("text/plain") end - it "should consider the request singular if the path is equal to '/' plus the handler name" do - @handler.expects(:path).with(@request).returns "/foo" - @handler.expects(:handler).returns "foo" - - @handler.should be_singular(@request) - end - - it "should not consider the request singular unless the path is equal to '/' plus the handler name" do - @handler.expects(:path).with(@request).returns "/foo" - @handler.expects(:handler).returns "bar" + it "should create an indirection request from the path, parameters, and http method" do + @handler.expects(:path).with(@request).returns "mypath" + @handler.expects(:http_method).with(@request).returns "mymethod" + @handler.expects(:params).with(@request).returns "myparams" - @handler.should_not be_singular(@request) - end + @handler.expects(:uri2indirection).with("mypath", "myparams", "mymethod").returns stub("request", :method => :find) - it "should consider the request plural if the path is equal to '/' plus the handler name plus 's'" do - @handler.expects(:path).with(@request).returns "/foos" - @handler.expects(:handler).returns "foo" + @handler.stubs(:do_find) - @handler.should be_plural(@request) + @handler.process(@request, @response) end - it "should not consider the request plural unless the path is equal to '/' plus the handler name plus 's'" do - @handler.expects(:path).with(@request).returns "/foos" - @handler.expects(:handler).returns "bar" + it "should call the 'do' method associated with the indirection method" do + request = stub 'request' + @handler.expects(:uri2indirection).returns request - @handler.should_not be_plural(@request) - end + request.expects(:method).returns "mymethod" - it "should call the model find method if the request represents a singular HTTP GET" do - @handler.expects(:http_method).returns('GET') - @handler.expects(:singular?).returns(true) + @handler.expects(:do_mymethod).with(request, @request, @response) - @handler.expects(:do_find).with(@request, @response) @handler.process(@request, @response) end it "should serialize a controller exception when an exception is thrown while finding the model instance" do - @handler.expects(:http_method).returns('GET') - @handler.expects(:singular?).returns(true) + @handler.expects(:uri2indirection).returns stub("request", :method => :find) @handler.expects(:do_find).raises(ArgumentError, "The exception") @handler.expects(:set_response).with { |response, body, status| body == "The exception" and status == 400 } @handler.process(@request, @response) end - it "should call the model search method if the request represents a plural HTTP GET" do - @handler.stubs(:http_method).returns('GET') - @handler.stubs(:singular?).returns(false) - @handler.stubs(:plural?).returns(true) - - @handler.expects(:do_search).with(@request, @response) - @handler.process(@request, @response) - end - - it "should serialize a controller exception when an exception is thrown by search" do - @handler.stubs(:http_method).returns('GET') - @handler.stubs(:singular?).returns(false) - @handler.stubs(:plural?).returns(true) - - @model_class.expects(:search).raises(ArgumentError) - @handler.expects(:set_response).with { |response, data, status| status == 400 } - @handler.process(@request, @response) - end - - it "should call the model destroy method if the request represents an HTTP DELETE" do - @handler.stubs(:http_method).returns('DELETE') - @handler.stubs(:singular?).returns(true) - @handler.stubs(:plural?).returns(false) - - @handler.expects(:do_destroy).with(@request, @response) - - @handler.process(@request, @response) - end - - it "should serialize a controller exception when an exception is thrown by destroy" do - @handler.stubs(:http_method).returns('DELETE') - @handler.stubs(:singular?).returns(true) - @handler.stubs(:plural?).returns(false) - - @handler.expects(:do_destroy).with(@request, @response).raises(ArgumentError, "The exception") - @handler.expects(:set_response).with { |response, body, status| body == "The exception" and status == 400 } - - @handler.process(@request, @response) - end - - it "should call the model save method if the request represents an HTTP PUT" do - @handler.stubs(:http_method).returns('PUT') - @handler.stubs(:singular?).returns(true) - - @handler.expects(:do_save).with(@request, @response) - - @handler.process(@request, @response) - end - - it "should serialize a controller exception when an exception is thrown by save" do - @handler.stubs(:http_method).returns('PUT') - @handler.stubs(:singular?).returns(true) - @handler.stubs(:body).raises(ArgumentError) - - @handler.expects(:set_response).with { |response, body, status| status == 400 } - @handler.process(@request, @response) - end - - it "should fail if the HTTP method isn't supported" do - @handler.stubs(:http_method).returns('POST') - @handler.stubs(:singular?).returns(true) - @handler.stubs(:plural?).returns(false) - - @handler.expects(:set_response).with { |response, body, status| status == 400 } - @handler.process(@request, @response) - end - - it "should fail if delete request's pluralization is wrong" do - @handler.stubs(:http_method).returns('DELETE') - @handler.stubs(:singular?).returns(false) - @handler.stubs(:plural?).returns(true) - - @handler.expects(:set_response).with { |response, body, status| status == 400 } - @handler.process(@request, @response) - end - - it "should fail if put request's pluralization is wrong" do - @handler.stubs(:http_method).returns('PUT') - @handler.stubs(:singular?).returns(false) - @handler.stubs(:plural?).returns(true) - - @handler.expects(:set_response).with { |response, body, status| status == 400 } - @handler.process(@request, @response) - end - - it "should fail if the request is for an unknown path" do - @handler.stubs(:http_method).returns('GET') - @handler.expects(:singular?).returns false - @handler.expects(:plural?).returns false - - @handler.expects(:set_response).with { |response, body, status| status == 400 } - @handler.process(@request, @response) - end - it "should set the format to text/plain when serializing an exception" do @handler.expects(:set_content_type).with(@response, "text/plain") @handler.do_exception(@response, "A test", 404) @@ -320,52 +217,43 @@ describe Puppet::Network::HTTP::Handler do describe "when finding a model instance" do before do - @handler.stubs(:http_method).returns('GET') - @handler.stubs(:path).returns('/my_handler') - @handler.stubs(:singular?).returns(true) - @handler.stubs(:request_key).returns('key') + @irequest = stub 'indirection_request', :method => :find, :indirection_name => "my_handler", :options => {}, :key => "my_result" + @model_class.stubs(:find).returns @result @format = stub 'format', :suitable? => true Puppet::Network::FormatHandler.stubs(:format).returns @format end - it "should fail if the key is not specified" do - @handler.stubs(:request_key).returns(nil) - - lambda { @handler.do_find(@request, @response) }.should raise_error(ArgumentError) - end - it "should use the escaped request key" do - @handler.stubs(:request_key).returns URI.escape("my key") @model_class.expects(:find).with do |key, args| - key == "my key" + key == "my_result" end.returns @result - @handler.do_find(@request, @response) + @handler.do_find(@irequest, @request, @response) end it "should use a common method for determining the request parameters" do - @handler.stubs(:params).returns(:foo => :baz, :bar => :xyzzy) + @irequest.stubs(:options).returns(:foo => :baz, :bar => :xyzzy) @model_class.expects(:find).with do |key, args| args[:foo] == :baz and args[:bar] == :xyzzy end.returns @result - @handler.do_find(@request, @response) + @handler.do_find(@irequest, @request, @response) end it "should set the content type to the first format specified in the accept header" do @handler.expects(:accept_header).with(@request).returns "one,two" @handler.expects(:set_content_type).with(@response, "one") - @handler.do_find(@request, @response) + @handler.do_find(@irequest, @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) + lambda { @handler.do_find(@irequest, @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) + lambda { @handler.do_find(@irequest, @request, @response) }.should raise_error(RuntimeError) end it "should not use an unsuitable format" do @@ -377,19 +265,19 @@ describe Puppet::Network::HTTP::Handler do @handler.expects(:set_content_type).with(@response, "bar") # the suitable one - @handler.do_find(@request, @response) + @handler.do_find(@irequest, @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") - @handler.do_find(@request, @response) + @handler.do_find(@irequest, @request, @response) end it "should use the default status when a model find call succeeds" do @handler.expects(:set_response).with { |response, body, status| status.nil? } - @handler.do_find(@request, @response) + @handler.do_find(@irequest, @request, @response) end it "should return a serialized object when a model find call succeeds" do @@ -398,14 +286,14 @@ describe Puppet::Network::HTTP::Handler do @handler.expects(:set_response).with { |response, body, status| body == "my_rendered_object" } @model_class.stubs(:find).returns(@model_instance) - @handler.do_find(@request, @response) + @handler.do_find(@irequest, @request, @response) end it "should return a 404 when no model instance can be found" do @model_class.stubs(:name).returns "my name" @handler.expects(:set_response).with { |response, body, status| status == 404 } @model_class.stubs(:find).returns(nil) - @handler.do_find(@request, @response) + @handler.do_find(@irequest, @request, @response) end it "should serialize the result in with the appropriate format" do @@ -414,17 +302,13 @@ describe Puppet::Network::HTTP::Handler do @handler.expects(:format_to_use).returns "one" @model_instance.expects(:render).with("one").returns "my_rendered_object" @model_class.stubs(:find).returns(@model_instance) - @handler.do_find(@request, @response) + @handler.do_find(@irequest, @request, @response) end end describe "when searching for model instances" do before do - @handler.stubs(:http_method).returns('GET') - @handler.stubs(:path).returns('/my_handlers') - @handler.stubs(:singular?).returns(false) - @handler.stubs(:plural?).returns(true) - @handler.stubs(:request_key).returns('key') + @irequest = stub 'indirection_request', :method => :find, :indirection_name => "my_handler", :options => {}, :key => "key" @result1 = mock 'result1' @result2 = mock 'results' @@ -438,35 +322,23 @@ describe Puppet::Network::HTTP::Handler do end it "should use a common method for determining the request parameters" do - @handler.stubs(:params).returns(:foo => :baz, :bar => :xyzzy) + @irequest.stubs(:options).returns(:foo => :baz, :bar => :xyzzy) @model_class.expects(:search).with do |key, args| args[:foo] == :baz and args[:bar] == :xyzzy end.returns @result - @handler.do_search(@request, @response) - end - - it "should use an escaped request key if one is provided" do - @handler.expects(:request_key).with(@request).returns URI.escape("foo bar") - @model_class.expects(:search).with { |key, args| key == "foo bar" }.returns @result - @handler.do_search(@request, @response) - end - - it "should work with no request key if none is provided" do - @handler.expects(:request_key).with(@request).returns nil - @model_class.expects(:search).with { |args| args.is_a?(Hash) }.returns @result - @handler.do_search(@request, @response) + @handler.do_search(@irequest, @request, @response) end it "should use the default status when a model search call succeeds" do @model_class.stubs(:search).returns(@result) - @handler.do_search(@request, @response) + @handler.do_search(@irequest, @request, @response) end it "should set the content type to the first format returned by the accept header" do @handler.expects(:accept_header).with(@request).returns "one,two" @handler.expects(:set_content_type).with(@response, "one") - @handler.do_search(@request, @response) + @handler.do_search(@irequest, @request, @response) end it "should return a list of serialized objects when a model search call succeeds" do @@ -477,59 +349,51 @@ describe Puppet::Network::HTTP::Handler do @model_class.expects(:render_multiple).with("one", @result).returns "my rendered instances" @handler.expects(:set_response).with { |response, data| data == "my rendered instances" } - @handler.do_search(@request, @response) + @handler.do_search(@irequest, @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) + @handler.do_search(@irequest, @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) + @handler.do_search(@irequest, @request, @response) end end describe "when destroying a model instance" do before do - @handler.stubs(:http_method).returns('DELETE') - @handler.stubs(:path).returns('/my_handler/key') - @handler.stubs(:singular?).returns(true) - @handler.stubs(:request_key).returns('key') + @irequest = stub 'indirection_request', :method => :destroy, :indirection_name => "my_handler", :options => {}, :key => "key" @result = stub 'result', :render => "the result" @model_class.stubs(:destroy).returns @result end - it "should fail to destroy model if key is not specified" do - @handler.expects(:request_key).returns nil - lambda { @handler.do_destroy(@request, @response) }.should raise_error(ArgumentError) - end - it "should use the escaped request key to destroy the instance in the model" do - @handler.expects(:request_key).returns URI.escape("foo bar") + @irequest.expects(:key).returns "foo bar" @model_class.expects(:destroy).with do |key, args| key == "foo bar" end - @handler.do_destroy(@request, @response) + @handler.do_destroy(@irequest, @request, @response) end it "should use a common method for determining the request parameters" do - @handler.stubs(:params).returns(:foo => :baz, :bar => :xyzzy) + @irequest.stubs(:options).returns(:foo => :baz, :bar => :xyzzy) @model_class.expects(:destroy).with do |key, args| args[:foo] == :baz and args[:bar] == :xyzzy end - @handler.do_destroy(@request, @response) + @handler.do_destroy(@irequest, @request, @response) end it "should use the default status code a model destroy call succeeds" do @handler.expects(:set_response).with { |response, body, status| status.nil? } - @handler.do_destroy(@request, @response) + @handler.do_destroy(@irequest, @request, @response) end it "should return a yaml-encoded result when a model destroy call succeeds" do @@ -538,16 +402,13 @@ describe Puppet::Network::HTTP::Handler do @handler.expects(:set_response).with { |response, body, status| body == "the result" } - @handler.do_destroy(@request, @response) + @handler.do_destroy(@irequest, @request, @response) end end describe "when saving a model instance" do before do - @handler.stubs(:http_method).returns('PUT') - @handler.stubs(:path).returns('/my_handler/key') - @handler.stubs(:singular?).returns(true) - @handler.stubs(:request_key).returns('key') + @irequest = stub 'indirection_request', :method => :save, :indirection_name => "my_handler", :options => {}, :key => "key" @handler.stubs(:body).returns('my stuff') @result = stub 'result', :render => "the result" @@ -563,37 +424,37 @@ describe Puppet::Network::HTTP::Handler do @handler.expects(:body).returns "my body" @model_class.expects(:convert_from).with { |format, body| body == "my body" }.returns @model_instance - @handler.do_save(@request, @response) + @handler.do_save(@irequest, @request, @response) end it "should fail to save model if data is not specified" do @handler.stubs(:body).returns('') - lambda { @handler.do_save(@request, @response) }.should raise_error(ArgumentError) + lambda { @handler.do_save(@irequest, @request, @response) }.should raise_error(ArgumentError) end it "should use a common method for determining the request parameters" do - @handler.stubs(:params).returns(:foo => :baz, :bar => :xyzzy) + @irequest.stubs(:options).returns(:foo => :baz, :bar => :xyzzy) @model_instance.expects(:save).with do |args| args[:foo] == :baz and args[:bar] == :xyzzy end - @handler.do_save(@request, @response) + @handler.do_save(@irequest, @request, @response) end it "should use the default status when a model save call succeeds" do @handler.expects(:set_response).with { |response, body, status| status.nil? } - @handler.do_save(@request, @response) + @handler.do_save(@irequest, @request, @response) end it "should return the yaml-serialized result when a model save call succeeds" do @model_instance.stubs(:save).returns(@model_instance) @model_instance.expects(:to_yaml).returns('foo') - @handler.do_save(@request, @response) + @handler.do_save(@irequest, @request, @response) end it "should set the content to yaml" do @handler.expects(:set_content_type).with(@response, "yaml") - @handler.do_save(@request, @response) + @handler.do_save(@irequest, @request, @response) end end end |