BigW Consortium Gitlab

rack_middleware_spec.rb 3.35 KB
Newer Older
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
require 'spec_helper'

describe Gitlab::Metrics::RackMiddleware do
  let(:app) { double(:app) }

  let(:middleware) { described_class.new(app) }

  let(:env) { { 'REQUEST_METHOD' => 'GET', 'REQUEST_URI' => '/foo' } }

  describe '#call' do
    before do
      expect_any_instance_of(Gitlab::Metrics::Transaction).to receive(:finish)
    end

    it 'tracks a transaction' do
      expect(app).to receive(:call).with(env).and_return('yay')

      expect(middleware.call(env)).to eq('yay')
    end

    it 'tags a transaction with the name and action of a controller' do
      klass      = double(:klass, name: 'TestController')
      controller = double(:controller, class: klass, action_name: 'show')

      env['action_controller.instance'] = controller

      allow(app).to receive(:call).with(env)

      expect(middleware).to receive(:tag_controller).
        with(an_instance_of(Gitlab::Metrics::Transaction), env)

      middleware.call(env)
    end
34 35 36 37 38 39 40 41 42 43 44 45 46 47

    it 'tags a transaction with the method andpath of the route in the grape endpoint' do
      route    = double(:route, route_method: "GET", route_path: "/:version/projects/:id/archive(.:format)")
      endpoint = double(:endpoint, route: route)

      env['api.endpoint'] = endpoint

      allow(app).to receive(:call).with(env)

      expect(middleware).to receive(:tag_endpoint).
        with(an_instance_of(Gitlab::Metrics::Transaction), env)

      middleware.call(env)
    end
48 49 50 51 52 53 54 55 56
  end

  describe '#transaction_from_env' do
    let(:transaction) { middleware.transaction_from_env(env) }

    it 'returns a Transaction' do
      expect(transaction).to be_an_instance_of(Gitlab::Metrics::Transaction)
    end

57 58 59
    it 'stores the request method and URI in the transaction as values' do
      expect(transaction.values[:request_method]).to eq('GET')
      expect(transaction.values[:request_uri]).to eq('/foo')
60
    end
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

    context "when URI includes sensitive parameters" do
      let(:env) do
        {
          'REQUEST_METHOD' => 'GET',
          'REQUEST_URI'    => '/foo?private_token=my-token',
          'PATH_INFO' => '/foo',
          'QUERY_STRING' => 'private_token=my_token',
          'action_dispatch.parameter_filter' => [:private_token]
        }
      end

      it 'stores the request URI with the sensitive parameters filtered' do
        expect(transaction.values[:request_uri]).to eq('/foo?private_token=[FILTERED]')
      end
    end
77 78 79 80 81 82 83 84 85 86 87 88 89
  end

  describe '#tag_controller' do
    let(:transaction) { middleware.transaction_from_env(env) }

    it 'tags a transaction with the name and action of a controller' do
      klass      = double(:klass, name: 'TestController')
      controller = double(:controller, class: klass, action_name: 'show')

      env['action_controller.instance'] = controller

      middleware.tag_controller(transaction, env)

90
      expect(transaction.action).to eq('TestController#show')
91 92
    end
  end
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

  describe '#tag_endpoint' do
    let(:transaction) { middleware.transaction_from_env(env) }

    it 'tags a transaction with the method and path of the route in the grape endpount' do
      route    = double(:route, route_method: "GET", route_path: "/:version/projects/:id/archive(.:format)")
      endpoint = double(:endpoint, route: route)

      env['api.endpoint'] = endpoint

      middleware.tag_endpoint(transaction, env)

      expect(transaction.action).to eq('Grape#GET /projects/:id/archive')
    end
  end
108
end