Class: Sbmt::Pact::Consumer::HttpInteractionBuilder

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

Defined Under Namespace

Classes: CreateInteractionError, InteractionBuilderError, InteractionMismatchesError

Constant Summary collapse

DESCRIPTION_PREFIX =
"http: "
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/sbmt/pact/consumer/http_interaction_builder.rb', line 35

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

  @pact_handle = init_pact
  @pact_interaction = PactFfi.new_interaction(pact_handle, full_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/sbmt/pact/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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sbmt/pact/consumer/http_interaction_builder.rb', line 95

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)

  if mock_server.matched?
    mock_server.write_pacts!(pact_config.pact_dir)
  else
    msg = mismatches_error_msg(mock_server)
    raise InteractionMismatchesError.new(msg)
  end
ensure
  @used = true
  mock_server&.cleanup
end

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



45
46
47
48
49
50
51
52
53
# File 'lib/sbmt/pact/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/sbmt/pact/consumer/http_interaction_builder.rb', line 55

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

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sbmt/pact/consumer/http_interaction_builder.rb', line 61

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

  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

  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_response(status, headers: {}, body: nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sbmt/pact/consumer/http_interaction_builder.rb', line 80

def with_response(status, 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