capybara-typhoeus 0.2.12 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5d47a174ccfacb40f34d85580cc59edeb5504498
4
+ data.tar.gz: 4c68438940a7ced8999b840e73a31badc8bc8ce3
5
+ SHA512:
6
+ metadata.gz: 0ba0d5e3e65479ef80b00bde6be3ee9cd1c6330352412f723e708fd248b961a14cac7db6dd05e848d165161752cdd469ac2ebd3a972f53271318671a0d9587c4
7
+ data.tar.gz: fb8a7ab2d5cd59c3454ebf3abb13b09140c3d6ea65e37ca3445c2ffb0b2adfce6df123e99a3137b0f74fdfd0224570c9fdf86057bd2bb1f6631dba5cb9cd57cc
@@ -1,11 +1,23 @@
1
1
  require "capybara"
2
+ require "typhoeus"
2
3
 
3
4
  module Capybara
4
- module Driver
5
- autoload :Typhoeus, 'capybara/driver/typhoeus_driver'
5
+ module Typhoeus
6
+ autoload :Driver, "capybara/typhoeus/driver"
7
+ autoload :Browser, "capybara/typhoeus/browser"
8
+ autoload :Session, "capybara/typhoeus/session"
9
+ end
10
+
11
+ def current_session
12
+ key = "#{current_driver}:#{session_name}:#{app.object_id}"
13
+ session_pool[key] ||= if current_driver==:typhoeus
14
+ ::Capybara::Typhoeus::Session.new current_driver, app
15
+ else
16
+ ::Capybara::Session.new current_driver, app
17
+ end
6
18
  end
7
19
  end
8
20
 
9
21
  Capybara.register_driver :typhoeus do |app|
10
- Capybara::Driver::Typhoeus.new(app)
22
+ Capybara::Typhoeus::Driver.new app
11
23
  end
@@ -0,0 +1,105 @@
1
+ module Typhoeus
2
+ class Response
3
+ def redirect?
4
+ [301, 302, 303, 307].include? code
5
+ end
6
+
7
+ def [](key)
8
+ headers[key]
9
+ end
10
+ end
11
+ end
12
+
13
+ class Capybara::Typhoeus::Browser < Capybara::RackTest::Browser
14
+
15
+ attr_reader :last_response, :client
16
+
17
+ def initialize(driver)
18
+ @client = Typhoeus::Hydra.new
19
+ super
20
+ end
21
+
22
+ def reset_cache!
23
+ @xml = nil
24
+ @json = nil
25
+ super
26
+ end
27
+
28
+ def current_url
29
+ last_response ? last_response.effective_url : ""
30
+ end
31
+
32
+ def dom
33
+ @dom ||= begin
34
+ content_type = if last_response
35
+ last_response.headers["Content-Type"].to_s[/\A[^;]+/]
36
+ else
37
+ ""
38
+ end
39
+ if content_type.include? "/xml"
40
+ xml
41
+ else
42
+ Nokogiri::HTML html
43
+ end
44
+ end
45
+ end
46
+
47
+ def html
48
+ last_response ? last_response.body : ""
49
+ end
50
+
51
+ def xml
52
+ @xml ||= Nokogiri::XML html
53
+ end
54
+
55
+ def json
56
+ @json ||= Yajl::Parser.parse html
57
+ end
58
+
59
+ [:get, :post, :put, :delete, :head, :patch, :request].each do |method|
60
+ define_method(method) do |url, params={}, headers={}, &block|
61
+ uri = URI.parse url
62
+ opts = driver.with_options
63
+ opts[:method] = method
64
+ opts[:headers] = driver.with_headers.merge(
65
+ headers.merge("Content-Type" => driver.as, "Accept" => driver.as)
66
+ )
67
+ referer = opts[:headers].delete "HTTP_REFERER"
68
+ opts[:headers]["Referer"] = referer if referer
69
+ opts[:headers]["Cookie"] ||= cookie_jar.for uri
70
+ driver.options.each do |key, value|
71
+ next if Capybara::RackTest::Driver::DEFAULT_OPTIONS.has_key? key
72
+ opts[key] = value
73
+ end
74
+ if driver.auth?
75
+ opts[:httpauth] = :basic
76
+ opts[:userpwd] = "#{driver.login}:#{driver.password}"
77
+ end
78
+ if params.is_a? Hash
79
+ opts[:params] = driver.with_params.merge(params)
80
+ else
81
+ opts[:params] = driver.with_params
82
+ opts[:body] = params
83
+ end
84
+ request = Typhoeus::Request.new uri.to_s, opts
85
+ client.queue request
86
+ client.run
87
+ @last_response = request.response
88
+ if last_response.timed_out?
89
+ $stderr.puts "#{method.to_s.upcase} #{uri.to_s}: time out" if $DEBUG
90
+ elsif last_response.code==0
91
+ $stderr.puts "#{method.to_s.upcase} #{uri.to_s}: #{last_response.return_message}" if $DEBUG
92
+ else
93
+ cookie_jar.merge last_response.headers["Set-Cookie"], uri
94
+ end
95
+ last_response
96
+ end
97
+ end
98
+
99
+ private
100
+
101
+ def cookie_jar
102
+ @cookie_jar ||= Rack::Test::CookieJar.new [], current_host
103
+ end
104
+
105
+ end
@@ -1,6 +1,6 @@
1
- require 'capybara/typhoeus'
1
+ require "capybara/typhoeus"
2
2
 
