summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/sherlock/middleware_spec.rb
blob: 2016023df06f330358641dcd12007f48aec7a5cc (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
require 'spec_helper'

describe Gitlab::Sherlock::Middleware do
  let(:app) { double(:app) }
  let(:middleware) { described_class.new(app) }

  describe '#call' do
    describe 'when instrumentation is enabled' do
      it 'instruments a request' do
        allow(middleware).to receive(:instrument?).and_return(true)
        allow(middleware).to receive(:call_with_instrumentation)

        middleware.call({})
      end
    end

    describe 'when instrumentation is disabled' do
      it "doesn't instrument a request" do
        allow(middleware).to receive(:instrument).and_return(false)
        allow(app).to receive(:call)

        middleware.call({})
      end
    end
  end

  describe '#call_with_instrumentation' do
    it 'instruments a request' do
      trans = double(:transaction)
      retval = 'cats are amazing'
      env = {}

      allow(app).to receive(:call).with(env).and_return(retval)
      allow(middleware).to receive(:transaction_from_env).and_return(trans)
      allow(trans).to receive(:run).and_yield.and_return(retval)
      allow(Gitlab::Sherlock.collection).to receive(:add).with(trans)

      middleware.call_with_instrumentation(env)
    end
  end

  describe '#instrument?' do
    it 'returns false for a text/css request' do
      env = { 'HTTP_ACCEPT' => 'text/css', 'REQUEST_URI' => '/' }

      expect(middleware.instrument?(env)).to eq(false)
    end

    it 'returns false for a request to a Sherlock route' do
      env = {
        'HTTP_ACCEPT' => 'text/html',
        'REQUEST_URI' => '/sherlock/transactions'
      }

      expect(middleware.instrument?(env)).to eq(false)
    end

    it 'returns true for a request that should be instrumented' do
      env = {
        'HTTP_ACCEPT' => 'text/html',
        'REQUEST_URI' => '/cats'
      }

      expect(middleware.instrument?(env)).to eq(true)
    end
  end

  describe '#transaction_from_env' do
    it 'returns a Transaction' do
      env = {
        'HTTP_ACCEPT' => 'text/html',
        'REQUEST_URI' => '/cats'
      }

      expect(middleware.transaction_from_env(env))
        .to be_an_instance_of(Gitlab::Sherlock::Transaction)
    end
  end
end