Class: Pact::Consumer::MockServiceClient

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/consumer/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(name, port) ⇒ MockServiceClient

Returns a new instance of MockServiceClient.



9
10
11
# File 'lib/pact/consumer/mock_service_client.rb', line 9

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

Class Method Details

.clear_interactions(port, example_description) ⇒ Object



37
38
39
# File 'lib/pact/consumer/mock_service_client.rb', line 37

def self.clear_interactions port, example_description
  Net::HTTP.new("localhost", port).delete("/interactions?example_description=#{URI.encode(example_description)}", MOCK_SERVICE_ADMINISTRATON_HEADERS)
end

Instance Method Details

#add_expected_interaction(interaction) ⇒ Object



33
34
35
# File 'lib/pact/consumer/mock_service_client.rb', line 33

def add_expected_interaction interaction
  http.request_post('/interactions', MockServiceInteractionExpectation.new(interaction).to_json, MOCK_SERVICE_ADMINISTRATON_HEADERS)
end

#clear_interactions(example_description) ⇒ Object



29
30
31
# File 'lib/pact/consumer/mock_service_client.rb', line 29

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

#log(msg) ⇒ Object



18
19
20
# File 'lib/pact/consumer/mock_service_client.rb', line 18

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

#verify(example_description) ⇒ Object



13
14
15
16
# File 'lib/pact/consumer/mock_service_client.rb', line 13

def verify example_description
  response = http.request_get("/verify?example_description=#{URI.encode(example_description)}", MOCK_SERVICE_ADMINISTRATON_HEADERS)
  raise response.body unless response.is_a? Net::HTTPSuccess
end

#wait_for_interactions(wait_max_seconds, poll_interval) ⇒ Object



22
23
24
25
26
27
# File 'lib/pact/consumer/mock_service_client.rb', line 22

def wait_for_interactions wait_max_seconds, poll_interval
  wait_until_true(wait_max_seconds, poll_interval) do
    response = http.request_get("/number_of_missing_interactions", MOCK_SERVICE_ADMINISTRATON_HEADERS)
    response.body == '0'
  end
end