Class: Pact::MockService::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/mock_service/client.rb

Constant Summary collapse

MOCK_SERVICE_ADMINISTRATON_HEADERS =
{'X-Pact-Mock-Service' => 'true'}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port, host = 'localhost') ⇒ Client

Returns a new instance of Client.



16
17
18
# File 'lib/pact/mock_service/client.rb', line 16

def initialize port, host = 'localhost'
  @http = Net::HTTP.new(host, port)
end

Class Method Details

.clear_interactions(mock_service_base_url, example_description) ⇒ Object



49
50
51
52
# File 'lib/pact/mock_service/client.rb', line 49

def self.clear_interactions mock_service_base_url, example_description
  uri = URI(mock_service_base_url)
  Net::HTTP.new(uri.host, uri.port).delete("/interactions?example_description=#{CGI.escape(example_description)}", MOCK_SERVICE_ADMINISTRATON_HEADERS)
end

Instance Method Details

#add_expected_interaction(interaction) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/pact/mock_service/client.rb', line 40

def add_expected_interaction interaction
  response = http.request_post(
    '/interactions',
    interaction_json(interaction),
    MOCK_SERVICE_ADMINISTRATON_HEADERS.merge("Content-Type" => "application/json")
  )
  raise AddInteractionError.new("\e[31m#{response.body}\e[m") unless response.is_a? Net::HTTPSuccess
end

#clear_interactions(example_description) ⇒ Object



36
37
38
# File 'lib/pact/mock_service/client.rb', line 36

def clear_interactions example_description
  http.delete("/interactions?example_description=#{CGI.escape(example_description)}", MOCK_SERVICE_ADMINISTRATON_HEADERS)
end

#log(msg) ⇒ Object



25
26
27
# File 'lib/pact/mock_service/client.rb', line 25

def log msg
  http.request_get("/log?msg=#{CGI.escape(msg)}", MOCK_SERVICE_ADMINISTRATON_HEADERS)
end

#verify(example_description) ⇒ Object



20
21
22
23
# File 'lib/pact/mock_service/client.rb', line 20

def verify example_description
  response = http.request_get("/interactions/verification?example_description=#{CGI.escape(example_description)}", MOCK_SERVICE_ADMINISTRATON_HEADERS)
  raise VerificationFailedError.new("\e[31m#{response.body}\e[m") unless response.is_a? Net::HTTPSuccess
end

#wait_for_interactions(wait_max_seconds, poll_interval) ⇒ Object



29
30
31
32
33
34
# File 'lib/pact/mock_service/client.rb', line 29

def wait_for_interactions wait_max_seconds, poll_interval
  wait_until_true(wait_max_seconds, poll_interval) do
    response = http.request_get("/interactions/missing", MOCK_SERVICE_ADMINISTRATON_HEADERS)
    JSON.parse(response.body)['size'] == 0
  end
end

#write_pact(pacticipant_details) ⇒ Object

Raises:



54
55
56
57
58
# File 'lib/pact/mock_service/client.rb', line 54

def write_pact pacticipant_details
  response = http.request_post("/pact", pacticipant_details.to_json, MOCK_SERVICE_ADMINISTRATON_HEADERS.merge("Content-Type" => "application/json"))
  raise WritePactError.new("\e[31m#{response.body}\e[m") unless response.is_a? Net::HTTPSuccess
  response.body
end