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