Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rack_timeout_observer_spec.rb « cluster « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2d4a7a3b170e738c37d6cb9d35acc14e415264e6 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Cluster::RackTimeoutObserver do
  let(:counter) { Gitlab::Metrics::NullMetric.instance }

  before do
    allow(Gitlab::Metrics).to receive(:counter)
      .with(any_args)
      .and_return(counter)
  end

  describe '#callback' do
    context 'when request times out' do
      let(:env) do
        {
          ::Rack::Timeout::ENV_INFO_KEY => double(state: :timed_out),
          'action_dispatch.request.parameters' => {
            'controller' => 'foo',
            'action' => 'bar'
          }
        }
      end

      subject { described_class.new }

      it 'increments counter' do
        expect(counter)
          .to receive(:increment)
          .with({ controller: 'foo', action: 'bar', route: nil, state: :timed_out })

        subject.callback.call(env)
      end
    end

    context 'when request expires' do
      let(:endpoint) { double }
      let(:env) do
        {
          ::Rack::Timeout::ENV_INFO_KEY => double(state: :expired),
          Grape::Env::API_ENDPOINT => endpoint
        }
      end

      subject { described_class.new }

      it 'increments counter' do
        allow(endpoint).to receive_message_chain('route.pattern.origin') { 'foobar' }
        expect(counter)
          .to receive(:increment)
          .with({ controller: nil, action: nil, route: 'foobar', state: :expired })

        subject.callback.call(env)
      end
    end

    context 'when request is being processed' do
      let(:endpoint) { double }
      let(:env) do
        {
          ::Rack::Timeout::ENV_INFO_KEY => double(state: :active),
          Grape::Env::API_ENDPOINT => endpoint
        }
      end

      subject { described_class.new }

      it 'does not increment counter' do
        allow(endpoint).to receive_message_chain('route.pattern.origin') { 'foobar' }
        expect(counter).not_to receive(:increment)

        subject.callback.call(env)
      end
    end

    context 'when request contains invalid string' do
      let(:env) do
        {
          ::Rack::Timeout::ENV_INFO_KEY => double(state: :timed_out),
          'action_dispatch.request.parameters' => {
            'controller' => 'foo',
            'action' => '\u003c',
            'route' => '?8\u003c/x'
          }
        }
      end

      subject { described_class.new }

      it 'sanitizes string' do
        expect(counter)
          .to receive(:increment)
          .with({ controller: 'foo', action: '\\u003c', route: '?8\\u003c/x', state: :timed_out })

        subject.callback.call(env)
      end
    end
  end
end