3
- Before('@typhoeus') do
3
+ Before "@typhoeus" do
4
4
  Capybara.current_driver = :typhoeus
5
5
  page.driver.reset_with!
6
- end
6
+ end
@@ -0,0 +1,83 @@
1
+ class Capybara::Typhoeus::Driver < Capybara::RackTest::Driver
2
+
3
+ attr_writer :as, :with_headers, :with_params, :with_options
4
+ attr_reader :login, :password
5
+
6
+ def initialize(app, options = {})
7
+ raise ArgumentError, "typhoeus requires a rack application, but none was given" unless app
8
+ super app, {timeout: 3, forbid_reuse: true}.merge(options)
9
+ end
10
+
11
+ def browser
12
+ @browser ||= Capybara::Typhoeus::Browser.new self
13
+ end
14
+
15
+ def needs_server?
16
+ true
17
+ end
18
+
19
+ [:get, :post, :put, :delete, :head, :patch, :request].each do |method|
20
+ define_method(method) do |url, params={}, headers={}, &block|
21
+ browser.reset_host!
22
+ browser.process method, url, params, headers, &block
23
+ end
24
+ end
25
+
26
+ def reset!
27
+ @login = nil
28
+ @password = nil
29
+ super
30
+ end
31
+
32
+ def timed_out?
33
+ response.timed_out?
34
+ end
35
+
36
+ def reset_with!
37
+ @with_headers = {}
38
+ @with_params = {}
39
+ @with_options = {}
40
+ @as = nil
41
+ end
42
+
43
+ def as
44
+ @as ||= "application/json"
45
+ end
46
+
47
+ def with_headers
48
+ @with_headers ||= {}
49
+ end
50
+
51
+ def with_params
52
+ @with_params ||= {}
53
+ end
54
+
55
+ def with_options
56
+ @with_options ||= {}
57
+ end
58
+
59
+ def authenticate_with(login, password)
60
+ @login, @password = login, password
61
+ end
62
+
63
+ def auth?
64
+ login && password
65
+ end
66
+
67
+ def status_code
68
+ response.code
69
+ end
70
+
71
+ def body
72
+ browser.html
73
+ end
74
+
75
+ def xml
76
+ browser.json
77
+ end
78
+
79
+ def json
80
+ browser.json
81
+ end
82
+
83
+ end
@@ -0,0 +1,33 @@
1
+ class Capybara::Typhoeus::Session < Capybara::Session
2
+
3
+ [:get, :post, :put, :delete, :head, :patch, :request].each do |method|
4
+ define_method(method) do |url, params={}, headers={}, &block|
5
+ @touched = true
6
+
7
+ if url !~ /^http/ and Capybara.app_host
8
+ url = Capybara.app_host + url.to_s
9
+ end
10
+
11
+ if @server
12
+ url = "http://#{@server.host}:#{@server.port}" + url.to_s unless url =~ /^http/
13
+
14
+ if Capybara.always_include_port
15
+ uri = URI.parse url
16
+ uri.port = @server.port if uri.port == uri.default_port
17
+ url = uri.to_s
18
+ end
19
+ end
20
+
21
+ driver.send method, url, params, headers, &block
22
+ end
23
+ end
24
+
25
+ def authenticate_with(login, password)
26
+ driver.authenticate_with login, password
27
+ end
28
+
29
+ def timed_out?
30
+ driver.timed_out?
31
+ end
32
+
33
+ end
@@ -1,82 +1,72 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
- module TestSessions
4
- Typhoeus = Capybara::Session.new :typhoeus, TestApp
3
+ Capybara.register_driver :typhoeus_with_custom_timeout do |app|
4
+ Capybara::Typhoeus::Driver.new app, timeout: 1
5
5
  end
