summaryrefslogtreecommitdiffstats
path: root/spec/unit
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-07-28 11:23:08 -0500
committerLuke Kanies <luke@madstop.com>2008-07-29 00:51:22 -0500
commit4632cfd9e6ce0ff59dfa7562a02a1ae3f14488d4 (patch)
tree50f6c0ef591867bafe4ac9ea3c3f64d045bec573 /spec/unit
parente3350caeec3a662b0b92ec2dee372563a493fa11 (diff)
All error and format handling works over REST except searching.
Searching operates on multiple instances, and I have not yet figured out how we should handle converting multiple instances to a given format -- we can't use the instance method (e.g., to_yaml), because it would be on Array instead of the class we're operating on. That would work for yaml, but not, for instance, for xml.
Diffstat (limited to 'spec/unit')
-rwxr-xr-xspec/unit/indirector/rest.rb37
-rwxr-xr-xspec/unit/network/http/handler.rb12
-rwxr-xr-xspec/unit/network/http/webrick/rest.rb15
3 files changed, 45 insertions, 19 deletions
diff --git a/spec/unit/indirector/rest.rb b/spec/unit/indirector/rest.rb
index 350b8a1d0..8d88ca7bb 100755
--- a/spec/unit/indirector/rest.rb
+++ b/spec/unit/indirector/rest.rb
@@ -46,25 +46,32 @@ describe Puppet::Indirector::REST do
end
describe "when deserializing responses" do
- it "should return the results of converting from the format specified by the content-type header" do
- @model.expects(:convert_from).with("myformat", "mydata").returns "myobject"
-
+ it "should return nil if the response code is 404" do
response = mock 'response'
- response.expects(:[]).with("content-type").returns "myformat"
- response.expects(:body).returns "mydata"
- response.stubs(:code).returns "200"
+ response.expects(:code).returns "404"
- @searcher.deserialize(response)
+ @searcher.deserialize(response).should be_nil
end
- it "should fail if the response is not a success" do
+ it "should fail if the response code is not in the 200s" do
@model.expects(:convert_from).never
response = mock 'response'
- response.expects(:code).returns "300"
- response.expects(:error!).raises ArgumentError
+ response.stubs(:code).returns "300"
+ response.stubs(:message).returns "There was a problem"
- lambda { @searcher.deserialize(response) }.should raise_error(ArgumentError)
+ lambda { @searcher.deserialize(response) }.should raise_error(Net::HTTPError)
+ end
+
+ it "should return the results of converting from the format specified by the content-type header if the response code is in the 200s" do
+ @model.expects(:convert_from).with("myformat", "mydata").returns "myobject"
+
+ response = mock 'response'
+ response.stubs(:[]).with("content-type").returns "myformat"
+ response.stubs(:body).returns "mydata"
+ response.stubs(:code).returns "200"
+
+ @searcher.deserialize(response)
end
end
@@ -147,15 +154,15 @@ describe Puppet::Indirector::REST do
@searcher.search(@request).should == 'myobject'
end
- it "should use the indirection name as the path if there is no request key" do
- should_path = "/%s" % [@indirection.name.to_s]
+ it "should use the plural indirection name as the path if there is no request key" do
+ should_path = "/%ss" % [@indirection.name.to_s]
@request.stubs(:key).returns nil
@connection.expects(:get).with { |path, args| path == should_path }.returns(@response)
@searcher.search(@request)
end
- it "should use the indirection name and request key to create the path if the request key is set" do
- should_path = "/%s/%s" % [@indirection.name.to_s, "foo"]
+ it "should use the plural indirection name and request key to create the path if the request key is set" do
+ should_path = "/%ss/%s" % [@indirection.name.to_s, "foo"]
@connection.expects(:get).with { |path, args| path == should_path }.returns(@response)
@searcher.search(@request)
end
diff --git a/spec/unit/network/http/handler.rb b/spec/unit/network/http/handler.rb
index 36e566624..0f60b9b10 100755
--- a/spec/unit/network/http/handler.rb
+++ b/spec/unit/network/http/handler.rb
@@ -212,6 +212,11 @@ describe Puppet::Network::HTTP::Handler do
@handler.process(@request, @response)
end
+ it "should set the format to text/plain when serializing an exception" do
+ @handler.expects(:set_content_type).with(@response, "text/plain")
+ @handler.do_exception(@response, "A test", 404)
+ end
+
describe "when finding a model instance" do
before do
@handler.stubs(:http_method).returns('GET')
@@ -262,6 +267,13 @@ describe Puppet::Network::HTTP::Handler do
@handler.do_find(@request, @response)
end
+ it "should return a 404 when no model instance can be found" do
+ @model_class.stubs(:name).returns "my name"
+ @handler.expects(:set_response).with { |response, body, status| status == 404 }
+ @model_class.stubs(:find).returns(nil)
+ @handler.do_find(@request, @response)
+ end
+
it "should serialize the result in with the appropriate format" do
@model_instance = stub('model instance')
diff --git a/spec/unit/network/http/webrick/rest.rb b/spec/unit/network/http/webrick/rest.rb
index b42053d53..4c72ec545 100755
--- a/spec/unit/network/http/webrick/rest.rb
+++ b/spec/unit/network/http/webrick/rest.rb
@@ -33,7 +33,7 @@ describe Puppet::Network::HTTP::WEBrickREST do
describe "when using the Handler interface" do
it "should use the 'accept' request parameter as the Accept header" do
- @request.expects(:[]).with(:accept).returns "foobar"
+ @request.expects(:[]).with("accept").returns "foobar"
@handler.accept_header(@request).should == "foobar"
end
@@ -57,17 +57,24 @@ describe Puppet::Network::HTTP::WEBrickREST do
@handler.body(@request).should == "my body"
end
- it "should set the response's :content_type header when setting the content type" do
- @response.expects(:[]=).with(:content_type, "text/html")
+ it "should set the response's 'content-type' header when setting the content type" do
+ @response.expects(:[]=).with("content-type", "text/html")
@handler.set_content_type(@response, "text/html")
end
- it "should set the status and body on the response when setting the response" do
+ it "should set the status and body on the response when setting the response for a successful query" do
@response.expects(:status=).with 200
@response.expects(:body=).with "mybody"
@handler.set_response(@response, "mybody", 200)
end
+
+ it "should set the status and message on the response when setting the response for a failed query" do
+ @response.expects(:status=).with 400
+ @response.expects(:reason_phrase=).with "mybody"
+
+ @handler.set_response(@response, "mybody", 400)
+ end
end
describe "and determining the request parameters" do