capybara-json 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rbenv-gemsets ADDED
@@ -0,0 +1 @@
1
+ capybara-json
data/README.md CHANGED
@@ -1,13 +1,12 @@
1
- # capybara-json [![Build Status](https://secure.travis-ci.org/okitan/capybara-json.png)](http://travis-ci.org/okitan/capybara-json)
1
+ # capybara-json [![Build Status](https://secure.travis-ci.org/okitan/capybara-json.png?branch=master)](http://travis-ci.org/okitan/capybara-json)
2
2
 
3
3
  testing ruby: 1.9.2, 1.9.3 and ruby-head; Capybara: < 1.0 and > 1.0
4
4
 
5
5
  ## About capybara-json
6
6
 
7
- capybara-json provides the same interface to testing JSON API (both local and remote)
7
+ capybara-json provides the same interface to testing JSON API (both local and remote)
8
8
 
9
9
  Capybara is an acceptance test framework, and it has no interest with client error(4xx response).
10
- testing web application
11
10
 
12
11
  ## USAGE
13
12
  require 'capybara/json'
@@ -29,4 +28,3 @@ testing web application
29
28
  get '/errors/400'
30
29
  status_code #=> 400
31
30
  get! '/errors' #=> raise Capybara::Json::Error
32
-
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
@@ -15,19 +15,19 @@ Gem::Specification.new do |s|
15
15
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
16
  s.require_paths = ["lib"]
17
17
 
18
- [ [ 'capybara' ],
19
- [ 'httpclient' ],
20
- [ 'multi_json' ],
21
- ].each do |gem, version|
22
- s.add_runtime_dependency gem, version
23
- end
18
+ s.add_runtime_dependency "capybara"
19
+ s.add_runtime_dependency "httpclient"
20
+ s.add_runtime_dependency "multi_json"
24
21
 
25
- [ [ 'rspec', '~> 2.8.0.rc' ],
26
- [ 'sinatra' ],
27
- [ 'yajl-ruby' ],
28
- [ 'autowatchr' ],
29
- [ 'rake' ],
30
- ].each do |gem, version|
31
- s.add_development_dependency gem, version
32
- end
22
+ # for testing
23
+ s.add_development_dependency "rake"
24
+ s.add_development_dependency "rspec", "~> 2.8.0"
25
+ s.add_development_dependency "sinatra"
26
+ s.add_development_dependency "thin"
27
+ s.add_development_dependency "yajl-ruby"
28
+ s.add_development_dependency "autowatchr"
29
+
30
+ # for debug
31
+ s.add_development_dependency "pry"
32
+ s.add_development_dependency "tapp"
33
33
  end
@@ -1,6 +1,6 @@
1
1
  require 'httpclient'
2
2
 
3
- class Capybara::HTTPClientJson::Driver < Capybara::Driver::Base
3
+ class Capybara::HTTPClientJson::Driver < Capybara::Json::Driver::Base
4
4
  attr_reader :app, :current_url, :rack_server, :response, :cookies
5
5
 
6
6
  def client
@@ -8,6 +8,13 @@ class Capybara::HTTPClientJson::Driver < Capybara::Driver::Base
8
8
  @client = HTTPClient.new
9
9
  @client.follow_redirect_count = 5 + 1 # allows 5 redirection
10
10
  @client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
11
+
12
+ # hack for redirect
13
+ def @client.redirect_uri_callback_to_keep_new_uri(uri, res)
14
+ new_uri = default_redirect_uri_callback(uri, res)
15
+ @new_uri = new_uri
16
+ end
17
+ @client.redirect_uri_callback = @client.method(:redirect_uri_callback_to_keep_new_uri)
11
18
  end
12
19
  @client
13
20
  end
@@ -70,7 +77,7 @@ class Capybara::HTTPClientJson::Driver < Capybara::Driver::Base
70
77
  end
71
78
  }
72
79
  end
73
-
80
+
74
81
  def reset!
75
82
  @client = nil
76
83
  end
@@ -81,6 +88,12 @@ class Capybara::HTTPClientJson::Driver < Capybara::Driver::Base
81
88
 
82
89
  begin
83
90
  @response = client.__send__(method, @current_url, params, headers, options)
91
+
92
+ # hack for redirect
93
+ if new_uri = client.instance_variable_get(:@new_uri)
94
+ @current_url = new_uri.to_s
95
+ client.instance_variable_set(:@new_uri, nil)
96
+ end
84
97
  rescue HTTPClient::BadResponseError => e
