pacto 0.3.0.pre → 0.3.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 +7 -0
- data/.gitignore +2 -0
- data/.rubocop-todo.yml +0 -27
- data/.rubocop.yml +9 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -5
- data/CONTRIBUTING.md +112 -0
- data/Gemfile +5 -0
- data/Guardfile +18 -13
- data/README.md +157 -101
- data/Rakefile +3 -3
- data/features/configuration/strict_matchers.feature +97 -0
- data/features/evolve/README.md +11 -0
- data/features/evolve/existing_services.feature +82 -0
- data/features/generate/README.md +5 -0
- data/features/generate/generation.feature +28 -0
- data/features/steps/pacto_steps.rb +75 -0
- data/features/stub/README.md +2 -0
- data/features/stub/templates.feature +46 -0
- data/features/support/env.rb +11 -5
- data/features/validate/README.md +1 -0
- data/features/validate/body_only.feature +85 -0
- data/features/{journeys/validation.feature → validate/meta_validation.feature} +41 -24
- data/features/validate/validation.feature +36 -0
- data/lib/pacto.rb +61 -33
- data/lib/pacto/contract.rb +18 -15
- data/lib/pacto/contract_factory.rb +14 -11
- data/lib/pacto/contract_files.rb +17 -0
- data/lib/pacto/contract_list.rb +17 -0
- data/lib/pacto/contract_validator.rb +29 -0
- data/lib/pacto/core/configuration.rb +19 -17
- data/lib/pacto/core/contract_registry.rb +43 -0
- data/lib/pacto/core/{callback.rb → hook.rb} +3 -3
- data/lib/pacto/core/modes.rb +33 -0
- data/lib/pacto/core/validation_registry.rb +45 -0
- data/lib/pacto/erb_processor.rb +0 -1
- data/lib/pacto/extensions.rb +18 -4
- data/lib/pacto/generator.rb +34 -49
- data/lib/pacto/generator/filters.rb +41 -0
- data/lib/pacto/hooks/erb_hook.rb +4 -3
- data/lib/pacto/logger.rb +4 -2
- data/lib/pacto/meta_schema.rb +4 -2
- data/lib/pacto/rake_task.rb +28 -25
- data/lib/pacto/request_clause.rb +43 -0
- data/lib/pacto/request_pattern.rb +8 -0
- data/lib/pacto/response_clause.rb +15 -0
- data/lib/pacto/rspec.rb +102 -0
- data/lib/pacto/stubs/uri_pattern.rb +23 -0
- data/lib/pacto/stubs/webmock_adapter.rb +69 -0
- data/lib/pacto/stubs/webmock_helper.rb +71 -0
- data/lib/pacto/ui.rb +7 -0
- data/lib/pacto/uri.rb +9 -0
- data/lib/pacto/validation.rb +57 -0
- data/lib/pacto/validators/body_validator.rb +41 -0
- data/lib/pacto/validators/request_body_validator.rb +23 -0
- data/lib/pacto/validators/response_body_validator.rb +23 -0
- data/lib/pacto/validators/response_header_validator.rb +49 -0
- data/lib/pacto/validators/response_status_validator.rb +24 -0
- data/lib/pacto/version.rb +1 -1
- data/pacto.gemspec +33 -29
- data/resources/contract_schema.json +8 -176
- data/resources/draft-03.json +174 -0
- data/spec/integration/data/strict_contract.json +2 -2
- data/spec/integration/e2e_spec.rb +22 -31
- data/spec/integration/rspec_spec.rb +94 -0
- data/spec/integration/templating_spec.rb +9 -12
- data/{lib → spec}/pacto/server.rb +0 -0
- data/{lib → spec}/pacto/server/dummy.rb +11 -8
- data/{lib → spec}/pacto/server/playback_servlet.rb +1 -1
- data/spec/spec_helper.rb +2 -0
- data/spec/unit/hooks/erb_hook_spec.rb +15 -15
- data/spec/unit/pacto/configuration_spec.rb +2 -10
- data/spec/unit/pacto/contract_factory_spec.rb +16 -13
- data/spec/unit/pacto/contract_files_spec.rb +42 -0
- data/spec/unit/pacto/contract_list_spec.rb +35 -0
- data/spec/unit/pacto/contract_spec.rb +43 -44
- data/spec/unit/pacto/contract_validator_spec.rb +85 -0
- data/spec/unit/pacto/core/configuration_spec.rb +4 -11
- data/spec/unit/pacto/core/contract_registry_spec.rb +119 -0
- data/spec/unit/pacto/core/modes_spec.rb +18 -0
- data/spec/unit/pacto/core/validation_registry_spec.rb +76 -0
- data/spec/unit/pacto/core/validation_spec.rb +60 -0
- data/spec/unit/pacto/extensions_spec.rb +14 -23
- data/spec/unit/pacto/generator/filters_spec.rb +99 -0
- data/spec/unit/pacto/generator_spec.rb +34 -73
- data/spec/unit/pacto/meta_schema_spec.rb +46 -6
- data/spec/unit/pacto/pacto_spec.rb +17 -15
- data/spec/unit/pacto/{request_spec.rb → request_clause_spec.rb} +32 -44
- data/spec/unit/pacto/request_pattern_spec.rb +22 -0
- data/spec/unit/pacto/response_clause_spec.rb +54 -0
- data/spec/unit/pacto/stubs/uri_pattern_spec.rb +28 -0
- data/spec/unit/pacto/stubs/webmock_adapter_spec.rb +205 -0
- data/spec/unit/pacto/stubs/webmock_helper_spec.rb +20 -0
- data/spec/unit/pacto/uri_spec.rb +20 -0
- data/spec/unit/pacto/validators/body_validator_spec.rb +105 -0
- data/spec/unit/pacto/validators/response_header_validator_spec.rb +94 -0
- data/spec/unit/pacto/validators/response_status_validator_spec.rb +20 -0
- metadata +230 -146
- data/features/generation/generation.feature +0 -25
- data/lib/pacto/core/contract_repository.rb +0 -44
- data/lib/pacto/hash_merge_processor.rb +0 -14
- data/lib/pacto/request.rb +0 -57
- data/lib/pacto/response.rb +0 -63
- data/lib/pacto/response_adapter.rb +0 -24
- data/lib/pacto/stubs/built_in.rb +0 -57
- data/spec/unit/pacto/core/contract_repository_spec.rb +0 -133
- data/spec/unit/pacto/hash_merge_processor_spec.rb +0 -20
- data/spec/unit/pacto/response_adapter_spec.rb +0 -25
- data/spec/unit/pacto/response_spec.rb +0 -201
- data/spec/unit/pacto/stubs/built_in_spec.rb +0 -168
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
module Pacto
|
|
4
|
+
describe ContractList do
|
|
5
|
+
let(:contract1) { double(:contract_1) }
|
|
6
|
+
let(:contract2) { double(:contract_2) }
|
|
7
|
+
|
|
8
|
+
it 'holds a list of contracts' do
|
|
9
|
+
list = ContractList.new([contract1, contract2])
|
|
10
|
+
expect(list.contracts).to eq([contract1, contract2])
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
context 'when validating' do
|
|
14
|
+
it 'validates every contract on the list' do
|
|
15
|
+
expect(contract1).to receive(:validate_provider)
|
|
16
|
+
expect(contract2).to receive(:validate_provider)
|
|
17
|
+
|
|
18
|
+
list = ContractList.new([contract1, contract2])
|
|
19
|
+
list.validate_all
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
context 'when stubbing' do
|
|
24
|
+
let(:values) { Hash.new }
|
|
25
|
+
|
|
26
|
+
it 'stubs every contract on the list' do
|
|
27
|
+
expect(contract1).to receive(:stub_contract!).with(values)
|
|
28
|
+
expect(contract2).to receive(:stub_contract!).with(values)
|
|
29
|
+
|
|
30
|
+
list = ContractList.new([contract1, contract2])
|
|
31
|
+
list.stub_all(values)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -1,77 +1,76 @@
|
|
|
1
1
|
module Pacto
|
|
2
2
|
describe Contract do
|
|
3
3
|
let(:request) { double 'request' }
|
|
4
|
-
let(:
|
|
5
|
-
let(:response) { double 'response' }
|
|
4
|
+
let(:response) { double 'response definition' }
|
|
6
5
|
let(:provider) { double 'provider' }
|
|
7
|
-
let(:
|
|
6
|
+
let(:file) { 'contranct.json' }
|
|
7
|
+
let(:request_pattern_provider) { double(for: nil) }
|
|
8
8
|
|
|
9
|
-
subject(:contract) {
|
|
9
|
+
subject(:contract) { Contract.new(request, response, file, 'sample', request_pattern_provider) }
|
|
10
10
|
|
|
11
11
|
before do
|
|
12
|
-
response.stub(:instantiate => instantiated_response)
|
|
13
12
|
Pacto.configuration.provider = provider
|
|
14
13
|
end
|
|
15
14
|
|
|
15
|
+
it 'has a request pattern' do
|
|
16
|
+
pattern = double
|
|
17
|
+
expect(request_pattern_provider).to receive(:for).and_return(pattern)
|
|
18
|
+
expect(contract.request_pattern).to eq pattern
|
|
19
|
+
end
|
|
20
|
+
|
|
16
21
|
describe '#stub_contract!' do
|
|
17
|
-
it '
|
|
18
|
-
|
|
19
|
-
provider.should_receive(:stub_request!).with request, instantiated_response
|
|
22
|
+
it 'register a stub for the contract' do
|
|
23
|
+
provider.should_receive(:stub_request!).with(request, response)
|
|
20
24
|
contract.stub_contract!
|
|
21
25
|
end
|
|
22
26
|
end
|
|
23
27
|
|
|
24
|
-
|
|
28
|
+
context 'validations' do
|
|
29
|
+
let(:fake_response) { double('fake response') }
|
|
30
|
+
let(:validation_result) { double 'validation result' }
|
|
31
|
+
|
|
25
32
|
before do
|
|
26
|
-
|
|
27
|
-
request.stub(:execute => fake_response)
|
|
33
|
+
allow(Pacto::ContractValidator).to receive(:validate).with(contract, request, fake_response, {}).and_return validation_result
|
|
28
34
|
end
|
|
29
35
|
|
|
30
|
-
|
|
31
|
-
|
|
36
|
+
describe '#validate_consumer' do
|
|
37
|
+
it 'returns the result of the validation' do
|
|
38
|
+
expect(Pacto::ContractValidator).to receive(:validate).with(contract, request, fake_response, {})
|
|
39
|
+
expect(contract.validate_consumer request, fake_response).to eq validation_result
|
|
40
|
+
end
|
|
32
41
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
42
|
+
it 'does not generate another response' do
|
|
43
|
+
request.should_not_receive :execute
|
|
44
|
+
contract.validate_consumer request, fake_response
|
|
45
|
+
end
|
|
36
46
|
end
|
|
37
47
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
48
|
+
describe '#validate_provider' do
|
|
49
|
+
before do
|
|
50
|
+
allow(request).to receive(:execute).and_return fake_response
|
|
51
|
+
end
|
|
41
52
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
53
|
+
it 'generates the response' do
|
|
54
|
+
request.should_receive :execute
|
|
55
|
+
contract.validate_provider
|
|
56
|
+
end
|
|
46
57
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
contract.validate fake_response
|
|
58
|
+
it 'returns the result of the validating the generated response' do
|
|
59
|
+
expect(Pacto::ContractValidator).to receive(:validate).with(contract, request, fake_response, {})
|
|
60
|
+
expect(contract.validate_provider).to eq validation_result
|
|
51
61
|
end
|
|
52
62
|
end
|
|
53
63
|
end
|
|
54
64
|
|
|
55
65
|
describe '#matches?' do
|
|
56
|
-
let(:
|
|
57
|
-
|
|
58
|
-
matcher.stub(:matches?) { |r| r == request_signature }
|
|
59
|
-
end
|
|
60
|
-
end
|
|
66
|
+
let(:request_pattern) { double(matches?: true) }
|
|
67
|
+
let(:request_signature) { double }
|
|
61
68
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
end
|
|
66
|
-
end
|
|
69
|
+
it 'delegates to the request pattern' do
|
|
70
|
+
expect(request_pattern_provider).to receive(:for).and_return(request_pattern)
|
|
71
|
+
expect(request_pattern).to receive(:matches?).with(request_signature)
|
|
67
72
|
|
|
68
|
-
|
|
69
|
-
it 'returns true if it matches the request' do
|
|
70
|
-
provider.should_receive(:stub_request!).with(request, instantiated_response).and_return(request_matcher)
|
|
71
|
-
contract.stub_contract!
|
|
72
|
-
expect(contract.matches? request_signature).to be_true
|
|
73
|
-
expect(contract.matches? :anything).to be_false
|
|
74
|
-
end
|
|
73
|
+
contract.matches?(request_signature)
|
|
75
74
|
end
|
|
76
75
|
end
|
|
77
76
|
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
module Pacto
|
|
2
|
+
describe ContractValidator do
|
|
3
|
+
let(:validation_errors) { ['some error', 'another error'] }
|
|
4
|
+
|
|
5
|
+
let(:expected_response) do
|
|
6
|
+
ResponseClause.new(
|
|
7
|
+
'status' => 200,
|
|
8
|
+
'headers' => {
|
|
9
|
+
'Content-Type' => 'application/json'
|
|
10
|
+
},
|
|
11
|
+
'body' => {:type => 'object', :required => true, :properties => double('body definition properties')}
|
|
12
|
+
)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
let(:actual_response) do
|
|
16
|
+
double(
|
|
17
|
+
:status => 200,
|
|
18
|
+
:headers => {'Content-Type' => 'application/json', 'Age' => '60'},
|
|
19
|
+
:body => { 'message' => 'response' }
|
|
20
|
+
)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
let(:actual_request) { double :actual_request }
|
|
24
|
+
|
|
25
|
+
let(:expected_request) do
|
|
26
|
+
RequestClause.new(
|
|
27
|
+
'http://example.com',
|
|
28
|
+
'body' => {
|
|
29
|
+
:type => 'object',
|
|
30
|
+
:required => true,
|
|
31
|
+
:properties => double('body definition properties')
|
|
32
|
+
}
|
|
33
|
+
)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
let(:contract) do
|
|
37
|
+
request_pattern_provider = double(for: nil)
|
|
38
|
+
Contract.new(expected_request, expected_response, 'some_file.json', 'sample', request_pattern_provider)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
let(:opts) { {} }
|
|
42
|
+
|
|
43
|
+
describe '.validate' do
|
|
44
|
+
before do
|
|
45
|
+
allow(Pacto::Validators::RequestBodyValidator).to receive(:validate).with(expected_request.schema, actual_request).and_return([])
|
|
46
|
+
allow(Pacto::Validators::ResponseBodyValidator).to receive(:validate).with(expected_response.schema, actual_response).and_return([])
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
context 'default validator stack' do
|
|
50
|
+
it 'calls the RequestBodyValidator' do
|
|
51
|
+
expect(Pacto::Validators::RequestBodyValidator).to receive(:validate).with(expected_request.schema, actual_request).and_return(validation_errors)
|
|
52
|
+
expect(ContractValidator.validate contract, actual_request, actual_response, opts).to eq(validation_errors)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it 'calls the ResponseStatusValidator' do
|
|
56
|
+
expect(Pacto::Validators::ResponseStatusValidator).to receive(:validate).with(expected_response.status, actual_response.status).and_return(validation_errors)
|
|
57
|
+
expect(ContractValidator.validate contract, actual_request, actual_response, opts).to eq(validation_errors)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it 'calls the ResponseHeaderValidator' do
|
|
61
|
+
expect(Pacto::Validators::ResponseHeaderValidator).to receive(:validate).with(expected_response.headers, actual_response.headers).and_return(validation_errors)
|
|
62
|
+
expect(ContractValidator.validate contract, actual_request, actual_response, opts).to eq(validation_errors)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it 'calls the ResponseBodyValidator' do
|
|
66
|
+
expect(Pacto::Validators::ResponseBodyValidator).to receive(:validate).with(expected_response.schema, actual_response).and_return(validation_errors)
|
|
67
|
+
expect(ContractValidator.validate contract, actual_request, actual_response, opts).to eq(validation_errors)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
context 'when headers and body match and the ResponseStatusValidator reports no errors' do
|
|
72
|
+
it 'does not return any errors' do
|
|
73
|
+
# JSON::Validator.should_receive(:fully_validate).
|
|
74
|
+
# with(definition['body'], fake_response.body, :version => :draft3).
|
|
75
|
+
# and_return([])
|
|
76
|
+
expect(Pacto::Validators::RequestBodyValidator).to receive(:validate).with(expected_request.schema, actual_request).and_return([])
|
|
77
|
+
expect(Pacto::Validators::ResponseStatusValidator).to receive(:validate).with(expected_response.status, actual_response.status).and_return([])
|
|
78
|
+
expect(Pacto::Validators::ResponseHeaderValidator).to receive(:validate).with(expected_response.headers, actual_response.headers).and_return([])
|
|
79
|
+
expect(Pacto::Validators::ResponseBodyValidator).to receive(:validate).with(expected_response.schema, actual_response).and_return([])
|
|
80
|
+
expect(ContractValidator.validate contract, actual_request, actual_response, opts).to be_empty
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -1,13 +1,6 @@
|
|
|
1
1
|
describe Pacto do
|
|
2
2
|
describe '.configure' do
|
|
3
3
|
let(:contracts_path) { 'path_to_contracts' }
|
|
4
|
-
it 'allows preprocessor manual configuration' do
|
|
5
|
-
expect(Pacto.configuration.preprocessor).to_not be_nil
|
|
6
|
-
Pacto.configure do |c|
|
|
7
|
-
c.preprocessor = nil
|
|
8
|
-
end
|
|
9
|
-
expect(Pacto.configuration.preprocessor).to be_nil
|
|
10
|
-
end
|
|
11
4
|
|
|
12
5
|
it 'allows contracts_path manual configuration' do
|
|
13
6
|
expect(Pacto.configuration.contracts_path).to be_nil
|
|
@@ -17,12 +10,12 @@ describe Pacto do
|
|
|
17
10
|
expect(Pacto.configuration.contracts_path).to eq(contracts_path)
|
|
18
11
|
end
|
|
19
12
|
|
|
20
|
-
it 'register a Pacto
|
|
21
|
-
|
|
13
|
+
it 'register a Pacto Hook' do
|
|
14
|
+
hook_block = Pacto::Hook.new {}
|
|
22
15
|
Pacto.configure do |c|
|
|
23
|
-
c.
|
|
16
|
+
c.register_hook(hook_block)
|
|
24
17
|
end
|
|
25
|
-
expect(Pacto.configuration.
|
|
18
|
+
expect(Pacto.configuration.hook).to eq(hook_block)
|
|
26
19
|
end
|
|
27
20
|
end
|
|
28
21
|
end
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
require_relative '../../../../lib/pacto/core/contract_registry'
|
|
2
|
+
|
|
3
|
+
module Pacto
|
|
4
|
+
describe ContractRegistry do
|
|
5
|
+
let(:tag) { 'contract_tag' }
|
|
6
|
+
let(:another_tag) { 'another_tag' }
|
|
7
|
+
let(:contract) { double('contract') }
|
|
8
|
+
let(:contract_factory) { double }
|
|
9
|
+
let(:another_contract) { double('another_contract') }
|
|
10
|
+
let(:request_signature) { double('request signature') }
|
|
11
|
+
let(:contracts_that_match) { create_contracts 2, true }
|
|
12
|
+
let(:contracts_that_dont_match) { create_contracts 3, false }
|
|
13
|
+
let(:all_contracts) { contracts_that_match + contracts_that_dont_match }
|
|
14
|
+
|
|
15
|
+
subject(:contract_list) do
|
|
16
|
+
ContractRegistry.new
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
describe '.register' do
|
|
20
|
+
context 'no tag' do
|
|
21
|
+
it 'registers the contract with the default tag' do
|
|
22
|
+
contract_list.register contract
|
|
23
|
+
expect(contract_list[:default]).to include(contract)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
context 'one tag' do
|
|
28
|
+
it 'registers a contract under a given tag' do
|
|
29
|
+
contract_list.register(contract, tag)
|
|
30
|
+
expect(contract_list[tag]).to include(contract)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it 'does not duplicate a contract when it has already been registered with the same tag' do
|
|
34
|
+
contract_list
|
|
35
|
+
.register(contract, tag)
|
|
36
|
+
.register(contract, tag)
|
|
37
|
+
|
|
38
|
+
expect(contract_list[tag]).to include(contract)
|
|
39
|
+
expect(contract_list[tag]).to have(1).items
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
context 'multiple tags' do
|
|
44
|
+
it 'registers a contract using different tags' do
|
|
45
|
+
contract_list.register(contract, tag, another_tag)
|
|
46
|
+
expect(contract_list[tag]).to include(contract)
|
|
47
|
+
expect(contract_list[another_tag]).to include(contract)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it 'registers a tag with different contracts ' do
|
|
51
|
+
contract_list
|
|
52
|
+
.register(contract, tag)
|
|
53
|
+
.register(another_contract, tag)
|
|
54
|
+
|
|
55
|
+
expect(contract_list[tag]).to include(contract, another_contract)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
describe '.use' do
|
|
62
|
+
before do
|
|
63
|
+
contract_list
|
|
64
|
+
.register(contract, tag)
|
|
65
|
+
.register(another_contract, :default)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
context 'when a contract has been registry' do
|
|
69
|
+
let(:response_body) { double('response_body') }
|
|
70
|
+
|
|
71
|
+
it 'stubs a contract with default values' do
|
|
72
|
+
contract.should_receive(:stub_contract!)
|
|
73
|
+
another_contract.should_receive(:stub_contract!)
|
|
74
|
+
contract_list.use(tag)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it 'stubs default contract if unused tag' do
|
|
78
|
+
another_contract.should_receive(:stub_contract!)
|
|
79
|
+
contract_list.use(another_tag)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
context 'when contract has not been registry' do
|
|
84
|
+
it 'raises an argument error' do
|
|
85
|
+
contract_list = ContractRegistry.new
|
|
86
|
+
expect { contract_list.use('unregistry') }.to raise_error ArgumentError
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
describe '.contracts_for' do
|
|
92
|
+
context 'when no contracts are found for a request' do
|
|
93
|
+
it 'returns an empty list' do
|
|
94
|
+
expect(contract_list.contracts_for request_signature).to be_empty
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
context 'when contracts are found for a request' do
|
|
99
|
+
it 'returns the matching contracts' do
|
|
100
|
+
register_and_use all_contracts
|
|
101
|
+
expect(contract_list.contracts_for request_signature).to eq(contracts_that_match)
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def create_contracts(total, matches)
|
|
107
|
+
total.times.map do
|
|
108
|
+
double('contract',
|
|
109
|
+
:stub_contract! => double('request matcher'),
|
|
110
|
+
:matches? => matches)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def register_and_use(contracts)
|
|
115
|
+
contracts.each { |contract| contract_list.register contract }
|
|
116
|
+
contract_list.use :default
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
describe Pacto do
|
|
2
|
+
modes = %w{generate validate}
|
|
3
|
+
modes.each do |mode|
|
|
4
|
+
enable_method = "#{mode}!".to_sym # generate!
|
|
5
|
+
query_method = "#{mode[0..-2]}ing?".to_sym # generating?
|
|
6
|
+
disable_method = "stop_#{mode[0..-2]}ing!".to_sym # stop_generating!
|
|
7
|
+
describe ".#{mode}!" do
|
|
8
|
+
it "tells the provider to enable #{mode} mode" do
|
|
9
|
+
expect(subject.send query_method).to be_false
|
|
10
|
+
subject.send enable_method
|
|
11
|
+
expect(subject.send query_method).to be_true
|
|
12
|
+
|
|
13
|
+
subject.send disable_method
|
|
14
|
+
expect(subject.send query_method).to be_false
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
describe Pacto::ValidationRegistry do
|
|
2
|
+
subject(:registry) { Pacto::ValidationRegistry.instance }
|
|
3
|
+
let(:request_pattern) { WebMock::RequestPattern.new(:get, 'www.example.com') }
|
|
4
|
+
let(:request_signature) { WebMock::RequestSignature.new(:get, 'www.example.com') }
|
|
5
|
+
let(:different_request_signature) { WebMock::RequestSignature.new(:get, 'www.thoughtworks.com') }
|
|
6
|
+
let(:validation) { Pacto::Validation.new(request_signature, double, nil) }
|
|
7
|
+
let(:validation_for_a_similar_request) { Pacto::Validation.new(request_signature, double, nil) }
|
|
8
|
+
let(:validation_for_a_different_request) { Pacto::Validation.new(different_request_signature, double, nil) }
|
|
9
|
+
|
|
10
|
+
before(:each) do
|
|
11
|
+
registry.reset!
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
describe 'reset!' do
|
|
15
|
+
before(:each) do
|
|
16
|
+
registry.register_validation(validation)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'cleans validations' do
|
|
20
|
+
expect { registry.reset! }.to change { registry.validated? request_pattern }.from([validation]).to(nil)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
describe 'registering and reporting registered validations' do
|
|
25
|
+
it 'returns registered validation' do
|
|
26
|
+
expect(registry.register_validation validation).to eq(validation)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it 'reports if validation is not registered' do
|
|
30
|
+
expect(registry.validated? request_pattern).to be_false
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it 'registers and returns matching validations' do
|
|
34
|
+
registry.register_validation(validation)
|
|
35
|
+
registry.register_validation(validation_for_a_similar_request)
|
|
36
|
+
registry.register_validation(validation_for_a_different_request)
|
|
37
|
+
expect(registry.validated? request_pattern).to eq([validation, validation_for_a_similar_request])
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
describe '.unmatched_validations' do
|
|
42
|
+
let(:contract) { double('contract', :name => 'test') }
|
|
43
|
+
|
|
44
|
+
it 'returns validations with no contract' do
|
|
45
|
+
allow(contract).to receive(:validate_consumer).with(different_request_signature, anything).and_return(double('results', :empty? => true))
|
|
46
|
+
validation_with_results = Pacto::Validation.new(different_request_signature, double, contract)
|
|
47
|
+
registry.register_validation(validation)
|
|
48
|
+
registry.register_validation(validation_for_a_similar_request)
|
|
49
|
+
registry.register_validation(validation_for_a_different_request)
|
|
50
|
+
registry.register_validation(validation_with_results)
|
|
51
|
+
|
|
52
|
+
expect(registry.unmatched_validations).to match_array([validation, validation_for_a_similar_request, validation_for_a_different_request])
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
describe '.failed_validations' do
|
|
57
|
+
let(:contract) { double('contract', :name => 'test') }
|
|
58
|
+
let(:results2) { double('results2', :empty? => false, :join => 'wtf') }
|
|
59
|
+
|
|
60
|
+
it 'returns validations with unsuccessful results' do
|
|
61
|
+
allow(contract).to receive(:name).and_return 'test'
|
|
62
|
+
allow(contract).to receive(:validate_consumer).with(request_signature, anything).and_return(results2)
|
|
63
|
+
validation_with_successful_results = Pacto::Validation.new(request_signature, double, contract)
|
|
64
|
+
validation_with_unsuccessful_results = Pacto::Validation.new(request_signature, double, contract)
|
|
65
|
+
|
|
66
|
+
expect(validation_with_successful_results).to receive(:successful?).twice.and_return true
|
|
67
|
+
expect(validation_with_unsuccessful_results).to receive(:successful?).twice.and_return false
|
|
68
|
+
|
|
69
|
+
registry.register_validation(validation)
|
|
70
|
+
registry.register_validation(validation_with_successful_results)
|
|
71
|
+
registry.register_validation(validation_with_unsuccessful_results)
|
|
72
|
+
|
|
73
|
+
expect(registry.failed_validations).to match_array([validation_with_unsuccessful_results])
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|