diff options
| author | Rick Bradley <rick@rickbradley.com> | 2007-10-16 13:57:56 -0500 |
|---|---|---|
| committer | Rick Bradley <rick@rickbradley.com> | 2007-10-16 13:57:56 -0500 |
| commit | 2a497fff66a7827059b712e84dcaff171ccab6be (patch) | |
| tree | 1e66995191c44dd3e5951f13a1dd73263bf60395 /spec/unit/network | |
| parent | 6ab78f62ee589e542fd653a54109c0f5141ea026 (diff) | |
| download | puppet-2a497fff66a7827059b712e84dcaff171ccab6be.tar.gz puppet-2a497fff66a7827059b712e84dcaff171ccab6be.tar.xz puppet-2a497fff66a7827059b712e84dcaff171ccab6be.zip | |
Refactored to use a Handler base class for server+protocol handlers. Finally eliminated dependency on Puppet.start, etc., from WEBrick HTTP server class. {webrick,mongrel}+REST now support request handling uniformly; need encode/decode next.
Diffstat (limited to 'spec/unit/network')
| -rw-r--r-- | spec/unit/network/http/webrick.rb | 14 | ||||
| -rw-r--r-- | spec/unit/network/http/webrick/rest.rb | 56 |
2 files changed, 51 insertions, 19 deletions
diff --git a/spec/unit/network/http/webrick.rb b/spec/unit/network/http/webrick.rb index 9ba04f164..3ed223e7e 100644 --- a/spec/unit/network/http/webrick.rb +++ b/spec/unit/network/http/webrick.rb @@ -14,10 +14,8 @@ end describe Puppet::Network::HTTP::WEBrick, "when turning on listening" do before do - Puppet.stubs(:start) - Puppet.stubs(:newservice) @mock_webrick = mock('webrick') - @mock_webrick.stubs(:mount) + [:mount, :start, :shutdown].each {|meth| @mock_webrick.stubs(meth)} WEBrick::HTTPServer.stubs(:new).returns(@mock_webrick) @server = Puppet::Network::HTTP::WEBrick.new @listen_params = { :address => "127.0.0.1", :port => 31337, :handlers => [ :node, :configuration ], :protocols => [ :rest, :xmlrpc ] } @@ -45,7 +43,7 @@ describe Puppet::Network::HTTP::WEBrick, "when turning on listening" do end it "should order a webrick server to start" do - Puppet.expects(:start) + @mock_webrick.expects(:start) @server.listen(@listen_params) end @@ -92,13 +90,10 @@ end describe Puppet::Network::HTTP::WEBrick, "when turning off listening" do before do - Puppet.stubs(:start) - Puppet.stubs(:newservice) @mock_webrick = mock('webrick') - @mock_webrick.stubs(:mount) + [:mount, :start, :shutdown].each {|meth| @mock_webrick.stubs(meth)} WEBrick::HTTPServer.stubs(:new).returns(@mock_webrick) @server = Puppet::Network::HTTP::WEBrick.new - @server.stubs(:shutdown) @listen_params = { :address => "127.0.0.1", :port => 31337, :handlers => [ :node, :configuration ], :protocols => [ :rest, :xmlrpc ] } end @@ -107,8 +102,7 @@ describe Puppet::Network::HTTP::WEBrick, "when turning off listening" do end it "should order webrick server to stop" do - @server.should respond_to(:shutdown) - @server.expects(:shutdown) + @mock_webrick.expects(:shutdown) @server.listen(@listen_params) @server.unlisten end diff --git a/spec/unit/network/http/webrick/rest.rb b/spec/unit/network/http/webrick/rest.rb index 418c97b6f..472a09aca 100644 --- a/spec/unit/network/http/webrick/rest.rb +++ b/spec/unit/network/http/webrick/rest.rb @@ -49,19 +49,57 @@ describe Puppet::Network::HTTP::WEBrickREST, "when initializing" do 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" + before do + @mock_request = mock('webrick http request') + @mock_response = mock('webrick http response') + @mock_model_class = mock('indirected model class') + Puppet::Indirector::Indirection.stubs(:model).with(:foo).returns(@mock_model_class) + @mock_webrick = mock('mongrel http server') + @mock_webrick.stubs(:mount) + @handler = Puppet::Network::HTTP::WEBrickREST.new(:server => @mock_webrick, :handler => :foo) + end - it "should call the controller find method if the request represents a singular HTTP GET" + 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') + @mock_model_class.expects(:find) + @handler.service(@mock_request, @mock_response) + end + + it "should call the model search method if the request represents a plural HTTP GET" do + @mock_request.stubs(:request_method).returns('GET') + @mock_request.stubs(:path).returns('/foos') + @mock_model_class.expects(:search) + @handler.service(@mock_request, @mock_response) + end - it "should call the controller search method if the request represents a plural HTTP GET" + 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') + @mock_model_class.expects(:destroy) + @handler.service(@mock_request, @mock_response) + end + + it "should call the model save method if the request represents an HTTP PUT" do + @mock_request.stubs(:request_method).returns('PUT') + @mock_request.stubs(:path).returns('/foo') + mock_model_instance = mock('indirected model instance') + mock_model_instance.expects(:save) + @mock_model_class.expects(:new).returns(mock_model_instance) + @handler.service(@mock_request, @mock_response) + end - it "should call the controller destroy method if the request represents an HTTP DELETE" + it "should fail if the HTTP method isn't supported" do + @mock_request.stubs(:request_method).returns('POST') + @mock_request.stubs(:path).returns('/foo') + Proc.new { @handler.service(@mock_request, @mock_response) }.should raise_error(ArgumentError) + end + + it "should unpack request information from WEBrick" - it "should call the controller save method if the request represents an HTTP PUT" + it "should unpack parameters from the request for passing to controller methods" - it "should serialize the result from the controller method for return back to WEBrick" + 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 WEBrick" + it "should serialize a controller exception result for return back to Mongrel" end |
