diff options
author | Rick Bradley <rick@rickbradley.com> | 2007-10-16 18:08:18 -0500 |
---|---|---|
committer | Rick Bradley <rick@rickbradley.com> | 2007-10-16 18:08:18 -0500 |
commit | 705f76fa1d9a95c54560a82e68c1c47b27755361 (patch) | |
tree | 20406046da4b83489252beb1ff2a70e3789d1d24 | |
parent | ce349683b76ab9d21f4d89e2ec818c0755848a1d (diff) | |
download | puppet-705f76fa1d9a95c54560a82e68c1c47b27755361.tar.gz puppet-705f76fa1d9a95c54560a82e68c1c47b27755361.tar.xz puppet-705f76fa1d9a95c54560a82e68c1c47b27755361.zip |
Argument passing now supported on {webrick,mongrel}+REST.
-rw-r--r-- | lib/puppet/network/http/handler.rb | 16 | ||||
-rw-r--r-- | lib/puppet/network/http/mongrel/rest.rb | 4 | ||||
-rw-r--r-- | lib/puppet/network/http/webrick/rest.rb | 4 | ||||
-rw-r--r-- | spec/unit/network/http/mongrel/rest.rb | 70 | ||||
-rw-r--r-- | spec/unit/network/http/webrick/rest.rb | 48 |
5 files changed, 123 insertions, 19 deletions
diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb index fb7b5c323..1f63024d6 100644 --- a/lib/puppet/network/http/handler.rb +++ b/lib/puppet/network/http/handler.rb @@ -19,22 +19,26 @@ class Puppet::Network::HTTP::Handler def do_find(request, response) key = request_key(request) || raise(ArgumentError, "Could not locate lookup key in request path [#{path}]") - @model.find(key) + args = params(request) + @model.find(key, args) end def do_search(request, response) - @model.search + args = params(request) + @model.search(args) end def do_destroy(request, response) key = request_key(request) || raise(ArgumentError, "Could not locate lookup key in request path [#{path}]") - @model.destroy(key) + args = params(request) + @model.destroy(key, args) end def do_save(request, response) data = body(request) raise ArgumentError, "No data to save" if !data or data.empty? - @model.new.save(:data => data) + args = params(request) + @model.new.save(args.merge(:data => data)) end def find_model_for_handler(handler) @@ -83,4 +87,8 @@ class Puppet::Network::HTTP::Handler def body(request) raise NotImplementedError end + + def params(request) + raise NotImplementedError + end end diff --git a/lib/puppet/network/http/mongrel/rest.rb b/lib/puppet/network/http/mongrel/rest.rb index f7807b19f..d87f9c733 100644 --- a/lib/puppet/network/http/mongrel/rest.rb +++ b/lib/puppet/network/http/mongrel/rest.rb @@ -24,4 +24,8 @@ class Puppet::Network::HTTP::MongrelREST < Puppet::Network::HTTP::Handler def body(request) request.body end + + def params(request) + Mongrel::HttpRequest.query_parse(request.params["QUERY_STRING"]) + end end diff --git a/lib/puppet/network/http/webrick/rest.rb b/lib/puppet/network/http/webrick/rest.rb index bfcd0784d..33b1ad8dc 100644 --- a/lib/puppet/network/http/webrick/rest.rb +++ b/lib/puppet/network/http/webrick/rest.rb @@ -29,4 +29,8 @@ class Puppet::Network::HTTP::WEBrickREST < Puppet::Network::HTTP::Handler def body(request) request.body end + + def params(request) + request.query + end end
\ No newline at end of file diff --git a/spec/unit/network/http/mongrel/rest.rb b/spec/unit/network/http/mongrel/rest.rb index 16fe94edb..0ad9be2bc 100644 --- a/spec/unit/network/http/mongrel/rest.rb +++ b/spec/unit/network/http/mongrel/rest.rb @@ -64,25 +64,33 @@ describe Puppet::Network::HTTP::MongrelREST, "when receiving a request" do end it "should call the model find method if the request represents a singular HTTP GET" do - @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'GET', Mongrel::Const::REQUEST_PATH => '/foo/key'}) - @mock_model_class.expects(:find).with('key') + @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'GET', + Mongrel::Const::REQUEST_PATH => '/foo/key', + 'QUERY_STRING' => ''}) + @mock_model_class.expects(:find).with('key', {}) @handler.process(@mock_request, @mock_response) end it "should call the model search method if the request represents a plural HTTP GET" do - @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'GET', Mongrel::Const::REQUEST_PATH => '/foos'}) - @mock_model_class.expects(:search) + @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'GET', + Mongrel::Const::REQUEST_PATH => '/foos', + 'QUERY_STRING' => '' }) + @mock_model_class.expects(:search).with({}) @handler.process(@mock_request, @mock_response) end it "should call the model destroy method if the request represents an HTTP DELETE" do - @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'DELETE', Mongrel::Const::REQUEST_PATH => '/foo/key'}) - @mock_model_class.expects(:destroy).with('key') + @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'DELETE', + Mongrel::Const::REQUEST_PATH => '/foo/key', + 'QUERY_STRING' => '' }) + @mock_model_class.expects(:destroy).with('key', {}) @handler.process(@mock_request, @mock_response) end it "should call the model save method if the request represents an HTTP PUT" do - @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'PUT', Mongrel::Const::REQUEST_PATH => '/foo'}) + @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'PUT', + Mongrel::Const::REQUEST_PATH => '/foo', + 'QUERY_STRING' => '' }) @mock_request.stubs(:body).returns('this is a fake request body') mock_model_instance = mock('indirected model instance') mock_model_instance.expects(:save).with(:data => 'this is a fake request body') @@ -103,7 +111,9 @@ describe Puppet::Network::HTTP::MongrelREST, "when receiving a request" do end it "should fail if the request is for an unknown path" do - @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'GET', Mongrel::Const::REQUEST_PATH => '/bar/key'}) + @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'GET', + Mongrel::Const::REQUEST_PATH => '/bar/key', + 'QUERY_STRING' => '' }) Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError) end @@ -123,11 +133,49 @@ describe Puppet::Network::HTTP::MongrelREST, "when receiving a request" do Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError) end - - it "should unpack request information from Mongrel" + it "should pass HTTP request parameters to model find" do + @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'GET', + Mongrel::Const::REQUEST_PATH => '/foo/key', + 'QUERY_STRING' => 'foo=baz&bar=xyzzy'}) + @mock_model_class.expects(:find).with do |key, args| + key == 'key' and args['foo'] == 'baz' and args['bar'] == 'xyzzy' + end + @handler.process(@mock_request, @mock_response) + end - it "should unpack parameters from the request for passing to controller methods" + it "should pass HTTP request parameters to model search" do + @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'GET', + Mongrel::Const::REQUEST_PATH => '/foos', + 'QUERY_STRING' => 'foo=baz&bar=xyzzy'}) + @mock_model_class.expects(:search).with do |args| + args['foo'] == 'baz' and args['bar'] == 'xyzzy' + end + @handler.process(@mock_request, @mock_response) + end + + it "should pass HTTP request parameters to model delete" do + @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'DELETE', + Mongrel::Const::REQUEST_PATH => '/foo/key', + 'QUERY_STRING' => 'foo=baz&bar=xyzzy'}) + @mock_model_class.expects(:destroy).with do |key, args| + key == 'key' and args['foo'] == 'baz' and args['bar'] == 'xyzzy' + end + @handler.process(@mock_request, @mock_response) + end + it "should pass HTTP request parameters to model save" do + @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'PUT', + Mongrel::Const::REQUEST_PATH => '/foo', + 'QUERY_STRING' => 'foo=baz&bar=xyzzy'}) + @mock_request.stubs(:body).returns('this is a fake request body') + mock_model_instance = mock('indirected model instance') + mock_model_instance.expects(:save).with do |args| + args[:data] == 'this is a fake request body' and args['foo'] == 'baz' and args['bar'] == 'xyzzy' + end + @mock_model_class.expects(:new).returns(mock_model_instance) + @handler.process(@mock_request, @mock_response) + end + it "should serialize the result from the controller method for return back to Mongrel" it "should serialize a controller exception result for return back to Mongrel" diff --git a/spec/unit/network/http/webrick/rest.rb b/spec/unit/network/http/webrick/rest.rb index 1a2faa95f..e5132595b 100644 --- a/spec/unit/network/http/webrick/rest.rb +++ b/spec/unit/network/http/webrick/rest.rb @@ -51,6 +51,7 @@ end describe Puppet::Network::HTTP::WEBrickREST, "when receiving a request" do before do @mock_request = mock('webrick http request') + @mock_request.stubs(:query).returns({}) @mock_response = mock('webrick http response') @mock_model_class = mock('indirected model class') Puppet::Indirector::Indirection.stubs(:model).with(:foo).returns(@mock_model_class) @@ -62,7 +63,7 @@ describe Puppet::Network::HTTP::WEBrickREST, "when receiving a request" do it "should call the model find method if the request represents a singular HTTP GET" do @mock_request.stubs(:request_method).returns('GET') @mock_request.stubs(:path).returns('/foo/key') - @mock_model_class.expects(:find).with('key') + @mock_model_class.expects(:find).with('key', {}) @handler.service(@mock_request, @mock_response) end @@ -76,7 +77,7 @@ describe Puppet::Network::HTTP::WEBrickREST, "when receiving a request" do it "should call the model destroy method if the request represents an HTTP DELETE" do @mock_request.stubs(:request_method).returns('DELETE') @mock_request.stubs(:path).returns('/foo/key') - @mock_model_class.expects(:destroy).with('key') + @mock_model_class.expects(:destroy).with('key', {}) @handler.service(@mock_request, @mock_response) end @@ -130,10 +131,49 @@ describe Puppet::Network::HTTP::WEBrickREST, "when receiving a request" do Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError) end - it "should unpack request information from WEBrick" + it "should pass HTTP request parameters to model find" do + @mock_request.stubs(:query).returns(:foo => :baz, :bar => :xyzzy) + @mock_request.stubs(:request_method).returns('GET') + @mock_request.stubs(:path).returns('/foo/key') + @mock_model_class.expects(:find).with do |key, args| + key == 'key' and args[:foo] == :baz and args[:bar] == :xyzzy + end + @handler.service(@mock_request, @mock_response) + end - it "should unpack parameters from the request for passing to controller methods" + it "should pass HTTP request parameters to model search" do + @mock_request.stubs(:query).returns(:foo => :baz, :bar => :xyzzy) + @mock_request.stubs(:request_method).returns('GET') + @mock_request.stubs(:path).returns('/foos/key') + @mock_model_class.expects(:search).with do |args| + args[:foo] == :baz and args[:bar] == :xyzzy + end + @handler.service(@mock_request, @mock_response) + end + it "should pass HTTP request parameters to model destroy" do + @mock_request.stubs(:query).returns(:foo => :baz, :bar => :xyzzy) + @mock_request.stubs(:request_method).returns('DELETE') + @mock_request.stubs(:path).returns('/foo/key') + @mock_model_class.expects(:destroy).with do |key, args| + key == 'key' and args[:foo] == :baz and args[:bar] == :xyzzy + end + @handler.service(@mock_request, @mock_response) + end + + it "should pass HTTP request parameters to model save" do + @mock_request.stubs(:request_method).returns('PUT') + @mock_request.stubs(:path).returns('/foo') + @mock_request.stubs(:query).returns(:foo => :baz, :bar => :xyzzy) + @mock_request.stubs(:body).returns('This is a fake request body') + mock_model_instance = mock('indirected model instance') + mock_model_instance.expects(:save).with do |args| + args[:data] == 'This is a fake request body' and args[:foo] == :baz and args[:bar] == :xyzzy + end + @mock_model_class.expects(:new).returns(mock_model_instance) + @handler.service(@mock_request, @mock_response) + end + it "should serialize the result from the controller method for return back to Mongrel" it "should serialize a controller exception result for return back to Mongrel" |