api_matchers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/History.markdown +5 -0
- data/LICENSE +22 -0
- data/README.markdown +129 -0
- data/Rakefile +2 -0
- data/api_matchers.gemspec +21 -0
- data/lib/api_matchers/core/find_in_json.rb +25 -0
- data/lib/api_matchers/core/rspec_matchers.rb +49 -0
- data/lib/api_matchers/core/setup.rb +58 -0
- data/lib/api_matchers/headers/base.rb +14 -0
- data/lib/api_matchers/headers/be_json.rb +17 -0
- data/lib/api_matchers/headers/be_xml.rb +17 -0
- data/lib/api_matchers/http_status_code/base.rb +32 -0
- data/lib/api_matchers/http_status_code/be_bad_request.rb +17 -0
- data/lib/api_matchers/http_status_code/be_internal_server_error.rb +17 -0
- data/lib/api_matchers/http_status_code/be_unauthorized.rb +17 -0
- data/lib/api_matchers/http_status_code/create_resource.rb +17 -0
- data/lib/api_matchers/response_body/base.rb +46 -0
- data/lib/api_matchers/response_body/have_json_node.rb +25 -0
- data/lib/api_matchers/response_body/have_node.rb +6 -0
- data/lib/api_matchers/response_body/have_xml_node.rb +19 -0
- data/lib/api_matchers/version.rb +3 -0
- data/lib/api_matchers.rb +45 -0
- data/spec/api_matchers/core/find_in_json_spec.rb +27 -0
- data/spec/api_matchers/core/setup_spec.rb +4 -0
- data/spec/api_matchers/headers/base_spec.rb +12 -0
- data/spec/api_matchers/headers/be_json_spec.rb +23 -0
- data/spec/api_matchers/headers/be_xml_spec.rb +27 -0
- data/spec/api_matchers/http_status_code/base_spec.rb +12 -0
- data/spec/api_matchers/http_status_code/be_bad_request_spec.rb +43 -0
- data/spec/api_matchers/http_status_code/be_internal_server_error_spec.rb +43 -0
- data/spec/api_matchers/http_status_code/be_unauthorized_spec.rb +43 -0
- data/spec/api_matchers/http_status_code/create_resource_spec.rb +43 -0
- data/spec/api_matchers/response_body/base_spec.rb +47 -0
- data/spec/api_matchers/response_body/have_json_node_spec.rb +101 -0
- data/spec/api_matchers/response_body/have_node_spec.rb +27 -0
- data/spec/api_matchers/response_body/have_xml_node_spec.rb +102 -0
- data/spec/spec_helper.rb +10 -0
- metadata +134 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module APIMatchers::Core
|
4
|
+
describe FindInJSON do
|
5
|
+
describe "#find" do
|
6
|
+
context 'when node exists' do
|
7
|
+
it "should return the value of the expected key" do
|
8
|
+
FindInJSON.new('product' => 'gateway').find(node: 'product').should eql 'gateway'
|
9
|
+
end
|
10
|
+
|
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'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when node do not exists' do
|
17
|
+
it "should return nil if don't find the expected node" do
|
18
|
+
FindInJSON.new('product' => 'pabx').find(node: 'developers').should be nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return nil if don't find the expected node in the deep JSON" do
|
22
|
+
FindInJSON.new('transaction' => { 'id' => 150, 'error' => {} }).find(node: 'code').should be nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe APIMatchers::Headers::Base do
|
4
|
+
let(:setup) { OpenStruct.new }
|
5
|
+
subject { APIMatchers::Headers::Base.new(setup) }
|
6
|
+
|
7
|
+
describe "#matches?" do
|
8
|
+
it "should raise Not Implement Exception" do
|
9
|
+
expect { subject.matches?('application/xml') }.to raise_error(NotImplementedError, "not implemented on #{subject}")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe APIMatchers::Headers::BeJSON do
|
4
|
+
describe "actual.should be_json" do
|
5
|
+
it "should pass when the actual is json response" do
|
6
|
+
"application/json; charset=utf-8".should be_json
|
7
|
+
end
|
8
|
+
|
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'.})
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "actual.should_not be_json" do
|
15
|
+
it "should pass when the actual is not a json response" do
|
16
|
+
"application/xml; charset=utf-8".should_not be_json
|
17
|
+
end
|
18
|
+
|
19
|
+
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'.})
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe APIMatchers::Headers::BeXML do
|
4
|
+
describe "actual.should be_xml" do
|
5
|
+
it "should pass when the actual is json response" do
|
6
|
+
"application/xml; charset=utf-8".should be_xml
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not pass when the actual is not a json response" do
|
10
|
+
expect {
|
11
|
+
"application/json; charset=utf-8".should be_xml
|
12
|
+
}.to fail_with(%Q{expected a XML response with 'application/xml; charset=utf-8'. Got: 'application/json; charset=utf-8'.})
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "actual.should_not be_xml" do
|
17
|
+
it "should pass when the actual is not a json response" do
|
18
|
+
"application/json; charset=utf-8".should_not be_xml
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should not pass when the actual is a json response" do
|
22
|
+
expect {
|
23
|
+
"application/xml; charset=utf-8".should_not be_xml
|
24
|
+
}.to fail_with(%Q{expected to not be a XML response. Got: 'application/xml; charset=utf-8'.})
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe APIMatchers::HTTPStatusCode::Base do
|
4
|
+
let(:setup) { OpenStruct.new }
|
5
|
+
subject { APIMatchers::HTTPStatusCode::Base.new(setup) }
|
6
|
+
|
7
|
+
describe "#matches?" do
|
8
|
+
it "should raise Not Implement Exception" do
|
9
|
+
expect { subject.matches?(302) }.to raise_error(NotImplementedError, "not implemented on #{subject}")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe APIMatchers::HTTPStatusCode::BeBadRequest do
|
4
|
+
describe "should be_bad_request" do
|
5
|
+
it "should passes if the actual is equal to 400" do
|
6
|
+
400.should be_bad_request
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should fails if the actual is not equal to 400" do
|
10
|
+
expect { 401.should be_bad_request }.to fail_with(%Q{expected that '401' to be a Bad Request with the status '400'.})
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "should_not be_bad_request" do
|
15
|
+
it "should passes if the actual is not equal to 400" do
|
16
|
+
401.should_not be_bad_request
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should fail if the actual is equal to 400" do
|
20
|
+
expect { 400.should_not be_bad_request }.to fail_with(%Q{expected that '400' to NOT be a Bad Request with the status '400'.})
|
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 400" do
|
34
|
+
response = OpenStruct.new(:http_status => 400)
|
35
|
+
response.should be_bad_request
|
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_bad_request }.to fail_with(%Q{expected that '500' to be a Bad Request with the status '400'.})
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe APIMatchers::HTTPStatusCode::BeInternalServerError do
|
4
|
+
describe "should be_internal_server_error" do
|
5
|
+
it "should passes if the actual is equal to 500" do
|
6
|
+
500.should be_internal_server_error
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should fails if the actual is not equal to 500" do
|
10
|
+
expect { 401.should be_internal_server_error }.to fail_with(%Q{expected that '401' to be Internal Server Error with the status '500'.})
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "should_not be_internal_server_error" do
|
15
|
+
it "should passes if the actual is not equal to 500" do
|
16
|
+
400.should_not be_internal_server_error
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should fail if the actual is equal to 500" do
|
20
|
+
expect { 500.should_not be_internal_server_error }.to fail_with(%Q{expected that '500' to NOT be Internal Server Error.})
|
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 500" do
|
34
|
+
response = OpenStruct.new(:http_status => 500)
|
35
|
+
response.should be_internal_server_error
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should fail if the actual.http_status is not equal to 500" do
|
39
|
+
response = OpenStruct.new(:http_status => 402)
|
40
|
+
expect { response.should be_internal_server_error }.to fail_with(%Q{expected that '402' to be Internal Server Error with the status '500'.})
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe APIMatchers::HTTPStatusCode::BeUnauthorized do
|
4
|
+
describe "should be_unauthorized" do
|
5
|
+
it "should passes if the actual is equal to 401" do
|
6
|
+
401.should be_unauthorized
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should fails if the actual is not equal to 401" do
|
10
|
+
expect { 400.should be_unauthorized }.to fail_with(%Q{expected that '400' to be Unauthorized with the status '401'.})
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "should_not be_unauthorized" do
|
15
|
+
it "should passes if the actual is not equal to 401" do
|
16
|
+
201.should_not be_unauthorized
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should fail if the actual is equal equal to 401" do
|
20
|
+
expect { 401.should_not be_unauthorized }.to fail_with(%Q{expected that '401' to NOT be Unauthorized.})
|
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 401" do
|
34
|
+
response = OpenStruct.new(:http_status => 401)
|
35
|
+
response.should be_unauthorized
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should fail if the actual.http_status is not equal to 401" do
|
39
|
+
response = OpenStruct.new(:http_status => 402)
|
40
|
+
expect { response.should be_unauthorized }.to fail_with(%Q{expected that '402' to be Unauthorized with the status '401'.})
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe APIMatchers::HTTPStatusCode::CreateResource do
|
4
|
+
describe "should create_resource" do
|
5
|
+
it "should passes if the actual is equal to 201" do
|
6
|
+
201.should create_resource
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should fails if the actual is not equal to 201" do
|
10
|
+
expect { 200.should create_resource }.to fail_with(%Q{expected that '200' to be Created Resource with the status '201'.})
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "should_not create_resource" do
|
15
|
+
it "should passes if the actual is not equal to 201" do
|
16
|
+
401.should_not create_resource
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should fail if the actual is equal equal to 201" do
|
20
|
+
expect { 201.should_not create_resource }.to fail_with(%Q{expected that '201' to NOT be Created Resource.})
|
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 201" do
|
34
|
+
response = OpenStruct.new(:http_status => 201)
|
35
|
+
response.should create_resource
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should fail if the actual.http_status is not equal to 201" do
|
39
|
+
response = OpenStruct.new(:http_status => 402)
|
40
|
+
expect { response.should create_resource }.to fail_with(%Q{expected that '402' to be Created Resource with the status '201'.})
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe APIMatchers::ResponseBody::Base do
|
4
|
+
let(:setup) { OpenStruct.new(:response_body_method => :body) }
|
5
|
+
subject { APIMatchers::ResponseBody::Base.new(setup: setup, expected_node: :status) }
|
6
|
+
|
7
|
+
describe "#matches?" do
|
8
|
+
it "should raise Not Implemented Error" do
|
9
|
+
expect {
|
10
|
+
subject.matches?("foo")
|
11
|
+
}.to raise_error(NotImplementedError, "not implemented on #{subject}")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#setup" do
|
16
|
+
it "should read from the initialize" do
|
17
|
+
subject.setup.should equal setup
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#expected_node" do
|
22
|
+
it "should read from the initialize" do
|
23
|
+
subject.expected_node.should equal :status
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#response_body" do
|
28
|
+
let(:body) { { :foo => :bar}.to_json }
|
29
|
+
|
30
|
+
context 'when have configuration' do
|
31
|
+
it "should call the method when is config" do
|
32
|
+
subject.actual = OpenStruct.new(:body => body)
|
33
|
+
subject.response_body.should eql body
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when dont have configuration' do
|
38
|
+
let(:setup) { OpenStruct.new(:response_body_method => nil) }
|
39
|
+
subject { APIMatchers::ResponseBody::Base.new(setup: setup, expected_node: :status) }
|
40
|
+
|
41
|
+
it "should return the actual when do not have config" do
|
42
|
+
subject.actual = body
|
43
|
+
subject.response_body.should eql body
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe APIMatchers::ResponseBody::HaveJsonNode do
|
4
|
+
describe "actual.should have_json_node" do
|
5
|
+
context 'expected key and value in top level' do
|
6
|
+
it "should pass when the expected key exist" do
|
7
|
+
{ :product => 'gateway' }.to_json.should have_json_node(:product)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should fail when the expected key does not exist" do
|
11
|
+
expect {
|
12
|
+
{ :product => 'pabx' }.to_json.should have_json_node(:developers)
|
13
|
+
}.to fail_with(%Q{expected to have node called: 'developers'. Got: '{"product":"pabx"}'})
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should pass when the expected key exist with the expected value" do
|
17
|
+
{ :product => 'payment-gateway' }.to_json.should have_json_node(:product).with('payment-gateway')
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should fail when the expected key exist but the expected value don't exist" do
|
21
|
+
expect {
|
22
|
+
{ :product => 'payment-gateway' }.to_json.should have_json_node(:product).with('email-marketing')
|
23
|
+
}.to fail_with(%Q{expected to have node called: 'product' with value: 'email-marketing'. Got: '{"product":"payment-gateway"}'})
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should not parse the matcher for json when you pass a xml" do
|
27
|
+
expect {
|
28
|
+
"<product><name>webdesk</name></product>".should have_json_node(:name).with('webdesk')
|
29
|
+
}.to fail_with(%Q{expected to have node called: 'name' with value: 'webdesk'. Got: '<product><name>webdesk</name></product>'})
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'expected key and value in more deep in the JSON' do
|
34
|
+
it "should pass when the expected key exist" do
|
35
|
+
{ :transaction => { :id => 150 } }.to_json.should have_json_node(:id)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should pass when the expected key and expected value exist" do
|
39
|
+
{ :transaction => { :error => { :code => '999' } } }.to_json.should have_json_node(:code).with('999')
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should fail when the expected key does not exist" do
|
43
|
+
expect {
|
44
|
+
{ :transaction => { :id => 150, :error => {} } }.to_json.should have_json_node(:code)
|
45
|
+
}.to fail_with(%Q{expected to have node called: 'code'. Got: '{"transaction":{"id":150,"error":{}}}'})
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should fail when the expected key exist but don't exist the expected value" do
|
49
|
+
expect {
|
50
|
+
{ :transaction => { :id => 150, :error => { :code => '999' } } }.to_json.should have_json_node(:code).with('001')
|
51
|
+
}.to fail_with(%Q{expected to have node called: 'code' with value: '001'. Got: '{"transaction":{"id":150,"error":{"code":"999"}}}'})
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "actual.should_not have_json_node" do
|
57
|
+
it "should pass when don't have the expected node in root level" do
|
58
|
+
{ :product => 'gateway' }.to_json.should_not have_json_node(:status)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should pass when don't have the expected node in any level" do
|
62
|
+
{ :transaction => { :id => 12, :status => 'paid' } }.to_json.should_not have_json_node(:error)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should fail when the expected key exist" do
|
66
|
+
expect {
|
67
|
+
{ :status => 'paid' }.to_json.should_not have_json_node(:status)
|
68
|
+
}.to fail_with(%Q{expected to NOT have node called: 'status'. Got: '{"status":"paid"}'})
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should pass when have the expected key but have a different value" do
|
72
|
+
{ :status => 'paid' }.to_json.should_not have_json_node(:status).with('not_authorized')
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should fail when have the expected key and have the expected value" do
|
76
|
+
expect {
|
77
|
+
{ :status => 'paid' }.to_json.should_not have_json_node(:status).with('paid')
|
78
|
+
}.to fail_with(%Q{expected to NOT have node called: 'status' with value: 'paid'. Got: '{"status":"paid"}'})
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "with change configuration" do
|
83
|
+
before do
|
84
|
+
APIMatchers.setup { |config| config.response_body_method = :response_body }
|
85
|
+
end
|
86
|
+
|
87
|
+
after do
|
88
|
+
APIMatchers.setup { |config| config.response_body_method = nil }
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should pass if the actual.http_status is equal to 400" do
|
92
|
+
response = OpenStruct.new(:response_body => { :foo => :bar }.to_json)
|
93
|
+
response.should have_json_node(:foo).with('bar')
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should fail if the actual.http_status is not equal to 400" do
|
97
|
+
response = OpenStruct.new(:response_body => { :baz => :foo}.to_json)
|
98
|
+
expect { response.should have_json_node(:bar) }.to fail_with(%Q{expected to have node called: 'bar'. Got: '{"baz":"foo"}'})
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe APIMatchers::ResponseBody::HaveNode do
|
4
|
+
describe "in json" do
|
5
|
+
before do
|
6
|
+
APIMatchers.setup { |config| config.have_node_matcher = :json }
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should parse the matcher for json" do
|
10
|
+
{ :product => 'chat' }.to_json.should have_node(:product).with('chat')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "in xml" do
|
15
|
+
before do
|
16
|
+
APIMatchers.setup { |config| config.have_node_matcher = :xml }
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should parse the matcher for xml" do
|
20
|
+
"<product>chat</product>".should have_node(:product).with('chat')
|
21
|
+
end
|
22
|
+
|
23
|
+
after do
|
24
|
+
APIMatchers.setup { |config| config.have_node_matcher = :json }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe APIMatchers::ResponseBody::HaveXmlNode do
|
4
|
+
describe "actual.should have_xml_node" do
|
5
|
+
context 'expected key and value in top level' do
|
6
|
+
it "should pass when the expected key exist" do
|
7
|
+
"<product>gateway</product>".should have_xml_node(:product)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should fail when the expected key does not exist" do
|
11
|
+
expect {
|
12
|
+
"<product>pabx</product>".should have_xml_node(:developers)
|
13
|
+
}.to fail_with(%Q{expected to have node called: 'developers'. Got: '<product>pabx</product>'})
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should pass when the expected key exist with the expected value" do
|
17
|
+
"<product>payment-gateway</product>".should have_xml_node(:product).with('payment-gateway')
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should fail when the expected key exist but the expected value don't exist" do
|
21
|
+
expect {
|
22
|
+
"<product>payment-gateway</product>".should have_xml_node(:product).with('email-marketing')
|
23
|
+
}.to fail_with(%Q{expected to have node called: 'product' with value: 'email-marketing'. Got: '<product>payment-gateway</product>'})
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should not parse the matcher for xml when you pass a json" do
|
27
|
+
expect {
|
28
|
+
{ :name => 'webdesk'}.to_json.should have_xml_node(:name).with('webdesk')
|
29
|
+
}.to fail_with(%Q{expected to have node called: 'name' with value: 'webdesk'. Got: '{"name":"webdesk"}'})
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'expected key and value in more deep in the JSON' do
|
34
|
+
it "should pass when the expected key exist" do
|
35
|
+
"<transaction><id>150</id></transaction>".should have_xml_node(:id)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should pass when the expected key and expected value exist" do
|
39
|
+
"<transaction><error><code>999</code></error></transaction>".should have_xml_node(:code).with('999')
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should fail when the expected key does not exist" do
|
43
|
+
expect {
|
44
|
+
"<transaction><error></error></transaction>".should have_xml_node(:code)
|
45
|
+
}.to fail_with(%Q{expected to have node called: 'code'. Got: '<transaction><error></error></transaction>'})
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should fail when the expected key exist but don't exist the expected value" do
|
49
|
+
expect {
|
50
|
+
"<transaction><error><code>999</code></error></transaction>".should have_xml_node(:code).with('001')
|
51
|
+
}.to fail_with(%Q{expected to have node called: 'code' with value: '001'. Got: '<transaction><error><code>999</code></error></transaction>'})
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "actual.should_not have_xml_node" do
|
57
|
+
it "should pass when don't have the expected node in root level" do
|
58
|
+
"<product>gateway</product>".should_not have_xml_node(:status)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should pass when don't have the expected node in any level" do
|
62
|
+
"<transaction><id>12</id><status>paid</status></transaction>".should_not have_xml_node(:error)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should fail when the expected key exist" do
|
66
|
+
expect {
|
67
|
+
"<transaction><status>paid</status></transaction>".should_not have_xml_node(:status)
|
68
|
+
}.to fail_with(%Q{expected to NOT have node called: 'status'. Got: '<transaction><status>paid</status></transaction>'})
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should pass when have the expected key but have a different value" do
|
72
|
+
"<status>paid</status>".should_not have_xml_node(:status).with('not_authorized')
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should fail when have the expected key and have the expected value" do
|
76
|
+
expect {
|
77
|
+
"<transaction><status>paid</status></transaction>".should_not have_xml_node(:status).with('paid')
|
78
|
+
}.to fail_with(%Q{expected to NOT have node called: 'status' with value: 'paid'. Got: '<transaction><status>paid</status></transaction>'})
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
describe "with change configuration" do
|
84
|
+
before do
|
85
|
+
APIMatchers.setup { |config| config.response_body_method = :response_body }
|
86
|
+
end
|
87
|
+
|
88
|
+
after do
|
89
|
+
APIMatchers.setup { |config| config.response_body_method = nil }
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should pass if the actual.http_status is equal to 400" do
|
93
|
+
response = OpenStruct.new(:response_body => "<foo>bar</foo>")
|
94
|
+
response.should have_xml_node(:foo).with('bar')
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should fail if the actual.http_status is not equal to 400" do
|
98
|
+
response = OpenStruct.new(:response_body => "<baz>bar</baz>")
|
99
|
+
expect { response.should have_xml_node(:bar) }.to fail_with(%Q{expected to have node called: 'bar'. Got: '<baz>bar</baz>'})
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|