Class: Pact::Consumer::HttpInteractionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/consumer/http_interaction_builder.rb

Defined Under Namespace

Classes: CreateInteractionError, InteractionBuilderError, InteractionMismatchesError

Constant Summary collapse

CREATE_INTERACTION_ERRORS =
{
  1 => {reason: :internal_error, status: 1, description: "A general panic was caught"},
  2 => {reason: :mock_server_already_running, status: 2, description: "The mock server has already been started"},
  3 => {reason: :invalid_handle, status: 3, description: "The interaction handle is invalid"},
  4 => {reason: :invalid_content_type, status: 4, description: "The content type is not valid"},
  5 => {reason: :invalid_contents, status: 5, description: "The contents JSON is not valid JSON"},
  6 => {reason: :plugin_error, status: 6, description: "The plugin returned an error"}
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pact_config, description: nil) ⇒ HttpInteractionBuilder

Returns a new instance of HttpInteractionBuilder.



34
35
36
37
38
39
40
41
42
# File 'lib/pact/consumer/http_interaction_builder.rb', line 34

def initialize(pact_config, description: nil)
  @pact_config = pact_config
  @description = description || ""

  @pact_handle = pact_config.pact_handle ||= init_pact
  @pact_interaction = PactFfi.new_interaction(pact_handle, @description)

  ObjectSpace.define_finalizer(self, self.class.create_finalizer(pact_interaction))
end

Class Method Details

.create_finalizer(pact_handle) ⇒ Object



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

def create_finalizer(pact_handle)
  proc { PactFfi.free_pact_handle(pact_handle) }
end

Instance Method Details

#execute(&block) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pact/consumer/http_interaction_builder.rb', line 106

def execute(&block)
  raise InteractionBuilderError.new("interaction is designed to be used one-time only") if defined?(@used)

  mock_server = MockServer.create_for_http!(
    pact: pact_handle, host: pact_config.mock_host, port: pact_config.mock_port
  )

  yield(mock_server)

ensure
  if mock_server.matched?
    mock_server.write_pacts!(pact_config.pact_dir)
  else
    msg = mismatches_error_msg(mock_server)
    raise InteractionMismatchesError.new(msg)
  end
  @used = true
  mock_server&.cleanup
  # Reset the pact handle to allow for a new interaction to be built
  # without previous interactions being included
  @pact_config.reset_pact
end

#given(provider_state, metadata = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/pact/consumer/http_interaction_builder.rb', line 44

def given(provider_state,  = {})
  if .present?
    PactFfi.given_with_params(pact_interaction, provider_state, JSON.dump())
  else
    PactFfi.given(pact_interaction, provider_state)
  end

  self
end

#upon_receiving(description) ⇒ Object



54
55
56
57
58
# File 'lib/pact/consumer/http_interaction_builder.rb', line 54

def upon_receiving(description)
  @description = description
  PactFfi.upon_receiving(pact_interaction, @description)
  self
end

#will_respond_with(status: nil, headers: {}, body: nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pact/consumer/http_interaction_builder.rb', line 91

def will_respond_with(status: nil, headers: {}, body: nil)
  interaction_part = PactFfi::FfiInteractionPart["INTERACTION_PART_RESPONSE"]
  PactFfi.response_status(pact_interaction, status)

  InteractionContents.basic(headers).each_pair do |key, value_item|
    PactFfi.with_header_v2(pact_interaction, interaction_part, key.to_s, 0, format_value(value_item))
  end

  if body
    PactFfi.with_body(pact_interaction, interaction_part, "application/json", format_value(InteractionContents.basic(body)))
  end

  self
end

#with_request(method: nil, path: nil, query: {}, headers: {}, body: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pact/consumer/http_interaction_builder.rb', line 60

def with_request(method: nil, path: nil, query: {}, headers: {}, body: nil)
  interaction_part = PactFfi::FfiInteractionPart["INTERACTION_PART_REQUEST"]
  PactFfi.with_request(pact_interaction, method.to_s, format_value(path))

  # Processing as an array of hashes, allows us to consider duplicate keys
  # which should be passed to the core, at a non 0 index
  if query.is_a?(Array)
    key_index = Hash.new(0)
    query.each do |query_item|
      InteractionContents.basic(query_item).each_pair do |key, value_item|
        PactFfi.with_query_parameter_v2(pact_interaction, key.to_s, key_index[key], format_value(value_item))
        key_index[key] += 1
      end
    end
  else
    InteractionContents.basic(query).each_pair do |key, value_item|
      PactFfi.with_query_parameter_v2(pact_interaction, key.to_s, 0, format_value(value_item))
    end
  end

  InteractionContents.basic(headers).each_pair do |key, value_item|
    PactFfi.with_header_v2(pact_interaction, interaction_part, key.to_s, 0, format_value(value_item))
  end

  if body
    PactFfi.with_body(pact_interaction, interaction_part, "application/json", format_value(InteractionContents.basic(body)))
  end

  self
end