diff --git a/features/step_definitions/vcr_steps.rb b/features/step_definitions/vcr_steps.rb index 45ad1578..12dfd7ad 100644 --- a/features/step_definitions/vcr_steps.rb +++ b/features/step_definitions/vcr_steps.rb @@ -13,12 +13,12 @@ def responses_for_url(responses, url) match do |responses| regex = /#{regex_str}/i - responses_for_url(responses, url).detect { |r| get_body_string(r.response) =~ regex } + responses_for_url(responses, url).detect { |r| r.response.body =~ regex } end failure_message_for_should do |responses| responses = responses_for_url(responses, url) - response_bodies = responses.map { |r| get_body_string(r.response) } + response_bodies = responses.map { |r| r.response.body } "expected a response for #{url.inspect} to match /#{regex_str}/. Responses for #{url.inspect}:\n\n #{response_bodies.join("\n\n")}" end end @@ -115,7 +115,7 @@ def capture_response(url) When /^I make (?:an )?HTTP get request to "([^\"]*)"$/ do |url| capture_response(url) do |uri, path| - http_get(uri) + make_http_request(:get, url) end end diff --git a/features/support/env.rb b/features/support/env.rb index 8fa6f4df..7d38adcb 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -15,6 +15,16 @@ # to decide whether or not to hook into it. require ENV['HTTP_LIB'] +require 'http_library_adapters' + +World case ENV['HTTP_LIB'] + when 'patron' then PatronAdapter + when 'httpclient' then HTTPClientAdapter + when 'net/http' then NetHTTPAdapter + when 'em-http-request' then EmHTTPRequestAdapter + else raise ArgumentError.new("Unexpected HTTP_LIB: #{ENV['HTTP_LIB']}") +end + puts "\n\n---------------- Running features using #{ENV['HTTP_STUBBING_ADAPTER']} and #{ENV['HTTP_LIB']} -----------------\n" require 'vcr' diff --git a/features/support/http_libraries.rb b/features/support/http_libraries.rb deleted file mode 100644 index 42daae88..00000000 --- a/features/support/http_libraries.rb +++ /dev/null @@ -1,72 +0,0 @@ -module NetHTTPAdapter - def get_body_string(response) - response.body - end - - def http_get(uri) - Net::HTTP.get_response(uri) - end -end - -module PatronAdapter - def get_body_string(response) - response.body - end - - def patron_session(uri) - sess = Patron::Session.new - sess.base_url = "#{uri.host}:#{uri.port}" - sess - end - - def http_get(uri) - patron_session(uri).get(uri.path) - end -end - -module HTTPClientAdapter - def get_body_string(response) - case - when response.is_a?(String) then response - when response.is_a?(StringIO) then response.read - when response.respond_to?(:body) then get_body_string(response.body) - when response.respond_to?(:content) then get_body_string(response.content) - else raise ArgumentError.new("Unexpected response: #{response}") - end - end - - def http_get(uri) - HTTPClient.new.get(uri) - end -end - -module EmHTTPRequestAdapter - def get_body_string(response) - if response.respond_to?(:body) - response.body - else - response.response - end - end - - def http_get(uri) - url = uri.to_s - url << '/' if uri.path == '' - - http = nil - EventMachine.run do - http = EventMachine::HttpRequest.new(url).get - http.callback { EventMachine.stop } - end - http - end -end - -World case ENV['HTTP_LIB'] - when 'patron' then PatronAdapter - when 'httpclient' then HTTPClientAdapter - when 'net/http' then NetHTTPAdapter - when 'em-http-request' then EmHTTPRequestAdapter - else raise ArgumentError.new("Unexpected HTTP_LIB: #{ENV['HTTP_LIB']}") -end - diff --git a/spec/http_stubbing_adapters/webmock_spec.rb b/spec/http_stubbing_adapters/webmock_spec.rb index 179f9d36..f982f99c 100644 --- a/spec/http_stubbing_adapters/webmock_spec.rb +++ b/spec/http_stubbing_adapters/webmock_spec.rb @@ -6,70 +6,19 @@ context "using patron" do it_should_behave_like 'an http stubbing adapter that supports some HTTP library' do - def get_body_string(response); response.body; end - - def get_header(header_key, response) - response.headers[header_key] - end - - def make_http_request(method, url, body = {}) - uri = URI.parse(url) - sess = Patron::Session.new - sess.base_url = "#{uri.scheme}://#{uri.host}:#{uri.port}" - - case method - when :get - sess.get(uri.path) - when :post - sess.post(uri.path, body) - end - end + include PatronAdapter end end unless RUBY_PLATFORM =~ /java/ context "using httpclient" do it_should_behave_like 'an http stubbing adapter that supports some HTTP library' do - def get_body_string(response) - response.body.content - end - - def get_header(header_key, response) - response.header[header_key] - end - - def make_http_request(method, url, body = {}) - case method - when :get - HTTPClient.new.get(url) - when :post - HTTPClient.new.post(url, body) - end - end + include HTTPClientAdapter end end context "using em-http-request" do it_should_behave_like 'an http stubbing adapter that supports some HTTP library' do - def get_body_string(response) - response.response - end - - def get_header(header_key, response) - response.response_header[header_key.upcase.gsub('-', '_')].split(', ') - end - - def make_http_request(method, url, body = {}) - http = nil - EventMachine.run do - http = case method - when :get then EventMachine::HttpRequest.new(url).get - when :post then EventMachine::HttpRequest.new(url).post :body => body - end - - http.callback { EventMachine.stop } - end - http - end + include EmHTTPRequestAdapter end end unless RUBY_PLATFORM =~ /java/ diff --git a/spec/support/http_library_adapters.rb b/spec/support/http_library_adapters.rb new file mode 100644 index 00000000..be6cd93d --- /dev/null +++ b/spec/support/http_library_adapters.rb @@ -0,0 +1,80 @@ +module NetHTTPAdapter + def get_body_string(response); response.body; end + + def get_header(header_key, response) + response.get_fields(header_key) + end + + def make_http_request(method, url, body = {}) + case method + when :get + Net::HTTP.get_response(URI.parse(url)) + when :post + Net::HTTP.post_form(URI.parse(url), body) + end + end +end + +module PatronAdapter + def get_body_string(response); response.body; end + + def get_header(header_key, response) + response.headers[header_key] + end + + def make_http_request(method, url, body = {}) + uri = URI.parse(url) + sess = Patron::Session.new + sess.base_url = "#{uri.scheme}://#{uri.host}:#{uri.port}" + + case method + when :get + sess.get(uri.path) + when :post + sess.post(uri.path, body) + end + end +end + +module HTTPClientAdapter + def get_body_string(response) + string = response.body.content + string.respond_to?(:read) ? string.read : string + end + + def get_header(header_key, response) + response.header[header_key] + end + + def make_http_request(method, url, body = {}) + case method + when :get + HTTPClient.new.get(url) + when :post + HTTPClient.new.post(url, body) + end + end +end + +module EmHTTPRequestAdapter + def get_body_string(response) + response.response + end + + def get_header(header_key, response) + response.response_header[header_key.upcase.gsub('-', '_')].split(', ') + end + + def make_http_request(method, url, body = {}) + http = nil + EventMachine.run do + http = case method + when :get then EventMachine::HttpRequest.new(url).get + when :post then EventMachine::HttpRequest.new(url).post :body => body + end + + http.callback { EventMachine.stop } + end + http + end +end diff --git a/spec/support/http_stubbing_adapter.rb b/spec/support/http_stubbing_adapter.rb index bde093b1..4af26388 100644 --- a/spec/support/http_stubbing_adapter.rb +++ b/spec/support/http_stubbing_adapter.rb @@ -55,20 +55,7 @@ shared_examples_for "an http stubbing adapter that supports Net::HTTP" do context "using Net::HTTP" do it_should_behave_like 'an http stubbing adapter that supports some HTTP library' do - def get_body_string(response); response.body; end - - def get_header(header_key, response) - response.get_fields(header_key) - end - - def make_http_request(method, url, body = {}) - case method - when :get - Net::HTTP.get_response(URI.parse(url)) - when :post - Net::HTTP.post_form(URI.parse(url), body) - end - end + include NetHTTPAdapter end end end