6
6
 
7
- describe Capybara::Session do
8
- context 'with typhoeus driver' do
9
- before(:all) do
10
- @session = TestSessions::Typhoeus
11
- end
12
-
13
- after do
14
- @session.reset_session!
15
- end
7
+ describe Capybara::Typhoeus::Session do
16
8
 
17
- def extract_results(session)
18
- YAML.load Nokogiri::HTML(session.body).xpath("//pre[@id='results']").first.text
19
- end
9
+ Capybara::SpecHelper.run_specs described_class.new(:typhoeus, TestApp), "Typhoeus", skip: [
10
+ :js,
11
+ :screenshot,
12
+ :frames,
13
+ :windows,
14
+ :server,
15
+ :hover
16
+ ]
20
17
 
21
- describe '#driver' do
22
- it "should be a typhoeus driver" do
23
- @session.driver.should be_an_instance_of Capybara::Driver::Typhoeus
18
+ context "with typhoeus driver" do
19
+ context "basic authentication" do
20
+ subject do
21
+ app = Sinatra.new do
22
+ use Rack::Auth::Basic do |username, password|
23
+ username=="admin" && password=="secret"
24
+ end
25
+ get("/"){ "Success!" }
26
+ end
27
+ described_class.new :typhoeus, app
24
28
  end
25
- end
26
29
 
27
- describe '#mode' do
28
- it "should remember the mode" do
29
- @session.mode.should == :typhoeus
30
+ it "allow access with right credentials" do
31
+ subject.authenticate_with "admin", "secret"
32
+ subject.get "/"
33
+ subject.status_code.should be 200
34
+ subject.source.should == "Success!"
30
35
  end
31
- end
32
36
 
33
- describe '#app' do
34
- it "should remember the application" do
35
- @session.app.should == TestApp
37
+ it "deny access with wrong credentials" do
38
+ subject.authenticate_with "admin", "admin"
39
+ subject.get "/"
40
+ subject.status_code.should be 401
36
41
  end
37
42
  end
38
43
 
39
- describe '#visit' do
40
- it "should fetch a response from the driver" do
41
- @session.visit('/')
42
- @session.body.should include('Hello world!')
43
- @session.visit('/foo')
44
- @session.body.should include('Another World')
45
- end
46
- end
44
+ context "timeout" do
45
+ context "default" do
46
+ subject{ described_class.new :typhoeus, TestApp }
47
+
48
+ it "is 3 seconds" do
49
+ subject.driver.options[:timeout].should == 3
50
+ end
47
51
 
48
- describe '#body' do
49
- it "should return the unmodified page body" do
50
- @session.visit('/')
51
- @session.body.should include('Hello world!')
52
+ it "is used during request" do
53
+ subject.get "/slow_response"
54
+ subject.should_not be_timed_out
55
+ end
52
56
  end
53
- end
54
57
 
