capybara-json 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.0.3
2
+ * ENHANCEMENT
3
+ * creae :httpclient_json driver
4
+
1
5
  ## 0.0.2
2
6
  * ENHANCEMENT
3
7
  * support capybara < 1.0
data/README.md CHANGED
@@ -18,13 +18,17 @@ testing web application
18
18
  body #=> parsed json response
19
19
  source #=> raw response body
20
20
 
21
+ Capybara.current_driver = :httpclient_json
22
+
23
+ post 'http://example.jp/', { "this is" => "json" }
24
+ body #=> parsed json response
25
+ source #=> raw response body
26
+
21
27
  ## ROADMAP
22
28
 
23
29
  * 0.0.1
24
30
  * create :rack_test_json driver which supports normal json response (2xx, 3xx)
25
31
  * 0.0.3
26
- * create :httpclient driver for remote testing
27
- * 0.0.4
28
32
  * create :httpclient_json driver with the same interface with :rack_test_json in normal json response
29
33
  * 0.1.0
30
34
  * ensure :rack_test_json and :httpclient_json has the same interface in error response (4xx, 5xx)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
16
16
  s.require_paths = ["lib"]
17
17
 
18
18
  [ [ 'capybara' ],
19
+ [ 'httpclient' ],
19
20
  [ 'multi_json' ],
20
21
  ].each do |gem, version|
21
22
  s.add_runtime_dependency gem, version
@@ -0,0 +1,76 @@
1
+ require 'httpclient'
2
+
3
+ class Capybara::HTTPClientJson::Driver < Capybara::Driver::Base
4
+ attr_reader :app, :current_url, :rack_server, :response, :cookies
5
+
6
+ def client
7
+ unless @client
8
+ @client = HTTPClient.new
9
+ @client.follow_redirect_count = 5 + 1 # allows 5 redirection
10
+ @client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
11
+ end
12
+ @client
13
+ end
14
+
15
+ def initialize(app)
16
+ @app = app
17
+ @rack_server = Capybara::Server.new(@app)
18
+ @rack_server.boot if Capybara.run_server
19
+ end
20
+
21
+ def status_code
22
+ response.code
23
+ end
24
+
25
+ def source
26
+ response.body
27
+ end
28
+
29
+ def body
30
+ MultiJson.decode(source) || {}
31
+ end
32
+
33
+ def response_headers
34
+ response.headers
35
+ end
36
+
37
+ def get(url, params = {}, headers = {})
38
+ process :get, url, params, headers, true # follow_redirect
39
+ end
40
+ alias visit get
41
+
42
+ def post(url, json, headers = {})
43
+ json = MultiJson.encode(json) unless json.is_a?(String)
44
+ headers['Content-Type'] = "application/json; charset=#{json.encoding.to_s.downcase}"
45
+ process :post, url, json, headers, true # follow_redirect
46
+ end
47
+
48
+ def put(url, json, headers = {})
49
+ json = MultiJson.encode(json) unless json.is_a?(String)
50
+ headers['Content-Type'] = "application/json; charset=#{json.encoding.to_s.downcase}"
51
+ process :put, url, json, headers
52
+ end
53
+
54
+ def delete(url, params = {}, headers = {})
55
+ process :delete, url, params, headers
56
+ end
57
+
58
+ def reset!
59
+ @client = nil
60
+ end
61
+
62
+ protected
63
+ def process(method, path, params = {}, headers = {}, options = {})
64
+ @current_url = @rack_server.url(path)
65
+
66
+ begin
67
+ @response = client.__send__(method, @current_url, params, headers, options)
68
+ rescue HTTPClient::BadResponseError => e
69
+ raise Capybara::InfiniteRedirectError
70
+ end
71
+ end
72
+
73
+ def url_for(path)
74
+ path
75
+ end
76
+ end
data/lib/capybara/json.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  require 'capybara'
2
2
  require 'capybara/dsl'
3
3
 
4
+ require 'multi_json'
5
+
4
6
  module Capybara
5
7
  module Json
6
8
  def self.to_include
@@ -32,8 +34,16 @@ module Capybara
32
34
  module RackTestJson
33
35
  autoload :Driver, 'capybara/rack_test_json/driver'
34
36
  end
37
+
38
+ module HTTPClientJson
39
+ autoload :Driver, 'capybara/httpclient_json/driver'
40
+ end
35
41
  end
36
42
 
37
43
  Capybara.register_driver :rack_test_json do |app|
38
44
  Capybara::RackTestJson::Driver.new(app)
