Skip to content

Commit

Permalink
More progress toward updating em-http-request (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdhull committed Jun 10, 2011
1 parent 0d1a4d0 commit ed33a33
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -5,6 +5,7 @@ gemspec
group :development do
gem 'guard-rspec'
gem 'rb-fsevent'
gem 'ruby-debug19', :require => 'ruby-debug'
end

platforms :jruby do
Expand Down
11 changes: 6 additions & 5 deletions lib/webmock/http_lib_adapters/em_http_request.rb
Expand Up @@ -6,6 +6,10 @@ module EventMachine
class WebMockHttpClient < EventMachine::HttpClient
include HttpEncoding

def uri
@req.uri
end

def setup(response, uri, error = nil)
@last_effective_url = @uri = uri
if error
Expand All @@ -26,10 +30,6 @@ def send_request_with_webmock(head, body)
webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
WebMock::CallbackRegistry.invoke_callbacks({:lib => :em_http_request}, request_signature, webmock_response)
on_error("WebMock timeout error") if webmock_response.should_timeout
self.response = webmock_response.body
webmock_response.headers.each do |k, v|
self.response_header[k.upcase.gsub('-','_')] = v
end if webmock_response.headers
setup(make_raw_response(webmock_response), @uri,
webmock_response.should_timeout ? "WebMock timeout error" : nil)
self
Expand Down Expand Up @@ -98,10 +98,12 @@ def make_raw_response(response)
response.raise_error_if_any

status, headers, body = response.status, response.headers, response.body
headers ||= {}

response_string = []
response_string << "HTTP/1.1 #{status[0]} #{status[1]}"

headers["Content-Length"] = body.length unless headers["Content-Length"]
headers.each do |header, value|
value = value.join(", ") if value.is_a?(Array)

Expand Down Expand Up @@ -131,5 +133,4 @@ def self.deactivate!
end

EventMachine::WebMockHttpClient.activate!

end
2 changes: 2 additions & 0 deletions spec/em_http_request_spec_helper.rb
Expand Up @@ -6,6 +6,7 @@ def failed
end

def http_request(method, uri, options = {}, &block)
@http = nil
response = nil
error = nil
uri = Addressable::URI.heuristic_parse(uri)
Expand Down Expand Up @@ -34,6 +35,7 @@ def http_request(method, uri, options = {}, &block)
})
EventMachine.stop
}
@http = http
}
raise error if error
response
Expand Down
29 changes: 23 additions & 6 deletions spec/webmock_shared.rb
Expand Up @@ -532,7 +532,11 @@ class MyException < StandardError; end;

it "should return declared status message" do
stub_http_request(:get, "www.example.com").to_return(:status => [500, "Internal Server Error"])
http_request(:get, "http://www.example.com/").message.should == "Internal Server Error"
response = http_request(:get, "http://www.example.com/")
# not supported by em-http-request, it always returns "unknown" for http_reason
unless @http.is_a?(EventMachine::WebMockHttpClient)
response.message.should == "Internal Server Error"
end
end

it "should return default status code" do
Expand All @@ -542,7 +546,11 @@ class MyException < StandardError; end;

it "should return default empty message" do
stub_http_request(:get, "www.example.com")
http_request(:get, "http://www.example.com/").message.should == ""
response = http_request(:get, "http://www.example.com/")
# not supported by em-http-request, it always returns "unknown" for http_reason
unless @http.is_a?(EventMachine::WebMockHttpClient)
response.message.should == ""
end
end

it "should return body declared as IO" do
Expand Down Expand Up @@ -640,7 +648,10 @@ def call(request)
end

it "should return recorded status message" do
@response.message.should == "OK"
# not supported by em-http-request, it always returns "unknown" for http_reason
unless @http.is_a?(EventMachine::WebMockHttpClient)
@response.message.should == "OK"
end
end

it "should ensure file is closed" do
Expand Down Expand Up @@ -676,7 +687,10 @@ def call(request)
end

it "should return recorded status message" do
@response.message.should == "OK"
# not supported by em-http-request, it always returns "unknown" for http_reason
unless @http.is_a?(EventMachine::WebMockHttpClient)
@response.message.should == "OK"
end
end
end

Expand Down Expand Up @@ -1491,8 +1505,11 @@ def call(request)
end

it "should pass response with status and message" do
@response.status[0].should == 302
@response.status[1].should == "Found"
# not supported by em-http-request, it always returns "unknown" for http_reason
unless @http.is_a?(EventMachine::WebMockHttpClient)
@response.status[0].should == 302
@response.status[1].should == "Found"
end
end

it "should pass response with headers" do
Expand Down
2 changes: 1 addition & 1 deletion webmock.gemspec
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec', '>= 2.0.0'
s.add_development_dependency 'httpclient', '>= 2.1.5.2'
s.add_development_dependency 'patron', '>= 0.4.9'
s.add_development_dependency 'em-http-request', '>= 0.2.14'
s.add_development_dependency 'em-http-request', '>= 1.0.0.beta.4'
s.add_development_dependency 'curb', '>= 0.7.8'

s.files = `git ls-files`.split("\n")
Expand Down

0 comments on commit ed33a33

Please sign in to comment.