capybara-webmock 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46242d695b6329b75f9b405cb002207f06b9d91a
4
- data.tar.gz: fca7a29ef9ee4b3a8f2db6a715cd7f6c1777c56b
3
+ metadata.gz: e2c1c6bf43bd5b107c8f060e58c1fd108ae59b4b
4
+ data.tar.gz: 8af76108b2d5287647ce73cb892387e6e519df7d
5
5
  SHA512:
6
- metadata.gz: 62b602849ff43d841cd90b46b5527d1b30d3e27c2593c4db11ac6dc38e4e04c2a59b5c8c11d4dabc49219416c1d1aea6c5cd7eb0e2e87118d3d3bfaeac9989e4
7
- data.tar.gz: 0a6e9d81180148c13781619af2378558cdce80a2c030faec3be48a20ecd7c46561e106f199130e642feef9580cf040f2dc804fae6a2e9caba74a4350223bd8a2
6
+ metadata.gz: 6925ef8010bb52e315b31d49ab067ac3af88b8830d10b7e46dc9d6299b0f3c29626cdef92a9b26e76d7cdf9964570b01a6d5870a7f0bbaf51a53d7aeea7f4906
7
+ data.tar.gz: a6351773d12fd974572d514ab851bb6895161c7bacd5ae0c318613085418e416b6f2fe05627fe5767ce5aaa9554a7e6a9cc21c5c35655dca0d439a8de824f7e4
data/README.md CHANGED
@@ -10,16 +10,16 @@ External JavaScript libraries, CDN's, images, analytics, and more can slow an
10
10
  integration test suite to a crawl.
11
11
 
12
12
  `Capybara::Webmock` is a Rack proxy server that sits between your Ruby on Rails
13
- Selenium Firefox test suite and the Internet, blocking external requests.
13
+ Selenium test suite and the Internet, blocking external requests.
14
14
 
15
15
  Adding this gem to your project can significantly speed up the test suite. No
16
16
  more waiting on irrelevant external requests.
17
17
 
18
- `localhost` and `lvh.me` are the only whitelisted domains. Custom domain
18
+ `localhost`, `127.0.0.1`, `*.lvh.me`, and `lvh.me` are the only whitelisted domains. Custom domain
19
19
  support is a planned feature.
20
20
 
21
- This gem currently supports Ruby on Rails applications with an RSpec test
22
- suite, using the Selenium Firefox driver. ChromeDriver support is in the works.
21
+ This gem currently supports Ruby on Rails applications with RSpec and
22
+ Cucumber test suites, using the Selenium Firefox and Chrome drivers.
23
23
 
24
24
  ### Installation
25
25
 
@@ -63,12 +63,37 @@ RSpec.configure do |config|
63
63
  end
64
64
  ```
65
65
 
66
+ Or, your Cucumber configuration:
67
+
68
+ ```ruby
69
+ Capybara::Webmock.start
70
+
71
+ at_exit do
72
+ Capybara::Webmock.stop
73
+ end
74
+ ```
75
+
66
76
  Then use the `capybara_webmock` JavaScript driver:
67
77
 
68
78
  ```ruby
79
+ # Use Firefox Driver
69
80
  Capybara.javascript_driver = :capybara_webmock
