summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/puppet/network/http/handler.rb8
-rw-r--r--lib/puppet/network/http/mongrel/rest.rb4
-rw-r--r--lib/puppet/network/http/webrick/rest.rb4
-rw-r--r--spec/unit/network/http/mongrel/rest.rb62
-rw-r--r--spec/unit/network/http/webrick/rest.rb60
5 files changed, 116 insertions, 22 deletions
diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb
index 2a537c767..172939538 100644
--- a/lib/puppet/network/http/handler.rb
+++ b/lib/puppet/network/http/handler.rb
@@ -13,6 +13,8 @@ class Puppet::Network::HTTP::Handler
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)}'"
+ rescue Exception => e
+ return do_exception(request, response, e)
end
private
@@ -47,6 +49,10 @@ class Puppet::Network::HTTP::Handler
encode_result(request, response, result)
end
+ def do_exception(request, response, exception, status=404)
+ encode_result(request, response, exception.to_s, status)
+ end
+
def find_model_for_handler(handler)
Puppet::Indirector::Indirection.model(handler) ||
raise(ArgumentError, "Cannot locate indirection [#{handler}].")
@@ -98,7 +104,7 @@ class Puppet::Network::HTTP::Handler
raise NotImplementedError
end
- def encode_result(request, response, result)
+ def encode_result(request, response, result, status = 200)
raise NotImplementedError
end
end
diff --git a/lib/puppet/network/http/mongrel/rest.rb b/lib/puppet/network/http/mongrel/rest.rb
index f22b4c4c9..db63613ab 100644
--- a/lib/puppet/network/http/mongrel/rest.rb
+++ b/lib/puppet/network/http/mongrel/rest.rb
@@ -29,8 +29,8 @@ class Puppet::Network::HTTP::MongrelREST < Puppet::Network::HTTP::Handler
Mongrel::HttpRequest.query_parse(request.params["QUERY_STRING"])
end
- def encode_result(request, response, result)
- response.start(200) do |head, body|
+ def encode_result(request, response, result, status = 200)
+ response.start(status) do |head, body|
body.write(result)
end
end
diff --git a/lib/puppet/network/http/webrick/rest.rb b/lib/puppet/network/http/webrick/rest.rb
index 8782df14f..dd0c84d61 100644
--- a/lib/puppet/network/http/webrick/rest.rb
+++ b/lib/puppet/network/http/webrick/rest.rb
@@ -34,8 +34,8 @@ class Puppet::Network::HTTP::WEBrickREST < Puppet::Network::HTTP::Handler
request.query
end
- def encode_result(request, response, result)
- response.status = 200
+ def encode_result(request, response, result, status = 200)
+ response.status = status
response.body = result
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 1cdc147dc..9b2feb6ee 100644
--- a/spec/unit/network/http/mongrel/rest.rb
+++ b/spec/unit/network/http/mongrel/rest.rb
@@ -34,7 +34,7 @@ describe Puppet::Network::HTTP::MongrelREST, "when initializing" 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 itself with the mongrel server for the singular HTTP methods" do
@mock_mongrel.expects(:register).with do |*args|
args.first == '/foo' and args.last.is_a? Puppet::Network::HTTP::MongrelREST
@@ -95,6 +95,10 @@ describe Puppet::Network::HTTP::MongrelREST, "when receiving a request" do
@mock_model_class.stubs(:new).returns(@mock_model_instance)
end
+ def setup_bad_request
+ @mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'POST', Mongrel::Const::REQUEST_PATH => '/foos'})
+ end
+
it "should call the model find method if the request represents a singular HTTP GET" do
setup_find_request
@mock_model_class.expects(:find).with('key', {})
@@ -121,37 +125,45 @@ describe Puppet::Network::HTTP::MongrelREST, "when receiving a request" do
it "should fail if the HTTP method isn't supported" do
@mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'POST', Mongrel::Const::REQUEST_PATH => '/foo'})
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
end
it "should fail if the request's pluralization is wrong" do
@mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'DELETE', Mongrel::Const::REQUEST_PATH => '/foos/key'})
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
+
@mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'PUT', Mongrel::Const::REQUEST_PATH => '/foos/key'})
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
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',
'QUERY_STRING' => '' })
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
end
it "should fail to find model if key is not specified" do
@mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'GET', Mongrel::Const::REQUEST_PATH => '/foo'})
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
end
it "should fail to destroy model if key is not specified" do
@mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'DELETE', Mongrel::Const::REQUEST_PATH => '/foo'})
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
end
it "should fail to save model if data is not specified" do
@mock_request.stubs(:params).returns({ Mongrel::Const::REQUEST_METHOD => 'PUT', Mongrel::Const::REQUEST_PATH => '/foo'})
@mock_request.stubs(:body).returns('')
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
end
it "should pass HTTP request parameters to model find" do
@@ -239,5 +251,37 @@ describe Puppet::Network::HTTP::MongrelREST, "when receiving a request" do
@handler.process(@mock_request, @mock_response)
end
- it "should serialize a controller exception when an exception is thrown by the handler"
+ it "should serialize a controller exception when an exception is thrown by find" do
+ setup_find_request
+ @mock_model_class.expects(:find).raises(ArgumentError)
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should serialize a controller exception when an exception is thrown by search" do
+ setup_search_request
+ @mock_model_class.expects(:search).raises(ArgumentError)
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should serialize a controller exception when an exception is thrown by destroy" do
+ setup_destroy_request
+ @mock_model_class.expects(:destroy).raises(ArgumentError)
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should serialize a controller exception when an exception is thrown by save" do
+ setup_save_request
+ @mock_model_instance.expects(:save).raises(ArgumentError)
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should serialize a controller exception if the request fails" do
+ setup_bad_request
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
end
diff --git a/spec/unit/network/http/webrick/rest.rb b/spec/unit/network/http/webrick/rest.rb
index 14505f919..aa7a3d53a 100644
--- a/spec/unit/network/http/webrick/rest.rb
+++ b/spec/unit/network/http/webrick/rest.rb
@@ -83,6 +83,11 @@ describe Puppet::Network::HTTP::WEBrickREST, "when receiving a request" do
@mock_model_class.stubs(:new).returns(@mock_model_instance)
end
+ def setup_bad_request
+ @mock_request.stubs(:request_method).returns('POST')
+ @mock_request.stubs(:path).returns('/foos')
+ end
+
it "should call the model find method if the request represents a singular HTTP GET" do
setup_find_request
@mock_model_class.expects(:find).with('key', {})
@@ -111,42 +116,49 @@ describe Puppet::Network::HTTP::WEBrickREST, "when receiving a request" do
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)
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
end
it "should fail if the request's pluralization is wrong" do
@mock_request.stubs(:request_method).returns('DELETE')
@mock_request.stubs(:path).returns('/foos/key')
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
@mock_request.stubs(:request_method).returns('PUT')
@mock_request.stubs(:path).returns('/foos/key')
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
end
it "should fail if the request is for an unknown path" do
@mock_request.stubs(:request_method).returns('GET')
@mock_request.stubs(:path).returns('/bar/key')
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
end
it "should fail to find model if key is not specified" do
@mock_request.stubs(:request_method).returns('GET')
@mock_request.stubs(:path).returns('/foo')
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
end
it "should fail to destroy model if key is not specified" do
@mock_request.stubs(:request_method).returns('DELETE')
@mock_request.stubs(:path).returns('/foo')
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
end
it "should fail to save model if data is not specified" do
@mock_request.stubs(:request_method).returns('PUT')
@mock_request.stubs(:path).returns('/foo')
@mock_request.stubs(:body).returns('')
- Proc.new { @handler.process(@mock_request, @mock_response) }.should raise_error(ArgumentError)
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
end
it "should pass HTTP request parameters to model find" do
@@ -238,5 +250,37 @@ describe Puppet::Network::HTTP::WEBrickREST, "when receiving a request" do
@handler.process(@mock_request, @mock_response)
end
- it "should serialize a controller exception when an exception is thrown by the handler"
+ it "should serialize a controller exception when an exception is thrown by find" do
+ setup_find_request
+ @mock_model_class.expects(:find).raises(ArgumentError)
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should serialize a controller exception when an exception is thrown by search" do
+ setup_search_request
+ @mock_model_class.expects(:search).raises(ArgumentError)
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should serialize a controller exception when an exception is thrown by destroy" do
+ setup_destroy_request
+ @mock_model_class.expects(:destroy).raises(ArgumentError)
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should serialize a controller exception when an exception is thrown by save" do
+ setup_save_request
+ @mock_model_instance.expects(:save).raises(ArgumentError)
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should serialize a controller exception if the request fails" do
+ setup_bad_request
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
end