From f1fecd9b4e38c289b678bc2aadb406265963c528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 19 Oct 2011 22:09:36 +0200 Subject: [PATCH] Make tests run on 1.8.x, add integration setup. --- .../action_dispatch/middleware/request_id.rb | 5 +- actionpack/test/dispatch/request_id_test.rb | 84 ++++++++++--------- 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/request_id.rb b/actionpack/lib/action_dispatch/middleware/request_id.rb index 4728e9f71ec..f4d721f9bff 100644 --- a/actionpack/lib/action_dispatch/middleware/request_id.rb +++ b/actionpack/lib/action_dispatch/middleware/request_id.rb @@ -18,20 +18,19 @@ module ActionDispatch def call(env) env["action_dispatch.request_id"] = external_request_id(env) || internal_request_id - status, headers, body = @app.call(env) headers["X-Request-Id"] = env["action_dispatch.request_id"] [ status, headers, body ] end - + private def external_request_id(env) if env["HTTP_X_REQUEST_ID"].present? env["HTTP_X_REQUEST_ID"].gsub(/[^\w\d\-]/, "").first(255) end end - + def internal_request_id SecureRandom.hex(16) end diff --git a/actionpack/test/dispatch/request_id_test.rb b/actionpack/test/dispatch/request_id_test.rb index 230ff548898..ece8353810c 100644 --- a/actionpack/test/dispatch/request_id_test.rb +++ b/actionpack/test/dispatch/request_id_test.rb @@ -8,52 +8,58 @@ class RequestIdTest < ActiveSupport::TestCase 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 end - + 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 end - + test "generating a request id when none is supplied" do assert_match /\w+/, stub_request.uuid end private - def stub_request(env = {}) - ActionDispatch::RequestId.new(->(env) { [ 200, env, [] ] }).call(env) - ActionDispatch::Request.new(env) - end + + def stub_request(env = {}) + ActionDispatch::RequestId.new(lambda { |env| [ 200, env, [] ] }).call(env) + ActionDispatch::Request.new(env) + end end -# FIXME: Testing end-to-end doesn't seem to work -# -# 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 '/' -# puts @response.headers.inspect -# assert_equal "internal-uu-rid", @response.headers["X-Request-Id"] -# end -# end -# -# -# private -# def with_test_route_set -# with_routing do |set| -# set.draw do -# match ':action', to: ::RequestIdResponseTest::TestController -# end -# -# @app = self.class.build_app(set) do |middleware| -# middleware.use ActionDispatch::RequestId -# end -# -# yield -# end -# end -# end \ No newline at end of file +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 + get '/', {}, 'HTTP_X_REQUEST_ID' => 'X' * 500 + assert_equal "X" * 255, @response.headers["X-Request-Id"] + end + end + + + private + + def with_test_route_set + with_routing do |set| + set.draw do + match '/', :to => ::RequestIdResponseTest::TestController.action(:index) + end + + @app = self.class.build_app(set) do |middleware| + middleware.use ActionDispatch::RequestId + end + + yield + end + end +end \ No newline at end of file