|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -require "tmpdir" |
4 | | - |
5 | 3 | describe Singed::RackMiddleware do |
6 | 4 | subject do |
7 | 5 | instance.call(env) |
|
12 | 10 | let(:env) { Rack::MockRequest.env_for("/", headers) } |
13 | 11 | let(:headers) { {} } |
14 | 12 |
|
15 | | - before do |
16 | | - allow_any_instance_of(Singed::Flamegraph).to receive(:open) |
17 | | - Singed.output_directory = Dir.mktmpdir("singed-spec") |
18 | | - end |
19 | | - |
20 | 13 | it "returns a proper rack response" do |
21 | 14 | linted_app = Rack::Lint.new(instance) |
22 | 15 | expect { linted_app.call(env) }.not_to raise_error |
23 | 16 | end |
24 | 17 |
|
25 | | - it "does not capture a flamegraph by default" do |
26 | | - expect(instance).not_to receive(:flamegraph) |
27 | | - subject |
| 18 | + it "passes through the app response unchanged" do |
| 19 | + expect(subject).to eq([200, {"content-type" => "text/plain"}, ["OK"]]) |
28 | 20 | end |
29 | 21 |
|
30 | 22 | context "when enabled" do |
31 | | - before { allow(instance).to receive(:capture_flamegraph?).and_return(true) } |
| 23 | + before do |
| 24 | + allow_any_instance_of(Singed::Flamegraph).to receive(:open) |
| 25 | + allow(instance).to receive(:capture_flamegraph?).and_return(true) |
| 26 | + end |
32 | 27 |
|
33 | 28 | it "captures a flamegraph" do |
34 | 29 | expect(instance).to receive(:flamegraph).and_call_original |
35 | 30 | subject |
36 | 31 | end |
| 32 | + |
| 33 | + it "returns a proper rack response" do |
| 34 | + linted_app = Rack::Lint.new(instance) |
| 35 | + expect { linted_app.call(env) }.not_to raise_error |
| 36 | + end |
37 | 37 | end |
38 | 38 |
|
39 | | - describe ".capture_flamegraph?" do |
| 39 | + describe "#capture_flamegraph?" do |
40 | 40 | subject { instance.capture_flamegraph?(env) } |
41 | 41 |
|
42 | 42 | it { is_expected.to be false } |
|
46 | 46 |
|
47 | 47 | it { is_expected.to be true } |
48 | 48 | end |
| 49 | + |
| 50 | + context "when SINGED_MIDDLEWARE_ALWAYS_CAPTURE env var is set" do |
| 51 | + around do |example| |
| 52 | + original = ENV["SINGED_MIDDLEWARE_ALWAYS_CAPTURE"] |
| 53 | + described_class.remove_instance_variable(:@always_capture) if described_class.instance_variable_defined?(:@always_capture) |
| 54 | + example.run |
| 55 | + ensure |
| 56 | + if original.nil? |
| 57 | + ENV.delete("SINGED_MIDDLEWARE_ALWAYS_CAPTURE") |
| 58 | + else |
| 59 | + ENV["SINGED_MIDDLEWARE_ALWAYS_CAPTURE"] = original |
| 60 | + end |
| 61 | + described_class.remove_instance_variable(:@always_capture) if described_class.instance_variable_defined?(:@always_capture) |
| 62 | + end |
| 63 | + |
| 64 | + %w[true 1 yes].each do |truthy_value| |
| 65 | + context "when SINGED_MIDDLEWARE_ALWAYS_CAPTURE=#{truthy_value}" do |
| 66 | + before { ENV["SINGED_MIDDLEWARE_ALWAYS_CAPTURE"] = truthy_value } |
| 67 | + |
| 68 | + it { is_expected.to be true } |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + context "when SINGED_MIDDLEWARE_ALWAYS_CAPTURE=false" do |
| 73 | + before { ENV["SINGED_MIDDLEWARE_ALWAYS_CAPTURE"] = "false" } |
| 74 | + |
| 75 | + it { is_expected.to be false } |
| 76 | + end |
| 77 | + end |
49 | 78 | end |
50 | 79 | end |
0 commit comments