<commit_before>require 'spec_helper'

describe HerokuRequestId::Middleware do

  include Rack::Test::Methods

  let(:inner_app) do
    lambda { |env| [200, {'Content-Type' => 'text/html'}, ['<body>All good!</body>']] }
  end

  let(:app) { HerokuRequestId::Middleware.new(inner_app) }

  subject{ get "/", {}, { "HTTP_HEROKU_REQUEST_ID" => "the_id_string" } }

  it "prints the request id to stdout by default" do
    output = capture_stdout { subject }
    output.should match("heroku-request-id")
    output.should match("the_id_string")
  end

  it "does not print the request id to stdout if log_line == false" do
    HerokuRequestId::Middleware.log_line = false
    output = capture_stdout { subject }
    output.should_not match("heroku-request-id")
    output.should_not match("the_id_string")
    # reset html_comment so that random test order works
    HerokuRequestId::Middleware.log_line = true
  end

  it "does not add a comment with the Heroku request id by default" do
    capture_stdout { subject }
    last_response.body.should_not match("Heroku request id")
    last_response.body.should_not match("the_id_string")
  end

  it "does add a comment with the Heroku request id if html_comment == true" do
    HerokuRequestId::Middleware.html_comment = true
    capture_stdout { subject }
    last_response.body.should match("Heroku request id")
    last_response.body.should match("the_id_string")
    # reset html_comment so that random test order works
    HerokuRequestId::Middleware.html_comment = false
  end

  it "makes no change to response status" do
    capture_stdout { subject }
    last_response.should be_ok
  end


 # my silly but effective way to test if the header gets replicated/passed forward.
 # i searched and couldn't figure out the typical way of testing
 # if a header is passed down the stack, perhaps someone who knows can replace this.
  describe "header replication into x-request-id" do
    let(:inner_app) do
      lambda { |env|
        [200, {'Content-Type' => 'text/html'}, [env["HTTP_X_REQUEST_ID"]]]
      }
    end
    let(:app){ HerokuRequestId::Middleware.new(inner_app) }
    context "by default" do
      it "does not replicate the heroku header into rails header" do
        subject
        last_response.body.should eq ""
      end
    end
    context "when x_request_id_replication == true" do
      before{HerokuRequestId::Middleware.x_request_id_replication = true}
      after{HerokuRequestId::Middleware.x_request_id_replication = false}
      it "replicates the heroku header into rails header" do
        subject
        last_response.body.should eq "the_id_string"
      end
    end
  end

end
<commit_msg>A little bit of spec cleanup.<commit_after>require 'spec_helper'

describe HerokuRequestId::Middleware do

  include Rack::Test::Methods

  let(:inner_app) do
    lambda { |env| [200, {'Content-Type' => 'text/html'}, ['<body>All good!</body>']] }
  end

  let(:app) { HerokuRequestId::Middleware.new(inner_app) }

  subject do
    capture_stdout { get("/", {}, { "HTTP_HEROKU_REQUEST_ID" => "the_id_string" }) }
  end

  it "prints the request id to stdout by default" do
    output = subject
    output.should match("heroku-request-id")
    output.should match("the_id_string")
  end

  it "does not print the request id to stdout if log_line == false" do
    HerokuRequestId::Middleware.log_line = false
    output = subject
    output.should_not match("heroku-request-id")
    output.should_not match("the_id_string")
    # reset html_comment so that random test order works
    HerokuRequestId::Middleware.log_line = true
  end

  it "does not add a comment with the Heroku request id by default" do
    subject
    last_response.body.should_not match("Heroku request id")
    last_response.body.should_not match("the_id_string")
  end

  it "does add a comment with the Heroku request id if html_comment == true" do
    HerokuRequestId::Middleware.html_comment = true
    subject
    last_response.body.should match("Heroku request id")
    last_response.body.should match("the_id_string")
    # reset html_comment so that random test order works
    HerokuRequestId::Middleware.html_comment = false
  end

  it "makes no change to response status" do
    subject
    last_response.should be_ok
  end


 # my silly but effective way to test if the header gets replicated/passed forward.
 # i searched and couldn't figure out the typical way of testing
 # if a header is passed down the stack, perhaps someone who knows can replace this.
  describe "header replication into x-request-id" do
    let(:inner_app) do
      lambda { |env|
        [200, {'Content-Type' => 'text/html'}, [env["HTTP_X_REQUEST_ID"]]]
      }
    end
    let(:app){ HerokuRequestId::Middleware.new(inner_app) }
    context "by default" do
      it "does not replicate the heroku header into rails header" do
        subject
        last_response.body.should eq ""
      end
    end
    context "when x_request_id_replication == true" do
      before{HerokuRequestId::Middleware.x_request_id_replication = true}
      after{HerokuRequestId::Middleware.x_request_id_replication = false}
      it "replicates the heroku header into rails header" do
        subject
        last_response.body.should eq "the_id_string"
      end
    end
  end

end

{'fraction_non_alphanumeric': 0.08970727101038715, 'fraction_numerical': 0.0022662889518413596, 'mean_word_length': 3.5694564279551337, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3820466', 'n_tokens_mistral': 1612, 'n_tokens_neox': 1583, 'n_words': 497}