Skip to content

Commit

Permalink
Merge branch 'master' into fix-version-checking
Browse files Browse the repository at this point in the history
  • Loading branch information
bblimke authored Aug 19, 2023
2 parents f6ed904 + 0771fbc commit 0e9519c
Show file tree
Hide file tree
Showing 25 changed files with 151 additions and 39 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,27 @@ jobs:
matrix:
ruby:
- head
- '3.2'
- '3.1'
- '3.0'
- '2.7'
- '2.6'
- '2.5'
- jruby
continue-on-error: ${{ matrix.ruby == 'head' }}
name: Ruby ${{ matrix.ruby }}
env:
JRUBY_OPTS: "--debug"
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Install Apt Packages
run: |
sudo apt-get install libcurl4-openssl-dev -y
- uses: ruby/setup-ruby@v1
continue-on-error: true
continue-on-error: false
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
rubygems: 'latest'
bundler: 'latest'
- run: |
bundle exec rake
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ WebMock
[![Build Status](https://github.com/bblimke/webmock/workflows/CI/badge.svg?branch=master)](https://github.com/bblimke/webmock/actions)
[![Code Climate](https://codeclimate.com/github/bblimke/webmock/badges/gpa.svg)](https://codeclimate.com/github/bblimke/webmock)
[![Mentioned in Awesome Ruby](https://awesome.re/mentioned-badge.svg)](https://github.com/markets/awesome-ruby)
[![Inline docs](http://inch-ci.org/github/bblimke/webmock.svg?branch=master)](http://inch-ci.org/github/bblimke/webmock)
[![SemVer](https://api.dependabot.com/badges/compatibility_score?dependency-name=webmock&package-manager=bundler&version-scheme=semver)](https://dependabot.com/compatibility-score.html?dependency-name=webmock&package-manager=bundler&version-scheme=semver)

Library for stubbing and setting expectations on HTTP requests in Ruby.

Expand Down Expand Up @@ -40,12 +38,12 @@ Supported HTTP libraries

Supported Ruby Interpreters
---------------------------

* MRI 2.5
* MRI 2.6
* MRI 2.7
* MRI 3.0
* MRI 3.1
* MRI 3.2
* JRuby
* Rubinius

## Installation

Expand Down Expand Up @@ -540,7 +538,7 @@ RestClient.get('www.example.org:8080', '/') # ===> Allowed
With a `Regexp` matching the URI:

```ruby
WebMock.disable_net_connect!(allow: %r{ample.org/foo})
WebMock.disable_net_connect!(allow: %r{ample\.org/foo})

RestClient.get('www.example.org', '/foo/bar') # ===> Allowed
RestClient.get('sample.org', '/foo') # ===> Allowed
Expand Down
18 changes: 12 additions & 6 deletions lib/webmock/http_lib_adapters/em_http_request_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def setup(response, uri, error = nil)

def connection_completed
@state = :response_header
send_request(request_signature.headers, request_signature.body)
send_request(*headers_and_body_processed_by_middleware)
end

def send_request(head, body)
Expand Down Expand Up @@ -168,12 +168,18 @@ def build_webmock_response
webmock_response
end

def build_request_signature
headers, body = build_request, @req.body

@conn.middleware.select {|m| m.respond_to?(:request) }.each do |m|
headers, body = m.request(self, headers, body)
def headers_and_body_processed_by_middleware
@headers_and_body_processed_by_middleware ||= begin
head, body = build_request, @req.body
@conn.middleware.each do |m|
head, body = m.request(self, head, body) if m.respond_to?(:request)
end
[head, body]
end
end

def build_request_signature
headers, body = headers_and_body_processed_by_middleware

method = @req.method
uri = @req.uri.clone
Expand Down
2 changes: 1 addition & 1 deletion lib/webmock/request_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def matches?(body, content_type = "")
matching_body_hashes?(body_as_hash(body, content_type), @pattern, content_type)
elsif (@pattern).is_a?(Array)
matching_body_array?(body_as_hash(body, content_type), @pattern, content_type)
elsif (@pattern).is_a?(WebMock::Matchers::HashIncludingMatcher)
elsif (@pattern).is_a?(WebMock::Matchers::HashArgumentMatcher)
@pattern == body_as_hash(body, content_type)
else
empty_string?(@pattern) && empty_string?(body) ||
Expand Down
1 change: 0 additions & 1 deletion lib/webmock/util/version_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ def compare_version
when @major < @min_major then :too_low
when @major == @min_major && @minor < @min_minor then :too_low
when @major == @min_major && @minor == @min_minor && @patch < @min_patch then :too_low

when @max_major && @major > @max_major then :too_high
when @max_major && @major == @max_major && @max_minor && @minor > @max_minor then :too_high

Expand Down
4 changes: 2 additions & 2 deletions minitest/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
require 'minitest/autorun'
require 'webmock/minitest'

test_class = defined?(MiniTest::Test) ? MiniTest::Test : MiniTest::Unit::TestCase
test_class = defined?(Minitest::Test) ? Minitest::Test : MiniTest::Unit::TestCase

test_class.class_eval do

Expand All @@ -28,7 +28,7 @@ def assert_raise_with_message(e, message, &block)
end

def assert_fail(message, &block)
assert_raise_with_message(MiniTest::Assertion, message, &block)
assert_raise_with_message(Minitest::Assertion, message, &block)
end

end
2 changes: 1 addition & 1 deletion minitest/test_webmock.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
require File.expand_path(File.dirname(__FILE__) + '/../test/shared_test')

test_class = defined?(MiniTest::Test) ? MiniTest::Test : MiniTest::Unit::TestCase
test_class = defined?(Minitest::Test) ? Minitest::Test : MiniTest::Unit::TestCase


class MiniTestWebMock < test_class
Expand Down
6 changes: 3 additions & 3 deletions spec/acceptance/async_http_client/async_http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
require 'acceptance/webmock_shared'
require_relative './async_http_client_spec_helper'

require 'protocol/http/body/file'
unless RUBY_PLATFORM =~ /java/
require 'protocol/http/body/file'

Async.logger.debug! if ENV['ASYNC_LOGGER_DEBUG']
Async.logger.debug! if ENV['ASYNC_LOGGER_DEBUG']

unless RUBY_PLATFORM =~ /java/
describe 'Async::HTTP::Client' do
include AsyncHttpClientSpecHelper

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def connection_refused_exception_class
Errno::ECONNREFUSED
end

def connection_error_class
HTTP::ConnectionError
end

def http_library
:async_http_client
end
Expand Down
5 changes: 4 additions & 1 deletion spec/acceptance/curb/curb_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def setup_request(uri, curl, options={})
curl.password = options[:basic_auth][1]
end
curl.timeout = 30
curl.connect_timeout = 30
curl.connect_timeout = 5

if headers = options[:headers]
headers.each {|k,v| curl.headers[k] = v }
Expand All @@ -54,6 +54,9 @@ def connection_refused_exception_class
Curl::Err::ConnectionFailedError
end

def connection_error_class
end

def http_library
:curb
end
Expand Down
12 changes: 12 additions & 0 deletions spec/acceptance/em_http_request/em_http_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,18 @@ def response(resp)
before { WebMock.allow_net_connect! }
include_examples "em-http-request middleware/after_request hook integration"

it "doesn't modify headers" do
EM.run do
conn = EventMachine::HttpRequest.new(webmock_server_url)
http = conn.post(head: { 'content-length' => '4' }, body: 'test')
expect(conn).to receive(:send_data).with(/POST \/ HTTP\/1.1\r\nContent-Length: 4\r\nConnection: close\r\nHost: localhost:\d+\r\nUser-Agent: EventMachine HttpClient\r\nAccept-Encoding: gzip, compressed\r\n\r\n/).and_call_original
expect(conn).to receive(:send_data).with('test')
http.callback do
EM.stop
end
end
end

it "only calls request middleware once" do
middleware = Class.new do
def self.called!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ def http_request(method, uri, options = {}, &block)
end

def client_timeout_exception_class
'Errno::ETIMEDOUT'
RuntimeError # 'Errno::ETIMEDOUT'
end

def connection_refused_exception_class
RuntimeError
end

def connection_error_class
end

def http_library
:em_http_request
end
Expand Down
3 changes: 3 additions & 0 deletions spec/acceptance/excon/excon_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def connection_refused_exception_class
Excon::Errors::SocketError
end

def connection_error_class
end

def http_library
:excon
end
Expand Down
3 changes: 3 additions & 0 deletions spec/acceptance/http_rb/http_rb_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def connection_refused_exception_class
HTTP::ConnectionError
end

def connection_error_class
end

def http_library
:http_rb
end
Expand Down
3 changes: 3 additions & 0 deletions spec/acceptance/httpclient/httpclient_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def connection_refused_exception_class
Errno::ECONNREFUSED
end

def connection_error_class
end

def http_library
:httpclient
end
Expand Down
3 changes: 3 additions & 0 deletions spec/acceptance/manticore/manticore_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def connection_refused_exception_class
Manticore::SocketException
end

def connection_error_class
end

def http_library
:manticore
end
Expand Down
3 changes: 3 additions & 0 deletions spec/acceptance/net_http/net_http_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def connection_refused_exception_class
Errno::ECONNREFUSED
end

def connection_error_class
end

def http_library
:net_http
end
Expand Down
3 changes: 3 additions & 0 deletions spec/acceptance/patron/patron_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ def connection_refused_exception_class
Patron::ConnectionFailed
end

def connection_error_class
end

def http_library
:patron
end
Expand Down
44 changes: 38 additions & 6 deletions spec/acceptance/shared/stubbing_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,44 @@
body: "foo=1").status).to eq("200")
end

it "should match if stubbed request body is hash_included" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => hash_including("bar" => '1')})
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
body: "foo[bar]=1").status).to eq("200")
context "when using hash_including to match the request body" do
it "should match if stubbed request body is including hash" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => hash_including("bar" => '1')})
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
body: "foo[bar]=1").status).to eq("200")
end

it "should not match if stubbed request body is excluding hash" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => hash_including("bar" => '1')})
expect {
http_request(:post, "http://www.example.com/",
headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
body: "foo[xyz]=1")
}.to raise_error(WebMock::NetConnectNotAllowedError)
end
end

context "when using hash_excluding to match the request body" do
it "should match if stubbed request body is excluding hash" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => hash_excluding("bar" => '1')})
expect(http_request(
:post, "http://www.example.com/", headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
body: "foo[xyz]=1").status).to eq("200")
end

it "should not match if stubbed request body is including hash" do
WebMock.reset!
stub_request(:post, "www.example.com").with(body: {"foo" => hash_excluding("bar" => '1')})
expect {
http_request(:post, "http://www.example.com/",
headers: {'Content-Type' => 'application/x-www-form-urlencoded'},
body: "foo[bar]=1")
}.to raise_error(WebMock::NetConnectNotAllowedError)
end
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def connection_refused_exception_class
FakeTyphoeusHydraConnectError
end

def connection_error_class
end

def http_library
:typhoeus
end
Expand Down
8 changes: 8 additions & 0 deletions spec/acceptance/webmock_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
WebMock.reset!
end

around(:each, net_connect: true) do |ex|
ex.run_with_retry retry: 2, exceptions_to_retry: [
client_timeout_exception_class,
connection_refused_exception_class,
connection_error_class
].compact
end

include_context "allowing and disabling net connect", *adapter_info

include_context "stubbing requests", *adapter_info
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'rspec'
require 'rspec/retry'

require 'webmock/rspec'

Expand Down
6 changes: 6 additions & 0 deletions spec/unit/util/version_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,11 @@ module WebMock
expect(Kernel).to receive(:warn).with(%r{You are using foo 2.0.0. WebMock does not support this version. WebMock supports versions >= 1.0.0, < 3.1, except versions 2.0.0.})
checker.check_version!
end

it "does not raise an error or print a warning when the max_minor_version is 1.0 and the version is 0.9" do
checker = VersionChecker.new('foo', '0.9.0', '0.8.0', '1.0')
expect(Kernel).not_to receive(:warn)#.with(%r{You are using foo 0.9.0. WebMock does not support this version. WebMock supports versions >= 0.8.0, < 1.0.})
checker.check_version!
end
end
end
Loading

0 comments on commit 0e9519c

Please sign in to comment.