85
98
  if e.message == "retry count exceeded"
86
99
  raise Capybara::InfiniteRedirectError
@@ -0,0 +1,13 @@
1
+ module Capybara::Json::Driver
2
+ class Base
3
+ %w[ get post put delete get! post! put! delete!
4
+ reset!
5
+ current_url
6
+ source body status_headers status_code
7
+ ].each do |method|
8
+ define_method(method) do
9
+ raise NotImplementedError
10
+ end
11
+ end
12
+ end
13
+ end
data/lib/capybara/json.rb CHANGED
@@ -31,10 +31,15 @@ module Capybara
31
31
  end
32
32
 
33
33
  autoload :Error, 'capybara/json/error'
34
+
35
+ module Driver
36
+ autoload :Base, "capybara/json/driver/base"
37
+ end
34
38
  end
35
39
 
36
40
  module RackTestJson
37
41
  autoload :Driver, 'capybara/rack_test_json/driver'
42
+ autoload :Client, "capybara/rack_test_json/client"
38
43
  end
39
44
 
40
45
  module HTTPClientJson
@@ -0,0 +1,37 @@
1
+ require 'rack/test'
2
+
3
+ module Capybara::RackTestJson
4
+ class Client
5
+ include Rack::Test::Methods
6
+
7
+ attr_accessor :app
8
+
9
+ def initialize(app)
10
+ @app = app
11
+ end
12
+
13
+ %w[ get post put delete ].each do |method|
14
+ module_eval %{
15
+ def #{method}(uri, params = {}, env = {}, &block)
16
+ env.merge(:method => "#{method.upcase}", :params => params)
17
+ request_with_follow_redirect(uri, env, &block)
18
+ end
19
+ }
20
+ end
21
+
22
+ def request_with_follow_redirect(uri, env)
23
+ request(uri, env)
24
+ (5+1).times do
25
+ if last_response.redirect?
26
+ request(last_response["Location"], env.merge(:method => "GET"))
27
+ else
28
+ return
29
+ end
30
+ end
31
+
32
+ # 6 times redirect cause InfiniteRedirectError
33
+ raise Capybara::InfiniteRedirectError,
34
+ "redirected more than 5 times, check for infinite redirects."
35
+ end
36
+ end
37
+ end
@@ -1,14 +1,24 @@
1
- to_inherit = Capybara.const_defined?("RackTest") ? Capybara::RackTest::Driver : Capybara::Driver::RackTest
1
+ class Capybara::RackTestJson::Driver < Capybara::Json::Driver::Base
2
+ def initialize(app)
3
+ @app = app
4
+ end
2
5
 
3
- class Capybara::RackTestJson::Driver < to_inherit
4
- def body
5
- MultiJson.decode(source) || {}
6
+ def client
7
+ @client ||= Capybara::RackTestJson::Client.new(@app)
8
+ end
9
+
10
+ def last_request
11
+ client.last_request
12
+ end
13
+
14
+ def last_response
15
+ client.last_response
6
16
  end
7
17
 
8
18
  %w[ get delete ].each do |method|
9
19
  class_eval %{
10
20
  def #{method}(path, params = {}, env = {})
11
- super(path, params, env_for_rack(env))
21
+ client.#{method}(path, params, env_for_rack(env))
12
22
  end
13
23
 
14
24
  def #{method}!(path, params = {}, env = {})
@@ -16,6 +26,7 @@ class Capybara::RackTestJson::Driver < to_inherit
16
26
  end
17
27
  }
18
28
  end
29
+ alias visit get
19
30
 
20
31
  %w[ post put ].each do |method|
21
32
  class_eval %{
@@ -24,11 +35,11 @@ class Capybara::RackTestJson::Driver < to_inherit
24
35
 
25
36
  request_env = {
26
37
  'CONTENT_LENGTH' => json.size,
27
- 'CONTENT_TYPE' => "application/json; charset=\#{json.encoding.to_s.downcase}",
38
+ 'CONTENT_TYPE' => "application/json; charset=\#{json.encoding.to_s.downcase}",
28
39
  'rack.input' => StringIO.new(json)
29
40
  }.merge(env_for_rack(env))
30
-
31
- super(path, {}, request_env)
41
+
42
+ client.#{method}(path, {}, request_env)
32
43
  end
33
44
 
34
45
  def #{method}!(path, json, env = {})
@@ -37,6 +48,30 @@ class Capybara::RackTestJson::Driver < to_inherit
37
48
  }