39
45
  end
46
+
47
+ Capybara.register_driver :httpclient_json do |app|
48
+ Capybara::HTTPClientJson::Driver.new(app)
49
+ end
@@ -1,5 +1,3 @@
1
- require 'multi_json'
2
-
3
1
  to_inherit = Capybara.const_defined?("RackTest") ? Capybara::RackTest::Driver : Capybara::Driver::RackTest
4
2
 
5
3
  class Capybara::RackTestJson::Driver < to_inherit
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ klass = Capybara::HTTPClientJson::Driver
4
+
5
+ describe klass do
6
+ before { @driver = described_class.new(JsonTestApp) }
7
+
8
+ it_should_behave_like 'driver'
9
+ it_should_behave_like 'driver with header support'
10
+ it_should_behave_like 'driver with status code support'
11
+ it_should_behave_like 'driver with cookies support'
12
+ it_should_behave_like 'driver with infinite redirect detection'
13
+
14
+ it_should_behave_like 'driver to post json'
15
+ it_should_behave_like 'driver to put json'
16
+ end
@@ -1,43 +1,45 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "to require 'capybara/json'" do
4
- it 'should register driver' do
5
- Capybara.drivers.should have_key(:rack_test_json)
6
- end
7
- end
8
-
9
- describe Capybara::Json do
10
- include described_class
11
-
12
- before(:all) do
13
- Capybara.app = JsonTestApp
14
- Capybara.current_driver = :rack_test_json
15
- end
16
-
17
- after(:all) do
18
- Capybara.app = nil
19
- Capybara.current_driver = Capybara.default_driver
20
- end
21
-
22
- %w[ get delete ].each do |method|
23
- it "register #{method}" do
24
- __send__(method, '/')
25
- body.should == { 'Hello world!' => 'Hello world!' }
3
+ [ :rack_test_json, :httpclient_json ].each do |driver|
4
+ describe "to require 'capybara/json'" do
5
+ it "should register driver #{driver}" do
6
+ Capybara.drivers.should have_key(driver)
26
7
  end
27
8
  end
28
9
 
29
- %w[ post put ].each do |method|
30
- it "register #{method}" do
31
- __send__(method, '/', {})
32
- body.should == { 'Hello world!' => 'Hello world!' }
10
+ describe Capybara::Json do
11
+ include described_class
12
+
13
+ before(:all) do
14
+ Capybara.app = JsonTestApp
15
+ Capybara.current_driver = driver
33
16
  end
34
-
35
- it "#{method} send json" do
36
- json = { "some" => "args" }
37
- __send__(method, '/env', json)
38
-
39
- body['content_type'].should =~ %r"application/json"
40
- body['rack.input'].should == MultiJson.encode(json)
17
+
18
+ after(:all) do
19
+ Capybara.app = nil
20
+ Capybara.current_driver = Capybara.default_driver
21
+ end
22
+
23
+ %w[ get delete ].each do |method|
24
+ it "register #{method}" do
25
+ __send__(method, '/')
26
+ body.should == { 'Hello world!' => 'Hello world!' }
27
+ end
28
+ end
29
+
30
+ %w[ post put ].each do |method|
31
+ it "register #{method}" do
32
+ __send__(method, '/', {})
33
+ body.should == { 'Hello world!' => 'Hello world!' }
34
+ end
35
+
36
+ it "#{method} send json" do
37
+ json = { "some" => "args" }
38
+ __send__(method, '/env', json)
39
+
40
+ body['content_type'].should =~ %r"application/json"
41
+ body['rack.input'].should == MultiJson.encode(json)
42
+ end
41
43
  end
42
44
  end
43
45
  end
@@ -1,8 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- # this code is written in capybara's spec/spec_helper
4
- alias :running :lambda
5
-
6
3
  klass = Capybara::RackTestJson::Driver
7
4
 
8
5
  describe klass do
@@ -1,5 +1,8 @@
1
1
  require 'capybara/spec/driver'
2
2
 
3
+ # this code is written in capybara's spec/spec_helper
4
+ alias :running :lambda
5
+
3
6
  [ 'driver', 'driver with header support' ].each do |shared|
4
7
  RSpec.world.shared_example_groups.delete(shared)
5
8
  end
@@ -49,7 +52,7 @@ end
49
52
  json = { :some => :args }
50
53
 
51
54
  @driver.__send__(method, '/env', json)
52
- @driver.body['content_length'].should == MultiJson.encode(json).length
55
+ @driver.body['content_length'].to_i.should == MultiJson.encode(json).length
53
56
  end
