2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2011-10-19 13:59:33 -04:00
|
|
|
|
|
|
|
class RequestIdTest < ActiveSupport::TestCase
|
|
|
|
test "passing on the request id from the outside" do
|
2020-10-29 22:20:04 -04:00
|
|
|
assert_equal "external-uu-rid", stub_request({ "HTTP_X_REQUEST_ID" => "external-uu-rid" }).request_id
|
2011-10-19 13:59:33 -04:00
|
|
|
end
|
|
|
|
|
2020-06-04 16:52:54 -04:00
|
|
|
test "passing on the request id via a configured header" do
|
|
|
|
assert_equal "external-uu-rid", stub_request({ "HTTP_TRACER_ID" => "external-uu-rid" }, header: "Tracer-Id").request_id
|
|
|
|
end
|
|
|
|
|
2011-10-19 13:59:33 -04:00
|
|
|
test "ensure that only alphanumeric uurids are accepted" do
|
2020-10-29 22:20:04 -04:00
|
|
|
assert_equal "X-Hacked-HeaderStuff", stub_request({ "HTTP_X_REQUEST_ID" => "; X-Hacked-Header: Stuff" }).request_id
|
2011-10-19 13:59:33 -04:00
|
|
|
end
|
2011-10-19 16:09:36 -04:00
|
|
|
|
2018-01-28 12:53:11 -05:00
|
|
|
test "accept Apache mod_unique_id format" do
|
|
|
|
mod_unique_id = "abcxyz@ABCXYZ-0123456789"
|
2020-10-29 22:20:04 -04:00
|
|
|
assert_equal mod_unique_id, stub_request({ "HTTP_X_REQUEST_ID" => mod_unique_id }).request_id
|
2018-01-28 12:53:11 -05:00
|
|
|
end
|
|
|
|
|
2011-10-19 13:59:33 -04:00
|
|
|
test "ensure that 255 char limit on the request id is being enforced" do
|
2020-10-29 22:20:04 -04:00
|
|
|
assert_equal "X" * 255, stub_request({ "HTTP_X_REQUEST_ID" => "X" * 500 }).request_id
|
2011-10-19 13:59:33 -04:00
|
|
|
end
|
2011-10-19 16:09:36 -04:00
|
|
|
|
2011-10-19 13:59:33 -04:00
|
|
|
test "generating a request id when none is supplied" do
|
2015-02-20 17:45:01 -05:00
|
|
|
assert_match(/\w+-\w+-\w+-\w+-\w+/, stub_request.request_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "uuid alias" do
|
2020-10-29 22:20:04 -04:00
|
|
|
assert_equal "external-uu-rid", stub_request({ "HTTP_X_REQUEST_ID" => "external-uu-rid" }).uuid
|
2011-10-19 13:59:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2020-10-29 20:41:59 -04:00
|
|
|
def stub_request(env = {}, header: "X-Request-Id")
|
2020-06-04 16:52:54 -04:00
|
|
|
ActionDispatch::RequestId.new(lambda { |environment| [ 200, environment, [] ] }, header: header).call(env)
|
2016-08-06 13:55:02 -04:00
|
|
|
ActionDispatch::Request.new(env)
|
|
|
|
end
|
2011-10-19 13:59:33 -04:00
|
|
|
end
|
|
|
|
|
2011-10-19 16:09:36 -04:00
|
|
|
class RequestIdResponseTest < ActionDispatch::IntegrationTest
|
|
|
|
class TestController < ActionController::Base
|
|
|
|
def index
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "request id is passed all the way to the response" do
|
|
|
|
with_test_route_set do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/"
|
2011-10-19 16:09:36 -04:00
|
|
|
assert_match(/\w+/, @response.headers["X-Request-Id"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "request id given on request is passed all the way to the response" do
|
|
|
|
with_test_route_set do
|
2016-08-06 12:54:50 -04:00
|
|
|
get "/", headers: { "HTTP_X_REQUEST_ID" => "X" * 500 }
|
2011-10-19 16:09:36 -04:00
|
|
|
assert_equal "X" * 255, @response.headers["X-Request-Id"]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-04 16:52:54 -04:00
|
|
|
test "using a custom request_id header key" do
|
|
|
|
with_test_route_set(header: "X-Tracer-Id") do
|
|
|
|
get "/"
|
|
|
|
assert_match(/\w+/, @response.headers["X-Tracer-Id"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-19 16:09:36 -04:00
|
|
|
private
|
2020-10-29 20:41:59 -04:00
|
|
|
def with_test_route_set(header: "X-Request-Id")
|
2016-08-06 13:55:02 -04:00
|
|
|
with_routing do |set|
|
|
|
|
set.draw do
|
|
|
|
get "/", to: ::RequestIdResponseTest::TestController.action(:index)
|
|
|
|
end
|
2011-10-19 16:09:36 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
@app = self.class.build_app(set) do |middleware|
|
2020-06-04 16:52:54 -04:00
|
|
|
middleware.use ActionDispatch::RequestId, header: header
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2011-10-19 16:09:36 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
yield
|
|
|
|
end
|
2011-10-19 16:09:36 -04:00
|
|
|
end
|
2012-04-24 23:32:09 -04:00
|
|
|
end
|