Class: Pact::V2::Consumer::HttpInteractionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/v2/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.



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

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



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

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

Instance Method Details

#execute(&block) ⇒ Object



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

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



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

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



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

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

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



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

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



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
90
# File 'lib/pact/v2/consumer/http_interaction_builder.rb', line 61

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