capybara-json 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
+ ## 0.1.1
2
+ * ENHANCEMENT
3
+ * get!, post!, put!, delete!
4
+ * BUGFIX
5
+ * ensure the same interface sending header to app
6
+
1
7
  ## 0.1.0
2
- * gurantee the same interface in error response
8
+ * ENHANCEMENT
9
+ * gurantee the same interface in error response
3
10
 
4
11
  ## 0.0.3
5
12
  * ENHANCEMENT
data/README.md CHANGED
@@ -17,20 +17,16 @@ testing web application
17
17
  post '/', { "this is" => "json" } # POST '/'
18
18
  body #=> parsed json response
19
19
  source #=> raw response body
20
+ get '/errors/400'
21
+ status_code #=> 400
22
+ get! '/errors' #=> raise Capybara::Json::Error
20
23
 
21
24
  Capybara.current_driver = :httpclient_json
22
25
  Capybara.app_host = 'http://example.com'
23
26
  post '/', { "this is" => "json" } # POST 'http://example.com/'
24
27
  body #=> parsed json response
25
28
  source #=> raw response body
29
+ get '/errors/400'
30
+ status_code #=> 400
31
+ get! '/errors' #=> raise Capybara::Json::Error
26
32
 
27
- ## ROADMAP
28
-
29
- * 0.0.1
30
- * create :rack_test_json driver which supports normal json response (2xx, 3xx)
31
- * 0.0.3
32
- * create :httpclient_json driver with the same interface with :rack_test_json in normal json response
33
- * 0.1.0
34
- * ensure :rack_test_json and :httpclient_json has the same interface in error response (4xx, 5xx)
35
- * 0.2.0
36
- * add jsonpath? interface to search response
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -54,6 +54,22 @@ class Capybara::HTTPClientJson::Driver < Capybara::Driver::Base
54
54
  def delete(url, params = {}, headers = {})
55
55
  process :delete, url, params, headers
56
56
  end
