2011-10-19 13:59:33 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
|
|
|
|
class RequestIdTest < ActiveSupport::TestCase
|
|
|
|
test "passing on the request id from the outside" do
|
2015-02-20 17:45:01 -05: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
|
|
|
|
|
|
|
|
test "ensure that only alphanumeric uurids are accepted" do
|
2015-02-20 17:45:01 -05: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
|
|
|
|
2011-10-19 13:59:33 -04:00
|
|
|
test "ensure that 255 char limit on the request id is being enforced" do
|
2015-02-20 17:45:01 -05: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
|
|
|
|
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
|
2011-10-19 16:09:36 -04:00
|
|
|
|
|
|
|
def stub_request(env = {})
|
2011-10-22 07:00:48 -04:00
|
|
|
ActionDispatch::RequestId.new(lambda { |environment| [ 200, environment, [] ] }).call(env)
|
2011-10-19 16:09:36 -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
|
|
|
|
get '/'
|
|
|
|
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
|
2015-01-29 09:19:41 -05: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
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def with_test_route_set
|
|
|
|
with_routing do |set|
|
|
|
|
set.draw do
|
2012-04-24 23:32:09 -04:00
|
|
|
get '/', :to => ::RequestIdResponseTest::TestController.action(:index)
|
2011-10-19 16:09:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@app = self.class.build_app(set) do |middleware|
|
|
|
|
middleware.use ActionDispatch::RequestId
|
|
|
|
end
|
|
|
|
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
2012-04-24 23:32:09 -04:00
|
|
|
end
|