spec/unit/response_spec.rb in restfulness-0.2.1 vs spec/unit/response_spec.rb in restfulness-0.2.2

- old
+ new

@@ -34,10 +34,11 @@ describe "#run" do context "without route" do it "should not do anything" do request.stub(:route).and_return(nil) + request.stub(:uri).and_return(URI('http://test.com/test')) obj.run obj.status.should eql(404) obj.payload.should be_empty obj.headers['Content-Type'].should match(/text\/plain/) obj.headers['Content-Length'].should eql(0.to_s) @@ -49,10 +50,11 @@ end it "should try to build resource and run it" do request.stub(:route).and_return(route) request.action = :get + request.stub(:uri).and_return(URI('http://test.com/test')) resource = double(:Resource) resource.should_receive(:check_callbacks) resource.should_receive(:call).and_return({:foo => 'bar'}) route.stub(:build_resource).and_return(resource) obj.run @@ -64,10 +66,11 @@ end it "should call resource and set 204 result if no content" do request.stub(:route).and_return(route) request.action = :get + request.stub(:uri).and_return(URI('http://test.com/test')) resource = double(:Resource) resource.should_receive(:check_callbacks) resource.should_receive(:call).and_return(nil) route.stub(:build_resource).and_return(resource) obj.run @@ -76,10 +79,11 @@ end it "should set string content type if payload is a string" do request.stub(:route).and_return(route) request.action = :get + request.stub(:uri).and_return(URI('http://test.com/test')) resource = double(:Resource) resource.should_receive(:check_callbacks) resource.should_receive(:call).and_return("This is a text message") route.stub(:build_resource).and_return(resource) obj.run @@ -94,10 +98,11 @@ end it "should update the status and payload" do request.stub(:route).and_return(route) request.action = :get + request.stub(:uri).and_return(URI('http://test.com/test')) resource = double(:Resource) txt = "This is a text error" resource.stub(:check_callbacks) do raise Restfulness::HTTPException.new(418, txt) end @@ -109,10 +114,11 @@ end it "should update the status and provide JSON payload" do request.stub(:route).and_return(route) request.action = :get + request.stub(:uri).and_return(URI('http://test.com/test')) resource = double(:Resource) err = {:error => "This is a text error"} resource.stub(:check_callbacks) do raise Restfulness::HTTPException.new(418, err) end @@ -121,9 +127,25 @@ obj.status.should eql(418) obj.headers['Content-Type'].should match(/application\/json/) obj.payload.should eql(err.to_json) end + context "for non http errors" do + + it "should catch error and provide result" do + request.stub(:route).and_return(route) + request.action = :get + request.stub(:uri).and_return(URI('http://test.com/test')) + resource = double(:Resource) + resource.stub(:check_callbacks) do + raise SyntaxError, 'Bad writing' + end + route.stub(:build_resource).and_return(resource) + obj.run + obj.status.should eql(500) + end + + end end end