api_matchers 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +1 -0
  3. data/History.markdown +9 -0
  4. data/LICENSE +1 -1
  5. data/README.markdown +3 -1
  6. data/TODO.markdown +2 -1
  7. data/api_matchers.gemspec +1 -1
  8. data/lib/api_matchers.rb +1 -0
  9. data/lib/api_matchers/core/rspec_matchers.rb +4 -0
  10. data/lib/api_matchers/headers/base.rb +1 -1
  11. data/lib/api_matchers/headers/be_json.rb +7 -3
  12. data/lib/api_matchers/headers/be_xml.rb +7 -3
  13. data/lib/api_matchers/http_status_code/base.rb +1 -1
  14. data/lib/api_matchers/http_status_code/be_bad_request.rb +7 -3
  15. data/lib/api_matchers/http_status_code/be_internal_server_error.rb +7 -3
  16. data/lib/api_matchers/http_status_code/be_not_found.rb +7 -3
  17. data/lib/api_matchers/http_status_code/be_ok.rb +7 -3
  18. data/lib/api_matchers/http_status_code/be_unauthorized.rb +7 -3
  19. data/lib/api_matchers/http_status_code/be_unprocessable_entity.rb +21 -0
  20. data/lib/api_matchers/http_status_code/create_resource.rb +7 -3
  21. data/lib/api_matchers/response_body/base.rb +6 -2
  22. data/lib/api_matchers/response_body/have_json.rb +6 -3
  23. data/lib/api_matchers/version.rb +1 -1
  24. data/spec/api_matchers/core/find_in_json_spec.rb +4 -4
  25. data/spec/api_matchers/headers/be_json_spec.rb +17 -9
  26. data/spec/api_matchers/headers/be_xml_spec.rb +13 -9
  27. data/spec/api_matchers/http_status_code/be_bad_request_spec.rb +12 -6
  28. data/spec/api_matchers/http_status_code/be_internal_server_error_spec.rb +12 -6
  29. data/spec/api_matchers/http_status_code/be_not_found_spec.rb +12 -6
  30. data/spec/api_matchers/http_status_code/be_ok_spec.rb +12 -6
  31. data/spec/api_matchers/http_status_code/be_unauthorized_spec.rb +12 -6
  32. data/spec/api_matchers/http_status_code/be_unprocessable_entity_spec.rb +27 -0
  33. data/spec/api_matchers/http_status_code/create_resource_spec.rb +12 -6
  34. data/spec/api_matchers/response_body/base_spec.rb +4 -4
  35. data/spec/api_matchers/response_body/have_json_node_spec.rb +63 -57
  36. data/spec/api_matchers/response_body/have_json_spec.rb +7 -7
  37. data/spec/api_matchers/response_body/have_node_spec.rb +2 -2
  38. data/spec/api_matchers/response_body/have_xml_node_spec.rb +51 -38
  39. metadata +9 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 55a5fba1fb8dd8e0534a451e2db5af8662312b0e
4
- data.tar.gz: dfe3690699a99aeffe353d50c7401a5eb741e084
3
+ metadata.gz: e256da4250d9b304bed9733e30b1949b13f60c27
4
+ data.tar.gz: 361d34707eafd42661b8dea22bdaa95470eb8632
5
5
  SHA512:
6
- metadata.gz: 8a28c82e8bfeede2915149c45e89f0698c9c1600dfeaefb4ddda8e06ad0e88649144a59dd0f40cbe4f2b6b2465b5ea5ca0b205d41bcf8110a416354691d12582
7
- data.tar.gz: 0d594066a2ba47024bb9cd1126578e0cef31f81a6b97ad009d8f7161f7290384a7a641127b35639b02043662f8b56f44acfeba49c14ed73f75999a6db521c443
6
+ metadata.gz: 35c9a63f902431792e244e97a8b6f11307b7b4afc5cb8250a960b43039b3e2aadce22321347c3cf00536169f5074449dfeadccc994ee72275935a26ffd5c206e
7
+ data.tar.gz: 803747cf15d4d5d461a5ad23d84541a6ede2a878a08e34e5e0808967c35136fb25493b80aaf98bb219fa851c91ce4368ef42c735b872a9ebdb9f7145f6a129bb
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/History.markdown CHANGED
@@ -1,3 +1,12 @@
1
+ ## development
2
+
3
+ * Internals - Migrate to the RSpec expect syntax.
4
+
5
+ ## v0.4.0
6
+
7
+ * jRuby support;
8
+ * have_json matcher;
9
+
1
10
  ## v0.1.1
