pacto 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +2 -0
- data/lib/pacto.rb +1 -0
- data/lib/pacto/contract_factory.rb +39 -0
- data/lib/pacto/exceptions/invalid_contract.rb +2 -0
- data/lib/pacto/version.rb +1 -1
- data/spec/unit/data/contract.json +2 -1
- data/spec/unit/pacto/contract_factory_spec.rb +30 -0
- metadata +4 -3
data/.travis.yml
CHANGED
data/lib/pacto.rb
CHANGED
@@ -3,9 +3,48 @@ module Pacto
|
|
3
3
|
def self.build_from_file(contract_path, host, file_pre_processor)
|
4
4
|
contract_definition_expanded = file_pre_processor.process(File.read(contract_path))
|
5
5
|
definition = JSON.parse(contract_definition_expanded)
|
6
|
+
validate_contract definition, contract_path
|
6
7
|
request = Request.new(host, definition["request"])
|
7
8
|
response = Response.new(definition["response"])
|
8
9
|
Contract.new(request, response)
|
9
10
|
end
|
11
|
+
|
12
|
+
def self.validate_contract definition, contract_path
|
13
|
+
contract_format = {
|
14
|
+
type: "object",
|
15
|
+
required: true,
|
16
|
+
properties: {
|
17
|
+
request: {
|
18
|
+
type: "object",
|
19
|
+
required: true,
|
20
|
+
properties: {
|
21
|
+
method: {type: "string", required: true, pattern: "(GET)|(POST)|(PUT)|(DELETE)"},
|
22
|
+
path: {type: "string", required: true},
|
23
|
+
params: {type: "object", required: true},
|
24
|
+
headers: {type: "object", required: true}
|
25
|
+
}
|
26
|
+
},
|
27
|
+
response: {
|
28
|
+
type: "object",
|
29
|
+
required: true,
|
30
|
+
properties: {
|
31
|
+
status: {type: "integer", required: true},
|
32
|
+
headers: {type: "object", required: true},
|
33
|
+
body: {
|
34
|
+
type: "object",
|
35
|
+
required: false,
|
36
|
+
properties: {
|
37
|
+
type: { type: "string", required: true, pattern: "(string)|(object)|(array)"}
|
38
|
+
}
|
39
|
+
}
|
40
|
+
}
|
41
|
+
}
|
42
|
+
}
|
43
|
+
}.to_json
|
44
|
+
errors = JSON::Validator.fully_validate(contract_format, definition)
|
45
|
+
unless errors.empty?
|
46
|
+
raise InvalidContract, errors.join("\n")
|
47
|
+
end
|
48
|
+
end
|
10
49
|
end
|
11
50
|
end
|
data/lib/pacto/version.rb
CHANGED
@@ -18,5 +18,35 @@ module Pacto
|
|
18
18
|
described_class.build_from_file(contract_path, host, file_pre_processor)
|
19
19
|
end
|
20
20
|
end
|
21
|
+
|
22
|
+
describe '.validate_contract' do
|
23
|
+
it 'should not raise error if contract is correct' do
|
24
|
+
expect {
|
25
|
+
definition = {
|
26
|
+
'request' => {
|
27
|
+
'method' => 'GET',
|
28
|
+
'path' => '/a/path',
|
29
|
+
'params' => {},
|
30
|
+
'headers' => {}
|
31
|
+
},
|
32
|
+
'response' => {
|
33
|
+
'status' => 200,
|
34
|
+
'headers' => {},
|
35
|
+
'body' => {
|
36
|
+
'type' => 'string',
|
37
|
+
'required' => true
|
38
|
+
}
|
39
|
+
}
|
40
|
+
}
|
41
|
+
described_class.validate_contract(definition, contract_path)
|
42
|
+
}.not_to raise_error
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should raise InvalidContract if contract do not contain a Request' do
|
46
|
+
expect {
|
47
|
+
described_class.validate_contract({}, contract_path)
|
48
|
+
}.to raise_error(InvalidContract)
|
49
|
+
end
|
50
|
+
end
|
21
51
|
end
|
22
52
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pacto
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -241,6 +241,7 @@ files:
|
|
241
241
|
- lib/pacto.rb
|
242
242
|
- lib/pacto/contract.rb
|
243
243
|
- lib/pacto/contract_factory.rb
|
244
|
+
- lib/pacto/exceptions/invalid_contract.rb
|
244
245
|
- lib/pacto/extensions.rb
|
245
246
|
- lib/pacto/file_pre_processor.rb
|
246
247
|
- lib/pacto/instantiated_contract.rb
|
@@ -281,7 +282,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
281
282
|
version: '0'
|
282
283
|
segments:
|
283
284
|
- 0
|
284
|
-
hash:
|
285
|
+
hash: 3687165509434544581
|
285
286
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
286
287
|
none: false
|
287
288
|
requirements:
|
@@ -290,7 +291,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
290
291
|
version: '0'
|
291
292
|
segments:
|
292
293
|
- 0
|
293
|
-
hash:
|
294
|
+
hash: 3687165509434544581
|
294
295
|
requirements: []
|
295
296
|
rubyforge_project:
|
296
297
|
rubygems_version: 1.8.25
|