1
1
require 'net/http'
2
2
require 'pact/mock_service/interaction_decorator'
3
+ require 'pact/errors'
3
4
4
5
module Pact
5
6
module MockService
7
+
8
+ class VerificationFailedError < Pact ::Error ; end
9
+ class AddInteractionError < Pact ::Error ; end
10
+ class WritePactError < Pact ::Error ; end
11
+
6
12
class Client
7
13
8
14
MOCK_SERVICE_ADMINISTRATON_HEADERS = { 'X-Pact-Mock-Service' => 'true' }
@@ -13,7 +19,7 @@ def initialize port
13
19
14
20
def verify example_description
15
21
response = http . request_get ( "/interactions/verification?example_description=#{ CGI . escape ( example_description ) } " , MOCK_SERVICE_ADMINISTRATON_HEADERS )
16
- raise "\e [31m#{ response . body } \e [m" unless response . is_a? Net ::HTTPSuccess
22
+ raise VerificationFailedError . new ( "\e [31m#{ response . body } \e [m" ) unless response . is_a? Net ::HTTPSuccess
17
23
end
18
24
19
25
def log msg
@@ -37,7 +43,7 @@ def add_expected_interaction interaction
37
43
interaction_json ( interaction ) ,
38
44
MOCK_SERVICE_ADMINISTRATON_HEADERS . merge ( "Content-Type" => "application/json" )
39
45
)
40
- raise "\e [31m#{ response . body } \e [m" unless response . is_a? Net ::HTTPSuccess
46
+ raise AddInteractionError . new ( "\e [31m#{ response . body } \e [m" ) unless response . is_a? Net ::HTTPSuccess
41
47
end
42
48
43
49
def self . clear_interactions port , example_description
@@ -46,7 +52,7 @@ def self.clear_interactions port, example_description
46
52
47
53
def write_pact pacticipant_details
48
54
response = http . request_post ( "/pact" , pacticipant_details . to_json , MOCK_SERVICE_ADMINISTRATON_HEADERS . merge ( "Content-Type" => "application/json" ) )
49
- raise "\e [31m#{ response . body } \e [m" unless response . is_a? Net ::HTTPSuccess
55
+ raise WritePactError . new ( "\e [31m#{ response . body } \e [m" ) unless response . is_a? Net ::HTTPSuccess
50
56
response . body
51
57
end
52
58
@@ -67,7 +73,6 @@ def wait_until_true timeout=3, interval=0.1
67
73
def interaction_json interaction
68
74
Pact ::MockService ::InteractionDecorator . new ( interaction ) . to_json
69
75
end
70
-
71
76
end
72
77
end
73
- end
78
+ end
0 commit comments