Use HTTPClient instead of Net::HTTP

This commit is contained in:
Steve Bazyl committed 2015-12-18 14:37:21 -08:00
1 parent c102c571e0
commit e8481dd14d
8 files changed
+126 -14

No files matched your search

-8
View File
@@ -37,12 +37,4 @@ RSpec.describe Google::Apis::RequestOptions do
it 'should allow nil in merge' do
expect(options.merge(nil)).to be_an_instance_of(Google::Apis::RequestOptions)
end
it 'should override default options' do
Google::Apis::RequestOptions.default.header = 'Content-Length: 50'
opts = Google::Apis::RequestOptions.new
opts.header = 'Content-Length: 70'
expect(options.merge(opts).header).to eq 'Content-Length: 70'
end
end
+1
View File
@@ -15,6 +15,7 @@ RSpec.describe Google::Apis::AdsenseV1_4, :if => run_integration_tests? do
end
it 'should download a report with multiple dimensions' do
pending "Not enabled for test account"
report = @adsense.generate_report( Date.today.to_s, Date.today.to_s, dimension: ["DATE", "AD_UNIT_NAME"] )
report_header_names = report.headers.map { |h| h.name }
+3 -3
View File
@@ -1,10 +1,10 @@
require 'spec_helper'
require 'google/apis/pubsub_v1beta2'
require 'google/apis/pubsub_v1'
require 'googleauth'
Pubsub = Google::Apis::PubsubV1beta2
Pubsub = Google::Apis::PubsubV1
RSpec.describe Google::Apis::PubsubV1beta2, :if => run_integration_tests? do
RSpec.describe Google::Apis::PubsubV1, :if => run_integration_tests? do
before(:context) do
WebMock.allow_net_connect!
+33 -1
View File
@@ -87,11 +87,12 @@ end
# Enable retries for tests
Google::Apis::RequestOptions.default.retries = 5
# Allow testing different adapters
Google::Apis::ClientOptions.default.use_net_http = true if ENV['USE_NET_HTTP']
# Temporarily patch WebMock to allow chunked responses for Net::HTTP
module Net
module WebMockHTTPResponse
def eval_chunk(chunk)
puts chunk.is_a? Exception
chunk if chunk.is_a?(String)
chunk.read if chunk.is_a?(IO)
chunk.call if chunk.is_a?(Proc)
@@ -121,6 +122,37 @@ module Net
end
end
class WebMockHTTPClient
def eval_chunk(chunk)
chunk if chunk.is_a?(String)
chunk.read if chunk.is_a?(IO)
chunk.call if chunk.is_a?(Proc)
fail HTTPClient::TimeoutError if chunk == ::Timeout::Error
fail chunk if chunk.is_a?(Class)
chunk
end
def build_httpclient_response(webmock_response, stream = false, req_header = nil, &block)
body = stream ? StringIO.new(webmock_response.body) : webmock_response.body
response = HTTP::Message.new_response(body, req_header)
response.header.init_response(webmock_response.status[0])
response.reason = webmock_response.status[1]
webmock_response.headers.to_a.each { |name, value| response.header.set(name, value) }
raise HTTPClient::TimeoutError if webmock_response.should_timeout
webmock_response.raise_error_if_any
body_parts = Array(webmock_response.body)
body_parts.each do |chunk|
chunk = eval_chunk(chunk)
block.call(response, chunk) if block
end
response
end
end
def run_integration_tests?
ENV['GOOGLE_APPLICATION_CREDENTIALS'] && ENV['GOOGLE_PROJECT_ID']
end