diff options
-rw-r--r-- | lib/puppet/indirector/indirection.rb | 8 | ||||
-rw-r--r-- | lib/puppet/network/http/mongrel.rb | 4 | ||||
-rw-r--r-- | lib/puppet/network/http/mongrel/rest.rb | 14 | ||||
-rw-r--r-- | lib/puppet/network/http/webrick.rb | 6 | ||||
-rw-r--r-- | lib/puppet/network/http/webrick/rest.rb | 14 | ||||
-rwxr-xr-x | spec/unit/indirector/indirection.rb | 14 | ||||
-rw-r--r-- | spec/unit/network/http/mongrel.rb | 8 | ||||
-rw-r--r-- | spec/unit/network/http/mongrel/rest.rb | 46 | ||||
-rw-r--r-- | spec/unit/network/http/mongrel/xmlrpc.rb | 0 | ||||
-rw-r--r-- | spec/unit/network/http/webrick.rb | 8 | ||||
-rw-r--r-- | spec/unit/network/http/webrick/rest.rb | 46 | ||||
-rw-r--r-- | spec/unit/network/http/webrick/xmlrpc.rb | 0 | ||||
-rw-r--r-- | spec/unit/network/server.rb | 17 |
13 files changed, 150 insertions, 35 deletions
diff --git a/lib/puppet/indirector/indirection.rb b/lib/puppet/indirector/indirection.rb index 0d814c5ef..6930af494 100644 --- a/lib/puppet/indirector/indirection.rb +++ b/lib/puppet/indirector/indirection.rb @@ -13,6 +13,14 @@ class Puppet::Indirector::Indirection @@indirections.find { |i| i.name == name } end + # Find an indirected model by name. This is provided so that Terminus classes + # can specifically hook up with the indirections they are associated with. + def self.model(name) + match = @@indirections.find { |i| i.name == name } + return nil unless match + match.model + end + attr_accessor :name, :model # Create and return our cache terminus. diff --git a/lib/puppet/network/http/mongrel.rb b/lib/puppet/network/http/mongrel.rb index 3efc465ad..bec94ac13 100644 --- a/lib/puppet/network/http/mongrel.rb +++ b/lib/puppet/network/http/mongrel.rb @@ -38,9 +38,7 @@ class Puppet::Network::HTTP::Mongrel def setup_handlers @protocols.each do |protocol| - @handlers.each do |handler| - class_for_protocol(protocol).new(:server => @server, :handler => handler) - end + class_for_protocol(protocol).new(:server => @server, :handlers => @handlers) end end diff --git a/lib/puppet/network/http/mongrel/rest.rb b/lib/puppet/network/http/mongrel/rest.rb index 6e454c7d9..34f1d8f90 100644 --- a/lib/puppet/network/http/mongrel/rest.rb +++ b/lib/puppet/network/http/mongrel/rest.rb @@ -1,4 +1,18 @@ class Puppet::Network::HTTP::MongrelREST def initialize(args = {}) + raise ArgumentError unless args[:server] + raise ArgumentError if !args[:handlers] or args[:handlers].empty? + + @models = {} + args[:handlers].each do |handler| + @models[handler] = find_model_for_handler(handler) + end + end + + private + + def find_model_for_handler(handler) + Puppet::Indirector::Indirection.model(handler) || + raise(ArgumentError, "Cannot locate indirection [#{handler}].") end end diff --git a/lib/puppet/network/http/webrick.rb b/lib/puppet/network/http/webrick.rb index 6df7804c6..21d191d06 100644 --- a/lib/puppet/network/http/webrick.rb +++ b/lib/puppet/network/http/webrick.rb @@ -41,10 +41,8 @@ class Puppet::Network::HTTP::WEBrick private def setup_handlers - @handlers.each do |handler| - @protocols.each do |protocol| - class_for_protocol(protocol).new(:server => @server, :handler => handler) - end + @protocols.each do |protocol| + class_for_protocol(protocol).new(:server => @server, :handlers => @handlers) end end diff --git a/lib/puppet/network/http/webrick/rest.rb b/lib/puppet/network/http/webrick/rest.rb index 5e9ccfc45..f70f2030f 100644 --- a/lib/puppet/network/http/webrick/rest.rb +++ b/lib/puppet/network/http/webrick/rest.rb @@ -1,4 +1,18 @@ class Puppet::Network::HTTP::WEBrickREST def initialize(args = {}) + raise ArgumentError unless args[:server] + raise ArgumentError if !args[:handlers] or args[:handlers].empty? + + @models = {} + args[:handlers].each do |handler| + @models[handler] = find_model_for_handler(handler) + end + end + + private + + def find_model_for_handler(handler) + Puppet::Indirector::Indirection.model(handler) || + raise(ArgumentError, "Cannot locate indirection [#{handler}].") end end
\ No newline at end of file diff --git a/spec/unit/indirector/indirection.rb b/spec/unit/indirector/indirection.rb index 455b5dc67..cb1b4b8e8 100755 --- a/spec/unit/indirector/indirection.rb +++ b/spec/unit/indirector/indirection.rb @@ -23,7 +23,7 @@ describe Puppet::Indirector::Indirection do @indirection.find(@name).should == @instance end - it "should handle removing model instances from a terminus letting the appropriate terminus remove the instance" do + it "should handle removing model instances from a terminus by letting the appropriate terminus remove the instance" do @terminus.expects(:destroy).with(@name).returns(@instance) @indirection.destroy(@name).should == @instance end @@ -103,11 +103,21 @@ describe Puppet::Indirector::Indirection, " when managing indirection instances" @indirection = Puppet::Indirector::Indirection.new(mock('model'), :test) Puppet::Indirector::Indirection.instance(:test).should equal(@indirection) end - + it "should return nil when the named indirection has not been created" do Puppet::Indirector::Indirection.instance(:test).should be_nil end + it "should allow an indirection's model to be retrieved by name" do + mock_model = mock('model') + @indirection = Puppet::Indirector::Indirection.new(mock_model, :test) + Puppet::Indirector::Indirection.model(:test).should equal(mock_model) + end + + it "should return nil when no model matches the requested name" do + Puppet::Indirector::Indirection.model(:test).should be_nil + end + after do @indirection.delete if defined? @indirection end diff --git a/spec/unit/network/http/mongrel.rb b/spec/unit/network/http/mongrel.rb index 161080109..3364efb92 100644 --- a/spec/unit/network/http/mongrel.rb +++ b/spec/unit/network/http/mongrel.rb @@ -64,11 +64,9 @@ describe Puppet::Network::HTTP::Mongrel, "when turning on listening" do @listen_params[:protocols].each do |protocol| mock_handler = mock("handler instance for [#{protocol}]") mock_handler_class = mock("handler class for [#{protocol}]") - @listen_params[:handlers].each do |handler| - mock_handler_class.expects(:new).with {|args| - args[:server] == @mock_mongrel and args[:handler] == handler - }.returns(mock_handler) - end + mock_handler_class.expects(:new).with {|args| + args[:server] == @mock_mongrel and args[:handlers] == @listen_params[:handlers] + }.returns(mock_handler) @server.expects(:class_for_protocol).with(protocol).at_least_once.returns(mock_handler_class) end @server.listen(@listen_params) diff --git a/spec/unit/network/http/mongrel/rest.rb b/spec/unit/network/http/mongrel/rest.rb new file mode 100644 index 000000000..4a6524ef6 --- /dev/null +++ b/spec/unit/network/http/mongrel/rest.rb @@ -0,0 +1,46 @@ +#!/usr/bin/env ruby +# +# Created by Rick Bradley on 2007-10-16. +# Copyright (c) 2007. All rights reserved. + +require File.dirname(__FILE__) + '/../../../../spec_helper' +require 'puppet/network/http' + +describe Puppet::Network::HTTP::MongrelREST, "when initializing" do + before do + @mock_mongrel = mock('Mongrel server') + @params = { :server => @mock_mongrel, :handlers => [ :foo ] } + end + + it "should require access to a Mongrel server" do + Proc.new { Puppet::Network::HTTP::MongrelREST.new(@params.delete_if {|k,v| :server == k })}.should raise_error(ArgumentError) + end + + it "should require at least one indirection name" do + Proc.new { Puppet::Network::HTTP::MongrelREST.new(@params.delete_if {|k,v| :handlers == k })}.should raise_error(ArgumentError) + end + + it "should look up the indirection model from the indirection name" do + mock_model = mock('indirected model') + Puppet::Indirector::Indirection.expects(:model).with(:foo).returns(mock_model) + Puppet::Network::HTTP::MongrelREST.new(@params) + end + + it "should fail if a handler is not indirected" do + Puppet::Indirector::Indirection.expects(:model).with(:foo).returns(nil) + Proc.new { Puppet::Network::HTTP::MongrelREST.new(@params) }.should raise_error(ArgumentError) + end + + it "should register a listener for each indirection with the provided Mongrel server" +end + +describe Puppet::Network::HTTP::MongrelREST, "when receiving a request" do + it "should unpack request information from Mongrel" + it "should unpack parameters from the request for passing to controller methods" + it "should call the controller find method if the request represents a singular HTTP GET" + it "should call the controller search method if the request represents a plural HTTP GET" + it "should call the controller destroy method if the request represents an HTTP DELETE" + it "should call the controller save method if the request represents an HTTP PUT" + it "should serialize the result from the controller method for return back to Mongrel" + it "should serialize a controller expection result for return back to Mongrel" +end diff --git a/spec/unit/network/http/mongrel/xmlrpc.rb b/spec/unit/network/http/mongrel/xmlrpc.rb new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/spec/unit/network/http/mongrel/xmlrpc.rb diff --git a/spec/unit/network/http/webrick.rb b/spec/unit/network/http/webrick.rb index 81b2a0fa9..4bf742b6e 100644 --- a/spec/unit/network/http/webrick.rb +++ b/spec/unit/network/http/webrick.rb @@ -64,11 +64,9 @@ describe Puppet::Network::HTTP::WEBrick, "when turning on listening" do @listen_params[:protocols].each do |protocol| mock_handler = mock("handler instance for [#{protocol}]") mock_handler_class = mock("handler class for [#{protocol}]") - @listen_params[:handlers].each do |handler| - mock_handler_class.expects(:new).with {|args| - args[:server] == @mock_webrick and args[:handler] == handler - }.returns(mock_handler) - end + mock_handler_class.expects(:new).with {|args| + args[:server] == @mock_webrick and args[:handlers] == @listen_params[:handlers] + }.returns(mock_handler) @server.expects(:class_for_protocol).with(protocol).at_least_once.returns(mock_handler_class) end @server.listen(@listen_params) diff --git a/spec/unit/network/http/webrick/rest.rb b/spec/unit/network/http/webrick/rest.rb new file mode 100644 index 000000000..f17f89a15 --- /dev/null +++ b/spec/unit/network/http/webrick/rest.rb @@ -0,0 +1,46 @@ +#!/usr/bin/env ruby +# +# Created by Rick Bradley on 2007-10-16. +# Copyright (c) 2007. All rights reserved. + +require File.dirname(__FILE__) + '/../../../../spec_helper' +require 'puppet/network/http' + +describe Puppet::Network::HTTP::WEBrickREST, "when initializing" do + before do + @mock_webrick = mock('WEBrick server') + @params = { :server => @mock_webrick, :handlers => [ :foo ] } + end + + it "should require access to a WEBrick server" do + Proc.new { Puppet::Network::HTTP::WEBrickREST.new(@params.delete_if {|k,v| :server == k })}.should raise_error(ArgumentError) + end + + it "should require at least one indirection name" do + Proc.new { Puppet::Network::HTTP::WEBrickREST.new(@params.delete_if {|k,v| :handlers == k })}.should raise_error(ArgumentError) + end + + it "should look up the indirection model from the indirection name" do + mock_model = mock('indirected model') + Puppet::Indirector::Indirection.expects(:model).with(:foo).returns(mock_model) + Puppet::Network::HTTP::WEBrickREST.new(@params) + end + + it "should fail if a handler is not indirected" do + Puppet::Indirector::Indirection.expects(:model).with(:foo).returns(nil) + Proc.new { Puppet::Network::HTTP::WEBrickREST.new(@params) }.should raise_error(ArgumentError) + end + + it "should register a listener for each indirection with the provided WEBrick server" +end + +describe Puppet::Network::HTTP::WEBrickREST, "when receiving a request" do + it "should unpack request information from WEBrick" + it "should unpack parameters from the request for passing to controller methods" + it "should call the controller find method if the request represents a singular HTTP GET" + it "should call the controller search method if the request represents a plural HTTP GET" + it "should call the controller destroy method if the request represents an HTTP DELETE" + it "should call the controller save method if the request represents an HTTP PUT" + it "should serialize the result from the controller method for return back to Mongrel" + it "should serialize a controller expection result for return back to Mongrel" +end diff --git a/spec/unit/network/http/webrick/xmlrpc.rb b/spec/unit/network/http/webrick/xmlrpc.rb new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/spec/unit/network/http/webrick/xmlrpc.rb diff --git a/spec/unit/network/server.rb b/spec/unit/network/server.rb index 659eb1930..97ab4443a 100644 --- a/spec/unit/network/server.rb +++ b/spec/unit/network/server.rb @@ -273,25 +273,10 @@ describe Puppet::Network::Server, "when listening is being turned off" do end - # TODO / FIXME : HERE -- need to begin resolving the model behind the indirection - # looks like: get the handler class, providing @server to it - # have the handler class register the handler @ the right URL - # handler class knows the correct path to use, correct registration method to call - # handler also know how to get the model class from the indirection name - # do we change indirection name to indirection (instead of saying "handlers")? - - -describe Class.new, "Puppet::Network::Handler::*::* (handler class (e.g., webrick+rest or mongrel+xmlrpc))" do - it "should be able to unserialize a request from the given httpserver answering for the given protocol handler, to be used by a controller" - it "should be able to serialize a result from a controller for return by the given httpserver responding with the given protocol" - it "should properly encode an exception from a controller for use by the httpserver for the given protocol handler" - it "should call the appropriate controller method" - it "should properly encode parameters on the request for use by the controller methods" -end - describe Class.new, "put these somewhere" do it "should allow indirections to deny access to services based upon which client is connecting, or whether the client is authorized" it "should deny access to clients based upon rules" end + |