aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/dispatch/request_id_test.rb
blob: aa3175c9862cd634d5060a17502762c493fb419f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# frozen_string_literal: true

require "abstract_unit"

class RequestIdTest < ActiveSupport::TestCase
  test "passing on the request id from the outside" do
    assert_equal "external-uu-rid", stub_request("HTTP_X_REQUEST_ID" => "external-uu-rid").request_id
  end

  test "ensure that only alphanumeric uurids are accepted" do
    assert_equal "X-Hacked-HeaderStuff", stub_request("HTTP_X_REQUEST_ID" => "; X-Hacked-Header: Stuff").request_id
  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).request_id
  end

  test "generating a request id when none is supplied" do
    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
  end

  private

    def stub_request(env = {})
      ActionDispatch::RequestId.new(lambda { |environment| [ 200, environment, [] ] }).call(env)
      ActionDispatch::Request.new(env)
    end
end

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 "/", headers: { "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
          get "/", to: ::RequestIdResponseTest::TestController.action(:index)
        end

        @app = self.class.build_app(set) do |middleware|
          middleware.use ActionDispatch::RequestId
        end

        yield
      end
    end
end