2
11
 
3
12
  1) Support Datetime, Date and Time comparison (Thanks to Stephen Orens).
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Tomas D'Stefano
1
+ Copyright (c) 2012-2013 Tomas D'Stefano
2
2
 
3
3
  MIT License
4
4
 
data/README.markdown CHANGED
@@ -129,7 +129,9 @@ end
129
129
  response.should have_node(:foo).with('bar')
130
130
  ```
131
131
 
132
- # Instead of:
132
+ Instead of:
133
+
134
+ ```ruby
133
135
  response.body.should have_node(:foo)
134
136
  ```
135
137
 
data/TODO.markdown CHANGED
@@ -1,2 +1,3 @@
1
1
  * Refactor Core::FindInJSON#find method.
2
- * Adding some include matcher about node arrays.
2
+ * Improve the error messages from all matchers.
3
+ * Adding some include matcher about node arrays.
data/api_matchers.gemspec CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = APIMatchers::VERSION
17
17
 
18
- gem.add_dependency 'rspec', '>= 2.10.0'
18
+ gem.add_dependency 'rspec', '>= 2.14'
19
19
  gem.add_dependency 'activesupport', '>= 3.2.5'
20
20
  gem.add_dependency 'nokogiri', '>= 1.5.2'
21
21
  end
data/lib/api_matchers.rb CHANGED
@@ -14,6 +14,7 @@ module APIMatchers
14
14
  autoload :BeInternalServerError, 'api_matchers/http_status_code/be_internal_server_error'
15
15
  autoload :BeUnauthorized, 'api_matchers/http_status_code/be_unauthorized'
16
16
  autoload :BeOk, 'api_matchers/http_status_code/be_ok'
17
+ autoload :BeUnprocessableEntity, 'api_matchers/http_status_code/be_unprocessable_entity'
17
18
  autoload :CreateResource, 'api_matchers/http_status_code/create_resource'
18
19
  end
19
20
 
@@ -22,6 +22,10 @@ module APIMatchers
22
22
  ::APIMatchers::HTTPStatusCode::BeOk.new(::APIMatchers::Core::Setup)
23
23
  end
24
24
 
25
+ def be_unprocessable_entity
26
+ ::APIMatchers::HTTPStatusCode::BeUnprocessableEntity.new(::APIMatchers::Core::Setup)
27
+ end
28
+
25
29
  def create_resource
26
30
  ::APIMatchers::HTTPStatusCode::CreateResource.new(::APIMatchers::Core::Setup)
27
31
  end
@@ -29,4 +29,4 @@ module APIMatchers
29
29
  end
30
30
  end
31
31
  end
32
- end
32
+ end
@@ -5,13 +5,17 @@ module APIMatchers
5
5
  'application/json; charset=utf-8'
6
6
  end
7
7
 
8
- def failure_message_for_should
8
+ def failure_message
9
9
  %Q{expected a JSON response with '#{expected_content_type}'. Got: '#{content_type_response}'.}
10
10
  end
11
11
 
12
- def failure_message_for_should_not
12
+ def failure_message_when_negated
13
13
  %Q{expected to not be a JSON response. Got: '#{expected_content_type}'.}
14
14
  end
15
+
16
+ # RSpec 2 compatibility:
17
+ alias_method :failure_message_for_should, :failure_message
18
+ alias_method :failure_message_for_should_not, :failure_message_when_negated
15
19
  end
16
20
  end
17
- end
21
+ end
@@ -5,13 +5,17 @@ module APIMatchers
5
5
  'application/xml; charset=utf-8'
6
6
  end
7
7
 
8
- def failure_message_for_should
8
+ def failure_message
9
9
  %Q{expected a XML response with '#{expected_content_type}'. Got: '#{content_type_response}'.}
10
10
  end
11
11
 
12
- def failure_message_for_should_not
12
+ def failure_message_when_negated
13
13
  %Q{expected to not be a XML response. Got: '#{expected_content_type}'.}
14
14
  end
15
+
16
+ # RSpec 2 compatibility:
17
+ alias_method :failure_message_for_should, :failure_message
18
+ alias_method :failure_message_for_should_not, :failure_message_when_negated
15
19
  end
16
20
  end
17
- end
21
+ end
@@ -29,4 +29,4 @@ module APIMatchers
29
29
  end
30
30
  end
31
31
  end
32
- end
32
+ end
@@ -5,13 +5,17 @@ module APIMatchers
5
5
  400
6
6
  end
7
7
 
8
- def failure_message_for_should
8
+ def failure_message
9
9
  %Q{expected that '#{@http_status_code}' to be a Bad Request with the status '400'.}
10
10
  end
11
11
 
12
- def failure_message_for_should_not
12
+ def failure_message_when_negated
13
13
  %Q{expected that '#{@http_status_code}' to NOT be a Bad Request with the status '400'.}
14
14
  end
15
+
16
+ # RSpec 2 compatibility:
17
+ alias_method :failure_message_for_should, :failure_message
18
+ alias_method :failure_message_for_should_not, :failure_message_when_negated
15
19
  end
16
20
  end
17
- end
21
+ end
@@ -5,13 +5,17 @@ module APIMatchers
5
5
  500
6
6
  end
7
7
 
8
- def failure_message_for_should
8
+ def failure_message
9
9
  %Q{expected that '#{@http_status_code}' to be Internal Server Error with the status '500'.}
10
10
  end
11
11
 
12
- def failure_message_for_should_not
12
+ def failure_message_when_negated
13
13
  %Q{expected that '#{@http_status_code}' to NOT be Internal Server Error.}
14
14
  end
15
+
16
+ # RSpec 2 compatibility:
17
+ alias_method :failure_message_for_should, :failure_message
18
+ alias_method :failure_message_for_should_not, :failure_message_when_negated
15
19
  end
16
20
  end
17
- end
21
+ end
@@ -5,13 +5,17 @@ module APIMatchers
5
5
  404
6
6
  end
7
7
 
8
- def failure_message_for_should
8
+ def failure_message
9
9
  %Q{expected that '#{@http_status_code}' to be Not Found with the status '404'.}
10
10
  end
11
11
 
12
- def failure_message_for_should_not
12
+ def failure_message_when_negated
13
13
  %Q{expected that '#{@http_status_code}' to NOT be Not Found with the status '404'.}
14
14
  end
15
+
16
+ # RSpec 2 compatibility:
17
+ alias_method :failure_message_for_should, :failure_message
18
+ alias_method :failure_message_for_should_not, :failure_message_when_negated
15
19
  end
16
20
  end
17
- end
21
+ end
@@ -5,13 +5,17 @@ module APIMatchers
5
5
  200
6
6
  end
7
7
 
8
- def failure_message_for_should
8
+ def failure_message
9
9
  %Q{expected that '#{@http_status_code}' to be ok with the status '200'.}
10
10
  end
11
11
 
12
- def failure_message_for_should_not
12
+ def failure_message_when_negated
13
13
  %Q{expected that '#{@http_status_code}' to NOT be ok with the status '200'.}
14
14
  end
15
+
16
+ # RSpec 2 compatibility:
17
+ alias_method :failure_message_for_should, :failure_message
18
+ alias_method :failure_message_for_should_not, :failure_message_when_negated
15
19
  end
16
20
  end
17
- end
21
+ end
@@ -5,13 +5,17 @@ module APIMatchers
5
5
  401
6
6
  end
7
7
 
8
- def failure_message_for_should
8
+ def failure_message
9
9
  %Q{expected that '#{@http_status_code}' to be Unauthorized with the status '401'.}
10
10
  end
11
11
 
12
- def failure_message_for_should_not
12
+ def failure_message_when_negated
13
13
  %Q{expected that '#{@http_status_code}' to NOT be Unauthorized.}
14
14
  end
15
+
16
+ # RSpec 2 compatibility:
17
+ alias_method :failure_message_for_should, :failure_message
18
+ alias_method :failure_message_for_should_not, :failure_message_when_negated
15
19
  end
16
20
  end
17
- end
21
+ end
@@ -0,0 +1,21 @@
1
+ module APIMatchers
2
+ module HTTPStatusCode
3
+ class BeUnprocessableEntity < Base
4
+ def expected_status_code
5
+ 422
6
+ end
7
+
8
+ def failure_message
9
+ %Q{expected that '#{@http_status_code}' to be Unprocessable entity with the status '#{expected_status_code}'.}
10
+ end
11
+
12
+ def failure_message_when_negated
13
+ %Q{expected that '#{@http_status_code}' to NOT be Unprocessable entity with the status '#{expected_status_code}'.}
14
+ end
15
+
16
+ # RSpec 2 compatibility:
17
+ alias_method :failure_message_for_should, :failure_message
18
+ alias_method :failure_message_for_should_not, :failure_message_when_negated
19
+ end
20
+ end
21
+ end
@@ -5,13 +5,17 @@ module APIMatchers
5
5
  201
6
6
  end
7
7
 
8
- def failure_message_for_should
8
+ def failure_message
9
9
  %Q{expected that '#{@http_status_code}' to be Created Resource with the status '201'.}
10
10
  end
11
11
 
12
- def failure_message_for_should_not
12
+ def failure_message_when_negated
13
13
  %Q{expected that '#{@http_status_code}' to NOT be Created Resource.}
14
14
  end
15
+
16
+ # RSpec 2 compatibility:
17
+ alias_method :failure_message_for_should, :failure_message
18
+ alias_method :failure_message_for_should_not, :failure_message_when_negated
15
19
  end
16
20
  end
17
- end
21
+ end
@@ -31,14 +31,18 @@ module APIMatchers
31
31
  end
32
32
  end
33
33
 
34
- def failure_message_for_should
34
+ def failure_message
35
35
  "expected to have node called: '#{@expected_node}'" << added_message << ". Got: '#{response_body}'"
36
36
  end
37
37
 
38
- def failure_message_for_should_not
38
+ def failure_message_when_negated
39
39
  "expected to NOT have node called: '#{@expected_node}'" << added_message << ". Got: '#{response_body}'"
40
40
  end
41
41
 
42
+ # RSpec 2 compatibility:
43
+ alias_method :failure_message_for_should, :failure_message
44
+ alias_method :failure_message_for_should_not, :failure_message_when_negated
45
+
42
46
  def added_message
43
47
  if @with_value
44
48
  " with value: '#{@with_value}'"
@@ -14,14 +14,17 @@ module APIMatchers
14
14
  @expected_json == json
15
15
  end
16
16
 
17
- def failure_message_for_should
17
+ def failure_message
18
18
  "expect to have json: '#{response_body}'. Got: '#{json}'."
19
19
  end
20
20
 
21
- def failure_message_for_should_not
21
+ def failure_message_when_negated
22
22
  "expect to NOT have json: '#{response_body}'."
23
23
  end
24
+
25
+ # RSpec 2 compatibility:
26
+ alias_method :failure_message_for_should, :failure_message
27
+ alias_method :failure_message_for_should_not, :failure_message_when_negated
24
28
  end
25
29
  end
26
30
  end
27
-
@@ -1,3 +1,3 @@
1
1
  module APIMatchers
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -5,21 +5,21 @@ module APIMatchers::Core
5
5
  describe "#find" do
6
6
  context 'when node exists' do
7
7
  it "should return the value of the expected key" do
8
- FindInJSON.new('product' => 'gateway').find(node: 'product').should eql 'gateway'
8
+ expect(FindInJSON.new('product' => 'gateway').find(node: 'product')).to eql 'gateway'
9
9
  end
10
10
 
11
11
  it "should return the value of the deep expected key in the json" do
12
- FindInJSON.new('transaction' => { 'error' => { 'code' => '999' } }).find(node: 'code').should eql '999'
12
+ expect(FindInJSON.new('transaction' => { 'error' => { 'code' => '999' } }).find(node: 'code')).to eql '999'
13
13
  end
14
14
  end
15
15
 
16
16
  context 'when node do not exists' do
17
17
  it "should return nil if don't find the expected node" do
18
- expect{ FindInJSON.new('product' => 'pabx').find(node: 'developers').should raise_error( ::APIMatchers::Core::Exceptions::KeyNotFound ) }
18
+ expect { FindInJSON.new('product' => 'pabx').find(node: 'developers') }.to raise_error( ::APIMatchers::Core::Exceptions::KeyNotFound)
19
19
  end
20
20
 
21
21
  it "should return nil if don't find the expected node in the deep JSON" do
22
- expect{ FindInJSON.new('transaction' => { 'id' => 150, 'error' => {} }).find(node: 'code').should raise_error( ::APIMatchers::Core::Exceptions::KeyNotFound ) }
22
+ expect { FindInJSON.new('transaction' => { 'id' => 150, 'error' => {} }).find(node: 'code') }.to raise_error( ::APIMatchers::Core::Exceptions::KeyNotFound)
23
23
  end
24
24
  end
25
25
  end
@@ -1,23 +1,27 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe APIMatchers::Headers::BeJSON do
4
- describe "actual.should be_json" do
4
+ describe "actual.to be_json" do
5
5
  it "should pass when the actual is json response" do
6
- "application/json; charset=utf-8".should be_json
6
+ expect("application/json; charset=utf-8").to be_json
7
7
  end
8
8
 
9
9
  it "should not pass when the actual is not a json response" do
10
- expect { "application/xml; charset=utf-8".should be_json }.to fail_with(%Q{expected a JSON response with 'application/json; charset=utf-8'. Got: 'application/xml; charset=utf-8'.})
10
+ expect {
11
+ expect("application/xml; charset=utf-8").to be_json
12
+ }.to fail_with(%Q{expected a JSON response with 'application/json; charset=utf-8'. Got: 'application/xml; charset=utf-8'.})
11
13
  end
12
14
  end
13
15
 
14
- describe "actual.should_not be_json" do
16
+ describe "actual.not_to be_json" do
15
17
  it "should pass when the actual is not a json response" do
16
- "application/xml; charset=utf-8".should_not be_json
18
+ expect("application/xml; charset=utf-8").not_to be_json
17
19
  end
18
20
 
19
21
  it "should not pass when the actual is a json response" do
20
- expect { "application/json; charset=utf-8".should_not be_json }.to fail_with(%Q{expected to not be a JSON response. Got: 'application/json; charset=utf-8'.})
22
+ expect{
23
+ expect("application/json; charset=utf-8").not_to be_json
24
+ }.to fail_with(%Q{expected to not be a JSON response. Got: 'application/json; charset=utf-8'.})
21
25
  end
22
26
  end
23
27
 
@@ -38,17 +42,21 @@ describe APIMatchers::Headers::BeJSON do
38
42
 
39
43
  it "should pass if the actual.response_header is equal to application/json" do
40
44
  response = OpenStruct.new(:response_header => { 'Content-Type' => "application/json; charset=utf-8"})
41
- response.should be_json
45
+ expect(response).to be_json
42
46
  end
43
47
 
44
48
  it "should fail if the actual.response_header is not equal to application/json" do
45
49
  response = OpenStruct.new(:response_header => { "Content-Type" => "application/xml; charset=utf-8"})
46
- expect { response.should be_json }.to fail_with(%Q{expected a JSON response with 'application/json; charset=utf-8'. Got: 'application/xml; charset=utf-8'.})
50
+ expect {
51
+ expect(response).to be_json
52
+ }.to fail_with(%Q{expected a JSON response with 'application/json; charset=utf-8'. Got: 'application/xml; charset=utf-8'.})
47
53
  end
48
54
 
49
55
  it "should fail if the actual.response_header is not equal to application/json" do
50
56
  response = OpenStruct.new(:response_header => { "foo-bar" => "application/xml; charset=utf-8"})
51
- expect { response.should be_json }.to fail_with(%Q{expected a JSON response with 'application/json; charset=utf-8'. Got: '{"foo-bar"=>"application/xml; charset=utf-8"}'.})
57
+ expect {
58
+ expect(response).to be_json
59
+ }.to fail_with(%Q{expected a JSON response with 'application/json; charset=utf-8'. Got: '{"foo-bar"=>"application/xml; charset=utf-8"}'.})
52
60
  end
53
61
  end
54
62
  end
@@ -1,26 +1,26 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe APIMatchers::Headers::BeXML do
4
- describe "actual.should be_xml" do
4
+ describe "actual.to be_xml" do
5
5
  it "should pass when the actual is json response" do
6
- "application/xml; charset=utf-8".should be_xml
6
+ expect("application/xml; charset=utf-8").to be_xml
7
7
  end
8
8
 
9
9
  it "should not pass when the actual is not a json response" do
10
10
  expect {
11
- "application/json; charset=utf-8".should be_xml
11
+ expect("application/json; charset=utf-8").to be_xml
12
12
  }.to fail_with(%Q{expected a XML response with 'application/xml; charset=utf-8'. Got: 'application/json; charset=utf-8'.})
13
13
  end
14
14
  end
15
15
 
16
- describe "actual.should_not be_xml" do
16
+ describe "actual.not_to be_xml" do
17
17
  it "should pass when the actual is not a json response" do
18
- "application/json; charset=utf-8".should_not be_xml
18
+ expect("application/json; charset=utf-8").not_to be_xml
19
19
  end
20
20
 
21
21
  it "should not pass when the actual is a json response" do
22
22
  expect {
23
- "application/xml; charset=utf-8".should_not be_xml
23
+ expect("application/xml; charset=utf-8").not_to be_xml
24
24
  }.to fail_with(%Q{expected to not be a XML response. Got: 'application/xml; charset=utf-8'.})
25
25
  end
26
26
  end
@@ -42,17 +42,21 @@ describe APIMatchers::Headers::BeXML do
42
42
 
43
43
  it "should pass if the actual.response_header is equal to application/xml" do
44
44
  response = OpenStruct.new(:response_header => { 'Content-Type' => "application/xml; charset=utf-8"})
45
- response.should be_xml
45
+ expect(response).to be_xml
46
46
  end
47
47
 
48
48
  it "should fail if the actual.response_header is not equal to application/xml" do
49
49
  response = OpenStruct.new(:response_header => { "Content-Type" => "application/json; charset=utf-8"})
50
- expect { response.should be_xml }.to fail_with(%Q{expected a XML response with 'application/xml; charset=utf-8'. Got: 'application/json; charset=utf-8'.})
50
+ expect {
51
+ expect(response).to be_xml
52
+ }.to fail_with(%Q{expected a XML response with 'application/xml; charset=utf-8'. Got: 'application/json; charset=utf-8'.})
51
53
  end
52
54
 
53
55
  it "should fail when pass the actual that have headers but not the content type key" do
54
56
  response = OpenStruct.new(:response_header => { "foo-baz" => "application/json; charset=utf-8"})
55
- expect { response.should be_xml }.to fail_with(%Q{expected a XML response with 'application/xml; charset=utf-8'. Got: '{"foo-baz"=>"application/json; charset=utf-8"}'.})
57
+ expect {
58
+ expect(response).to be_xml
59
+ }.to fail_with(%Q{expected a XML response with 'application/xml; charset=utf-8'. Got: '{"foo-baz"=>"application/json; charset=utf-8"}'.})
56
60
  end
57
61
  end
58
62
  end