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

external_http_spec.rb « views « peek « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cc1813db62272c7a3d07035239a5076fb1ab0714 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Peek::Views::ExternalHttp, :request_store do
  subject { described_class.new }

  let(:subscriber) { Gitlab::Metrics::Subscribers::ExternalHttp.new }

  before do
    allow(Gitlab::PerformanceBar).to receive(:enabled_for_request?).and_return(true)
  end

  let(:event_1) do
    double(:event, payload: {
      method: 'POST', code: "200", duration: 0.03,
      scheme: 'https', host: 'gitlab.com', port: 80, path: '/api/v4/projects',
      query: 'current=true'
    })
  end

  let(:event_2) do
    double(:event, payload: {
      method: 'POST', duration: 1.3,
      scheme: 'http', host: 'gitlab.com', port: 80, path: '/api/v4/projects/2/issues',
      query: 'current=true',
      exception_object: Net::ReadTimeout.new
    })
  end

  let(:event_3) do
    double(:event, payload: {
      method: 'GET', code: "301", duration: 0.005,
      scheme: 'http', host: 'gitlab.com', port: 80, path: '/api/v4/projects/2',
      query: 'current=true',
      proxy_host: 'proxy.gitlab.com', proxy_port: 8080
    })
  end

  it 'returns no results' do
    expect(subject.results).to eq(
      calls: 0, details: [], duration: "0ms", warnings: []
    )
  end

  it 'returns aggregated results' do
    subscriber.request(event_1)
    subscriber.request(event_2)
    subscriber.request(event_3)

    results = subject.results
    expect(results[:calls]).to eq(3)
    expect(results[:duration]).to eq("1335.00ms")
    expect(results[:details].count).to eq(3)

    expected = [
      {
        duration: 30.0,
        label: "POST https://gitlab.com:80/api/v4/projects?current=true",
        code: "Response status: 200",
        proxy: nil,
        error: nil,
        warnings: []
      },
      {
        duration: 1300,
        label: "POST http://gitlab.com:80/api/v4/projects/2/issues?current=true",
        code: nil,
        proxy: nil,
        error: "Exception: Net::ReadTimeout",
        warnings: ["1300.0 over 100"]
      },
      {
        duration: 5.0,
        label: "GET http://gitlab.com:80/api/v4/projects/2?current=true",
        code: "Response status: 301",
        proxy: nil,
        error: nil,
        warnings: []
      }
    ]

    expect(
      results[:details].map { |data| data.slice(:duration, :label, :code, :proxy, :error, :warnings) }
    ).to match_array(expected)
  end

  context 'when the host is in IPv4 format' do
    it 'displays IPv4 in the label' do
      subscriber.request(
        double(:event, payload: {
          method: 'POST', code: "200", duration: 0.03,
          scheme: 'https', host: '1.2.3.4', port: 80, path: '/api/v4/projects',
          query: 'current=true'
        })
      )
      expect(
        subject.results[:details].map { |data| data.slice(:duration, :label, :code, :proxy, :error, :warnings) }
      ).to match_array(
        [
          {
            duration: 30.0,
            label: "POST https://1.2.3.4:80/api/v4/projects?current=true",
            code: "Response status: 200",
            proxy: nil,
            error: nil,
            warnings: []
          }
        ]
      )
    end
  end

  context 'when the host is in IPv6 foramat' do
    it 'displays IPv6 in the label' do
      subscriber.request(
        double(:event, payload: {
          method: 'POST', code: "200", duration: 0.03,
          scheme: 'https', host: '2606:4700:90:0:f22e:fbec:5bed:a9b9', port: 80, path: '/api/v4/projects',
          query: 'current=true'
        })
      )
      expect(
        subject.results[:details].map { |data| data.slice(:duration, :label, :code, :proxy, :error, :warnings) }
      ).to match_array(
        [
          {
            duration: 30.0,
            label: "POST https://[2606:4700:90:0:f22e:fbec:5bed:a9b9]:80/api/v4/projects?current=true",
            code: "Response status: 200",
            proxy: nil,
            error: nil,
            warnings: []
          }
        ]
      )
    end
  end

  context 'when the host is invalid' do
    it 'displays unknown in the label' do
      subscriber.request(
        double(:event, payload: {
          method: 'POST', code: "200", duration: 0.03,
          scheme: 'https', host: '!@#%!@#%!@#%', port: 80, path: '/api/v4/projects',
          query: 'current=true'
        })
      )
      expect(
        subject.results[:details].map { |data| data.slice(:duration, :label, :code, :proxy, :error, :warnings) }
      ).to match_array(
        [
          {
            duration: 30.0,
            label: "POST unknown",
            code: "Response status: 200",
            proxy: nil,
            error: nil,
            warnings: []
          }
        ]
      )
    end
  end

  context 'when another URI component is invalid' do
    it 'displays unknown in the label' do
      subscriber.request(
        double(:event, payload: {
          method: 'POST', code: "200", duration: 0.03,
          scheme: 'https', host: 'invalid', port: 'invalid', path: '/api/v4/projects',
          query: 'current=true'
        })
      )
      expect(
        subject.results[:details].map { |data| data.slice(:duration, :label, :code, :proxy, :error, :warnings) }
      ).to match_array(
        [
          {
            duration: 30.0,
            label: "POST unknown",
            code: "Response status: 200",
            proxy: nil,
            error: nil,
            warnings: []
          }
        ]
      )
    end
  end
end