-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
182 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
require 'zlib' | ||
|
||
RSpec.describe EventSource::Operations::PayloadCodec do | ||
subject { described_class.new(payload: test_payload) } | ||
|
||
let(:test_payload) { { key: 'value' }.to_json } | ||
|
||
describe '#compress' do | ||
context 'when the payload is valid' do | ||
it 'returns a compressed string' do | ||
result = subject.compress | ||
|
||
expect(result).to be_success | ||
output = result.value! | ||
expect(output.encoding).to eq(Encoding::BINARY) | ||
expect { Zlib.inflate(output) }.not_to raise_error | ||
end | ||
end | ||
|
||
context 'when the payload is invalid' do | ||
let(:test_payload) { 12345 } # Invalid payload | ||
|
||
it 'returns a failure with an error message' do | ||
result = subject.compress | ||
|
||
expect(result).to be_failure | ||
expect(result.failure).to eq('Payload must be a Hash or String') | ||
end | ||
end | ||
end | ||
|
||
describe '#decompress' do | ||
let(:compressed_payload) { Zlib.deflate(test_payload) } | ||
subject { described_class.new(payload: compressed_payload) } | ||
|
||
context 'when the payload is valid' do | ||
it 'returns the decompressed string' do | ||
result = subject.decompress | ||
|
||
expect(result).to be_success | ||
expect(result.value!).to eq(test_payload) | ||
end | ||
end | ||
|
||
context 'when decompression fails' do | ||
let(:compressed_payload) { 'invalid_data' } | ||
|
||
it 'returns a failure with an error message' do | ||
result = subject.decompress | ||
|
||
expect(result).to be_failure | ||
expect(result.failure).to match(/Decompression failed:/) | ||
end | ||
end | ||
end | ||
|
||
describe '#decompress_if_binary' do | ||
context 'when the payload is not binary' do | ||
it 'returns the original payload' do | ||
result = subject.decompress_if_binary | ||
|
||
expect(result).to be_success | ||
expect(result.value!).to eq(test_payload) | ||
end | ||
end | ||
|
||
context 'when the payload is binary' do | ||
let(:compressed_payload) { Zlib.deflate(test_payload) } | ||
subject { described_class.new(payload: compressed_payload.force_encoding(Encoding::BINARY)) } | ||
|
||
it 'decompresses the payload' do | ||
result = subject.decompress_if_binary | ||
|
||
expect(result).to be_success | ||
expect(result.value!).to eq(test_payload) | ||
end | ||
end | ||
end | ||
|
||
describe '#validate_payload_for_compression' do | ||
context 'when the payload is a valid Hash' do | ||
let(:test_payload) { { key: 'value' } } | ||
|
||
it 'returns the JSON string representation' do | ||
result = subject.send(:validate_payload_for_compression) | ||
|
||
expect(result).to be_success | ||
expect(result.value!).to eq(test_payload.to_json) | ||
end | ||
end | ||
|
||
context 'when the payload is a valid String' do | ||
let(:test_payload) { 'valid_string' } | ||
|
||
it 'returns the string as is' do | ||
result = subject.send(:validate_payload_for_compression) | ||
|
||
expect(result).to be_success | ||
expect(result.value!).to eq(test_payload) | ||
end | ||
end | ||
|
||
context 'when the payload is invalid' do | ||
let(:test_payload) { 12345 } | ||
|
||
it 'returns a failure with an error message' do | ||
result = subject.send(:validate_payload_for_compression) | ||
|
||
expect(result).to be_failure | ||
expect(result.failure).to eq('Payload must be a Hash or String') | ||
end | ||
end | ||
end | ||
|
||
describe '#binary_payload?' do | ||
|
||
before do | ||
subject { described_class.new(payload: test_payload) } | ||
end | ||
|
||
context 'when the payload is binary' do | ||
let(:test_payload) { Zlib.deflate({ key: 'value' }.to_json) } | ||
|
||
it 'returns true' do | ||
result = subject.send(:binary_payload?) | ||
|
||
expect(result).to be true | ||
end | ||
end | ||
|
||
context 'when the payload is not binary' do | ||
it 'returns false' do | ||
result = subject.send(:binary_payload?) | ||
|
||
expect(result).to be false | ||
end | ||
end | ||
end | ||
end |