Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 47 additions & 7 deletions lib/pact/consumer/message_interaction_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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 = {
Expand Down Expand Up @@ -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
Expand All @@ -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])
Expand All @@ -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)
Expand All @@ -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
9 changes: 9 additions & 0 deletions spec/internal/deps/services/pet_store/avro/pet.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"namespace": "pet_store.v1",
"type": "record",
"name": "Pet",
"fields": [
{"name": "id", "type": "long"},
{"name": "name", "type": "string"}
]
}
31 changes: 31 additions & 0 deletions spec/lib/consumer/message_interaction_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions spec/pact/providers/pact-ruby-v2-test-app/plugin_message_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Loading