55
- describe '#source' do
56
- it "should return the unmodified page source" do
57
- @session.visit('/')
58
- @session.source.should include('Hello world!')
58
+ context "accepts custom timeout" do
59
+ subject{ described_class.new :typhoeus_with_custom_timeout, TestApp }
60
+
61
+ it "is stored in options" do
62
+ subject.driver.options[:timeout].should == 1
63
+ end
64
+
65
+ it "is used during request" do
66
+ subject.get "/slow_response"
67
+ subject.should be_timed_out
68
+ end
59
69
  end
60
70
  end
61
-
62
- it_should_behave_like "all"
63
- it_should_behave_like "first"
64
- it_should_behave_like "find_button"
65
- it_should_behave_like "find_field"
66
- it_should_behave_like "find_link"
67
- it_should_behave_like "find_by_id"
68
- it_should_behave_like "has_content"
69
- it_should_behave_like "has_css"
70
- it_should_behave_like "has_selector"
71
- it_should_behave_like "has_xpath"
72
- it_should_behave_like "has_link"
73
- it_should_behave_like "has_button"
74
- it_should_behave_like "has_field"
75
- it_should_behave_like "has_select"
76
- it_should_behave_like "has_table"
77
- it_should_behave_like "current_url"
78
- it_should_behave_like "session without javascript support"
79
- it_should_behave_like "session with headers support"
80
- it_should_behave_like "session with status code support"
81
71
  end
82
72
  end
@@ -1,16 +1,28 @@
1
- require 'bundler/setup'
2
- require 'capybara'
3
- require 'capybara/typhoeus'
4
- require 'capybara/spec/test_app'
5
- require 'capybara/spec/driver'
6
- require 'capybara/spec/session'
1
+ require "bundler/setup"
2
+ require "capybara/typhoeus"
3
+ require "capybara/spec/spec_helper"
7
4
 
8
- alias :running :lambda
9
-
10
- Capybara.default_wait_time = 0 # less timeout so tests run faster
5
+ module Capybara
6
+ module SpecHelper
7
+ class << self
8
+ alias_method :run_specs_without_skip, :run_specs
9
+ def run_specs(session, name, options={})
10
+ skip = [
11
+ "#attach_file",
12
+ "#check",
13
+ "#click_button",
14
+ "#click_link",
15
+ "#select",
16
+ "#uncheck",
17
+ "#unselect",
18
+ ]
19
+ @specs.reject!{|spec| skip.include? spec.first}
20
+ run_specs_without_skip session, name, options
21
+ end
22
+ end
23
+ end
24
+ end
11
25
 
12
26
  RSpec.configure do |config|
13
- config.after do
14
- Capybara.default_selector = :xpath
15
- end
27
+ Capybara::SpecHelper.configure config
16
28
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-typhoeus
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.2.12
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Joseph HALTER
@@ -14,131 +13,119 @@ date: 2011-04-22 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: capybara
17
- type: :runtime
18
16
  requirement: !ruby/object:Gem::Requirement
19
17
  requirements:
20
18
  - - ~>
21
19
  - !ruby/object:Gem::Version
22
- version: 1.1.4
23
- none: false
20
+ version: 2.1.0
21
+ type: :runtime
22
+ prerelease: false
24
23
  version_requirements: !ruby/object:Gem::Requirement
25
24
  requirements:
26
25
  - - ~>
27
26
  - !ruby/object:Gem::Version
28
- version: 1.1.4
29
- none: false
30
- prerelease: false
27
+ version: 2.1.0
31
28
  - !ruby/object:Gem::Dependency
32
29
  name: typhoeus
33
- type: :runtime
34
30
  requirement: !ruby/object:Gem::Requirement
35
31
  requirements:
36
32
  - - '='
37
33
  - !ruby/object:Gem::Version
38
34
  version: 0.6.3
39
- none: false
35
+ type: :runtime
36
+ prerelease: false
40
37
  version_requirements: !ruby/object:Gem::Requirement
41
38
  requirements:
42
39
  - - '='
43
40
  - !ruby/object:Gem::Version
44
41
  version: 0.6.3
45
- none: false
46
- prerelease: false
47
42
  - !ruby/object:Gem::Dependency
48
43
  name: yajl-ruby
49
- type: :runtime
50
44
  requirement: !ruby/object:Gem::Requirement
