Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update Ruby HTTP Gem adapter to be 0.6.0 compat

  • Loading branch information...
commit 4c5d4e9800b981c43db08af27943f8b1ef683c70 1 parent 7fe2a20
@ixti ixti authored
View
14 lib/webmock/http_lib_adapters/http_gem/client.rb
@@ -0,0 +1,14 @@
+module HTTP
+ class Client
+ alias_method :__perform__, :perform
+
+ def perform(request, options)
+ return __perform__(request, options) unless webmock_enabled?
+ WebMockPerform.new(request) { __perform__(request, options) }.exec
+ end
+
+ def webmock_enabled?
+ ::WebMock::HttpLibAdapters::HttpGemAdapter.enabled?
+ end
+ end
+end
View
10 lib/webmock/http_lib_adapters/http_gem/request.rb
@@ -0,0 +1,10 @@
+module HTTP
+ class Request
+ def webmock_signature
+ ::WebMock::RequestSignature.new(verb, uri.to_s, {
+ :headers => headers.to_h,
+ :body => body
+ })
+ end
+ end
+end
View
21 lib/webmock/http_lib_adapters/http_gem/response.rb
@@ -0,0 +1,21 @@
+module HTTP
+ class Response
+ def to_webmock
+ webmock_response = ::WebMock::Response.new
+
+ webmock_response.status = [status, reason]
+ webmock_response.body = body.to_s
+ webmock_response.headers = headers.to_h
+
+ webmock_response
+ end
+
+ def self.from_webmock(webmock_response)
+ status = webmock_response.status.first
+ headers = webmock_response.headers || {}
+ body = Body.new Streamer.new webmock_response.body
+
+ new(status, "1.1", headers, body)
+ end
+ end
+end
View
13 lib/webmock/http_lib_adapters/http_gem/streamer.rb
@@ -0,0 +1,13 @@
+module HTTP
+ class Response
+ class Streamer
+ def initialize(str)
+ @io = StringIO.new str
+ end
+
+ def readpartial(size = HTTP::Client::BUFFER_SIZE)
+ @io.read size
+ end
+ end
+ end
+end
View
62 lib/webmock/http_lib_adapters/http_gem/webmock.rb
@@ -0,0 +1,62 @@
+module HTTP
+ class WebMockPerform
+ def initialize(request, &perform)
+ @request = request
+ @perform = perform
+ end
+
+ def exec
+ replay || perform || halt
+ end
+
+ def request_signature
+ unless @request_signature
+ @request_signature = @request.webmock_signature
+ register_request(@request_signature)
+ end
+
+ @request_signature
+ end
+
+ protected
+
+ def response_for_request(signature)
+ ::WebMock::StubRegistry.instance.response_for_request(signature)
+ end
+
+ def register_request(signature)
+ ::WebMock::RequestRegistry.instance.requested_signatures.put(signature)
+ end
+
+ def replay
+ webmock_response = response_for_request request_signature
+
+ return unless webmock_response
+
+ raise Errno::ETIMEDOUT if webmock_response.should_timeout
+ webmock_response.raise_error_if_any
+
+ invoke_callbacks(webmock_response, :real_request => false)
+ ::HTTP::Response.from_webmock webmock_response
+ end
+
+ def perform
+ return unless ::WebMock.net_connect_allowed?(request_signature.uri)
+ response = @perform.call
+ invoke_callbacks(response.to_webmock, :real_request => true)
+ response
+ end
+
+ def halt
+ raise ::WebMock::NetConnectNotAllowedError.new request_signature
+ end
+
+ def invoke_callbacks(webmock_response, options = {})
+ ::WebMock::CallbackRegistry.invoke_callbacks(
+ options.merge({ :lib => :http_gem }),
+ request_signature,
+ webmock_response
+ )
+ end
+ end
+end
View
170 lib/webmock/http_lib_adapters/http_gem_adapter.rb
@@ -1,170 +1,38 @@
begin
require "http"
+ __http_gem_found__ = true
rescue LoadError
- # HTTP gem not found
+ __http_gem_found__ = false
end
+if __http_gem_found__
+ WebMock::VersionChecker.new("HTTP Gem", HTTP::VERSION, "0.6.0").check_version!
-if defined?(HTTP::Client)
module WebMock
module HttpLibAdapters
class HttpGemAdapter < HttpLibAdapter
-
adapter_for :http_gem
+ class << self
+ def enable!
+ @enabled = true
+ end
- def self.enable!
- ::HTTP.enable_webmock!
- end
-
+ def disable!
+ @enabled = false
+ end
- def self.disable!
- ::HTTP.disable_webmock!
+ def enabled?
+ @enabled
+ end
end
-
end
end
end
-
- module HTTP
- class Request
-
- def webmock_signature
- ::WebMock::RequestSignature.new(method, uri.to_s, {
- :headers => headers,
- :body => body
- })
- end
-
- end
-
-
- class Response
-
- def to_webmock
- webmock_response = ::WebMock::Response.new
-
- webmock_response.status = [status, reason]
- webmock_response.body = body.to_s
- webmock_response.headers = headers
-
- webmock_response
- end
-
-
- def self.from_webmock(webmock_response)
- status = webmock_response.status.first
- headers = webmock_response.headers || {}
- body = webmock_response.body
-
- new(status, "1.1", headers, body)
- end
-
- end
-
-
- class WebMockPerform
-
- def initialize request, &perform
- @request = request
- @perform = perform
- end
-
-
- def exec
- replay || perform || halt
- end
-
-
- def request_signature
- unless @request_signature
- @request_signature = @request.webmock_signature
- register_request(@request_signature)
- end
-
- @request_signature
- end
-
-
- protected
-
-
- def response_for_request(signature)
- ::WebMock::StubRegistry.instance.response_for_request(signature)
- end
-
-
- def register_request(signature)
- ::WebMock::RequestRegistry.instance.requested_signatures.put(signature)
- end
-
-
- def replay
- webmock_response = response_for_request(request_signature)
-
- return unless webmock_response
-
- raise Errno::ETIMEDOUT if webmock_response.should_timeout
- webmock_response.raise_error_if_any
-
- invoke_callbacks(webmock_response, :real_request => false)
- ::HTTP::Response.from_webmock webmock_response
- end
-
-
- def perform
- return unless ::WebMock.net_connect_allowed?(request_signature.uri)
- response = @perform.call
- invoke_callbacks(response.to_webmock, :real_request => true)
- response
- end
-
-
- def halt
- raise ::WebMock::NetConnectNotAllowedError.new request_signature
- end
-
-
- def invoke_callbacks webmock_response, options = {}
- ::WebMock::CallbackRegistry.invoke_callbacks(
- options.merge({ :lib => :http_gem }),
- request_signature,
- webmock_response
- )
- end
-
- end
-
-
- class Client
-
- alias :__perform__ :perform
-
- def perform request, options
- return __perform__(request, options) unless ::HTTP.webmock_enabled?
- WebMockPerform.new(request) { __perform__(request, options) }.exec
- end
-
- end
-
-
- class << self
-
- def enable_webmock!
- @webmock_enabled = true
- end
-
-
- def disable_webmock!
- @webmock_enabled = false
- end
-
-
- def webmock_enabled?
- @webmock_enabled
- end
-
- end
- end
+ require "webmock/http_lib_adapters/http_gem/client"
+ require "webmock/http_lib_adapters/http_gem/request"
+ require "webmock/http_lib_adapters/http_gem/response"
+ require "webmock/http_lib_adapters/http_gem/streamer"
+ require "webmock/http_lib_adapters/http_gem/webmock"
end
View
40 spec/acceptance/http_gem/http_gem_spec.rb
@@ -5,50 +5,46 @@
require "acceptance/http_gem/http_gem_spec_helper"
describe "HTTP Gem" do
-
include HttpGemSpecHelper
-
include_examples "with WebMock", :no_status_message
+ context "streaming body" do
+ let(:response) { HTTP.get "http://example.com" }
+ before { stub_simple_request "example.com", 302, {}, "abc" }
+
+ it "works as if it was streamed from socket" do
+ expect(response.body.readpartial 1).to eq "a"
+ end
- context "when not following redirects" do
+ it "fails if body was already streamed" do
+ response.body.to_s
+ expect { response.body.readpartial 1 }.to raise_error
+ end
+ end
+ context "without following redirects" do
let(:response) { http_request(:get, "http://example.com") }
let(:headers) { response.headers }
it "stops on first request" do
- stub_simple_request("example.com", 302, "Location" => "www.example.com")
+ stub_simple_request("example.com", 302, "Location" => "http://www.example.com")
stub_simple_request("www.example.com")
expect(headers).to include "Host" => "example.com"
end
-
end
-
- context "when following redirects" do
-
- let(:response) { http_request(:get, "http://example.com", :follow => true) }
+ context "following redirects" do
+ let(:options) { { :follow => true } }
+ let(:response) { http_request(:get, "http://example.com", options) }
let(:headers) { response.headers }
-
it "returns response of destination" do
- stub_simple_request("example.com", 302, "Location" => "www.example.com")
+ stub_simple_request("example.com", 302, "Location" => "http://www.example.com")
stub_simple_request("www.example.com")
expect(headers).to include "Host" => "www.example.com"
end
-
-
- it "works with more than one redirect" do
- stub_simple_request("example.com", 302, "Location" => "www.example.com")
- stub_simple_request("www.example.com", 302, "Location" => "blog.example.com")
- stub_simple_request("blog.example.com")
-
- expect(headers).to include "Host" => "blog.example.com"
- end
-
end
-
end
View
49 spec/acceptance/http_gem/http_gem_spec_helper.rb
@@ -1,52 +1,43 @@
require "ostruct"
-
module HttpGemSpecHelper
-
- def http_request(method, uri, options = {}, &block)
- response = HTTP.request(method, normalize_uri(uri), options).response
+ def http_request(method, uri, options = {})
+ response = HTTP.request(method, normalize_uri(uri), options)
OpenStruct.new({
- :body => response.body,
- :headers => normalize_headers(response.headers),
+ :body => response.body.to_s,
+ :headers => normalize_headers(response.headers.to_h),
:status => response.status.to_s,
:message => response.reason
})
end
-
- def normalize_uri(uri)
- Addressable::URI.heuristic_parse(uri).normalize.to_s
- end
-
-
- def normalize_headers headers
- WebMock::Util::Headers.normalize_headers(Hash[headers.map { |k, v|
- [k, v.is_a?(Array) ? v.join(", ") : v]
- }])
- end
-
-
- def stub_simple_request host, status = 200, headers = {}
- stub_request(:any, host).to_return({
- :status => status,
- :headers => headers.merge({ "Host" => host })
- })
- end
-
-
def client_timeout_exception_class
Errno::ETIMEDOUT
end
-
def connection_refused_exception_class
Errno::ECONNREFUSED
end
-
def http_library
:http_gem
end
+ def normalize_uri(uri)
+ Addressable::URI.heuristic_parse(uri).normalize.to_s
+ end
+
+ def normalize_headers(headers)
+ headers = Hash[headers.map { |k, v| [k, Array(v).join(", ")] }]
+ WebMock::Util::Headers.normalize_headers headers
+ end
+
+ def stub_simple_request(host, status = 200, headers = {}, body = nil)
+ stub_request(:any, host).to_return({
+ :status => status,
+ :headers => headers.merge({ "Host" => host }),
+ :body => body
+ })
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.