38
49
  end
39
50
 
51
+ def current_url
52
+ last_request.url
53
+ end
54
+
55
+ def source
56
+ last_response.body
57
+ end
58
+
59
+ def body
60
+ MultiJson.decode(source) || {}
61
+ end
62
+
63
+ def response_headers
64
+ last_response.headers
65
+ end
66
+
67
+ def status_code
68
+ last_response.status
69
+ end
70
+
71
+ def reset!
72
+ @client = nil
73
+ end
74
+
40
75
  protected
41
76
  def env_for_rack(env)
42
77
  env.inject({}) do |rack_env, (key, value)|
@@ -50,6 +85,6 @@ class Capybara::RackTestJson::Driver < to_inherit
50
85
 
51
86
  def handle_error(&block)
52
87
  yield
53
- raise(Capybara::Json::Error, response) if status_code >= 400
88
+ raise(Capybara::Json::Error, last_response) if status_code >= 400
54
89
  end
55
90
  end
@@ -10,6 +10,7 @@ describe klass do
10
10
  it_should_behave_like 'driver with custom header support'
11
11
  it_should_behave_like 'driver with status code support'
12
12
  it_should_behave_like 'driver with cookies support'
13
+ it_should_behave_like "driver with redirect support"
13
14
  it_should_behave_like 'driver with infinite redirect detection'
14
15
 
15
16
  it_should_behave_like 'driver to post json'
@@ -10,6 +10,7 @@ describe klass do
10
10
  it_should_behave_like 'driver with custom header support'
11
11
  it_should_behave_like 'driver with status code support'
12
12
  it_should_behave_like 'driver with cookies support'
13
+ it_should_behave_like "driver with redirect support"
13
14
  it_should_behave_like 'driver with infinite redirect detection'
14
15
 
15
16
  it_should_behave_like 'driver to post json'
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  ROOT = File.dirname(__FILE__)
2
2
  $: << File.expand_path('../lib', ROOT)
3
3
 
4
+ require "pry"
5
+
4
6
  require 'capybara/json'
5
7
  Dir[File.expand_path("support/**/*.rb", ROOT)].each {|f| require f }
6
8
 
@@ -47,6 +47,12 @@ shared_examples_for 'driver with custom header support' do
47
47
  end
48
48
  end
49
49
 
50
+ shared_examples_for "driver with redirect support" do
51
+ it "should update current_url" do
52
+ @driver.get "/redirect"
53
+ URI.parse(@driver.current_url).path.should == "/landed"
54
+ end
55
+ end
50
56
 
51
57
  %w[ post put ].each do |method|
52
58
  shared_examples_for "driver to #{method} json" do
@@ -54,17 +60,17 @@ end
54
60
  @driver.__send__(method, '/env', {})
55
61
  @driver.body['content_type'].should =~ %r"^application/json"
56
62
  end
57
-
63
+
58
64
  it 'should set content length' do
59
65
  json = { :some => :args }
60
-
66
+
61
67
  @driver.__send__(method, '/env', json)
62
68
  @driver.body['content_length'].to_i.should == MultiJson.encode(json).length
63
69
  end
64
-
70
+
65
71
  it 'should post body' do
66
72
  json = { :some => :args }
67
-
73
+
68
74
  @driver.__send__(method, '/env', json)
69
75
  @driver.body['rack.input'].should == MultiJson.encode(json)
70
76
  end
@@ -13,7 +13,7 @@ module Capybara
13
13
  end
14
14
  end
15
15
  end
16
- end
16
+ end
17
17
  end
18
18
 
19
19
 
@@ -27,7 +27,7 @@ class JsonTestApp < TestApp
27
27
  status(res.shift)
28
28
  body(res.pop)
29
29
  headers(*res)
30
- elsif res.respond_to? :each
30
+ elsif res.respond_to? :each
31
31
  content_type :json
32
32
  body res
