Skip to content

Commit

Permalink
Merge branch 'master' into template-in-host-part
Browse files Browse the repository at this point in the history
  • Loading branch information
valscion committed Mar 9, 2020
2 parents b846f55 + a9cf1b7 commit 16d6386
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
@@ -1,11 +1,11 @@
before_install:
- gem update --system
- gem update bundler
- gem update --system -N
rvm:
- 2.3.8
- 2.4.6
- 2.5.5
- 2.6.3
- 2.7.0
- rbx-2
- ruby-head
- jruby-9.1.17.0
Expand Down
54 changes: 54 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,59 @@
# Changelog

## 3.8.2

* Support correct encoding parameter for HTTP.rb 2.x and earlier

Thanks to [Alex Coomans](https://github.com/drcapulet)

## 3.8.1

* Added support for mocking non-ASCII bodies when making requests with HTTP.rb

Thanks to [Patrik Ragnarsson](https://github.com/dentarg)

## 3.8.0

* Fixed options handling when initialising Async::HTTP::Client

Thanks to [Samuel Williams](https://github.com/ioquatix)

* Ruby 2.7 support.

Thanks to [Ryan Davis](https://github.com/zenspider) and [Brandur](https://github.com/brandur)

## 3.7.6

* Suppressed keyword argument warnings in Ruby 2.7 in async-http adapter.

Thanks to [Koichi ITO](https://github.com/koic)

## 3.7.5

* Suppress Excon warning generated by extra key

Thanks to [Marco Costa](https://github.com/marcotc)

## 3.7.4

* Resetting memoized response fields in Curb adapter.

Thanks to [Andrei Sidorov](https://github.com/heretge)

## 3.7.3

* Fix for http.rb. Allow passing an output buffer to HTTP::Response::Body#readpartial

Thanks to [George Claghorn](https://github.com/georgeclaghorn)

* Fixed Manticore adapter to invoke Manticore failure handler on stubbed timeout

Thanks to [Alex Junger](https://github.com/alexJunger)

* Added project metadata to the gemspec

Thanks to [Orien Madgwick](https://github.com/orien)

## 3.7.2

* Fixed handling of non UTF-8 encoded urls
Expand Down
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -43,6 +43,7 @@ Supported Ruby Interpreters
* MRI 2.4
* MRI 2.5
* MRI 2.6
* MRI 2.7
* JRuby
* Rubinius

Expand Down Expand Up @@ -1109,7 +1110,16 @@ People who submitted patches and new features or suggested improvements. Many th
* Andriy Yanko
* y-yagi
* Rafael França

* George Claghorn
* Alex Junger
* Orien Madgwick
* Andrei Sidorov
* Marco Costa
* Ryan Davis
* Brandur
* Samuel Williams
* Patrik Ragnarsson
* Alex Coomans

For a full list of contributors you can visit the
[contributors](https://github.com/bblimke/webmock/contributors) page.
Expand Down
6 changes: 3 additions & 3 deletions lib/webmock/http_lib_adapters/async_http_client_adapter.rb
Expand Up @@ -36,12 +36,12 @@ def initialize(
protocol = endpoint.protocol,
scheme = endpoint.scheme,
authority = endpoint.authority,
options = {}
**options
)
webmock_endpoint = WebMockEndpoint.new(scheme, authority, protocol)

@network_client = WebMockClient.new(endpoint, protocol, scheme, authority, options)
@webmock_client = WebMockClient.new(webmock_endpoint, protocol, scheme, authority, options)
@network_client = WebMockClient.new(endpoint, protocol, scheme, authority, **options)
@webmock_client = WebMockClient.new(webmock_endpoint, protocol, scheme, authority, **options)

@scheme = scheme
@authority = authority
Expand Down
4 changes: 4 additions & 0 deletions lib/webmock/http_lib_adapters/curb_adapter.rb
Expand Up @@ -340,6 +340,10 @@ def reset_webmock_method

def reset
instance_variable_set(:@body_str, nil)
instance_variable_set(:@content_type, nil)
instance_variable_set(:@header_str, nil)
instance_variable_set(:@last_effective_url, nil)
instance_variable_set(:@response_code, nil)
super
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/webmock/http_lib_adapters/excon_adapter.rb
Expand Up @@ -159,4 +159,7 @@ def self.new(args = {})
end
end
end

# Suppresses Excon connection argument validation warning
Excon::VALID_CONNECTION_KEYS << :__construction_args
end
12 changes: 11 additions & 1 deletion lib/webmock/http_lib_adapters/http_rb/response.rb
Expand Up @@ -14,9 +14,19 @@ class << self
def from_webmock(webmock_response, request_signature = nil)
status = Status.new(webmock_response.status.first)
headers = webmock_response.headers || {}
body = Body.new Streamer.new webmock_response.body
uri = normalize_uri(request_signature && request_signature.uri)

# HTTP.rb 3.0+ uses a keyword argument to pass the encoding, but 1.x
# and 2.x use a positional argument, and 0.x don't support supplying
# the encoding.
body = if HTTP::VERSION < "1.0.0"
Body.new(Streamer.new(webmock_response.body))
elsif HTTP::VERSION < "3.0.0"
Body.new(Streamer.new(webmock_response.body), webmock_response.body.encoding)
else
Body.new(Streamer.new(webmock_response.body), encoding: webmock_response.body.encoding)
end

return new(status, "1.1", headers, body, uri) if HTTP::VERSION < "1.0.0"

new({
Expand Down
4 changes: 2 additions & 2 deletions lib/webmock/http_lib_adapters/http_rb/streamer.rb
Expand Up @@ -5,7 +5,7 @@ def initialize(str)
@io = StringIO.new str
end

def readpartial(size = nil)
def readpartial(size = nil, outbuf = nil)
unless size
if defined?(HTTP::Client::BUFFER_SIZE)
size = HTTP::Client::BUFFER_SIZE
Expand All @@ -14,7 +14,7 @@ def readpartial(size = nil)
end
end

@io.read size
@io.read size, outbuf
end

def close
Expand Down
24 changes: 16 additions & 8 deletions lib/webmock/http_lib_adapters/manticore_adapter.rb
Expand Up @@ -24,6 +24,12 @@ def self.disable!
Manticore.instance_variable_set(:@manticore_facade, OriginalManticoreClient.new)
end

class StubbedTimeoutResponse < Manticore::StubbedResponse
def call
@handlers[:failure].call(Manticore::ConnectTimeout.new("Too slow (mocked timeout)"))
end
end

class WebMockManticoreClient < Manticore::Client
def request(klass, url, options={}, &block)
super(klass, WebMock::Util::URI.normalize_uri(url).to_s, format_options(options))
Expand Down Expand Up @@ -106,14 +112,16 @@ def split_array_values(headers = [])
end

def generate_manticore_response(webmock_response)
raise Manticore::ConnectTimeout if webmock_response.should_timeout

Manticore::StubbedResponse.stub(
code: webmock_response.status[0],
body: webmock_response.body,
headers: webmock_response.headers,
cookies: {}
)
if webmock_response.should_timeout
StubbedTimeoutResponse.new
else
Manticore::StubbedResponse.stub(
code: webmock_response.status[0],
body: webmock_response.body,
headers: webmock_response.headers,
cookies: {}
)
end
end

def generate_webmock_response(manticore_response)
Expand Down
42 changes: 26 additions & 16 deletions lib/webmock/http_lib_adapters/net_http.rb
Expand Up @@ -228,9 +228,9 @@ class PatchedStringIO < StringIO #:nodoc:

alias_method :orig_read_nonblock, :read_nonblock

def read_nonblock(size, *args)
def read_nonblock(size, *args, **kwargs)
args.reject! {|arg| !arg.is_a?(Hash)}
orig_read_nonblock(size, *args)
orig_read_nonblock(size, *args, **kwargs)
end

end
Expand All @@ -257,7 +257,7 @@ def readuntil(*args)
module Net #:nodoc: all

class WebMockNetBufferedIO < BufferedIO
def initialize(io, *args)
def initialize(io, *args, **kwargs)
io = case io
when Socket, OpenSSL::SSL::SSLSocket, IO
io
Expand All @@ -268,23 +268,33 @@ def initialize(io, *args)
end
raise "Unable to create local socket" unless io

super
# Prior to 2.4.0 `BufferedIO` only takes a single argument (`io`) with no
# options. Here we pass through our full set of arguments only if we're
# on 2.4.0 or later, and use a simplified invocation otherwise.
if RUBY_VERSION >= '2.4.0'
super
else
super(io)
end
end

if RUBY_VERSION >= '2.6.0'
# https://github.com/ruby/ruby/blob/7d02441f0d6e5c9d0a73a024519eba4f69e36dce/lib/net/protocol.rb#L208
# Modified version of method from ruby, so that nil is always passed into orig_read_nonblock to avoid timeout
def rbuf_fill
current_thread_id = Thread.current.object_id

trace = TracePoint.trace(:line) do |tp|
next unless Thread.current.object_id == current_thread_id
if tp.binding.local_variable_defined?(:tmp)
tp.binding.local_variable_set(:tmp, nil)
end
end

super
ensure
trace.disable
case rv = @io.read_nonblock(BUFSIZE, nil, exception: false)
when String
return if rv.nil?
@rbuf << rv
rv.clear
return
when :wait_readable
@io.to_io.wait_readable(@read_timeout) or raise Net::ReadTimeout
when :wait_writable
@io.to_io.wait_writable(@read_timeout) or raise Net::ReadTimeout
when nil
raise EOFError, 'end of file reached'
end while true
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/webmock/version.rb
@@ -1,3 +1,3 @@
module WebMock
VERSION = '3.7.2' unless defined?(::WebMock::VERSION)
VERSION = '3.8.2' unless defined?(::WebMock::VERSION)
end
17 changes: 12 additions & 5 deletions spec/acceptance/curb/curb_spec.rb
Expand Up @@ -474,18 +474,25 @@
include CurbSpecHelper::ClassPerform
end

describe "using .reset" do
describe "using #reset" do
before do
@curl = Curl::Easy.new
@curl.url = "http://example.com"
body = "on_success fired"
stub_request(:any, "example.com").to_return(body: body)
stub_request(:any, "example.com").
to_return(body: "abc",
headers: { "Content-Type" => "application/json" })
@curl.http_get
end

it "should clear the body_str" do
it "should clear all memoized response fields" do
@curl.reset
expect(@curl.body_str).to eq(nil)
expect(@curl).to have_attributes(
body_str: nil,
content_type: nil,
header_str: nil,
last_effective_url: nil,
response_code: 0,
)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/acceptance/excon/excon_spec_helper.rb
Expand Up @@ -28,6 +28,8 @@ def http_request(method, uri, options = {}, &block)
res
end

Excon.set_raise_on_warnings!(true)

OpenStruct.new \
body: response.body,
headers: headers,
Expand Down
11 changes: 11 additions & 0 deletions spec/acceptance/http_rb/http_rb_spec.rb
Expand Up @@ -72,6 +72,17 @@
end

context "streamer" do
it "can be read to a provided buffer" do
stub_request(:get, "example.com/foo")
.to_return(status: 200, body: "Hello world! ")
response = HTTP.get "http://example.com/foo"

buffer = ""
response.body.readpartial(1024, buffer)

expect(buffer).to eq "Hello world! "
end

it "can be closed" do
stub_request :get, "example.com/foo"
response = HTTP.get "http://example.com/foo"
Expand Down
19 changes: 19 additions & 0 deletions spec/acceptance/manticore/manticore_spec.rb
Expand Up @@ -51,6 +51,25 @@
response = Manticore.head("http://example-foo.com")
expect(response.code).to eq(204)
end

context "when a custom failure handler is defined" do
let(:failure_handler) { proc {} }

before do
allow(failure_handler).to receive(:call).with(kind_of(Manticore::Timeout)) do |ex|
raise ex
end
end

it "handles timeouts by invoking the failure handler" do
stub_request(:get, "http://example-foo.com").to_timeout
request = Manticore.get("http://example-foo.com").tap do |req|
req.on_failure(&failure_handler)
end
expect { request.call }.to raise_error(Manticore::Timeout)
expect(failure_handler).to have_received(:call)
end
end
end
end
end
8 changes: 8 additions & 0 deletions webmock.gemspec
Expand Up @@ -13,6 +13,14 @@ Gem::Specification.new do |s|
s.description = %q{WebMock allows stubbing HTTP requests and setting expectations on HTTP requests.}
s.license = "MIT"

s.metadata = {
'bug_tracker_uri' => 'https://github.com/bblimke/webmock/issues',
'changelog_uri' => "https://github.com/bblimke/webmock/blob/v#{s.version}/CHANGELOG.md",
'documentation_uri' => "https://www.rubydoc.info/gems/webmock/#{s.version}",
'source_code_uri' => "https://github.com/bblimke/webmock/tree/v#{s.version}",
'wiki_uri' => 'https://github.com/bblimke/webmock/wiki'
}

s.required_ruby_version = '>= 2.0'

s.add_dependency 'addressable', '>= 2.3.6'
Expand Down

0 comments on commit 16d6386

Please sign in to comment.