Skip to content

Commit

Permalink
That should do for a while
Browse files Browse the repository at this point in the history
  • Loading branch information
julik committed Feb 5, 2024
1 parent 4d076a8 commit 67382d8
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions spec/lib/appsignal/rack/body_wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
expect { |b| wrapped.each(&b) }.to yield_successive_args("a", "b", "c")
end

it "returns an Enumerator if each() gets called without a block" do
fake_body = double()
expect(fake_body).to receive(:each).once.and_yield("a").and_yield("b").and_yield("c")

wrapped = described_class.wrap(fake_body, _txn = nil)
enum = wrapped.each
expect(enum).to be_kind_of(Enumerator)
expect { |b| enum.each(&b) }.to yield_successive_args("a", "b", "c")
end

it "sets the exception raised inside each() into the Appsignal transaction" do
fake_body = double()
expect(fake_body).to receive(:each).once.and_raise(Exception.new("Oops"))
Expand All @@ -40,7 +50,9 @@
expect(txn).to receive(:set_error).once.with(instance_of(Exception))

wrapped = described_class.wrap(fake_body, txn)
expect { |b| wrapped.each(&b) }.to raise_error(/Oops/)
expect {
expect { |b| wrapped.each(&b) }.to yield_control
}.to raise_error(/Oops/)
end

it "closes the body and the transaction when it gets closed" do
Expand Down Expand Up @@ -72,11 +84,8 @@
end

describe "with a body supporting both to_ary and each" do
let(:fake_body) { double(each: nil, to_ary: []) }
it "wraps with appropriate class" do
fake_body = double()
allow(fake_body).to receive(:each)
allow(fake_body).to receive(:to_ary)

wrapped = described_class.wrap(fake_body, _txn = nil)
expect(wrapped).to respond_to(:each)
expect(wrapped).to respond_to(:to_ary)
Expand All @@ -86,28 +95,25 @@
end

it "reads out the body in full using each" do
fake_body = double()
allow(fake_body).to receive(:to_ary)
expect(fake_body).to receive(:each).once.and_yield("a").and_yield("b").and_yield("c")

wrapped = described_class.wrap(fake_body, _txn = nil)
expect { |b| wrapped.each(&b) }.to yield_successive_args("a", "b", "c")
end

it "sets the exception raised inside each() into the Appsignal transaction" do
fake_body = double()
expect(fake_body).to receive(:each).once.and_raise(Exception.new("Oops"))

txn = double("Appsignal transaction")
expect(txn).to receive(:set_error).once.with(instance_of(Exception))

wrapped = described_class.wrap(fake_body, txn)
expect { |b| wrapped.each(&b) }.to raise_error(/Oops/)
expect {
expect { |b| wrapped.each(&b) }.to yield_control
}.to raise_error(/Oops/)
end

it "reads out the body in full using to_ary" do
fake_body = double()
allow(fake_body).to receive(:each)
expect(fake_body).to receive(:to_ary).and_return(["one", "two", "three"])

wrapped = described_class.wrap(fake_body, _txn = nil)
Expand All @@ -130,11 +136,9 @@
end

describe "with a body supporting both to_path and each" do
it "wraps with appropriate class" do
fake_body = double()
allow(fake_body).to receive(:each)
allow(fake_body).to receive(:to_path)
let(:fake_body) { double(each: nil, to_path: nil) }

it "wraps with appropriate class" do
wrapped = described_class.wrap(fake_body, _txn = nil)
expect(wrapped).to respond_to(:each)
expect(wrapped).not_to respond_to(:to_ary)
Expand All @@ -144,28 +148,25 @@
end

it "reads out the body in full using each()" do
fake_body = double()
allow(fake_body).to receive(:to_path)
expect(fake_body).to receive(:each).once.and_yield("a").and_yield("b").and_yield("c")

wrapped = described_class.wrap(fake_body, _txn = nil)
expect { |b| wrapped.each(&b) }.to yield_successive_args("a", "b", "c")
end

it "sets the exception raised inside each() into the Appsignal transaction" do
fake_body = double()
allow(fake_body).to receive(:to_path)
expect(fake_body).to receive(:each).once.and_raise(Exception.new("Oops"))

txn = double("Appsignal transaction")
expect(txn).to receive(:set_error).once.with(instance_of(Exception))

wrapped = described_class.wrap(fake_body, txn)
expect { |b| wrapped.each(&b) }.to raise_error(/Oops/)
expect {
expect { |b| wrapped.each(&b) }.to yield_control
}.to raise_error(/Oops/)
end

it "exposes to_path to the sender" do
fake_body = double()
it "sets the exception raised inside to_path() into the Appsignal transaction" do
allow(fake_body).to receive(:to_path).once.and_raise(Exception.new("Oops"))

txn = double("Appsignal transaction")
Expand All @@ -176,8 +177,7 @@
expect { wrapped.to_path }.to raise_error(/Oops/)
end

it "sets the exception raised inside to_path() into the Appsignal transaction" do
fake_body = double()
it "exposes to_path to the sender" do
allow(fake_body).to receive(:to_path).and_return("/tmp/file.bin")

wrapped = described_class.wrap(fake_body, _txn = nil)
Expand All @@ -186,10 +186,8 @@
end

describe "with a body only supporting call()" do
let(:fake_body) { double(call: nil) }
it "wraps with appropriate class" do
fake_body = double()
allow(fake_body).to receive(:call)

wrapped = described_class.wrap(fake_body, _txn = nil)
expect(wrapped).not_to respond_to(:each)
expect(wrapped).not_to respond_to(:to_ary)
Expand All @@ -199,16 +197,14 @@
end

it "passes the stream into the call() of the body" do
fake_body = double()
fake_rack_stream = double()
allow(fake_body).to receive(:call).with(fake_rack_stream)
fake_rack_stream = double("stream")
expect(fake_body).to receive(:call).with(fake_rack_stream)

wrapped = described_class.wrap(fake_body, _txn = nil)
expect { wrapped.call(fake_rack_stream) }.not_to raise_error
end

it "sets the exception raised inside call() into the Appsignal transaction" do
fake_body = double()
fake_rack_stream = double()
allow(fake_body).to receive(:call).with(fake_rack_stream).and_raise(Exception.new("Oopsie"))

Expand Down

0 comments on commit 67382d8

Please sign in to comment.