51
45
  requirements:
52
46
  - - ~>
53
47
  - !ruby/object:Gem::Version
54
48
  version: 1.1.0
55
- none: false
49
+ type: :runtime
50
+ prerelease: false
56
51
  version_requirements: !ruby/object:Gem::Requirement
57
52
  requirements:
58
53
  - - ~>
59
54
  - !ruby/object:Gem::Version
60
55
  version: 1.1.0
61
- none: false
62
- prerelease: false
63
56
  - !ruby/object:Gem::Dependency
64
57
  name: rake
65
- type: :development
66
58
  requirement: !ruby/object:Gem::Requirement
67
59
  requirements:
68
- - - ! '>='
60
+ - - '>='
69
61
  - !ruby/object:Gem::Version
70
62
  version: '0'
71
- none: false
63
+ type: :development
64
+ prerelease: false
72
65
  version_requirements: !ruby/object:Gem::Requirement
73
66
  requirements:
74
- - - ! '>='
67
+ - - '>='
75
68
  - !ruby/object:Gem::Version
76
69
  version: '0'
77
- none: false
78
- prerelease: false
79
70
  - !ruby/object:Gem::Dependency
80
71
  name: rspec
81
- type: :development
82
72
  requirement: !ruby/object:Gem::Requirement
83
73
  requirements:
84
- - - ! '>='
74
+ - - '>='
85
75
  - !ruby/object:Gem::Version
86
76
  version: '0'
87
- none: false
77
+ type: :development
78
+ prerelease: false
88
79
  version_requirements: !ruby/object:Gem::Requirement
89
80
  requirements:
90
- - - ! '>='
81
+ - - '>='
91
82
  - !ruby/object:Gem::Version
92
83
  version: '0'
93
- none: false
94
- prerelease: false
95
84
  - !ruby/object:Gem::Dependency
96
85
  name: rack
97
- type: :development
98
86
  requirement: !ruby/object:Gem::Requirement
99
87
  requirements:
100
- - - ! '>='
88
+ - - '>='
101
89
  - !ruby/object:Gem::Version
102
90
  version: '0'
103
- none: false
91
+ type: :development
92
+ prerelease: false
104
93
  version_requirements: !ruby/object:Gem::Requirement
105
94
  requirements:
106
- - - ! '>='
95
+ - - '>='
107
96
  - !ruby/object:Gem::Version
108
97
  version: '0'
109
- none: false
110
- prerelease: false
111
98
  - !ruby/object:Gem::Dependency
112
99
  name: sinatra
113
- type: :development
114
100
  requirement: !ruby/object:Gem::Requirement
115
101
  requirements:
116
- - - ! '>='
102
+ - - '>='
117
103
  - !ruby/object:Gem::Version
118
104
  version: '0'
119
- none: false
105
+ type: :development
106
+ prerelease: false
120
107
  version_requirements: !ruby/object:Gem::Requirement
121
108
  requirements:
122
- - - ! '>='
109
+ - - '>='
123
110
  - !ruby/object:Gem::Version
124
111
  version: '0'
125
- none: false
126
- prerelease: false
127
112
  description: Typhoeus driver for Capybara, allowing testing of REST APIs
128
113
  email: joseph.halter@thetalentbox.com
129
114
  executables: []
130
115
  extensions: []
131
116
  extra_rdoc_files: []
132
117
  files:
133
- - lib/capybara/driver/typhoeus_driver.rb
118
+ - lib/capybara/typhoeus/browser.rb
134
119
  - lib/capybara/typhoeus/cucumber.rb
120
+ - lib/capybara/typhoeus/driver.rb
121
+ - lib/capybara/typhoeus/session.rb
135
122
  - lib/capybara/typhoeus.rb
136
- - spec/driver/typhoeus_driver_spec.rb
137
123
  - spec/session/typhoeus_spec.rb
138
124
  - spec/spec_helper.rb
139
125
  - README.md
140
126
  homepage: https://github.com/TalentBox/capybara-typhoeus
141
127
  licenses: []
128
+ metadata: {}
142
129
  post_install_message:
143
130
  rdoc_options:
144
131
  - --charset=UTF-8
@@ -146,23 +133,18 @@ require_paths:
146
133
  - lib
147
134
  required_ruby_version: !ruby/object:Gem::Requirement
148
135
  requirements:
149
- - - ! '>='
136
+ - - '>='
150
137
  - !ruby/object:Gem::Version
151
- segments:
152
- - 0
153
- hash: 1272068252246424913
154
138
  version: '0'
155
- none: false
156
139
  required_rubygems_version: !ruby/object:Gem::Requirement
157
140
  requirements:
158
- - - ! '>='
141
+ - - '>='
159
142
  - !ruby/object:Gem::Version
160
143
  version: '0'
161
- none: false
162
144
  requirements: []
163
145
  rubyforge_project:
164
- rubygems_version: 1.8.25
146
+ rubygems_version: 2.0.3
165
147
  signing_key:
166
- specification_version: 3
148
+ specification_version: 4
167
149
  summary: Typhoeus driver for Capybara
168
150
  test_files: []
@@ -1,196 +0,0 @@
1
- require "typhoeus"
2
-
3
- class Capybara::Driver::Typhoeus < Capybara::Driver::Base
4
- class Node < Capybara::RackTest::Node
5
- def click
6
- driver.process(:get, self[:href].to_s) if self[:href] && self[:href] != ""
7
- end
8
- end
9
-
10
- attr_writer :as, :with_headers, :with_params, :with_options
11
- attr_reader :app, :rack_server, :options, :response, :login, :password
12
-
13
- def client
14
- @client ||= Typhoeus::Hydra.new
15
- end
16
-
17
- def initialize(app, options={})
18
- @app = app
19
- @options = {
20
- :timeout => 3, # 3 seconds
21
- :forbid_reuse => true,
22
- }.merge options
23
- @rack_server = Capybara::Server.new(@app)
24
- @rack_server.boot if Capybara.run_server
25
- end
26
-
27
- def visit(url, params = {})
28
- reset_cache
29
- process :get, url, params
30
- end
31
-
32
- def get(url, params = {}, headers = {})
33
- reset_cache
34
- process :get, url, params, headers
35
- end
36
-
37
- def post(url, params = {}, headers = {})
38
- reset_cache
39
- process :post, url, params, headers
40
- end
41
-
42
- def put(url, params = {}, headers = {})
43
- reset_cache
44
- process :put, url, params, headers
45
- end
46
-
47
- def delete(url, params = {}, headers = {})
48
- reset_cache
49
- process :delete, url, params, headers
50
- end
51
-
52
- def head(url, params = {}, headers = {})
53
- reset_cache
54
- process :head, url, params, headers
55
- end
56
-
57
- def patch(url, params = {}, headers = {})
58
- reset_cache
59
- process :patch, url, params, headers
60
- end
61
-
62
- def request(url, params = {}, headers = {})
63
- reset_cache
64
- process :request, url, params, headers
65
- end
66
-
67
- def submit(method, path, attributes)
68
- path = request.path if not path or path.empty?
69
- process method.to_sym, path, attributes
70
- end
71
-
72
- def find(selector)
73
- dom.xpath(selector).map { |node| Node.new(self, node) }
74
- end
75
-
76
- def html
77
- @html ||= Nokogiri::HTML body
78
- end
79
-
80
- def xml
81
- @xml ||= Nokogiri::XML body
82
- end
83
-
84
- def json
85
- @json ||= Yajl::Parser.parse body
86
- end
87
-
88
- def body
89
- response.body
90
- end
91
- alias_method :source, :body
92
-
93
- def response_headers
94
- response.headers
95
- end
96
-
97
- def status_code
98
- response.code
99
- end
100
-
101
- def current_url
102
- @current_uri.to_s
103
- end
104
-
105
- def reset!
106
- @client = nil
107
- @response = nil
108
- @current_uri = nil
109
- @login = nil
110
- @password = nil
111
- reset_cache
112
- end
113
-
114
- def reset_with!
115
- @with_headers = {}
116
- @with_params = {}
117
- @with_options = {}
118
- end
119
-
120
- def as
121
- @as ||= "application/json"
122
- end
123
-
124
- def with_headers
125
- @with_headers ||= {}
126
- end
127
-
128
- def with_params
129
- @with_params ||= {}
130
- end
131
-
132
- def with_options
133
- @with_options ||= {}
134
- end
135
-
136
- def authenticate_with(login, password)
137
- @login, @password = login, password
138
- end
139
-
140
- def auth?
141
- login && password
142
- end
143
-
144
- def process(method, path, params = {}, headers = {})
145
- @current_uri = url path
146
- opts = {
147
- :method => method,
148
- :headers => with_headers.merge(headers.merge("Content-Type" => as, "Accept" => as)),
149
- }.merge(options)
150
- opts.merge!({
151
- :userpwd => "#{login}:#{password}",
152
- :httpauth => :basic,
153
- }) if auth?
154
- if params.is_a?(Hash)
155
- opts[:params] = with_params.merge(params)
156
- else
157
- opts[:params] = with_params
158
- opts[:body] = params
159
- end
160
- request = Typhoeus::Request.new @current_uri, with_options.merge(opts)
161
- client.queue request
162
- client.run
163
- @response = request.response
164
- if @response.timed_out?
165
- $stderr.puts "#{method.to_s.upcase} #{@current_uri}: time out" if $DEBUG
166
- elsif @response.code==0
167
- $stderr.puts "#{method.to_s.upcase} #{@current_uri}: #{@response.curl_error_message}" if $DEBUG
168
- end
169
- @response
170
- end
171
-
172
- def url(path)
173
- rack_server.url(path)
174
- end
175
-
176
- def dom
177
- content_type = response_headers["Content-Type"]
178
- case content_type.to_s[/\A[^;]+/]
179
- when "application/xml", "text/xml"
180
- xml
181
- when "text/html"
182
- html
183
- else
184
- raise "Content-Type: #{content_type} is not handling xpath search"
185
- end
186
- end
187
-
188
- private
189
-
190
- def reset_cache
191
- @xml = nil
192
- @html = nil
193
- @json = nil
194
- end
195
-
196
- end
@@ -1,72 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Capybara::Driver::Typhoeus do
4
- before do
5
- @driver = described_class.new TestApp
6
- end
7
-
8
- context "in remote mode" do
9
- it_should_behave_like "driver"
10
- it_should_behave_like "driver with header support"
11
- it_should_behave_like "driver with status code support"
12
- # neither supports cookies nor follows redirect automatically
13
- end
14
-
15
- context "basic authentication" do
16
- subject do
17
- app = Sinatra.new do
18
- use Rack::Auth::Basic do |username, password|
19
- username=="admin" && password=="secret"
20
- end
21
- get("/"){ "Success!" }
22
- end
23
- described_class.new app
24
- end
25
-
26
- it "allow access with right credentials" do
27
- subject.authenticate_with "admin", "secret"
28
- subject.get "/"
29
- subject.status_code.should be 200
30
- subject.source.should == "Success!"
31
- end
32
-
33
- it "deny access with wrong credentials" do
34
- subject.authenticate_with "admin", "admin"
35
- subject.get "/"
36
- subject.status_code.should be 401
37
- end
38
- end
39
-
40
- context "timeout" do
41
- context "default" do
42
- subject do
43
- driver = described_class.new TestApp
44
- end
45
-
46
- it "is 3 seconds" do
47
- subject.options[:timeout].should == 3
48
- end
49
-
50
- it "is used during request" do
51
- response = subject.get "/slow_response"
52
- response.should_not be_timed_out
53
- end
54
- end
55
-
56
- context "accepts custom timeout" do
57
- subject do
58
- driver = described_class.new TestApp, timeout: 1
59
- end
60
-
61
- it "is stored in options" do
62
- subject.options[:timeout].should == 1
63
- end
64
-
65
- it "is used during request" do
66
- response = subject.get "/slow_response"
67
- response.should be_timed_out
68
- end
69
- end
70
- end
71
-
72
- end