57
+
58
+ %w[ get delete ].each do |method|
59
+ class_eval %{
60
+ def #{method}!(url, params = {}, env = {})
61
+ handle_error { #{method}(url, params, env) }
62
+ end
63
+ }
64
+ end
65
+
66
+ %w[ post put ].each do |method|
67
+ class_eval %{
68
+ def #{method}!(url, json, headers = {})
69
+ handle_error { #{method}(url, json, headers) }
70
+ end
71
+ }
72
+ end
57
73
 
58
74
  def reset!
59
75
  @client = nil
@@ -73,4 +89,9 @@ class Capybara::HTTPClientJson::Driver < Capybara::Driver::Base
73
89
  end
74
90
  end
75
91
  end
92
+
93
+ def handle_error(&block)
94
+ yield
95
+ raise(Capybara::Json::Error, response) if status_code >= 400
96
+ end
76
97
  end
@@ -0,0 +1,6 @@
1
+ module Capybara
2
+ module Json
3
+ class Error < Exception
4
+ end
5
+ end
6
+ end
data/lib/capybara/json.rb CHANGED
@@ -29,6 +29,8 @@ module Capybara
29
29
  end
30
30
  }
31
31
  end
32
+
33
+ autoload :Error, 'capybara/json/error'
32
34
  end
33
35
 
34
36
  module RackTestJson
@@ -5,19 +5,51 @@ class Capybara::RackTestJson::Driver < to_inherit
5
5
  MultiJson.decode(source) || {}
6
6
  end
7
7
 
8
+ %w[ get delete ].each do |method|
9
+ class_eval %{
10
+ def #{method}(path, params = {}, env = {})
11
+ super(path, params, env_for_rack(env))
12
+ end
13
+
14
+ def #{method}!(path, params = {}, env = {})
15
+ handle_error { #{method}(path, params, env) }
16
+ end
17
+ }
18
+ end
19
+
8
20
  %w[ post put ].each do |method|
9
21
  class_eval %{
10
22
  def #{method}(path, json, env = {})
11
23
  json = MultiJson.encode(json) unless json.is_a?(String)
12
-
24
+
13
25
  request_env = {
14
26
  'CONTENT_LENGTH' => json.size,
15
27
  'CONTENT_TYPE' => "application/json; charset=\#{json.encoding.to_s.downcase}",
16
28
  'rack.input' => StringIO.new(json)
17
- }.merge(env)
29
+ }.merge(env_for_rack(env))
18
30
 
19
31
  super(path, {}, request_env)
20
32
  end
33
+
34
+ def #{method}!(path, json, env = {})
35
+ handle_error { #{method}(path, json, env) }
36
+ end
21
37
  }
22
38
  end
39
+
40
+ protected
41
+ def env_for_rack(env)
42
+ env.inject({}) do |rack_env, (key, value)|
43
+ env_key = key.upcase.gsub('-', '_')
44
+ env_key = "HTTP_" + env_key unless env_key == "CONTENT_TYPE"
45
+ rack_env[env_key] = value
46
+
47
+ rack_env
48
+ end
49
+ end
50
+
51
+ def handle_error(&block)
52
+ yield
53
+ raise(Capybara::Json::Error, response) if status_code >= 400
54
+ end
23
55
  end
@@ -7,6 +7,7 @@ describe klass do
7
7
 
8
8
  it_should_behave_like 'driver'
9
9
  it_should_behave_like 'driver with header support'
10
+ it_should_behave_like 'driver with custom header support'
10
11
  it_should_behave_like 'driver with status code support'
11
12
  it_should_behave_like 'driver with cookies support'
12
13
  it_should_behave_like 'driver with infinite redirect detection'
@@ -7,6 +7,7 @@ describe klass do
7
7
 
8
8
  it_should_behave_like 'driver'
9
9
  it_should_behave_like 'driver with header support'
10
+ it_should_behave_like 'driver with custom header support'
10
11
  it_should_behave_like 'driver with status code support'
11
12
  it_should_behave_like 'driver with cookies support'
12
13
  it_should_behave_like 'driver with infinite redirect detection'
@@ -40,6 +40,13 @@ shared_examples_for 'driver with header support' do
40
40
  end
41
41
  end
42
42
 
43
+ shared_examples_for 'driver with custom header support' do
44
+ it "should send custom header" do
45
+ @driver.get('/env', {}, { 'X-Custom-Header' => 'custom header'})
46
+ @driver.body['headers']['X_CUSTOM_HEADER'].should == 'custom header'
47
+ end
48
+ end
49
+
43
50
 
44
51
  %w[ post put ].each do |method|
45
52
  shared_examples_for "driver to #{method} json" do
@@ -83,6 +90,10 @@ shared_examples_for 'driver for client error' do
83
90
  @driver.get('/errors/400')
84
91
  @driver.body['status_code'].should == 400
85
92
  end
93
+
94
+ it 'should raise error using bang!' do
95
+ expect { @driver.get!('/errors/400') }.should raise_exception(Capybara::Json::Error)
96
+ end
86
97
  end
87
98
 
88
99
  shared_examples_for 'driver for server error' do
@@ -94,4 +105,8 @@ shared_examples_for 'driver for server error' do
94
105
  @driver.get('/errors/500')
95
106
  @driver.status_code.should == 500
96
107
  end
108
+
109
+ it 'should raise error using bang!' do
110
+ expect { @driver.get!('/errors/500') }.should raise_exception(Capybara::Json::Error)
111
+ end
97
112
  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.0
4
+ version: 0.1.1
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-15 00:00:00.000000000 Z
12
+ date: 2011-12-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capybara
16
- requirement: &79508610 !ruby/object:Gem::Requirement
16
+ requirement: &74677520 !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: *79508610
24
+ version_requirements: *74677520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: httpclient
27
- requirement: &79508060 !ruby/object:Gem::Requirement
27
+ requirement: &74676990 !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: *79508060
35
+ version_requirements: *74676990
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: multi_json
38
- requirement: &79507280 !ruby/object:Gem::Requirement
38
+ requirement: &74676250 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *79507280
46
+ version_requirements: *74676250
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &79506490 !ruby/object:Gem::Requirement
49
+ requirement: &74675420 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 2.8.0.rc
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *79506490
57
+ version_requirements: *74675420
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: sinatra
60
- requirement: &79505920 !ruby/object:Gem::Requirement
60
+ requirement: &74674860 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *79505920
68
+ version_requirements: *74674860
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yajl-ruby
71
- requirement: &79505530 !ruby/object:Gem::Requirement
71
+ requirement: &74674450 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *79505530
79
+ version_requirements: *74674450
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: autowatchr
82
- requirement: &79505310 !ruby/object:Gem::Requirement
82
+ requirement: &74674220 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *79505310
90
+ version_requirements: *74674220
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rake
93
- requirement: &79505050 !ruby/object:Gem::Requirement
93
+ requirement: &74673970 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *79505050
101
+ version_requirements: *74673970
102
102
  description: for testing json-api with capybara
103
103
  email:
104
104
  - okitakunio@gmail.com
@@ -121,6 +121,7 @@ files:
121
121
  - lib/capybara-json.rb
122
122
  - lib/capybara/httpclient_json/driver.rb
123
123
  - lib/capybara/json.rb
124
+ - lib/capybara/json/error.rb
124
125
  - lib/capybara/rack_test_json/driver.rb
125
126
  - spec/capybara/httpclient_json/driver_spec.rb
126
127
  - spec/capybara/json_spec.rb