api_matchers 0.0.1 → 0.0.2

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.
data/History.markdown CHANGED
@@ -1,4 +1,25 @@
1
- ## development
1
+ ## v0.0.2
2
+
3
+ 1) Put the headers method and the content type key in the setup class and that will be used by the headers matchers(be_json and be_xml).
4
+
5
+ This:
6
+
7
+ response.headers['Content-Type'].should be_in_json
8
+ response.headers['Content-Type'].should be_in_xml
9
+
10
+ With:
11
+
12
+ APIMatchers.setup do |config|
13
+ config.header_method = :headers
14
+ config.header_content_type_key = 'Content-Type'
15
+ end
16
+
17
+ Becomes:
18
+
19
+ response.should be_in_json
20
+ response.should be_in_xml
21
+
22
+ ## v0.0.1
2
23
 
3
24
  1) Headers Matchers: be_xml, be_json (**OBS:** Need to think about the setup!)
4
25
  2) HTTP Status Matchers: be_a_bad_request, be_internal_server_error, be_unauthorized, create_resource
data/README.markdown CHANGED
@@ -22,27 +22,28 @@ Collection of RSpec matchers for create your API.
22
22
 
23
23
  ## Usage
24
24
 
25
- ### Have Node Matcher
25
+ ### Including in RSpec
26
26
 
27
- "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:transaction)
27
+ To include all this matchers you need to include the APIMatchers::RSpecMatchers module:
28
28
 
29
- "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:id).with(54)
29
+ RSpec.configure do |config|
30
+ config.include APIMatchers::RSpecMatchers
31
+ end
30
32
 
31
- "{ 'error': '', 'transaction': { 'id': '55', 'status': 'waiting_payment' } }".should have_node(:error).with('not_authorized')
33
+ ### Have Node Matcher
32
34
 
33
- If you want to configure to make all **searches inside a root element**, you can do this:
35
+ The have_node matcher parse the actual and see if have the expcted node with the expected value.
36
+ **The default that have_node will parse is JSON.**
34
37
 
35
- APIMatchers.setup do |config|
36
- config.root_element = :transaction
37
- end
38
+ "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:transaction)
38
39
 
39
- "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:id).with(54) # WILL PASS
40
+ "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_node(:id).with(54)
40
41
 
41
- "{ 'error': '', 'transaction': { 'id': '55', 'status': 'waiting_payment' } }".should have_node(:error).with('not_authorized') # WILL NOT PASS BECAUSE THE ERROR NODE ISN'T INSIDE THE TRANSACTION NODE
42
+ "{ 'error': 'not_authorized', 'transaction': { 'id': '55' } }".should have_node(:error).with('not_authorized')
42
43
 
43
44
  ### HAVE NODE Matcher Configuration
44
45
 
45
- You can configure if you want xml or json(**JSON is the default**):
46
+ You can configure if you want xml(**JSON is the default**):
46
47
 
47
48
  APIMatchers.setup do |config|
48
49
  config.content_type = :xml
@@ -50,7 +51,7 @@ You can configure if you want xml or json(**JSON is the default**):
50
51
 
51
52
  '<transaction><id>200</id><status>paid</status></transaction>'.should have_node(:status).with('paid')
52
53
 
53
- **Observation: You can use the *have_xml_node* or *have_json_node* if you don't want to configure everytime.**
54
+ **If you work with xml and json in the same API, I recommend that you check the have_json_node and have_xml_node matchers.**
54
55
 
55
56
  You can configure the name of the method for example:
56
57
 
@@ -66,6 +67,15 @@ Then you can use without call the **#body** method:
66
67
 
67
68
  response.should have_node(:foo).with('bar')
68
69
 
70
+ ### Have JSON Node Matcher
71
+
72
+ "{ 'transaction': { 'id': '54', 'status': 'paid' } }".should have_json_node(:id).with(54)
73
+
74
+ ### Have XML Node Matcher
75
+
76
+ "<product><name>gateway</name></product>".should have_xml_node(:name).with('gateway')
77
+
78
+
69
79
  ### Create Resource Matcher
70
80
 
71
81
  This matchers see the HTTP STATUS CODE is equal to 201.
@@ -116,13 +126,27 @@ This configurations affects this matchers:
116
126
 
117
127
  This is a matcher that see if the content type is xml:
118
128
 
119
- response.content_type.should be_in_xml
129
+ response.headers['Content-Type'].should be_in_xml
120
130
 
121
131
  ### Be in JSON Matcher
122
132
 
123
133
  This is a matcher that see if the content type is in JSON:
124
134
 
125
- response.content_type.should be_in_json
135
+ response.headers['Content-Type'].should be_in_json
136
+
137
+ ### Headers Configuration
138
+
139
+ You can configure the name method to call the headers and content type:
140
+
141
+ APIMatchers.setup do |config|
142
+ config.header_method = :headers
143
+ config.header_content_type_key = 'Content-Type'
144
+ end
145
+
146
+ Then you can use without call the **#headers** calling the **#['Content-Type']** method:
147
+
148
+ response.should be_in_json
149
+ response.should be_in_xml
126
150
 
127
151
  ### Acknowlegments
128
152
 
@@ -53,6 +53,28 @@ module APIMatchers
53
53
  #
54
54
  cattr_accessor :have_node_matcher
55
55
  self.have_node_matcher = :json
