capybara-typhoeus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ Capybara-restfulie
2
+ ==================
3
+
4
+ This gems makes it possible to use [Faraday](http://github.com/technoweenie/faraday) for remote testing.
5
+
6
+ This gem is a [Capybara](http://github.com/jnicklas/capybara) extension. The structure of the gem is taken from the work done on [Capybara-mechanize](http://github.com/jeroenvandijk/capybara-mechanize).
7
+
8
+ ### Installation
9
+
10
+ gem install capybara-faraday
11
+
12
+ ### Usage without Cucumber
13
+
14
+ require 'capybara/faraday'
15
+
16
+ ### Usage with Cucumber and tags
17
+
18
+ A @faraday tag is added to your hooks when you add the following line to your env.rb
19
+
20
+ require 'capybara/faraday/cucumber'
21
+
22
+ The following scenario will then be using the Faraday driver
23
+
24
+ @faraday
25
+ Scenario: do something with the API
26
+ Given I send and accept JSON
27
+ When I send a GET request to /users
28
+
29
+ If you want to use a specific faraday supported adapter, you need to install the dependency and then specify the tag matching it:
30
+
31
+ To use Typhoeus
32
+
33
+ @faraday_typhoeus
34
+ Scenario: do something with the API
35
+ Given I send and accept JSON
36
+ When I send a GET request to /users
37
+
38
+ To use Patron
39
+
40
+ @faraday_patron
41
+ Scenario: do something with the API
42
+ Given I send and accept JSON
43
+ When I send a GET request to /users
44
+
45
+ ### Remote testing
46
+
47
+ When you want to use this driver to test a remote application. You have to set the app_host:
48
+
49
+ Capybara.app_host = "http://www.yourapp.com"
50
+
51
+ Note that I haven't tested this case for my self yet. The Capybara tests pass for this situation though so it should work! Please provide me with feedback if it doesn't.
52
+
53
+ ## Running tests
54
+
55
+ For the tests to make sense, you need to simulate a remote server, simply add this line to your hosts file:
56
+
57
+ 127.0.0.1 capybara-testapp.heroku.com
58
+
59
+ Run bundler
60
+
61
+ bundle install
62
+
63
+ Then you are ready to run the test like so
64
+
65
+ rake spec
66
+
67
+ Caveats
68
+ -------
69
+
70
+ The focus being to provide a working driver to test APIs, not all Capybara goodness are implemented.
71
+ Everything related to submitting forms, clicking buttons, clicking checkboxes or javascript will not work with this driver.
72
+
73
+ Todo
74
+ ----
75
+
76
+ Note on Patches/Pull Requests
77
+ -----------------------------
78
+
79
+ * Fork the project.
80
+ * Make your feature addition or bug fix.
81
+ * Add tests for it. This is important so I don't break it in a
82
+ future version unintentionally.
83
+ * Commit, do not mess with rakefile, version, or history.
84
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
85
+ * Send me a pull request. Bonus points for topic branches.
86
+
87
+ Copyright
88
+ ---------
89
+ Copyright (c) 2010 Tron Jonathan and HALTER Joseph. See LICENSE for details.
@@ -0,0 +1,155 @@
1
+ require "typhoeus"
2
+
3
+ class Capybara::Driver::Typhoeus < Capybara::Driver::Base
4
+ class Node < Capybara::Driver::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_accessor :as, :follow
11
+ attr_reader :app, :rack_server, :options, :response
12
+
13
+ def client
14
+ @client ||= Typhoeus::Hydra.new
15
+ end
16
+
17
+ def initialize(app, options={})
18
+ @app = app
19
+ @options = options
20
+ @rack_server = Capybara::Server.new(@app)
21
+ @rack_server.boot if Capybara.run_server
22
+ end
23
+
24
+ def visit(url, params = {})
25
+ reset_cache
26
+ process :get, url, params
27
+ end
28
+
29
+ def get(url, params = {}, headers = {})
30
+ reset_cache
31
+ process :get, url, params, headers
32
+ end
33
+
34
+ def post(url, params = {}, headers = {})
35
+ reset_cache
36
+ process :post, url, params, headers
37
+ end
38
+
39
+ def put(url, params = {}, headers = {})
40
+ reset_cache
41
+ process :put, url, params, headers
42
+ end
43
+
44
+ def delete(url, params = {}, headers = {})
45
+ reset_cache
46
+ process :delete, url, params, headers
47
+ end
48
+
49
+ def head(url, params = {}, headers = {})
50
+ reset_cache
51
+ process :head, url, params, headers
52
+ end
53
+
54
+ def patch(url, params = {}, headers = {})
55
+ reset_cache
56
+ process :patch, url, params, headers
57
+ end
58
+
59
+ def request(url, params = {}, headers = {})
60
+ reset_cache
61
+ process :request, url, params, headers
62
+ end
63
+
64
+ def submit(method, path, attributes)
65
+ path = request.path if not path or path.empty?
66
+ process method.to_sym, path, attributes
67
+ end
68
+
69
+ def find(selector)
70
+ content_type = response_headers["Content-Type"]
71
+ case content_type.to_s[/\A[^;]+/]
72
+ when "application/xml", "text/xml"
73
+ xml
74
+ when "text/html"
75
+ html
76
+ else
77
+ # $stdout.puts "response: #{response.inspect}"
78
+ raise "Content-Type: #{content_type} is not handling xpath search"
79
+ end.xpath(selector).map { |node| Node.new(self, node) }
80
+ end
81
+
82
+ def html
83
+ @html ||= Nokogiri::HTML body
84
+ end
85
+
86
+ def xml
87
+ @xml ||= Nokogiri::XML body
88
+ end
89
+
90
+ def json
91
+ @json ||= ActiveSupport::JSON.decode body
92
+ end
93
+
94
+ def body
95
+ # $stdout.puts "body: #{response.body}"
96
+ response.body
97
+ end
98
+ alias_method :source, :body
99
+
100
+ def response_headers
101
+ response.headers_hash
102
+ end
103
+
104
+ def status_code
105
+ response.code
106
+ end
107
+
108
+ def current_url
109
+ @current_uri.to_s
110
+ end
111
+
112
+ def reset!
113
+ @client = nil
114
+ @response = nil
115
+ @current_uri = nil
116
+ reset_cache
117
+ end
118
+
119
+ def as
120
+ @as ||= "application/json"
121
+ end
122
+
123
+ def with
124
+ @with ||= {}
125
+ end
126
+
127
+ def process(method, path, params = {}, headers = {})
128
+ @current_uri = url path
129
+ opts = {
130
+ :method => method,
131
+ :headers => headers.merge("Content-Type" => as, "Accept" => as),
132
+ :timeout => 100,
133
+ }
134
+ opts[method==:get ? :params : :body] = params
135
+ # $stdout.puts "current_uri: #{@current_uri}"
136
+ # $stdout.puts "opts: #{opts.inspect}"
137
+ request = Typhoeus::Request.new @current_uri, opts
138
+ client.queue request
139
+ client.run
140
+ @response = request.response
141
+ end
142
+
143
+ def url(path)
144
+ rack_server.url(path)
145
+ end
146
+
147
+ private
148
+
149
+ def reset_cache
150
+ @xml = nil
151
+ @html = nil
152
+ @json = nil
153
+ end
154
+
155
+ end
@@ -0,0 +1,17 @@
1
+ require 'capybara/spec/test_app'
2
+
3
+ class ExtendedTestApp < TestApp
4
+ set :environment, :production
5
+
6
+ get %r{/redirect_to/(.*)} do
7
+ redirect params[:captures]
8
+ end
9
+
10
+ get '/host' do
11
+ "current host is #{request.host}"
12
+ end
13
+ end
14
+
15
+ if __FILE__ == $0
16
+ Rack::Handler::Mongrel.run ExtendedTestApp, :Port => 8070
17
+ end
@@ -0,0 +1,9 @@
1
+ module Capybara
2
+ module Driver
3
+ autoload :Typhoeus, 'capybara/driver/typhoeus_driver'
4
+ end
5
+ end
6
+
7
+ Capybara.register_driver :typhoeus do |app|
8
+ Capybara::Driver::Typhoeus.new(app)
9
+ end
@@ -0,0 +1,5 @@
1
+ require 'capybara/typhoeus'
2
+
3
+ Before('@typhoeus') do
4
+ Capybara.current_driver = :typhoeus
5
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Capybara::Driver::Typhoeus do
4
+ before do
5
+ @driver = Capybara::Driver::Typhoeus.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
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ module TestSessions
4
+ Typhoeus = Capybara::Session.new :typhoeus, TestApp
5
+ end
6
+
7
+ describe Capybara::Session do
8
+ context 'with typhoeus driver' do
9
+ before(:all) do
10
+ @session = TestSessions::Typhoeus
11
+ end
12
+
13
+ describe '#driver' do
14
+ it "should be a typhoeus driver" do
15
+ @session.driver.should be_an_instance_of Capybara::Driver::Typhoeus
16
+ end
17
+ end
18
+
19
+ describe '#mode' do
20
+ it "should remember the mode" do
21
+ @session.mode.should == :typhoeus
22
+ end
23
+ end
24
+
25
+ def extract_results(session)
26
+ YAML.load Nokogiri::HTML(session.body).xpath("//pre[@id='results']").first.text
27
+ end
28
+
29
+ after do
30
+ @session.reset!
31
+ end
32
+
33
+ describe '#app' do
34
+ it "should remember the application" do
35
+ @session.app.should == TestApp
36
+ end
37
+ end
38
+
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
47
+
48
+ describe '#body' do
49
+ it "should return the unmodified page body" do
50
+ @session.visit('/')
51
+ @session.body.should include('Hello world!')
52
+ end
53
+ end
54
+
55
+ describe '#source' do
56
+ it "should return the unmodified page source" do
57
+ @session.visit('/')
58
+ @session.source.should include('Hello world!')
59
+ end
60
+ 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_css"
71
+ it_should_behave_like "has_selector"
72
+ it_should_behave_like "has_xpath"
73
+ it_should_behave_like "has_link"
74
+ it_should_behave_like "has_button"
75
+ it_should_behave_like "has_field"
76
+ it_should_behave_like "has_select"
77
+ it_should_behave_like "has_table"
78
+ it_should_behave_like "current_url"
79
+ it_should_behave_like "session without javascript support"
80
+ it_should_behave_like "session with headers support"
81
+ it_should_behave_like "session with status code support"
82
+ end
83
+ end
@@ -0,0 +1,16 @@
1
+ require 'bundler/setup'
2
+ require 'capybara'
3
+ require 'capybara/typhoeus'
4
+ require 'capybara/spec/extended_test_app'
5
+ require 'capybara/spec/driver'
6
+ require 'capybara/spec/session'
7
+
8
+ alias :running :lambda
9
+
10
+ Capybara.default_wait_time = 0 # less timeout so tests run faster
11
+
12
+ RSpec.configure do |config|
13
+ config.after do
14
+ Capybara.default_selector = :xpath
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capybara-typhoeus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joseph HALTER
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-04-22 00:00:00.000000000 +02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: capybara
17
+ requirement: &2165218620 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 0.4.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2165218620
26
+ - !ruby/object:Gem::Dependency
27
+ name: typhoeus
28
+ requirement: &2165218020 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *2165218020
37
+ description: Typhoeus driver for Capybara, allowing testing of REST APIs
38
+ email: joseph.halter@thetalentbox.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - lib/capybara/driver/typhoeus_driver.rb
44
+ - lib/capybara/spec/extended_test_app.rb
45
+ - lib/capybara/typhoeus/cucumber.rb
46
+ - lib/capybara/typhoeus.rb
47
+ - spec/driver/typhoeus_driver_spec.rb
48
+ - spec/session/typhoeus_spec.rb
49
+ - spec/spec_helper.rb
50
+ - README.md
51
+ has_rdoc: true
52
+ homepage: https://github.com/TalentBox/capybara-typhoeus
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --charset=UTF-8
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.6.2
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Typhoeus driver for Capybara
77
+ test_files: []