require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe "Cloudhdr::HTTP" do it "should have a default_options hash" do Cloudhdr::HTTP.default_options.is_a?(Hash).should be_true end it "should have a default HTTP backend" do Cloudhdr::HTTP.http_backend.should_not be_nil end it "should allow the changing of the HTTP backend" do Cloudhdr::HTTP.http_backend.should_not == Cloudhdr::HTTP::Typhoeus lambda { Cloudhdr::HTTP.http_backend = Cloudhdr::HTTP::Typhoeus }.should_not raise_error(Exception) Cloudhdr::HTTP.http_backend.should == Cloudhdr::HTTP::Typhoeus end it "should raise a Cloudhdr::HTTPError when there is an HTTP error" do Cloudhdr::HTTP.http_backend.should_receive(:get). with('https://example.com', Cloudhdr::HTTP.default_options). twice. and_raise(Errno::ECONNREFUSED) lambda { Cloudhdr::HTTP.get('https://example.com') }.should raise_error(Cloudhdr::HTTPError) begin Cloudhdr::HTTP.get('https://example.com') rescue Cloudhdr::HTTPError => e e.backtrace.first.should_not =~ /perform_method/ end end it "should return a Cloudhdr::Response" do Cloudhdr::HTTP.http_backend.stub(:post).and_return(stub(:code => 200, :body => '{"some": "hash"}')) Cloudhdr::HTTP.http_backend.stub(:put).and_return(stub(:code => 200, :body => '{"some": "hash"}')) Cloudhdr::HTTP.http_backend.stub(:get).and_return(stub(:code => 200, :body => '{"some": "hash"}')) Cloudhdr::HTTP.http_backend.stub(:delete).and_return(stub(:code => 200, :body => '{"some": "hash"}')) Cloudhdr::HTTP.post('https://example.com', '{"some": "hash"}').is_a?(Cloudhdr::Response).should be_true Cloudhdr::HTTP.put('https://example.com', '{"some": "hash"}').is_a?(Cloudhdr::Response).should be_true Cloudhdr::HTTP.get('https://example.com').is_a?(Cloudhdr::Response).should be_true Cloudhdr::HTTP.delete('https://example.com').is_a?(Cloudhdr::Response).should be_true end it "should store the raw response" do post_stub = stub(:code => 200, :body => '{"some": "hash"}') Cloudhdr::HTTP.http_backend.stub(:post).and_return(post_stub) Cloudhdr::HTTP.post('https://example.com', '{"some": "hash"}').raw_response.should == post_stub end it "should store the raw response body" do Cloudhdr::HTTP.http_backend.stub(:post).and_return(stub(:code => 200, :body => '{"some": "hash"}')) Cloudhdr::HTTP.post('https://example.com', '{"some": "hash"}').raw_body.should == '{"some": "hash"}' end it "should store the response code" do Cloudhdr::HTTP.http_backend.stub(:post).and_return(stub(:code => 200, :body => '{"some": "hash"}')) Cloudhdr::HTTP.post('https://example.com', '{"some": "hash"}').code.should == 200 end it "should JSON parse the response body" do Cloudhdr::HTTP.http_backend.stub(:put).and_return(stub(:code => 200, :body => '{"some": "hash"}')) Cloudhdr::HTTP.put('https://example.com', '{"some": "hash"}').body.should == {'some' => 'hash'} end it "should store the raw body if the body fails to be JSON parsed" do Cloudhdr::HTTP.http_backend.stub(:put).and_return(stub(:code => 200, :body => '{"some": bad json')) Cloudhdr::HTTP.put('https://example.com', '{"some": "hash"}').body.should == '{"some": bad json' end describe ".post" do it "should call post on the http_backend" do Cloudhdr::HTTP.http_backend.should_receive(:post). with('https://example.com', Cloudhdr::HTTP.default_options.merge(:body => '{}')). and_return(Cloudhdr::Response.new) Cloudhdr::HTTP.post('https://example.com', '{}').should_not be_nil end end describe ".put" do it "should call put on the http_backend" do Cloudhdr::HTTP.http_backend.should_receive(:put). with('https://example.com', Cloudhdr::HTTP.default_options.merge(:body => '{}')). and_return(Cloudhdr::Response.new) Cloudhdr::HTTP.put('https://example.com', '{}').should_not be_nil end end describe ".get" do it "should call post on the http_backend" do Cloudhdr::HTTP.http_backend.should_receive(:get). with('https://example.com', Cloudhdr::HTTP.default_options). and_return(Cloudhdr::Response.new) Cloudhdr::HTTP.get('https://example.com').should_not be_nil end end describe ".delete" do it "should call post on the http_backend" do Cloudhdr::HTTP.http_backend.should_receive(:delete). with('https://example.com', Cloudhdr::HTTP.default_options). and_return(Cloudhdr::Response.new) Cloudhdr::HTTP.delete('https://example.com').should_not be_nil end end end