Class: Pact::Consumer::PluginHttpInteractionBuilder
- Defined in:
- lib/pact/consumer/plugin_http_interaction_builder.rb
Defined Under Namespace
Classes: CreateInteractionError, InteractionBuilderError, InteractionMismatchesError, PluginInitError
Constant Summary collapse
- INIT_PLUGIN_ERRORS =
{ 1 => {reason: :internal_error, status: 1, description: "A general panic was caught"}, 2 => {reason: :plugin_load_failed, status: 2, description: "Failed to load the plugin"}, 3 => {reason: :invalid_handle, status: 3, description: "Pact Handle is not valid"} }.freeze
- 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
Instance Method Summary collapse
- #execute(&block) ⇒ Object
- #given(provider_state, metadata = {}) ⇒ Object
-
#initialize(pact_config, description: nil) ⇒ PluginHttpInteractionBuilder
constructor
A new instance of PluginHttpInteractionBuilder.
- #interaction_json ⇒ Object
- #reference(group, name, value) ⇒ Object
- #upon_receiving(description) ⇒ Object
- #validate! ⇒ Object
- #will_respond_with(status: nil, headers: {}, body: nil) ⇒ Object
- #with_content_type(content_type) ⇒ Object
- #with_plugin(plugin_name, plugin_version) ⇒ Object
- #with_plugin_metadata(meta_hash) ⇒ Object
- #with_request(method: nil, path: nil, query: {}, headers: {}, body: nil) ⇒ Object
- #with_transport(transport) ⇒ Object
Constructor Details
#initialize(pact_config, description: nil) ⇒ PluginHttpInteractionBuilder
Returns a new instance of PluginHttpInteractionBuilder.
36 37 38 39 40 41 42 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 36 def initialize(pact_config, description: nil) @pact_config = pact_config @description = description || "" @contents = nil @provider_state_meta = nil @references = [] end |
Instance Method Details
#execute(&block) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 121 def execute(&block) raise InteractionBuilderError.new("interaction is designed to be used one-time only") if defined?(@used) validate! pact_handle = init_pact init_plugin!(pact_handle) interaction = PactFfi.new_interaction(pact_handle, @description) @references.each do |group, name, value| PactFfi.add_interaction_reference(pact_handle, group, name, value) end @provider_state_meta&.each_pair do |provider_state, | if .present? .each_pair do |k, v| if v.nil? || (v.respond_to?(:empty?) && v.empty?) PactFfi.given(interaction, provider_state) else PactFfi.given_with_param(interaction, provider_state, k.to_s, v.to_s) end end else PactFfi.given(interaction, provider_state) end end PactFfi::HttpConsumer.with_request(interaction, @request[:method], @request[:path]) result = PactFfi::PluginConsumer.interaction_contents(interaction, 0, @request[:headers]["content-type"], format_value(@request[:body])) if CREATE_INTERACTION_ERRORS[result].present? error = CREATE_INTERACTION_ERRORS[result] raise CreateInteractionError.new("There was an error while trying to add interaction \"#{@description}\"", error[:reason], error[:status]) end result = PactFfi::PluginConsumer.interaction_contents(interaction, 1, @response[:headers]["content-type"], format_value(@response[:body])) if CREATE_INTERACTION_ERRORS[result].present? error = CREATE_INTERACTION_ERRORS[result] raise CreateInteractionError.new("There was an error while trying to add interaction \"#{@description}\"", error[:reason], error[:status]) end mock_server = MockServer.create_for_transport!(pact: pact_handle, transport: @transport || 'http', 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 PactFfi::PluginConsumer.cleanup_plugins(pact_handle) if pact_handle PactFfi.free_pact_handle(pact_handle) if pact_handle end |
#given(provider_state, metadata = {}) ⇒ Object
53 54 55 56 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 53 def given(provider_state, = {}) @provider_state_meta = {provider_state => } self end |
#interaction_json ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 107 def interaction_json result = { request: @request, response: @response } result.merge!(@plugin_metadata) if @plugin_metadata.is_a?(Hash) JSON.dump(result) end |
#reference(group, name, value) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 99 def reference(group, name, value) raise InteractionBuilderError.new("group is required") if group.blank? raise InteractionBuilderError.new("name is required") if name.blank? raise InteractionBuilderError.new("value is required") if value.blank? @references << [group, name, value] self end |
#upon_receiving(description) ⇒ Object
58 59 60 61 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 58 def upon_receiving(description) @description = description self end |
#validate! ⇒ Object
116 117 118 119 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 116 def validate! raise InteractionBuilderError.new("invalid request format, should be a hash") unless @request.is_a?(Hash) raise InteractionBuilderError.new("invalid response format, should be a hash") unless @response.is_a?(Hash) end |
#will_respond_with(status: nil, headers: {}, body: nil) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 74 def will_respond_with(status: nil, headers: {}, body: nil) @response = { status: status, headers: headers, body: body } self end |
#with_content_type(content_type) ⇒ Object
83 84 85 86 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 83 def with_content_type(content_type) @content_type = content_type self end |
#with_plugin(plugin_name, plugin_version) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 44 def with_plugin(plugin_name, plugin_version) raise InteractionBuilderError.new("plugin_name is required") if plugin_name.blank? raise InteractionBuilderError.new("plugin_version is required") if plugin_version.blank? @plugin_name = plugin_name @plugin_version = plugin_version self end |
#with_plugin_metadata(meta_hash) ⇒ Object
89 90 91 92 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 89 def () @plugin_metadata = self end |
#with_request(method: nil, path: nil, query: {}, headers: {}, body: nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 63 def with_request(method: nil, path: nil, query: {}, headers: {}, body: nil) @request = { method: method, path: path, query: query, headers: headers, body: body } self end |
#with_transport(transport) ⇒ Object
94 95 96 97 |
# File 'lib/pact/consumer/plugin_http_interaction_builder.rb', line 94 def with_transport(transport) @transport = transport self end |