56
+
57
+ # The headers method and the content type key that will be used by the headers matchers.
58
+ #
59
+ # ==== Examples
60
+ #
61
+ # response.response_header['Content-Type'].should be_json
62
+ # response.response_header['Content-Type'].should be_xml
63
+ #
64
+ # # Instead calling #response_header everytime, you can configure:
65
+ #
66
+ # APIMatchers.setup do |config|
67
+ # config.header_method = :response_header
68
+ # config.header_content_type_key = 'Content-Type'
69
+ # end
70
+ #
71
+ # Then:
72
+ #
73
+ # response.should be_json
74
+ # response.should be_xml
75
+ #
76
+ cattr_accessor :header_method
77
+ cattr_accessor :header_content_type_key
56
78
  end
57
79
  end
58
80
  end
@@ -1,9 +1,27 @@
1
1
  module APIMatchers
2
2
  module Headers
3
3
  class Base
4
+ attr_reader :setup
5
+
6
+ def initialize(setup)
7
+ @setup = setup
8
+ end
9
+
4
10
  def matches?(actual)
5
11
  @actual = actual
6
- actual.eql?(expected_content_type)
12
+
13
+ content_type_response.eql?(expected_content_type)
14
+ end
15
+
16
+ def content_type_response
17
+ if @setup.header_method.present? and @setup.header_content_type_key.present?
18
+ headers = @actual.send(@setup.header_method)
19
+ if headers.present?
20
+ headers[@setup.header_content_type_key] || headers
21
+ end
22
+ else
23
+ @actual
24
+ end
7
25
  end
8
26
 
9
27
  def expected_content_type
@@ -6,7 +6,7 @@ module APIMatchers
6
6
  end
7
7
 
8
8
  def failure_message_for_should
9
- %Q{expected a JSON response with '#{expected_content_type}'. Got: '#{@actual}'.}
9
+ %Q{expected a JSON response with '#{expected_content_type}'. Got: '#{content_type_response}'.}
10
10
  end
11
11
 
12
12
  def failure_message_for_should_not
@@ -6,7 +6,7 @@ module APIMatchers
6
6
  end
7
7
 
8
8
  def failure_message_for_should
9
- %Q{expected a XML response with '#{expected_content_type}'. Got: '#{@actual}'.}
9
+ %Q{expected a XML response with '#{expected_content_type}'. Got: '#{content_type_response}'.}
10
10
  end
11
11
 
12
12
  def failure_message_for_should_not
@@ -1,3 +1,3 @@
1
1
  module APIMatchers
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -20,4 +20,35 @@ describe APIMatchers::Headers::BeJSON do
20
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'.})
21
21
  end
22
22
  end
23
+
24
+ describe "with change configuration" do
25
+ before do
26
+ APIMatchers.setup do |config|
27
+ config.header_method = :response_header
28
+ config.header_content_type_key = 'Content-Type'
29
+ end
30
+ end
31
+
32
+ after do
33
+ APIMatchers.setup do |config|
34
+ config.header_method = nil
35
+ config.header_content_type_key = nil
36
+ end
37
+ end
38
+
39
+ it "should pass if the actual.response_header is equal to application/json" do
40
+ response = OpenStruct.new(:response_header => { 'Content-Type' => "application/json; charset=utf-8"})
41
+ response.should be_json
42
+ end
43
+
44
+ it "should fail if the actual.response_header is not equal to application/json" do
45
+ 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'.})
47
+ end
48
+
49
+ it "should fail if the actual.response_header is not equal to application/json" do
50
+ 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"}'.})
52
+ end
53
+ end
23
54
  end
@@ -24,4 +24,35 @@ describe APIMatchers::Headers::BeXML do
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
27
+
28
+ describe "with change configuration" do
29
+ before do
30
+ APIMatchers.setup do |config|
31
+ config.header_method = :response_header
32
+ config.header_content_type_key = 'Content-Type'
33
+ end
34
+ end
35
+
36
+ after do
37
+ APIMatchers.setup do |config|
38
+ config.header_method = nil
39
+ config.header_content_type_key = nil
40
+ end
41
+ end
42
+
43
+ it "should pass if the actual.response_header is equal to application/xml" do
44
+ response = OpenStruct.new(:response_header => { 'Content-Type' => "application/xml; charset=utf-8"})
45
+ response.should be_xml
46
+ end
47
+
48
+ it "should fail if the actual.response_header is not equal to application/xml" do
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'.})
51
+ end
52
+
53
+ it "should fail when pass the actual that have headers but not the content type key" do
54
+ 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"}'.})
56
+ end
57
+ end
27
58
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-07-16 00:00:00.000000000Z
12
+ date: 2012-07-31 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &2153725680 !ruby/object:Gem::Requirement
16
+ requirement: &2156860320 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.11.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2153725680
24
+ version_requirements: *2156860320
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &2153725180 !ruby/object:Gem::Requirement
27
+ requirement: &2156859820 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.2.6
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2153725180
35
+ version_requirements: *2156859820
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nokogiri
38
- requirement: &2153724720 !ruby/object:Gem::Requirement
38
+ requirement: &2156859360 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 1.5.5
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *2153724720
46
+ version_requirements: *2156859360
47
47
  description: Collection of RSpec matchers for create your API.
48
48
  email:
49
49
  - tomas_stefano@successoft.com