spec/httpi/httpi_spec.rb in httpi-0.3.0 vs spec/httpi/httpi_spec.rb in httpi-0.4.0

- old
+ new

@@ -1,8 +1,11 @@ require "spec_helper" require "httpi" +require "httpclient" +require "curb" + describe HTTPI do let(:client) { HTTPI } let(:default_adapter) { HTTPI::Adapter.find HTTPI::Adapter.use } let(:curb) { HTTPI::Adapter.find :curb } @@ -39,29 +42,33 @@ curb.any_instance.expects(:get).with(instance_of(HTTPI::Request)) client.get "http://example.com", :curb end end - - describe ".get" do + + shared_examples_for "a request method" do context "(with a block)" do it "should yield the HTTP client instance used for the request" do - client.get "http://example.com" do |http| + client.delete "http://example.com" do |http| http.should be_an(HTTPClient) end end end - it "should raise an ArgumentError in case of an invalid adapter" do - lambda { client.get HTTPI::Request.new, :invalid }.should raise_error(ArgumentError) + it "and raise an ArgumentError in case of an invalid adapter" do + lambda { client.delete HTTPI::Request.new, :invalid }.should raise_error(ArgumentError) end - it "should raise an ArgumentError in case of an invalid URL" do - lambda { client.get "invalid" }.should raise_error(ArgumentError) + it "and raise an ArgumentError in case of an invalid URL" do + lambda { client.delete "invalid" }.should raise_error(ArgumentError) end end + describe ".get" do + it_should_behave_like "a request method" + end + describe ".post(request)" do it "should execute an HTTP POST request using the default adapter" do request = HTTPI::Request.new default_adapter.any_instance.expects(:post).with(request) @@ -97,23 +104,131 @@ client.post "http://example.com", "<some>xml</some>", :curb end end describe ".post" do - context "(with a block)" do - it "should yield the HTTP client instance used for the request" do - client.get "http://example.com", :curb do |http| - http.should be_a(Curl::Easy) - end - end + it_should_behave_like "a request method" + end + + describe ".head(request)" do + it "should execute an HTTP HEAD request using the default adapter" do + request = HTTPI::Request.new + default_adapter.any_instance.expects(:head).with(request) + + client.head request end + end - it "should raise an ArgumentError in case of an invalid adapter" do - lambda { client.post HTTPI::Request.new, :invalid }.should raise_error(ArgumentError) + describe ".head(request, adapter)" do + it "should execute an HTTP HEAD request using the given adapter" do + request = HTTPI::Request.new + curb.any_instance.expects(:head).with(request) + + client.head request, :curb end + end - it "should raise an ArgumentError in case of an invalid URL" do - lambda { client.post "invalid" }.should raise_error(ArgumentError) + describe ".head(url)" do + it "should execute an HTTP HEAD request using the default adapter" do + HTTPI::Request.any_instance.expects(:url=).with("http://example.com") + default_adapter.any_instance.expects(:head).with(instance_of(HTTPI::Request)) + + client.head "http://example.com" end + end + + describe ".head(url, adapter)" do + it "should execute an HTTP HEAD request using the given adapter" do + HTTPI::Request.any_instance.expects(:url=).with("http://example.com") + curb.any_instance.expects(:head).with(instance_of(HTTPI::Request)) + + client.head "http://example.com", :curb + end + end + + describe ".head" do + it_should_behave_like "a request method" + end + + describe ".put(request)" do + it "should execute an HTTP PUT request using the default adapter" do + request = HTTPI::Request.new + default_adapter.any_instance.expects(:put).with(request) + + client.put request + end + end + + describe ".put(request, adapter)" do + it "should execute an HTTP PUT request using the given adapter" do + request = HTTPI::Request.new + curb.any_instance.expects(:put).with(request) + + client.put request, :curb + end + end + + describe ".put(url, body)" do + it "should execute an HTTP PUT request using the default adapter" do + HTTPI::Request.any_instance.expects(:url=).with("http://example.com") + HTTPI::Request.any_instance.expects(:body=).with("<some>xml</some>") + default_adapter.any_instance.expects(:put).with(instance_of(HTTPI::Request)) + + client.put "http://example.com", "<some>xml</some>" + end + end + + describe ".put(url, body, adapter)" do + it "should execute an HTTP PUT request using the given adapter" do + HTTPI::Request.any_instance.expects(:url=).with("http://example.com") + HTTPI::Request.any_instance.expects(:body=).with("<some>xml</some>") + curb.any_instance.expects(:put).with(instance_of(HTTPI::Request)) + + client.put "http://example.com", "<some>xml</some>", :curb + end + end + + describe ".put" do + it_should_behave_like "a request method" + end + + describe ".delete(request)" do + it "should execute an HTTP DELETE request using the default adapter" do + request = HTTPI::Request.new + default_adapter.any_instance.expects(:delete).with(request) + + client.delete request + end + end + + describe ".delete(request, adapter)" do + it "should execute an HTTP DELETE request using the given adapter" do + request = HTTPI::Request.new + curb.any_instance.expects(:delete).with(request) + + client.delete request, :curb + end + end + + describe ".delete(url)" do + it "should execute an HTTP DELETE request using the default adapter" do + HTTPI::Request.any_instance.expects(:url=).with("http://example.com") + default_adapter.any_instance.expects(:delete).with(instance_of(HTTPI::Request)) + + client.delete "http://example.com" + end + end + + describe ".delete(url, adapter)" do + it "should execute an HTTP DELETE request using the given adapter" do + HTTPI::Request.any_instance.expects(:url=).with("http://example.com") + curb.any_instance.expects(:delete).with(instance_of(HTTPI::Request)) + + client.delete "http://example.com", :curb + end + end + + describe ".delete" do + it_should_behave_like "a request method" end end