Skip to content

Commit

Permalink
Extracted the http calls from the specs for each http library into mo…
Browse files Browse the repository at this point in the history
…dules that we can use in our cucumber features to keep things DRY.
  • Loading branch information
myronmarston committed Aug 9, 2010
1 parent a40025f commit 49bf733
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 143 deletions.
6 changes: 3 additions & 3 deletions features/step_definitions/vcr_steps.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions features/support/env.rb
Expand Up @@ -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'
Expand Down
72 changes: 0 additions & 72 deletions features/support/http_libraries.rb

This file was deleted.

57 changes: 3 additions & 54 deletions spec/http_stubbing_adapters/webmock_spec.rb
Expand Up @@ -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/

Expand Down
80 changes: 80 additions & 0 deletions 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
15 changes: 1 addition & 14 deletions spec/support/http_stubbing_adapter.rb
Expand Up @@ -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
Expand Down

0 comments on commit 49bf733

Please sign in to comment.