-
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.
add specs for mime encode/decode operations
- Loading branch information
Showing
4 changed files
with
151 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe EventSource::Operations::MimeDecode do | ||
subject { described_class.new } | ||
|
||
describe "#call" do | ||
context "when the payload and mime type are valid" do | ||
let(:payload) { { message: "Hello, World!" } } | ||
let(:compressed_payload) { Zlib.deflate(payload.to_json) } | ||
let(:mime_type) { "application/zlib" } | ||
|
||
it "successfully decodes the payload" do | ||
result = subject.call(mime_type, compressed_payload) | ||
|
||
expect(result).to be_success | ||
expect(result.value!).to eq(payload.to_json) | ||
end | ||
end | ||
|
||
context "when the payload is not binary for application/zlib" do | ||
let(:invalid_payload) { "Not binary data" } | ||
let(:mime_type) { "application/zlib" } | ||
|
||
it "returns a failure" do | ||
result = subject.call(mime_type, invalid_payload) | ||
|
||
expect(result).to be_failure | ||
expect(result.failure).to eq("Payload must be binary-encoded for mime type 'application/zlib'.") | ||
end | ||
end | ||
|
||
context "when the mime type is invalid" do | ||
let(:payload) { { message: "Hello, World!" }.to_json } | ||
let(:mime_type) { "text/plain" } | ||
|
||
it "returns a failure" do | ||
result = subject.call(mime_type, payload) | ||
|
||
expect(result).to be_failure | ||
expect(result.failure).to eq("Invalid mime type 'text/plain'. Supported types are: application/zlib, application/json.") | ||
end | ||
end | ||
|
||
context "when decoding fails" do | ||
let(:invalid_compressed_payload) { "Invalid compressed data" } | ||
let(:mime_type) { "application/zlib" } | ||
|
||
it "returns a failure with an error message" do | ||
result = subject.call(mime_type, invalid_compressed_payload) | ||
|
||
expect(result).to be_failure | ||
expect(result.failure).to eq("Payload must be binary-encoded for mime type 'application/zlib'.") | ||
end | ||
end | ||
end | ||
end |
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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe EventSource::Operations::MimeEncode do | ||
subject { described_class.new } | ||
|
||
describe "#call" do | ||
context "when the payload and mime type are valid" do | ||
let(:payload) { { message: "Hello, World!" } } | ||
let(:mime_type) { "application/zlib" } | ||
|
||
it "successfully encodes the payload" 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 | ||
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) | ||
end | ||
end | ||
|
||
context "when the mime type is invalid" do | ||
let(:payload) { { message: "Hello, World!" } } | ||
let(:mime_type) { "text/plain" } | ||
|
||
it "returns a failure" do | ||
result = subject.call(mime_type, payload) | ||
|
||
expect(result).to be_failure | ||
expect(result.failure).to eq("Invalid mime type 'text/plain'. Supported types are: application/zlib, application/json.") | ||
end | ||
end | ||
|
||
context "when the payload is invalid" do | ||
let(:payload) { 1000 } | ||
let(:mime_type) { "application/json" } | ||
|
||
it "returns a failure" do | ||
result = subject.call(mime_type, payload) | ||
|
||
expect(result).to be_failure | ||
expect(result.failure).to eq("Invalid payload type. Expected a Hash or String, but received Integer.") | ||
end | ||
end | ||
end | ||
end |