70
81
  ```
71
82
 
83
+ or
84
+
85
+ ```ruby
86
+ # Use Chrome Driver
87
+ Capybara.javascript_driver = :capybara_webmock_chrome
88
+ ```
89
+
90
+ By default the proxy server run on port `9292`, but this can be customized
91
+ with the following configuration:
92
+
93
+ ```ruby
94
+ Capybara::Webmock.port_number = 8080
95
+ ```
96
+
72
97
  ### Development
73
98
 
74
99
  After checking out the repo, run `bin/setup` to install dependencies. Then, run
@@ -25,9 +25,11 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency "capybara", "~> 2.4"
26
26
  spec.add_dependency "rack", ">= 1.4"
27
27
  spec.add_dependency "rack-proxy", "0.6.0"
28
+ spec.add_dependency "selenium-webdriver", "~> 2.0"
28
29
 
29
30
  spec.add_development_dependency "bundler", "~> 1.13"
31
+ spec.add_development_dependency "pry", "~> 0.10.4"
30
32
  spec.add_development_dependency "rake", "~> 10.0"
31
33
  spec.add_development_dependency "rspec", "~> 3.0"
32
- spec.add_development_dependency "pry", "~> 0.10.4"
34
+ spec.add_development_dependency "simplecov"
33
35
  end
@@ -1,17 +1,19 @@
1
1
  require 'capybara'
2
+ require 'selenium-webdriver'
2
3
  require 'capybara/webmock/version'
3
4
  require 'capybara/webmock/proxy'
4
5
 
5
6
  module Capybara
6
7
  module Webmock
7
-
8
8
  class << self
9
9
 
10
+ attr_accessor :port_number
11
+
10
12
  def start
11
13
  log_file = File.join('log', 'test.log')
12
14
  gem_path = File.dirname(__FILE__)
13
15
  proxy_file = File.join(gem_path, 'webmock', 'config.ru')
14
- IO.popen("rackup #{proxy_file} >> #{log_file} 2>&1")
16
+ IO.popen("PROXY_PORT_NUMBER=#{port_number} rackup #{proxy_file} >> #{log_file} 2>&1")
15
17
  end
16
18
 
17
19
  def stop
@@ -21,18 +23,31 @@ module Capybara
21
23
  end
22
24
  end
23
25
 
26
+ def firefox_profile
27
+ proxy_host = '127.0.0.1'
28
+ profile = ::Selenium::WebDriver::Firefox::Profile.new
29
+ profile["network.proxy.type"] = 1
30
+ profile["network.proxy.http"] = proxy_host
31
+ profile["network.proxy.http_port"] = port_number
32
+ profile["network.proxy.ssl"] = proxy_host
33
+ profile["network.proxy.ssl_port"] = port_number
34
+ profile
35
+ end
36
+
37
+ def chrome_switches
38
+ ["--proxy-server=127.0.0.1:#{port_number}"]
39
+ end
40
+
24
41
  end
25
42
  end
26
43
  end
27
44
 
45
+ Capybara::Webmock.port_number ||= 9292
46
+
28
47
  Capybara.register_driver :capybara_webmock do |app|
29
- proxy_host = '127.0.0.1'
30
- proxy_port = 9292
31
- profile = Selenium::WebDriver::Firefox::Profile.new
32
- profile["network.proxy.type"] = 1
33
- profile["network.proxy.http"] = proxy_host
34
- profile["network.proxy.http_port"] = proxy_port
35
- profile["network.proxy.ssl"] = proxy_host
36
- profile["network.proxy.ssl_port"] = proxy_port
37
- Capybara::Selenium::Driver.new(app, browser: :firefox, profile: profile)
48
+ Capybara::Selenium::Driver.new(app, browser: :firefox, profile: Capybara::Webmock.firefox_profile)
49
+ end
50
+
51
+ Capybara.register_driver :capybara_webmock_chrome do |app|
52
+ Capybara::Selenium::Driver.new(app, browser: :chrome, switches: Capybara::Webmock.chrome_switches)
38
53
  end
@@ -9,4 +9,4 @@ trap('SIGHUP') {
9
9
  }
10
10
 
11
11
  app = Capybara::Webmock::Proxy.new(Process.pid)
12
- Rack::Handler::WEBrick.run app, { Port: 9292 }
12
+ Rack::Handler::WEBrick.run(app, Port: ENV.fetch('PROXY_PORT_NUMBER', 9292))
@@ -10,7 +10,9 @@ class Capybara::Webmock::Proxy < Rack::Proxy
10
10
 
11
11
  def perform_request(env)
12
12
  request = Rack::Request.new(env)
13
- if request.host =~ %r{.*\.lvh.me}
13
+ allowed_urls = ['127.0.0.1', 'localhost', %r{(.*\.|\A)lvh.me}]
14
+
15
+ if allowed_url?(allowed_urls, request.host)
14
16
  super(env)
15
17
  else
16
18
  ['200', {'Content-Type' => 'text/html'}, ['']]
@@ -23,6 +25,17 @@ class Capybara::Webmock::Proxy < Rack::Proxy
23
25
 
24
26
  private
25
27
 
28
+ def allowed_url?(urls, host)
29
+ case urls
30
+ when Array
31
+ urls.any? { |url| allowed_url?(url, host) }
32
+ when Regexp
33
+ urls =~ host
34
+ when String
35
+ urls == host
36
+ end
37
+ end
38
+
26
39
  def write_pid(pid)
27
40
  tmp_dir = 'tmp'
28
41
  pid_dir = File.join(tmp_dir, 'pids')
@@ -1,5 +1,5 @@
1
1
  module Capybara
2
2
  module Webmock
3
- VERSION = "0.2.3"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-webmock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jake Worth
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-10-08 00:00:00.000000000 Z
12
+ date: 2016-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capybara
@@ -53,6 +53,20 @@ dependencies:
53
53
  - - '='
54
54
  - !ruby/object:Gem::Version
55
55
  version: 0.6.0
56
+ - !ruby/object:Gem::Dependency
57
+ name: selenium-webdriver
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.0'
56
70
  - !ruby/object:Gem::Dependency
57
71
  name: bundler
58
72
  requirement: !ruby/object:Gem::Requirement
@@ -67,6 +81,20 @@ dependencies:
67
81
  - - "~>"
68
82
  - !ruby/object:Gem::Version
69
83
  version: '1.13'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: 0.10.4
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: 0.10.4
70
98
  - !ruby/object:Gem::Dependency
71
99
  name: rake
72
100
  requirement: !ruby/object:Gem::Requirement
@@ -96,19 +124,19 @@ dependencies:
96
124
  - !ruby/object:Gem::Version
97
125
  version: '3.0'
98
126
  - !ruby/object:Gem::Dependency
99
- name: pry
127
+ name: simplecov
100
128
  requirement: !ruby/object:Gem::Requirement
101
129
  requirements:
102
- - - "~>"
130
+ - - ">="
103
131
  - !ruby/object:Gem::Version
104
- version: 0.10.4
132
+ version: '0'
105
133
  type: :development
106
134
  prerelease: false
107
135
  version_requirements: !ruby/object:Gem::Requirement
108
136
  requirements:
109
- - - "~>"
137
+ - - ">="
110
138
  - !ruby/object:Gem::Version
111
- version: 0.10.4
139
+ version: '0'
112
140
  description: Mock external requests for Capybara Firefox drivers
113
141
  email:
114
142
  - dev@hashrocket.com