api_matchers 0.1.1 → 0.2.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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Mjc1NTQ2NTNjYTFlNTc1OWZhMmFlZTVlOTBjNTc3ODhjYzdmZGY1Mw==
5
+ data.tar.gz: !binary |-
6
+ MjlhYzlkMDRhZjA5YmU3YWExNTZlNTY3MTdhNzBhODFhOTA2OGJmOA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YjViMzY4MjgxNmI1NjI3MmI3MDAwZWNjMzQwYzJhMzhjNWU4MTc2YjNmYTVk
10
+ Y2JhMGZmNWRhYTRiN2Q0OTQ4NjdlZDlmNDMwNjEzMzU5NGY1NzNiOGU2MjE0
11
+ YThhZmZlM2U3NTNkNDcwNmMyZTJmM2U4NTI0ODViYzA4ZWMwNTc=
12
+ data.tar.gz: !binary |-
13
+ NDYzYThkMzYwYzE2YTlkM2MzY2FiOWMyOWJlZGM2MGVjZTU4MmQ3OTYzM2Zm
14
+ ODgyY2NiMmMwNGJlY2Q3MWQyNTAzZWMxNTg2ZTIwYmU3YzYxNWI1NjNkMWY3
15
+ ODE0YTIwZjk2YjAzOGJjYTc0YmU5MDkwYmU2ODc0YmU4YTMzYWQ=
data/.gitignore CHANGED
@@ -15,4 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
- *.DS_Store
18
+ *.DS_Store
19
+ .rvmrc
File without changes
data/README.markdown CHANGED
@@ -2,19 +2,25 @@
2
2
 
3
3
  Collection of RSpec matchers for create your API.
4
4
 
5
- ## Matchers
5
+ ## Response Body Matchers
6
6
 
7
- * be_in_xml
8
- * be_a_json
7
+ * have_node
8
+ * have_json_node
9
+ * have_xml_node
10
+
11
+ # Response Status Matchers
12
+
13
+ * be_ok_
9
14
  * create_resource
10
15
  * be_a_bad_request
11
16
  * be_unauthorized
12
17
  * be_internal_server_error
13
- * have_node
14
- * have_json_node
15
- * have_xml_node
16
- * be_json_eql
17
- * be_xml_eql
18
+ * be_not_found
19
+
20
+ # Other Matcher
21
+
22
+ * be_in_xml
23
+ * be_a_json
18
24
 
19
25
  ## Install
20
26
 
@@ -40,17 +46,17 @@ The have_node matcher parse the actual and see if have the expcted node with the
40
46
  You can verify if node exists:
41
47
 
42
48
  ```ruby
43
- "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:transaction)
49
+ '{ "transaction": { "id": 54, "status": "paid" } }'.should have_node(:transaction)
44
50
  ```
45
51
 
46
52
  Or if node exist with a value:
47
53
 
48
54
  ```ruby
49
- "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:id).with(54)
55
+ '{ "transaction": { "id": 54, "status": "paid" } }'.should have_node(:id).with(54)
50
56
  ```
51
57
 
52
58
  ```ruby
53
- "{ 'error': 'not_authorized', 'transaction': { 'id': '55' } }".should have_node(:error).with('not_authorized')
59
+ '{ "error": "not_authorized" }'.should have_node(:error).with('not_authorized')
54
60
  ```
55
61
 
56
62
  ```ruby
@@ -71,13 +77,13 @@ You can verify boolean values too:
71
77
 
72
78
  ### HAVE NODE Matcher Configuration
73
79
 
74
- You can configure if you want xml(**JSON is the default**):
80
+ You can configure if you want xml (JSON is the default):
75
81
 
76
82
  ```ruby
77
83
  APIMatchers.setup do |config|
78
84
  config.content_type = :xml
79
85
  end
80
- ```ruby
86
+ ```
81
87
 
82
88
  ```ruby
83
89
  '<transaction><id>200</id><status>paid</status></transaction>'.should have_node(:status)
@@ -113,7 +119,7 @@ You can configure the name of the method for example:
113
119
  ```ruby
