Skip to content

Commit

Permalink
spec fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
raghuramg committed Jan 17, 2025
1 parent 25b96c9 commit 54703b3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
5 changes: 3 additions & 2 deletions lib/event_source/operations/mime_encode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ def validate(mime_type)
def encode(mime_type, payload)
case mime_type
when 'application/zlib'
encoded_data = Zlib.deflate(payload.to_json)
json_payload = payload.to_json
encoded_data = Zlib.deflate(json_payload)
log_encoding_details(mime_type, json_payload, encoded_data)
when 'application/json'
encoded_data = payload.to_json
end
log_encoding_details(mime_type, payload, encoded_data) if encoded_data

Success(encoded_data || payload)
rescue JSON::GeneratorError => e
Expand Down
2 changes: 1 addition & 1 deletion lib/event_source/publish_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def determine_encoding
end

def amqp_protocol?
subject.is_a?(EventSource::Protocols::Amqp::BunnyPublisherProxy)
subject.is_a?(EventSource::Protocols::Amqp::BunnyExchangeProxy)
end
end
end
51 changes: 41 additions & 10 deletions spec/event_source/operations/mime_encode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,31 @@
subject { described_class.new }

describe "#call" do
context "when the payload and mime type are valid" do

let(:valid_payload) { { key: 'value' } }
let(:invalid_payload) { -> {} }

context "when MIME type is application/zlib" do
let(:payload) { { message: "Hello, World!" } }
let(:mime_type) { "application/zlib" }

it "successfully encodes the payload" do
it "compresses the payload using Zlib" do
result = subject.call(mime_type, payload)

expect(result).to be_success
expect(Zlib.inflate(result.value!)).to eq(payload.to_json)
end
end

context "when the payload is a string and mime type is valid" do
context "when MIME type is application/json" do
let(:payload) { "Hello, World!" }
let(:mime_type) { "application/json" }

it "returns the payload as JSON" do
result = subject.call(mime_type, payload)

expect(result).to be_success
expect(result.value!).to eq(payload)
expect(result.value!).to eq(payload.to_json)
end
end

Expand All @@ -40,15 +44,42 @@
end
end

context "when the payload is invalid" do
let(:payload) { 1000 }
let(:mime_type) { "application/json" }
context 'when payload cannot be converted to JSON' do
before do
allow(invalid_payload).to receive(:to_json).and_raise(JSON::GeneratorError)
end

it "returns a failure" do
result = subject.call(mime_type, payload)
it 'returns a failure with JSON::GeneratorError' do
result = subject.call('application/json', invalid_payload)

expect(result).to be_failure
expect(result.failure).to match(/Failed to encode payload to JSON:/)
end
end

context 'when Zlib compression fails' do
before do
allow(Zlib).to receive(:deflate).and_raise(Zlib::Error, 'Compression failed')
end

it 'returns a failure with Zlib::Error' do
result = subject.call('application/zlib', valid_payload)

expect(result).to be_failure
expect(result.failure).to eq('Failed to compress payload using Zlib: Compression failed')
end
end

context 'when an unexpected error occurs' do
before do
allow(valid_payload).to receive(:to_json).and_raise(StandardError, 'something went wrong')
end

it 'returns a failure with StandardError' do
result = subject.call('application/json', valid_payload)

expect(result).to be_failure
expect(result.failure).to eq("Invalid payload type. Expected a Hash or String, but received Integer.")
expect(result.failure).to eq('Unexpected error during encoding: something went wrong')
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/event_source/protocols/amqp/bunny_exchange_proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

describe '#publish' do
it 'publishes the payload with the correct bindings and headers' do
subject.publish(payload: payload, publish_bindings: publish_bindings, headers: headers)
subject.publish(payload: payload.to_json, publish_bindings: publish_bindings, headers: headers)

expect(bunny_exchange).to have_received(:publish).with(payload.to_json, {
correlation_id: '12345',
Expand All @@ -41,7 +41,7 @@
expect(subject.logger).to receive(:debug).with(/published message:/)
expect(subject.logger).to receive(:debug).with(/published message to exchange:/)

subject.publish(payload: payload, publish_bindings: publish_bindings, headers: headers)
subject.publish(payload: payload.to_json, publish_bindings: publish_bindings, headers: headers)
end

context 'when the payload is binary' do
Expand Down

0 comments on commit 54703b3

Please sign in to comment.