54
57
 
55
58
  it 'should post body' do
@@ -1,5 +1,22 @@
1
1
  require 'capybara/spec/test_app'
2
2
 
3
+ # for capybara < 1.0
4
+ module Capybara
5
+ class Server
6
+ class Identify
7
+ def call(env)
8
+ if env["PATH_INFO"] == "/__identify__"
9
+ #[200, {}, @app.object_id.to_s]
10
+ [200, {}, [ @app.object_id.to_s] ]
11
+ else
12
+ @app.call(env)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+
3
20
  class JsonTestApp < TestApp
4
21
  def invoke
5
22
  res = catch(:halt) { yield }
@@ -33,7 +50,7 @@ class JsonTestApp < TestApp
33
50
  hash[key.sub('HTTP_', '')] = value
34
51
  hash
35
52
  end
36
-
53
+
37
54
  envs.merge('params' => params, 'headers' => headers, 'rack.input' => env['rack.input'].string)
38
55
  end
39
56
  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.0.2
4
+ version: 0.0.3
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: 2011-12-10 00:00:00.000000000 Z
12
+ date: 2011-12-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capybara
16
- requirement: &74974930 !ruby/object:Gem::Requirement
16
+ requirement: &78122340 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *74974930
24
+ version_requirements: *78122340
25
+ - !ruby/object:Gem::Dependency
26
+ name: httpclient
27
+ requirement: &78121930 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *78121930
25
36
  - !ruby/object:Gem::Dependency
26
37
  name: multi_json
27
- requirement: &74974650 !ruby/object:Gem::Requirement
38
+ requirement: &78121580 !ruby/object:Gem::Requirement
28
39
  none: false
29
40
  requirements:
30
41
  - - ! '>='
@@ -32,10 +43,10 @@ dependencies:
32
43
  version: '0'
33
44
  type: :runtime
34
45
  prerelease: false
35
- version_requirements: *74974650
46
+ version_requirements: *78121580
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rspec
38
- requirement: &74974100 !ruby/object:Gem::Requirement
49
+ requirement: &78121140 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ~>
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: 2.8.0.rc
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *74974100
57
+ version_requirements: *78121140
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: sinatra
49
- requirement: &74956960 !ruby/object:Gem::Requirement
60
+ requirement: &78120910 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: '0'
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *74956960
68
+ version_requirements: *78120910
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: yajl-ruby
60
- requirement: &74956500 !ruby/object:Gem::Requirement
71
+ requirement: &78120570 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ! '>='
@@ -65,10 +76,10 @@ dependencies:
65
76
  version: '0'
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *74956500
79
+ version_requirements: *78120570
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: autowatchr
71
- requirement: &74956200 !ruby/object:Gem::Requirement
82
+ requirement: &78120320 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ! '>='
@@ -76,10 +87,10 @@ dependencies:
76
87
  version: '0'
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *74956200
90
+ version_requirements: *78120320
80
91
  - !ruby/object:Gem::Dependency
81
92
  name: rake
82
- requirement: &74955670 !ruby/object:Gem::Requirement
93
+ requirement: &78120060 !ruby/object:Gem::Requirement
83
94
  none: false
84
95
  requirements:
85
96
  - - ! '>='
@@ -87,7 +98,7 @@ dependencies:
87
98
  version: '0'
88
99
  type: :development
89
100
  prerelease: false
90
- version_requirements: *74955670
101
+ version_requirements: *78120060
91
102
  description: for testing json-api with capybara
92
103
  email:
93
104
  - okitakunio@gmail.com
@@ -108,8 +119,10 @@ files:
108
119
  - VERSION
109
120
  - capybara-json.gemspec
110
121
  - lib/capybara-json.rb
122
+ - lib/capybara/httpclient_json/driver.rb
111
123
  - lib/capybara/json.rb
112
124
  - lib/capybara/rack_test_json/driver.rb
125
+ - spec/capybara/httpclient_json/driver_spec.rb
113
126
  - spec/capybara/json_spec.rb
114
127
  - spec/capybara/rack_test_json/driver_spec.rb
115
128
  - spec/spec.watchr
@@ -141,6 +154,7 @@ signing_key:
141
154
  specification_version: 3
142
155
  summary: for testing json-api with capybara
143
156
  test_files:
157
+ - spec/capybara/httpclient_json/driver_spec.rb
144
158
  - spec/capybara/json_spec.rb
145
159
  - spec/capybara/rack_test_json/driver_spec.rb
146
160
  - spec/spec.watchr