33
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-json
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-26 00:00:00.000000000 Z
12
+ date: 2012-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capybara
16
- requirement: &81687960 !ruby/object:Gem::Requirement
16
+ requirement: &70155556441460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *81687960
24
+ version_requirements: *70155556441460
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: httpclient
27
- requirement: &81687570 !ruby/object:Gem::Requirement
27
+ requirement: &70155556457240 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *81687570
35
+ version_requirements: *70155556457240
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: multi_json
38
- requirement: &81686880 !ruby/object:Gem::Requirement
38
+ requirement: &70155556456800 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,21 +43,43 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *81686880
46
+ version_requirements: *70155556456800
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &70155556456360 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70155556456360
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: rspec
49
- requirement: &81685970 !ruby/object:Gem::Requirement
60
+ requirement: &70155556455820 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ~>
53
64
  - !ruby/object:Gem::Version
54
- version: 2.8.0.rc
65
+ version: 2.8.0
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *81685970
68
+ version_requirements: *70155556455820
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: sinatra
60
- requirement: &81685370 !ruby/object:Gem::Requirement
71
+ requirement: &70155556455400 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70155556455400
80
+ - !ruby/object:Gem::Dependency
81
+ name: thin
82
+ requirement: &70155556454860 !ruby/object:Gem::Requirement
61
83
  none: false
62
84
  requirements:
63
85
  - - ! '>='
@@ -65,10 +87,10 @@ dependencies:
65
87
  version: '0'
66
88
  type: :development
67
89
  prerelease: false
68
- version_requirements: *81685370
90
+ version_requirements: *70155556454860
69
91
  - !ruby/object:Gem::Dependency
70
92
  name: yajl-ruby
71
- requirement: &81684950 !ruby/object:Gem::Requirement
93
+ requirement: &70155556454000 !ruby/object:Gem::Requirement
72
94
  none: false
73
95
  requirements:
74
96
  - - ! '>='
@@ -76,10 +98,10 @@ dependencies:
76
98
  version: '0'
77
99
  type: :development
78
100
  prerelease: false
79
- version_requirements: *81684950
101
+ version_requirements: *70155556454000
80
102
  - !ruby/object:Gem::Dependency
81
103
  name: autowatchr
82
- requirement: &81684710 !ruby/object:Gem::Requirement
104
+ requirement: &70155556452980 !ruby/object:Gem::Requirement
83
105
  none: false
84
106
  requirements:
85
107
  - - ! '>='
@@ -87,10 +109,21 @@ dependencies:
87
109
  version: '0'
88
110
  type: :development
89
111
  prerelease: false
90
- version_requirements: *81684710
112
+ version_requirements: *70155556452980
91
113
  - !ruby/object:Gem::Dependency
92
- name: rake
93
- requirement: &81684470 !ruby/object:Gem::Requirement
114
+ name: pry
115
+ requirement: &70155556452080 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *70155556452080
124
+ - !ruby/object:Gem::Dependency
125
+ name: tapp
126
+ requirement: &70155556451560 !ruby/object:Gem::Requirement
94
127
  none: false
95
128
  requirements:
96
129
  - - ! '>='
@@ -98,7 +131,7 @@ dependencies:
98
131
  version: '0'
99
132
  type: :development
100
133
  prerelease: false
101
- version_requirements: *81684470
134
+ version_requirements: *70155556451560
102
135
  description: for testing json-api with capybara
103
136
  email:
104
137
  - okitakunio@gmail.com
@@ -107,6 +140,7 @@ extensions: []
107
140
  extra_rdoc_files: []
108
141
  files:
109
142
  - .gitignore
143
+ - .rbenv-gemsets
110
144
  - .rspec
111
145
  - .rvmrc
112
146
  - .rvmrc.capybara.lt.1.0
@@ -121,7 +155,9 @@ files:
121
155
  - lib/capybara-json.rb
122
156
  - lib/capybara/httpclient_json/driver.rb
123
157
  - lib/capybara/json.rb
158
+ - lib/capybara/json/driver/base.rb
124
159
  - lib/capybara/json/error.rb
160
+ - lib/capybara/rack_test_json/client.rb
125
161
  - lib/capybara/rack_test_json/driver.rb
126
162
  - spec/capybara/httpclient_json/driver_spec.rb
127
163
  - spec/capybara/json_spec.rb
@@ -150,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
186
  version: '0'
151
187
  requirements: []
152
188
  rubyforge_project: capybara-json
153
- rubygems_version: 1.8.10
189
+ rubygems_version: 1.8.11
154
190
  signing_key:
155
191
  specification_version: 3
156
192
  summary: for testing json-api with capybara