Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #unstub method, replace deprecated watir-webdriver with watir #212

Merged
merged 2 commits into from
Mar 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ before_script:
- phantomjs --version
- bundle --version
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.2.0
- 2.3.0
- 2.4.0
script: bundle exec rspec spec

11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ proxy.stub('http://example.com/api', :method => :options).and_return(
Stubs are reset between tests. Any requests that are not stubbed will be
proxied to the remote server.

If for any reason you'd need to reset stubs manually you can do it in two ways:

```ruby
# reset a single stub
proxy.unstub 'http://example.com/get/'
proxy.unstub 'http://example.com/update/', :method => :post

# reset all stubs
proxy.reset
```

## Cucumber Usage

An example feature:
Expand Down
7 changes: 4 additions & 3 deletions lib/billy/browsers/watir.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'billy'
require 'watir-webdriver'
require 'watir'

module Billy
module Browsers
Expand All @@ -25,8 +25,9 @@ def configure_chrome(args)
end

def configure_phantomjs(args)
args[:args] ||= []
args[:args] += %W[--proxy=#{Billy.proxy.host}:#{Billy.proxy.port}]
args[:driver_opts] ||= {}
args[:driver_opts][:args] ||= []
args[:driver_opts][:args] += %W[--proxy=#{Billy.proxy.host}:#{Billy.proxy.port}]
args
end

Expand Down
2 changes: 1 addition & 1 deletion lib/billy/handlers/request_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class RequestHandler
extend Forwardable
include Handler

def_delegators :stub_handler, :stub
def_delegators :stub_handler, :stub, :unstub

def handlers
@handlers ||= { stubs: StubHandler.new,
Expand Down
5 changes: 5 additions & 0 deletions lib/billy/handlers/stub_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def stub(url, options = {})
new_stub
end

def unstub(url, options = {:method => :get})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should unstub be given a previously created stub, rather than providing the values to search for one? 🤔

stub = proxy.stub(...)
proxy.unstub(stub)

method = options[:method].to_s.upcase
stubs.delete_if { |stub| stub.matches?(method, url) }
end

private

attr_writer :stubs
Expand Down
2 changes: 1 addition & 1 deletion lib/billy/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Proxy
extend Forwardable
attr_reader :request_handler

def_delegators :request_handler, :stub, :reset, :reset_cache, :restore_cache, :handle_request
def_delegators :request_handler, :stub, :unstub, :reset, :reset_cache, :restore_cache, :handle_request

def initialize
@request_handler = Billy::RequestHandler.new
Expand Down
7 changes: 3 additions & 4 deletions puffing-billy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'rb-inotify'
gem.add_development_dependency 'pry'
gem.add_development_dependency 'cucumber'
gem.add_development_dependency 'watir-webdriver', '0.9.1'
# addressable 2.5.0 drops support for ruby 1.9.3
gem.add_runtime_dependency 'addressable', '~> 2.4', '>= 2.4.0'
gem.add_runtime_dependency 'eventmachine', '1.2.0.1'
gem.add_development_dependency 'watir', '~> 6.10.0'
gem.add_runtime_dependency 'addressable', '~> 2.5'
gem.add_runtime_dependency 'eventmachine', '~> 1.0.4'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ronwsmith / @bwilczek Was this version change intentional? 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe watir required it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I believe once I changed watir-webdriver to watir I had to change versions of certain other gems - it wouldn't work otherwise. But I don't remember the exact details now.

gem.add_runtime_dependency 'em-synchrony'
gem.add_runtime_dependency 'em-http-request', '~> 1.1', '>= 1.1.0'
gem.add_runtime_dependency 'eventmachine_httpserver'
Expand Down
55 changes: 55 additions & 0 deletions spec/lib/billy/handlers/stub_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,61 @@
end
end

describe '#unstub' do
before do
handler.stub('http://example.post/', method: :post)
handler.stub('http://example.get/')
end

it 'removes the stub a one GET request' do
expect(handler.handles_request?('GET',
'http://example.get/',
request[:headers],
request[:body])).to be true
expect(handler.handles_request?('POST',
'http://example.post/',
request[:headers],
request[:body])).to be true

handler.unstub 'http://example.get/'

expect(handler.handles_request?('GET',
'http://example.get/',
request[:headers],
request[:body])).to be false
expect(handler.handles_request?('POST',
'http://example.post/',
request[:headers],
request[:body])).to be true
end

it 'removes the stub for a POST request' do
expect(handler.handles_request?('GET',
'http://example.get/',
request[:headers],
request[:body])).to be true
expect(handler.handles_request?('POST',
'http://example.post/',
request[:headers],
request[:body])).to be true

handler.unstub 'http://example.post/', :method => :post

expect(handler.handles_request?('GET',
'http://example.get/',
request[:headers],
request[:body])).to be true
expect(handler.handles_request?('POST',
'http://example.post/',
request[:headers],
request[:body])).to be false
end

it 'does not raise errors for not existing stub' do
expect { handler.unstub 'http://example.option/' }.not_to raise_error
end
end

it '#stubs requests' do
handler.stub('http://example.test:8080/index')
expect(handler.handles_request?('GET',
Expand Down