Make tests run on 1.8.x, add integration setup.

This commit is contained in:
José Valim 2011-10-19 22:09:36 +02:00
parent 18dbfcb363
commit f1fecd9b4e
2 changed files with 47 additions and 42 deletions

View File

@ -18,20 +18,19 @@ module ActionDispatch
def call(env) def call(env)
env["action_dispatch.request_id"] = external_request_id(env) || internal_request_id env["action_dispatch.request_id"] = external_request_id(env) || internal_request_id
status, headers, body = @app.call(env) status, headers, body = @app.call(env)
headers["X-Request-Id"] = env["action_dispatch.request_id"] headers["X-Request-Id"] = env["action_dispatch.request_id"]
[ status, headers, body ] [ status, headers, body ]
end end
private private
def external_request_id(env) def external_request_id(env)
if env["HTTP_X_REQUEST_ID"].present? if env["HTTP_X_REQUEST_ID"].present?
env["HTTP_X_REQUEST_ID"].gsub(/[^\w\d\-]/, "").first(255) env["HTTP_X_REQUEST_ID"].gsub(/[^\w\d\-]/, "").first(255)
end end
end end
def internal_request_id def internal_request_id
SecureRandom.hex(16) SecureRandom.hex(16)
end end

View File

@ -8,52 +8,58 @@ class RequestIdTest < ActiveSupport::TestCase
test "ensure that only alphanumeric uurids are accepted" do test "ensure that only alphanumeric uurids are accepted" do
assert_equal "X-Hacked-HeaderStuff", stub_request('HTTP_X_REQUEST_ID' => '; X-Hacked-Header: Stuff').uuid assert_equal "X-Hacked-HeaderStuff", stub_request('HTTP_X_REQUEST_ID' => '; X-Hacked-Header: Stuff').uuid
end end
test "ensure that 255 char limit on the request id is being enforced" do test "ensure that 255 char limit on the request id is being enforced" do
assert_equal "X" * 255, stub_request('HTTP_X_REQUEST_ID' => 'X' * 500).uuid assert_equal "X" * 255, stub_request('HTTP_X_REQUEST_ID' => 'X' * 500).uuid
end end
test "generating a request id when none is supplied" do test "generating a request id when none is supplied" do
assert_match /\w+/, stub_request.uuid assert_match /\w+/, stub_request.uuid
end end
private private
def stub_request(env = {})
ActionDispatch::RequestId.new(->(env) { [ 200, env, [] ] }).call(env) def stub_request(env = {})
ActionDispatch::Request.new(env) ActionDispatch::RequestId.new(lambda { |env| [ 200, env, [] ] }).call(env)
end ActionDispatch::Request.new(env)
end
end end
# FIXME: Testing end-to-end doesn't seem to work class RequestIdResponseTest < ActionDispatch::IntegrationTest
# class TestController < ActionController::Base
# class RequestIdResponseTest < ActionDispatch::IntegrationTest def index
# class TestController < ActionController::Base head :ok
# def index end
# head :ok end
# end
# end test "request id is passed all the way to the response" do
# with_test_route_set do
# test "request id is passed all the way to the response" do get '/'
# with_test_route_set do assert_match(/\w+/, @response.headers["X-Request-Id"])
# get '/' end
# puts @response.headers.inspect end
# assert_equal "internal-uu-rid", @response.headers["X-Request-Id"]
# end test "request id given on request is passed all the way to the response" do
# end with_test_route_set do
# get '/', {}, 'HTTP_X_REQUEST_ID' => 'X' * 500
# assert_equal "X" * 255, @response.headers["X-Request-Id"]
# private end
# def with_test_route_set end
# with_routing do |set|
# set.draw do
# match ':action', to: ::RequestIdResponseTest::TestController private
# end
# def with_test_route_set
# @app = self.class.build_app(set) do |middleware| with_routing do |set|
# middleware.use ActionDispatch::RequestId set.draw do
# end match '/', :to => ::RequestIdResponseTest::TestController.action(:index)
# end
# yield
# end @app = self.class.build_app(set) do |middleware|
# end middleware.use ActionDispatch::RequestId
# end end
yield
end
end
end