From c6cbb68d70793911461e112b0ef8ea9bbbc67ac4 Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Wed, 21 Oct 2026 00:14:34 +0100 Subject: [PATCH 1/2] fix: support plugin message contents without a transport has_plugin_async_message_pact_between always starts a mock server for the plugin's transport, so content-only plugins such as avro can't be used. The plain message builder already drives a content plugin and hands the reified bytes to the test, but only for protobuf. Add MessageInteractionBuilder#with_plugin and #with_plugin_contents so any content plugin can be used on the message DSL, with specs for protobuf and avro (skipped when the avro plugin isn't installed). Co-Authored-By: Claude Opus 5.5 --- .../consumer/message_interaction_builder.rb | 54 +++++++++++++-- .../deps/services/pet_store/avro/pet.avsc | 9 +++ .../message_interaction_builder_spec.rb | 31 +++++++++ .../plugin_message_spec.rb | 66 +++++++++++++++++++ 4 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 spec/internal/deps/services/pet_store/avro/pet.avsc create mode 100644 spec/pact/providers/pact-ruby-v2-test-app/plugin_message_spec.rb diff --git a/lib/pact/consumer/message_interaction_builder.rb b/lib/pact/consumer/message_interaction_builder.rb index 66706443..e7516ea3 100644 --- a/lib/pact/consumer/message_interaction_builder.rb +++ b/lib/pact/consumer/message_interaction_builder.rb @@ -55,6 +55,10 @@ def initialize(pact_config, description: nil) @proto_path = nil @proto_message_class = nil @proto_include_dirs = [] + @plugin_name = nil + @plugin_version = nil + @plugin_content_type = nil + @plugin_contents = nil @meta = {} @headers = {} @provider_state_meta = nil @@ -106,6 +110,25 @@ def with_proto_contents(contents_hash) self end + # Any content plugin that needs no transport (e.g. avro), see #with_plugin_contents + 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 + + # contents_hash is passed to the plugin as-is, e.g. {"pact:avro" => path, "pact:record-name" => "Order", ...} + def with_plugin_contents(content_type, contents_hash) + raise InteractionBuilderError.new("content_type is required") if content_type.blank? + + @plugin_content_type = content_type + @plugin_contents = InteractionContents.plugin(contents_hash) + self + end + def with_metadata(meta_hash) @meta = InteractionContents.basic(meta_hash) self @@ -122,7 +145,10 @@ def with_header(key, value) end def validate! - if proto_interaction? + if custom_plugin_interaction? + raise InteractionBuilderError.new("plugin is not defined, please set one with #with_plugin") if @plugin_name.blank? || @plugin_version.blank? + raise InteractionBuilderError.new("invalid request format, should be a hash") unless @plugin_contents.is_a?(Hash) + elsif proto_interaction? raise InteractionBuilderError.new("proto_path / proto_message are not defined, please set ones with #with_proto_message") if @proto_contents.blank? || @proto_message_class.blank? raise InteractionBuilderError.new("invalid request format, should be a hash") unless @proto_contents.is_a?(Hash) else @@ -136,7 +162,7 @@ def execute(&block) validate! pact_handle = init_pact - init_plugin!(pact_handle) if proto_interaction? + init_plugin!(pact_handle) if plugin_interaction? message_pact = PactFfi::MessageConsumer.new_message_interaction(pact_handle, @description) @@ -163,6 +189,7 @@ def execute(&block) end def build_interaction_json + return JSON.dump({"pact:content-type" => @plugin_content_type}.merge(@plugin_contents.transform_keys(&:to_s))) if custom_plugin_interaction? return JSON.dump(@json_contents) unless proto_interaction? contents = { @@ -240,7 +267,7 @@ def configure_provider_state(message_pact, reified_metadata) end def fetch_reified_message_body(message_handle) - if proto_interaction? + if plugin_interaction? len = PactFfi::MessageConsumer.get_contents_length(message_handle) ptr = PactFfi::MessageConsumer.get_contents_bin(message_handle) return nil if ptr.blank? || len == 0 @@ -257,8 +284,8 @@ def fetch_reified_message_body(message_handle) def configure_interaction!(message_pact) interaction_json = build_interaction_json - if proto_interaction? - result = PactFfi::PluginConsumer.interaction_contents(message_pact, 0, PROTO_CONTENT_TYPE, interaction_json) + if plugin_interaction? + result = PactFfi::PluginConsumer.interaction_contents(message_pact, 0, plugin_content_type, interaction_json) 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]) @@ -277,11 +304,12 @@ def configure_interaction!(message_pact) end def init_plugin!(pact_handle) - result = PactFfi::PluginConsumer.using_plugin(pact_handle, PROTOBUF_PLUGIN_NAME, @proto_plugin_version || PROTOBUF_PLUGIN_VERSION) + name, version = custom_plugin_interaction? ? [@plugin_name, @plugin_version] : [PROTOBUF_PLUGIN_NAME, @proto_plugin_version || PROTOBUF_PLUGIN_VERSION] + result = PactFfi::PluginConsumer.using_plugin(pact_handle, name, version) return result if INIT_PLUGIN_ERRORS[result].blank? error = INIT_PLUGIN_ERRORS[result] - raise PluginInitError.new("There was an error while trying to initialize plugin #{PROTOBUF_PLUGIN_NAME}/#{@proto_plugin_version || PROTOBUF_PLUGIN_VERSION}", error[:reason], error[:status]) + raise PluginInitError.new("There was an error while trying to initialize plugin #{name}/#{version}", error[:reason], error[:status]) end def serialize_metadata(metadata_hash) @@ -294,6 +322,18 @@ def serialize_metadata(metadata_hash) def proto_interaction? @proto_contents.present? end + + def custom_plugin_interaction? + @plugin_contents.present? + end + + def plugin_interaction? + proto_interaction? || custom_plugin_interaction? + end + + def plugin_content_type + custom_plugin_interaction? ? @plugin_content_type : PROTO_CONTENT_TYPE + end end end end diff --git a/spec/internal/deps/services/pet_store/avro/pet.avsc b/spec/internal/deps/services/pet_store/avro/pet.avsc new file mode 100644 index 00000000..0ca39ca0 --- /dev/null +++ b/spec/internal/deps/services/pet_store/avro/pet.avsc @@ -0,0 +1,9 @@ +{ + "namespace": "pet_store.v1", + "type": "record", + "name": "Pet", + "fields": [ + {"name": "id", "type": "long"}, + {"name": "name", "type": "string"} + ] +} diff --git a/spec/lib/consumer/message_interaction_builder_spec.rb b/spec/lib/consumer/message_interaction_builder_spec.rb index 6c9d4144..5b0a1030 100644 --- a/spec/lib/consumer/message_interaction_builder_spec.rb +++ b/spec/lib/consumer/message_interaction_builder_spec.rb @@ -23,6 +23,37 @@ end end + context 'when a plugin message is used' do + let(:builder) do + subject + .upon_receiving('message as avro') + .with_plugin('avro', '0.1.3') + .with_plugin_contents('avro/binary', 'pact:avro' => '/schemas/pet.avsc', 'pact:record-name' => 'Pet', + id: "notEmpty('1')") + end + + it 'builds proper json' do + result = JSON.parse(builder.build_interaction_json) + expect(result).to eq( + 'pact:content-type' => 'avro/binary', + 'pact:avro' => '/schemas/pet.avsc', + 'pact:record-name' => 'Pet', + 'id' => "notEmpty('1')" + ) + end + + it 'raises when no plugin is set' do + builder.instance_variable_set(:@plugin_name, nil) + expect { builder.validate! }.to raise_error(described_class::InteractionBuilderError, /with_plugin/) + end + + it 'raises when content_type is blank' do + expect do + subject.with_plugin_contents('', id: 1) + end.to raise_error(described_class::InteractionBuilderError, /content_type is required/) + end + end + context 'when json message is used' do let(:proto_path) { 'spec/internal/deps/services/pet_store/grpc/pet_store.proto' } let(:builder) do diff --git a/spec/pact/providers/pact-ruby-v2-test-app/plugin_message_spec.rb b/spec/pact/providers/pact-ruby-v2-test-app/plugin_message_spec.rb new file mode 100644 index 00000000..8d43843c --- /dev/null +++ b/spec/pact/providers/pact-ruby-v2-test-app/plugin_message_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'pact/rspec' + +# Message pacts whose contents come from a plugin without a transport, via #with_plugin / #with_plugin_contents +RSpec.describe 'Test plugin message contents without a transport', :pact, skip_windows: true do + has_message_pact_between 'pact-ruby-test-app-plugin-message-consumer', 'pact-ruby-test-app-plugin-message-provider' + + context 'with the protobuf plugin' do + let(:interaction) do + new_interaction + .given('pet exists', pet_id: 1) + .upon_receiving('pet message as proto via with_plugin') + .with_plugin('protobuf', Pact::Consumer::MessageInteractionBuilder::PROTOBUF_PLUGIN_VERSION) + .with_plugin_contents( + 'application/protobuf', + 'pact:proto' => File.expand_path('spec/internal/deps/services/pet_store/grpc/pet_store.proto'), + 'pact:message-type' => 'Pet', + id: match_any_integer(1), + name: match_any_string('some pet') + ) + .with_metadata(topic: match_any_string('some-topic')) + end + + it 'executes the pact test without errors' do + interaction.execute do |payload, meta| + pet = PetStore::Grpc::PetStore::V1::Pet.decode(payload) + expect(pet.id).to eq(1) + expect(pet.name).to eq('some pet') + expect(meta).to include('topic' => 'some-topic', + 'contentType' => 'application/protobuf;message=.pet_store.v1.Pet') + end + end + end + + context 'with the avro plugin' do + let(:avro_plugin_dir) do + File.join(ENV.fetch('PACT_PLUGIN_DIR', File.join(Dir.home, '.pact/plugins')), 'avro-0.1.3') + end + + before { skip "the avro plugin isn't installed in #{avro_plugin_dir}" unless File.directory?(avro_plugin_dir) } + + let(:interaction) do + new_interaction + .given('pet exists', pet_id: 1) + .upon_receiving('pet message as avro') + .with_plugin('avro', '0.1.3') + .with_plugin_contents( + 'avro/binary', + 'pact:avro' => File.expand_path('spec/internal/deps/services/pet_store/avro/pet.avsc'), + 'pact:record-name' => 'Pet', + id: "notEmpty('1')", + name: "notEmpty('some pet')" + ) + .with_metadata(topic: match_any_string('some-topic')) + end + + it 'executes the pact test without errors' do + interaction.execute do |payload, meta| + # Avro binary: zigzag long 1, then the string's zigzag length 8 and its bytes + expect(payload.b).to eq("\x02\x10some pet".b) + expect(meta).to include('topic' => 'some-topic') + end + end + end +end From 0c9666a1ddee38533036eff198e03e760738fb8c Mon Sep 17 00:00:00 2001 From: Yousaf Nabi Date: Wed, 21 Oct 2026 14:45:08 +0100 Subject: [PATCH 2/2] chore(ci): install avro plugin --- .github/workflows/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2990937b..e681a7c1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -25,6 +25,7 @@ jobs: bundler-cache: true - uses: pact-foundation/pact-cli@27100e9dab5bf4612f4caddc6f67d83cf1150cff # main - run: pact plugin install --yes https://github.com/mefellows/pact-matt-plugin/releases/tag/v0.1.1 + - run: pact plugin install --yes avro --version 0.1.3 - name: Test Pact-Ruby Specs run: "bundle exec rake" - name: Test Pact-Ruby Consumer Tests