summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRick Bradley <rick@rickbradley.com>2008-04-01 22:55:22 -0500
committerLuke Kanies <luke@madstop.com>2008-04-11 13:10:36 -0500
commit1e0f19bcbd0c3f5a83f9cad0e90eb5d6478e278b (patch)
tree4fedc7e8db8ba6b300718c75542c091c48feea00
parentd24c03c9bbcc35a94a8235c030a73233feabad57 (diff)
downloadpuppet-1e0f19bcbd0c3f5a83f9cad0e90eb5d6478e278b.tar.gz
puppet-1e0f19bcbd0c3f5a83f9cad0e90eb5d6478e278b.tar.xz
puppet-1e0f19bcbd0c3f5a83f9cad0e90eb5d6478e278b.zip
Make mongrel happy like WEBrick.
Refactored specs to put some of the lower-level find/save/search/destroy unit tests under their own contexts.
-rw-r--r--lib/puppet/network/http/mongrel.rb4
-rw-r--r--lib/puppet/network/http/mongrel/rest.rb7
-rw-r--r--spec/unit/network/http/mongrel.rb12
-rw-r--r--spec/unit/network/http/mongrel/rest.rb264
-rw-r--r--spec/unit/network/http/webrick/rest.rb274
5 files changed, 274 insertions, 287 deletions
diff --git a/lib/puppet/network/http/mongrel.rb b/lib/puppet/network/http/mongrel.rb
index 941ef0e43..9a4531c7a 100644
--- a/lib/puppet/network/http/mongrel.rb
+++ b/lib/puppet/network/http/mongrel.rb
@@ -38,8 +38,10 @@ class Puppet::Network::HTTP::Mongrel
def setup_handlers
@protocols.each do |protocol|
+ klass = class_for_protocol(protocol)
@handlers.each do |handler|
- class_for_protocol(protocol).new(:server => @server, :handler => handler)
+ @server.register('/' + handler.to_s, klass.new(:server => @server, :handler => handler))
+ @server.register('/' + handler.to_s + 's', klass.new(:server => @server, :handler => handler))
end
end
end
diff --git a/lib/puppet/network/http/mongrel/rest.rb b/lib/puppet/network/http/mongrel/rest.rb
index d7a1a1bdc..2a3d4f143 100644
--- a/lib/puppet/network/http/mongrel/rest.rb
+++ b/lib/puppet/network/http/mongrel/rest.rb
@@ -5,17 +5,12 @@ class Puppet::Network::HTTP::MongrelREST < Mongrel::HttpHandler
include Puppet::Network::HTTP::Handler
def initialize(args={})
+ super()
initialize_for_puppet(args)
end
private
- # have this mongrel @server listen for /foo and /foos REST endpoints
- def register_handler
- @server.register('/' + @handler.to_s, self)
- @server.register('/' + @handler.to_s + 's', self)
- end
-
# which HTTP verb was used in this request
def http_method(request)
request.params[Mongrel::Const::REQUEST_METHOD]
diff --git a/spec/unit/network/http/mongrel.rb b/spec/unit/network/http/mongrel.rb
index cd23ed9e1..ccfca2f55 100644
--- a/spec/unit/network/http/mongrel.rb
+++ b/spec/unit/network/http/mongrel.rb
@@ -65,27 +65,21 @@ describe Puppet::Network::HTTP::Mongrel, "when turning on listening" do
it "should instantiate a handler for each protocol+handler pair to configure web server routing" 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)
+ @mock_mongrel.expects(:register)
end
- @server.expects(:class_for_protocol).with(protocol).at_least_once.returns(mock_handler_class)
end
@server.listen(@listen_params)
end
it "should use a Mongrel + REST class to configure Mongrel when REST services are requested" do
- Puppet::Network::HTTP::MongrelREST.expects(:new).at_least_once
- @server.listen(@listen_params.merge(:protocols => [:rest]))
+ @server.expects(:class_for_protocol).with(:rest).at_least_once.returns(Puppet::Network::HTTP::MongrelREST)
+ @server.listen(@listen_params)
end
it "should fail if services from an unknown protocol are requested" do
Proc.new { @server.listen(@listen_params.merge(:protocols => [ :foo ]))}.should raise_error(ArgumentError)
end
-
end
describe Puppet::Network::HTTP::Mongrel, "when turning off listening" do
diff --git a/spec/unit/network/http/mongrel/rest.rb b/spec/unit/network/http/mongrel/rest.rb
index 9b2feb6ee..3de314852 100644
--- a/spec/unit/network/http/mongrel/rest.rb
+++ b/spec/unit/network/http/mongrel/rest.rb
@@ -1,8 +1,3 @@
-#!/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'
@@ -34,20 +29,6 @@ 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
- end
- Puppet::Network::HTTP::MongrelREST.new(@params)
- end
-
- it "should register itself with the mongrel server for the plural GET method" do
- @mock_mongrel.expects(:register).with do |*args|
- args.first == '/foos' and args.last.is_a? Puppet::Network::HTTP::MongrelREST
- end
- Puppet::Network::HTTP::MongrelREST.new(@params)
- end
end
describe Puppet::Network::HTTP::MongrelREST, "when receiving a request" do
@@ -147,137 +128,146 @@ describe Puppet::Network::HTTP::MongrelREST, "when receiving a request" do
@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'})
- @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'})
- @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('')
- @mock_response.expects(:start).with(404)
- @handler.process(@mock_request, @mock_response)
- end
+ describe "when finding a model instance" do |variable|
+ 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'})
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
- it "should pass HTTP request parameters to model find" do
- setup_find_request('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'
+ it "should pass HTTP request parameters to model find" do
+ setup_find_request('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
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should pass HTTP request parameters to model search" do
- setup_search_request('QUERY_STRING' => 'foo=baz&bar=xyzzy')
- @mock_model_class.expects(:search).with do |args|
- args['foo'] == 'baz' and args['bar'] == 'xyzzy'
- end.returns([])
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should pass HTTP request parameters to model delete" do
- setup_destroy_request('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'
+
+ it "should generate a 200 response when a model find call succeeds" do
+ setup_find_request
+ @mock_response.expects(:start).with(200)
+ @handler.process(@mock_request, @mock_response)
end
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should pass HTTP request parameters to model save" do
- setup_save_request('QUERY_STRING' => 'foo=baz&bar=xyzzy')
- @mock_model_instance.expects(:save).with do |args|
- args[:data] == 'this is a fake request body' and args['foo'] == 'baz' and args['bar'] == 'xyzzy'
+
+ it "should return a serialized object when a model find call succeeds" do
+ setup_find_request
+ @mock_model_instance = stub('model instance')
+ @mock_model_instance.expects(:to_yaml)
+ @mock_model_class.stubs(:find).returns(@mock_model_instance)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ 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
- @handler.process(@mock_request, @mock_response)
end
- it "should generate a 200 response when a model find call succeeds" do
- setup_find_request
- @mock_response.expects(:start).with(200)
- @handler.process(@mock_request, @mock_response)
+ describe "when destroying a model instance" do |variable|
+ 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'})
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should pass HTTP request parameters to model destroy" do
+ setup_destroy_request('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 generate a 200 response when a model destroy call succeeds" do
+ setup_destroy_request
+ @mock_response.expects(:start).with(200)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should return a serialized success result when a model destroy call succeeds" do
+ setup_destroy_request
+ @mock_model_class.stubs(:destroy).returns(true)
+ @mock_body.expects(:write).with("--- true\n")
+ @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
end
- it "should generate a 200 response when a model search call succeeds" do
- setup_search_request
- @mock_response.expects(:start).with(200)
- @handler.process(@mock_request, @mock_response)
+ describe "when saving a model instance" do |variable|
+ 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('')
+ @mock_response.expects(:start).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should pass HTTP request parameters to model save" do
+ setup_save_request('QUERY_STRING' => 'foo=baz&bar=xyzzy')
+ @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
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should generate a 200 response when a model save call succeeds" do
+ setup_save_request
+ @mock_response.expects(:start).with(200)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should return a serialized object when a model save call succeeds" do
+ setup_save_request
+ @mock_model_instance.stubs(:save).returns(@mock_model_instance)
+ @mock_model_instance.expects(:to_yaml).returns('foo')
+ @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
end
- it "should generate a 200 response when a model destroy call succeeds" do
- setup_destroy_request
- @mock_response.expects(:start).with(200)
- @handler.process(@mock_request, @mock_response)
- end
+ describe "when searching for model instances" do |variable|
+ it "should pass HTTP request parameters to model search" do
+ setup_search_request('QUERY_STRING' => 'foo=baz&bar=xyzzy')
+ @mock_model_class.expects(:search).with do |args|
+ args['foo'] == 'baz' and args['bar'] == 'xyzzy'
+ end.returns([])
+ @handler.process(@mock_request, @mock_response)
+ end
- it "should generate a 200 response when a model save call succeeds" do
- setup_save_request
- @mock_response.expects(:start).with(200)
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should return a serialized object when a model find call succeeds" do
- setup_find_request
- @mock_model_instance = stub('model instance')
- @mock_model_instance.expects(:to_yaml)
- @mock_model_class.stubs(:find).returns(@mock_model_instance)
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should return a list of serialized objects when a model search call succeeds" do
- setup_search_request
- mock_matches = [1..5].collect {|i| mock("model instance #{i}", :to_yaml => "model instance #{i}") }
- @mock_model_class.stubs(:search).returns(mock_matches)
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should return a serialized success result when a model destroy call succeeds" do
- setup_destroy_request
- @mock_model_class.stubs(:destroy).returns(true)
- @mock_body.expects(:write).with("--- true\n")
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should return a serialized object when a model save call succeeds" do
- setup_save_request
- @mock_model_instance.stubs(:save).returns(@mock_model_instance)
- @mock_model_instance.expects(:to_yaml).returns('foo')
- @handler.process(@mock_request, @mock_response)
- end
-
- 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 generate a 200 response when a model search call succeeds" do
+ setup_search_request
+ @mock_response.expects(:start).with(200)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should return a list of serialized objects when a model search call succeeds" do
+ setup_search_request
+ mock_matches = [1..5].collect {|i| mock("model instance #{i}", :to_yaml => "model instance #{i}") }
+ @mock_model_class.stubs(:search).returns(mock_matches)
+ @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 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
+ end
it "should serialize a controller exception if the request fails" do
setup_bad_request
diff --git a/spec/unit/network/http/webrick/rest.rb b/spec/unit/network/http/webrick/rest.rb
index cafd38b82..cdc1164fc 100644
--- a/spec/unit/network/http/webrick/rest.rb
+++ b/spec/unit/network/http/webrick/rest.rb
@@ -1,8 +1,3 @@
-#!/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'
@@ -77,6 +72,7 @@ describe Puppet::Network::HTTP::WEBrickREST, "when receiving a request" do
@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', {})
@@ -109,12 +105,14 @@ describe Puppet::Network::HTTP::WEBrickREST, "when receiving a request" do
@handler.process(@mock_request, @mock_response)
end
- it "should fail if the request's pluralization is wrong" do
+ it "should fail if delete request's pluralization is wrong" do
@mock_request.stubs(:request_method).returns('DELETE')
@mock_request.stubs(:path).returns('/foos/key')
@mock_response.expects(:status=).with(404)
@handler.process(@mock_request, @mock_response)
-
+ end
+
+ it "should fail if put request's pluralization is wrong" do
@mock_request.stubs(:request_method).returns('PUT')
@mock_request.stubs(:path).returns('/foos/key')
@mock_response.expects(:status=).with(404)
@@ -128,145 +126,153 @@ describe Puppet::Network::HTTP::WEBrickREST, "when receiving a request" do
@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')
- @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')
- @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('')
- @mock_response.expects(:status=).with(404)
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should pass HTTP request parameters to model find" do
- setup_find_request
- @mock_request.stubs(:query).returns(:foo => :baz, :bar => :xyzzy)
- @mock_model_class.expects(:find).with do |key, args|
- key == 'key' and args[:foo] == :baz and args[:bar] == :xyzzy
+ describe "when finding a model instance" do |variable|
+ 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')
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should pass HTTP request parameters to model find" do
+ setup_find_request
+ @mock_request.stubs(:query).returns(: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.service(@mock_request, @mock_response)
+ end
+
+ it "should generate a 200 response when a model find call succeeds" do
+ setup_find_request
+ @mock_response.expects(:status=).with(200)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should return a serialized object when a model find call succeeds" do
+ setup_find_request
+ @mock_model_instance = stub('model instance')
+ @mock_model_instance.expects(:to_yaml)
+ @mock_model_class.stubs(:find).returns(@mock_model_instance)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ 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
- @handler.service(@mock_request, @mock_response)
- end
-
- it "should pass HTTP request parameters to model search" do
- setup_search_request
- @mock_request.stubs(:query).returns(:foo => :baz, :bar => :xyzzy)
- @mock_model_class.expects(:search).with do |args|
- args[:foo] == :baz and args[:bar] == :xyzzy
- end.returns([])
- @handler.service(@mock_request, @mock_response)
end
- it "should pass HTTP request parameters to model destroy" do
- setup_destroy_request
- @mock_request.stubs(:query).returns(:foo => :baz, :bar => :xyzzy)
- @mock_model_class.expects(:destroy).with do |key, args|
- key == 'key' and args[:foo] == :baz and args[:bar] == :xyzzy
+ describe "when destroying a model instance" do |variable|
+ 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')
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should pass HTTP request parameters to model destroy" do
+ setup_destroy_request
+ @mock_request.stubs(:query).returns(: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.service(@mock_request, @mock_response)
+ end
+
+ it "should generate a 200 response when a model destroy call succeeds" do
+ setup_destroy_request
+ @mock_response.expects(:status=).with(200)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should return a serialized success result when a model destroy call succeeds" do
+ setup_destroy_request
+ @mock_model_class.stubs(:destroy).returns(true)
+ @mock_response.expects(:body=).with("--- true\n")
+ @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
- @handler.service(@mock_request, @mock_response)
end
- it "should pass HTTP request parameters to model save" do
- setup_save_request
- @mock_request.stubs(:query).returns(:foo => :baz, :bar => :xyzzy)
- @mock_model_instance.expects(:save).with do |args|
- args[:data] == 'This is a fake request body' and args[:foo] == :baz and args[:bar] == :xyzzy
+ describe "when saving a model instance" do
+ 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('')
+ @mock_response.expects(:status=).with(404)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should pass HTTP request parameters to model save" do
+ setup_save_request
+ @mock_request.stubs(:query).returns(:foo => :baz, :bar => :xyzzy)
+ @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
+ @handler.service(@mock_request, @mock_response)
+ end
+
+ it "should generate a 200 response when a model save call succeeds" do
+ setup_save_request
+ @mock_response.expects(:status=).with(200)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should return a serialized object when a model save call succeeds" do
+ setup_save_request
+ @mock_model_instance.stubs(:save).returns(@mock_model_instance)
+ @mock_model_instance.expects(:to_yaml).returns('foo')
+ @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
- @handler.service(@mock_request, @mock_response)
- end
-
- it "should generate a 200 response when a model find call succeeds" do
- setup_find_request
- @mock_response.expects(:status=).with(200)
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should generate a 200 response when a model search call succeeds" do
- setup_search_request
- @mock_response.expects(:status=).with(200)
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should generate a 200 response when a model destroy call succeeds" do
- setup_destroy_request
- @mock_response.expects(:status=).with(200)
- @handler.process(@mock_request, @mock_response)
end
- it "should generate a 200 response when a model save call succeeds" do
- setup_save_request
- @mock_response.expects(:status=).with(200)
- @handler.process(@mock_request, @mock_response)
- end
+ describe "when searching for model instances" do
+ it "should pass HTTP request parameters to model search" do
+ setup_search_request
+ @mock_request.stubs(:query).returns(:foo => :baz, :bar => :xyzzy)
+ @mock_model_class.expects(:search).with do |args|
+ args[:foo] == :baz and args[:bar] == :xyzzy
+ end.returns([])
+ @handler.service(@mock_request, @mock_response)
+ end
- it "should return a serialized object when a model find call succeeds" do
- setup_find_request
- @mock_model_instance = stub('model instance')
- @mock_model_instance.expects(:to_yaml)
- @mock_model_class.stubs(:find).returns(@mock_model_instance)
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should return a list of serialized objects when a model search call succeeds" do
- setup_search_request
- mock_matches = [1..5].collect {|i| mock("model instance #{i}", :to_yaml => "model instance #{i}") }
- @mock_model_class.stubs(:search).returns(mock_matches)
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should return a serialized success result when a model destroy call succeeds" do
- setup_destroy_request
- @mock_model_class.stubs(:destroy).returns(true)
- @mock_response.expects(:body=).with("--- true\n")
- @handler.process(@mock_request, @mock_response)
- end
-
- it "should return a serialized object when a model save call succeeds" do
- setup_save_request
- @mock_model_instance.stubs(:save).returns(@mock_model_instance)
- @mock_model_instance.expects(:to_yaml).returns('foo')
- @handler.process(@mock_request, @mock_response)
- end
-
- 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)
+ it "should generate a 200 response when a model search call succeeds" do
+ setup_search_request
+ @mock_response.expects(:status=).with(200)
+ @handler.process(@mock_request, @mock_response)
+ end
+
+ it "should return a list of serialized objects when a model search call succeeds" do
+ setup_search_request
+ mock_matches = [1..5].collect {|i| mock("model instance #{i}", :to_yaml => "model instance #{i}") }
+ @mock_model_class.stubs(:search).returns(mock_matches)
+ @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
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)