File tree 3 files changed +13
-35
lines changed
spec/lib/pact_broker/pacts
3 files changed +13
-35
lines changed Original file line number Diff line number Diff line change 1
1
require 'digest/sha1'
2
- require 'pact_broker/configuration'
3
- require 'pact_broker/pacts/sort_content'
4
- require 'pact_broker/pacts/parse'
5
- require 'pact_broker/pacts/content'
2
+ require 'pact_broker/pacts/order_object'
6
3
7
4
module PactBroker
8
5
module Pacts
9
6
module GenerateInteractionSha
10
- def self . call interaction_hash
11
- ordered_interaction_hash = interaction_hash . keys . sort . each_with_object ( { } ) do | key , new_interaction_hash |
12
- new_interaction_hash [ key ] = interaction_hash [ key ]
13
- end
7
+ extend OrderObject
14
8
15
- Digest ::SHA1 . hexdigest ( ordered_interaction_hash . to_json )
9
+ def self . call interaction_hash
10
+ Digest ::SHA1 . hexdigest ( order_object ( interaction_hash ) . to_json )
16
11
end
17
12
18
13
def generate_interaction_sha ( interaction_hash )
Original file line number Diff line number Diff line change 2
2
3
3
module PactBroker
4
4
module Pacts
5
- class OrderObject
5
+ module OrderObject
6
6
def self . call thing
7
7
case thing
8
8
when Hash then order_hash ( thing )
@@ -20,6 +20,10 @@ def self.order_hash hash
20
20
new_hash [ key ] = call ( hash [ key ] )
21
21
end
22
22
end
23
+
24
+ def order_object ( thing )
25
+ OrderObject . call ( thing )
26
+ end
23
27
end
24
28
end
25
29
end
Original file line number Diff line number Diff line change @@ -19,47 +19,26 @@ module Pacts
19
19
20
20
let ( :interaction_hash_with_different_key_order ) do
21
21
{
22
- "providerStates" => [
23
- "name" => "bar" ,
24
- "params" => {
25
- "wiffle" => "bar" ,
26
- "meep" => "eek"
27
- }
28
- ] ,
29
- "description" => "foo"
30
- }
31
- end
32
-
33
- let ( :interaction_hash_with_different_params_order ) do
34
- {
35
- "description" => "foo" ,
36
22
"providerStates" => [
37
23
"name" => "bar" ,
38
24
"params" => {
39
25
"meep" => "eek" ,
40
26
"wiffle" => "bar"
41
27
}
42
- ]
28
+ ] ,
29
+ "description" => "foo"
43
30
}
44
31
end
45
32
46
33
subject { GenerateInteractionSha . call ( interaction_hash ) }
47
34
48
35
it "generates a SHA based on the sorted keys" do
49
- expect ( subject ) . to eq "5ec1cc12132d3437a5a2ced5537cdab2d4f44521 "
36
+ expect ( subject ) . to eq "57d06e151eca35083e4d6b585b4d4fab2e2ed6b7 "
50
37
end
51
38
52
- it "generates the same SHA if the top level keys are ordered differently" do
39
+ it "generates the same SHA if the keys are ordered differently" do
53
40
expect ( subject ) . to eq GenerateInteractionSha . call ( interaction_hash_with_different_key_order )
54
41
end
55
-
56
- # This could be a whole lot smarter, but I'm not sure it's worth it.
57
- # eg. order of provider state params doesn't matter, but the ordering
58
- # of the provider states themselves may... who knows.
59
- # Let's not try and be too smart about it until we have a use case to flesh it out.
60
- it "generates a different SHA if any of the other keys are ordered differently" do
61
- expect ( subject ) . to_not eq GenerateInteractionSha . call ( interaction_hash_with_different_params_order )
62
- end
63
42
end
64
43
end
65
44
end
You can’t perform that action at this time.
0 commit comments