114
120
  ## YOU can do this
115
121
  APIMatchers.setup do |config|
116
- config.body_method = :body
122
+ config.response_body_method = :body
117
123
  end
118
124
  ```
119
125
 
@@ -126,7 +132,7 @@ Then you can use *without* call the **#body** method:
126
132
  ### Have JSON Node Matcher
127
133
 
128
134
  ```ruby
129
- "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_json_node(:id).with(54)
135
+ '{ "transaction": { "id": 54, "status": "paid" } }'.should have_json_node(:id).with(54)
130
136
  ```
131
137
 
132
138
  ### Have XML Node Matcher
@@ -188,10 +194,12 @@ Then you can use without call the **#status** method:
188
194
 
189
195
  This configurations affects this matchers:
190
196
 
197
+ * be_ok
191
198
  * create_resource
192
199
  * be_a_bad_request
193
200
  * be_internal_server_error
194
201
  * be_unauthorized
202
+ * be_not_found
195
203
 
196
204
  ### Be in XML Matcher
197
205
 
@@ -233,4 +241,4 @@ Then you can use without call the **#headers** calling the **#['Content-Type']**
233
241
 
234
242
  ### Contributors
235
243
 
236
- * Stephen Orens
244
+ * Stephen Orens
data/TODO.markdown ADDED
@@ -0,0 +1,2 @@
1
+ * Refactor Core::FindInJSON#find method.
2
+ * Adding some include matcher about node arrays.
@@ -1,8 +1,11 @@
1
1
  module APIMatchers
2
2
  module Core
3
- module Exceptions
4
- class KeyNotFound < StandardError
5
- end
6
- end
3
+ module Exceptions
4
+ class KeyNotFound < StandardError
5
+ end
6
+
7
+ class InvalidJSON < StandardError
8
+ end
9
+ end
7
10
  end
8
11
  end
@@ -23,6 +23,7 @@ module APIMatchers
23
23
 
24
24
  return value if value == expected_value or expected_value.nil?
25
25
  end
26
+
26
27
  # do we have more to recurse through?
27
28
  keep_going = nil
28
29
  if value.is_a? Hash or value.is_a? Array
@@ -5,6 +5,10 @@ module APIMatchers
5
5
  end
6
6
  alias :be_a_bad_request :be_bad_request
7
7
 
8
+ def be_not_found
9
+ ::APIMatchers::HTTPStatusCode::BeNotFound.new(::APIMatchers::Core::Setup)
10
+ end
11
+
8
12
  def be_internal_server_error
9
13
  ::APIMatchers::HTTPStatusCode::BeInternalServerError.new(::APIMatchers::Core::Setup)
10
14
  end
@@ -14,6 +18,10 @@ module APIMatchers
14
18
  ::APIMatchers::HTTPStatusCode::BeUnauthorized.new(::APIMatchers::Core::Setup)
15
19
  end
16
20
 
21
+ def be_ok
22
+ ::APIMatchers::HTTPStatusCode::BeOk.new(::APIMatchers::Core::Setup)
23
+ end
24
+
17
25
  def create_resource
18
26
  ::APIMatchers::HTTPStatusCode::CreateResource.new(::APIMatchers::Core::Setup)
19
27
  end
@@ -0,0 +1,17 @@
1
+ module APIMatchers
2
+ module HTTPStatusCode
3
+ class BeNotFound < Base
4
+ def expected_status_code
5
+ 404
6
+ end
7
+
8
+ def failure_message_for_should
9
+ %Q{expected that '#{@http_status_code}' to be Not Found with the status '404'.}
10
+ end
11
+
12
+ def failure_message_for_should_not
13
+ %Q{expected that '#{@http_status_code}' to NOT be Not Found with the status '404'.}
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module APIMatchers
2
+ module HTTPStatusCode
3
+ class BeOk < Base
4
+ def expected_status_code
5
+ 200
6
+ end
7
+
8
+ def failure_message_for_should
9
+ %Q{expected that '#{@http_status_code}' to be ok with the status '200'.}
10
+ end
11
+
12
+ def failure_message_for_should_not
13
+ %Q{expected that '#{@http_status_code}' to NOT be ok with the status '200'.}
14
+ end
15
+ end
16
+ end
17
+ end
@@ -6,32 +6,25 @@ module APIMatchers
6
6
  class HaveJsonNode < Base
7
7
  def matches?(actual)
8
8
  @actual = actual
9
- json = begin
10
- JSON.parse(response_body)
11
- rescue
12
- {}
13
- end
14
9
 
15
10
  begin
16
- options = {}
17
- options[:node] = @expected_node.to_s
18
- unless @with_value.nil?
19
- options[:value] = @with_value
20
- end
21
-
22
- node = Core::FindInJSON.new(json).find( options )
11
+ node = Core::FindInJSON.new(json).find(node: @expected_node.to_s, value: @with_value)
23
12
 
24
13
  if @expected_including_text
25
14
  node.to_s.include?(@expected_including_text)
26
15
  else
27
- # the node is present
28
- true
16
+ true # the node is present
29
17
  end
30
- rescue ::APIMatchers::Core::Exceptions::KeyNotFound
31
- # the key was not found
32
- false
18
+ rescue ::APIMatchers::KeyNotFound
19
+ false # the key was not found
33
20
  end
34
21
  end
22
+
23
+ def json
24
+ JSON.parse(response_body)
25
+ rescue JSON::ParserError => exception
26
+ raise ::APIMatchers::InvalidJSON.new("Invalid JSON: '#{response_body}'")
27
+ end
35
28
  end
36
29
  end
37
30
  end
@@ -1,3 +1,3 @@
1
1
  module APIMatchers
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/api_matchers.rb CHANGED
@@ -10,8 +10,10 @@ module APIMatchers
10
10
  module HTTPStatusCode
11
11
  autoload :Base, 'api_matchers/http_status_code/base'
12
12
  autoload :BeBadRequest, 'api_matchers/http_status_code/be_bad_request'
13
+ autoload :BeNotFound, 'api_matchers/http_status_code/be_not_found'
13
14
  autoload :BeInternalServerError, 'api_matchers/http_status_code/be_internal_server_error'
14
15
  autoload :BeUnauthorized, 'api_matchers/http_status_code/be_unauthorized'
16
+ autoload :BeOk, 'api_matchers/http_status_code/be_ok'
15
17
  autoload :CreateResource, 'api_matchers/http_status_code/create_resource'
16
18
  end
17
19
 
@@ -39,6 +41,7 @@ module APIMatchers
39
41
  autoload :Setup, 'api_matchers/core/setup'
40
42
  autoload :Exceptions, 'api_matchers/core/exceptions'
41
43
  end
44
+ include ::APIMatchers::Core::Exceptions
42
45
 
43
46
  def self.setup
44
47
  yield(::APIMatchers::Core::Setup)
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe APIMatchers::HTTPStatusCode::BeBadRequest do
4
+ describe "should be_not_found" do
5
+ it "should passes if the actual is equal to 404" do
6
+ 404.should be_not_found
7
+ end
8
+
9
+ it "should fails if the actual is not equal to 404" do
10
+ expect { 401.should be_not_found }.to fail_with(%Q{expected that '401' to be Not Found with the status '404'.})
11
+ end
12
+ end
13
+
14
+ describe "should_not be_not_found" do
15
+ it "should passes if the actual is not equal to 404" do
16
+ 401.should_not be_not_found
17
+ end
18
+
19
+ it "should fail if the actual is equal to 404" do
20
+ expect { 404.should_not be_not_found }.to fail_with(%Q{expected that '404' to NOT be Not Found with the status '404'.})
21
+ end
22
+ end
23
+
24
+ describe "with change configuration" do
25
+ before do
26
+ APIMatchers.setup { |config| config.http_status_method = :http_status }
27
+ end
28
+
29
+ after do
30
+ APIMatchers.setup { |config| config.http_status_method = nil }
31
+ end
32
+
33
+ it "should pass if the actual.http_status is equal to 404" do
34
+ response = OpenStruct.new(:http_status => 404)
35
+ response.should be_not_found
36
+ end
37
+
38
+ it "should fail if the actual.http_status is not equal to 400" do
39
+ response = OpenStruct.new(:http_status => 500)
40
+ expect { response.should be_not_found }.to fail_with(%Q{expected that '500' to be Not Found with the status '404'.})
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe APIMatchers::HTTPStatusCode::BeOk do
4
+ describe "should be_ok" do
5
+ it "should passes if the actual is equal to 200" do
6
+ 200.should be_ok
7
+ end
8
+
9
+ it "should fails if the actual is not equal to 200" do
10
+ expect { 201.should be_ok }.to fail_with(%Q{expected that '201' to be ok with the status '200'.})
11
+ end
12
+ end
13
+
14
+ describe "should_not be_ok_request" do
15
+ it "should passes if the actual is not equal to 200" do
16
+ 201.should_not be_ok
17
+ end
18
+
19
+ it "should fail if the actual is equal to 200" do
20
+ expect { 200.should_not be_ok }.to fail_with(%Q{expected that '200' to NOT be ok with the status '200'.})
21
+ end
22
+ end
23
+
24
+ describe "with change configuration" do
25
+ before do
26
+ APIMatchers.setup { |config| config.http_status_method = :http_status }
27
+ end
28
+
29
+ after do
30
+ APIMatchers.setup { |config| config.http_status_method = nil }
31
+ end
32
+
33
+ it "should pass if the actual.http_status is equal to 200" do
34
+ response = OpenStruct.new(:http_status => 200)
35
+ response.should be_ok
36
+ end
37
+
38
+ it "should fail if the actual.http_status is not equal to 200" do
39
+ response = OpenStruct.new(:http_status => 500)
40
+ expect { response.should be_ok }.to fail_with(%Q{expected that '500' to be ok with the status '200'.})
41
+ end
42
+ end
43
+ end
@@ -61,7 +61,7 @@ describe APIMatchers::ResponseBody::HaveJsonNode do
61
61
  it "should not parse the matcher for json when you pass a xml" do
62
62
  expect {
63
63
  "<product><name>webdesk</name></product>".should have_json_node(:name).with('webdesk')
64
- }.to fail_with(%Q{expected to have node called: 'name' with value: 'webdesk'. Got: '<product><name>webdesk</name></product>'})
64
+ }.to raise_error(APIMatchers::InvalidJSON, "Invalid JSON: '<product><name>webdesk</name></product>'")
65
65
  end
66
66
  end
67
67
 
@@ -224,6 +224,32 @@ describe APIMatchers::ResponseBody::HaveJsonNode do
224
224
  end
225
225
  end
226
226
 
227
+ describe "some assumptions" do
228
+ it "shouldn't fail" do
229
+ '{ "transaction": { "id": 54, "status": "paid" } }'.should have_json_node(:transaction)
230
+ end
231
+
232
+ it "also shouldn't fail" do
233
+ '{ "transaction": { "id": 54, "status": "paid" } }'.should have_json_node(:id).with(54)
234
+ end
235
+
236
+ it "should have json node including text" do
237
+ '{"error": "Transaction error: Name cant be blank"}'.should have_json_node(:error).including_text("Transaction error")
238
+ end
239
+
240
+ it "should have json node with boolean value" do
241
+ '{"creditcard": true}'.should have_json_node(:creditcard).with(true)
242
+ end
243
+
244
+ it "should have json node with value" do
245
+ '{ "error": "not_authorized", "transaction": { "id": "55" } }'.should have_node(:error).with('not_authorized')
246
+ end
247
+
248
+ it "should have json node with integer" do
249
+ '{"parcels": 1 }'.should have_node(:parcels).with(1)
250
+ end
251
+ end
252
+
227
253
  describe "with change configuration" do
228
254
  before do
229
255
  APIMatchers.setup { |config| config.response_body_method = :response_body }
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Tomas D'Stefano
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-10-08 00:00:00.000000000 Z
11
+ date: 2013-06-24 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rspec
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: activesupport
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: nokogiri
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -67,13 +60,13 @@ extensions: []
67
60
  extra_rdoc_files: []
68
61
  files:
69
62
  - .gitignore
70
- - .rspec
71
- - .rvmrc
63
+ - .rvmrc.example
72
64
  - Gemfile
73
65
  - History.markdown
74
66
  - LICENSE
75
67
  - README.markdown
76
68
  - Rakefile
69
+ - TODO.markdown
77
70
  - api_matchers.gemspec
78
71
  - lib/api_matchers.rb
79
72
  - lib/api_matchers/core/exceptions.rb
@@ -86,6 +79,8 @@ files:
86
79
  - lib/api_matchers/http_status_code/base.rb
87
80
  - lib/api_matchers/http_status_code/be_bad_request.rb
88
81
  - lib/api_matchers/http_status_code/be_internal_server_error.rb
82
+ - lib/api_matchers/http_status_code/be_not_found.rb
83
+ - lib/api_matchers/http_status_code/be_ok.rb
89
84
  - lib/api_matchers/http_status_code/be_unauthorized.rb
90
85
  - lib/api_matchers/http_status_code/create_resource.rb
91
86
  - lib/api_matchers/response_body/base.rb
@@ -101,6 +96,8 @@ files:
101
96
  - spec/api_matchers/http_status_code/base_spec.rb
102
97
  - spec/api_matchers/http_status_code/be_bad_request_spec.rb
103
98
  - spec/api_matchers/http_status_code/be_internal_server_error_spec.rb
99
+ - spec/api_matchers/http_status_code/be_not_found_spec.rb
100
+ - spec/api_matchers/http_status_code/be_ok_spec.rb
104
101
  - spec/api_matchers/http_status_code/be_unauthorized_spec.rb
105
102
  - spec/api_matchers/http_status_code/create_resource_spec.rb
106
103
  - spec/api_matchers/response_body/base_spec.rb
@@ -110,27 +107,26 @@ files:
110
107
  - spec/spec_helper.rb
111
108
  homepage: https://github.com/tomas-stefano/api_matchers
112
109
  licenses: []
110
+ metadata: {}
113
111
  post_install_message:
114
112
  rdoc_options: []
115
113
  require_paths:
116
114
  - lib
117
115
  required_ruby_version: !ruby/object:Gem::Requirement
118
- none: false
119
116
  requirements:
120
117
  - - ! '>='
121
118
  - !ruby/object:Gem::Version
122
119
  version: '0'
123
120
  required_rubygems_version: !ruby/object:Gem::Requirement
124
- none: false
125
121
  requirements:
126
122
  - - ! '>='
127
123
  - !ruby/object:Gem::Version
128
124
  version: '0'
129
125
  requirements: []
130
126
  rubyforge_project:
131
- rubygems_version: 1.8.24
127
+ rubygems_version: 2.0.3
132
128
  signing_key:
133
- specification_version: 3
129
+ specification_version: 4
134
130
  summary: Collection of RSpec matchers for create your API.
135
131
  test_files:
136
132
  - spec/api_matchers/core/find_in_json_spec.rb
@@ -141,6 +137,8 @@ test_files:
141
137
  - spec/api_matchers/http_status_code/base_spec.rb
142
138
  - spec/api_matchers/http_status_code/be_bad_request_spec.rb
143
139
  - spec/api_matchers/http_status_code/be_internal_server_error_spec.rb
140
+ - spec/api_matchers/http_status_code/be_not_found_spec.rb
141
+ - spec/api_matchers/http_status_code/be_ok_spec.rb
144
142
  - spec/api_matchers/http_status_code/be_unauthorized_spec.rb
145
143
  - spec/api_matchers/http_status_code/create_resource_spec.rb
146
144
  - spec/api_matchers/response_body/base_spec.rb
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --color -f d