Skip to content

Commit

Permalink
Add tests for parameter validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-stripe committed Jul 31, 2018
1 parent e2820ba commit 0147158
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/excon.rb
Expand Up @@ -63,7 +63,7 @@ def display_warning(warning)
end

if @raise_on_warnings
raise warning
raise Error::Warning.new(warning)
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/excon/error.rb
Expand Up @@ -6,6 +6,7 @@ class Error < StandardError

class StubNotFound < Error; end
class InvalidStub < Error; end
class Warning < Error; end

# Socket related errors
class Socket < Error
Expand Down
12 changes: 2 additions & 10 deletions spec/requests/unix_socket_spec.rb
@@ -1,25 +1,17 @@
require "spec_helper"

describe Excon::Connection do
include_context('stubs')
context "when speaking to a UNIX socket" do
context "Host header handling" do
before do
responder = ->(req) do
Excon.stub do |req|
{
body: req[:headers].to_json,
status: 200,
}
end

@original_mock = Excon.defaults[:mock]
Excon.defaults[:mock] = true
Excon.stub({}, responder)
end

after do
Excon.defaults[:mock] = @original_mock
end

it "sends an empty Host= by default" do
conn = Excon::Connection.new(
scheme: "unix",
Expand Down
80 changes: 80 additions & 0 deletions spec/requests/validation_spec.rb
@@ -0,0 +1,80 @@
describe Excon::Connection do
include_context('stubs')
describe 'validating parameters' do
class FooMiddleware < Excon::Middleware::Base
def self.valid_parameter_keys
[:foo]
end
end

let(:foo_stack) do
Excon.defaults[:middlewares] + [FooMiddleware]
end

def expect_parameter_warning(validation, key)
expect { yield }.to raise_error(Excon::Error::Warning, "Invalid Excon #{validation} keys: #{key.inspect}")
end

context 'with default middleware' do
it 'Connection.new warns on invalid parameter keys' do
expect_parameter_warning('connection', :foo) do
Excon.new('http://foo', :foo => :bar)
end
end

it 'Connection#request warns on invalid parameter keys' do
conn = Excon.new('http://foo')
expect_parameter_warning('request', :foo) do
conn.request(:foo => :bar)
end
end
end

context 'with custom middleware at instantiation' do
it 'Connection.new accepts parameters that are valid for the provided middleware' do
Excon.new('http://foo', :foo => :bar, :middlewares => foo_stack)
end

it 'Connection.new warns on parameters that are not valid for the provided middleware' do
expect_parameter_warning('connection', :bar) do
Excon.new('http://foo', :bar => :baz, :middlewares => foo_stack)
end
end

it 'Connection#request accepts parameters that are valid for the provided middleware' do
Excon.stub({}, {})
conn = Excon.new('http://foo', :middlewares => foo_stack)
conn.request(:foo => :bar)
end

it 'Connection#request warns on parameters that are not valid for the provided middleware' do
conn = Excon.new('http://foo', :middlewares => foo_stack)
expect_parameter_warning('request', :bar) do
conn.request(:bar => :baz)
end
end
end

context 'with custom middleware at request time' do
it 'Connection#request accepts parameters that are valid for the provided middleware' do
Excon.stub({}, {})
conn = Excon.new('http://foo')
conn.request(:foo => :bar, :middlewares => foo_stack)
end

it 'Connection#request warns on parameters that are not valid for the request middleware' do
conn = Excon.new('http://foo')
expect_parameter_warning('request', :bar) do
conn.request(:bar => :baz, :middlewares => foo_stack)
end
end

it 'Connection#request warns on parameters from instantiation that are not valid for the request middleware' do
conn = Excon.new('http://foo', :foo => :bar, :middlewares => foo_stack)
expect_parameter_warning('connection', :foo) do
conn.request(:middlewares => Excon.defaults[:middlewares])
end
end
end
end
end
11 changes: 11 additions & 0 deletions spec/support/shared_contexts/test_stub_context.rb
@@ -0,0 +1,11 @@
shared_context "stubs" do
before do
@original_mock = Excon.defaults[:mock]
Excon.defaults[:mock] = true
end

after do
Excon.defaults[:mock] = @original_mock
Excon.stubs.clear
end
end

0 comments on commit 0147158

Please sign in to comment.