Commit 2f765e2a authored by Quang-Minh Nguyen's avatar Quang-Minh Nguyen Committed by Kamil Trzciński

Conver hashy query to query string

Issue https://gitlab.com/gitlab-org/gitlab/-/issues/301009
parent 0015e540
...@@ -86,12 +86,16 @@ module Peek ...@@ -86,12 +86,16 @@ module Peek
uri.hostname = call[:host] uri.hostname = call[:host]
uri.port = call[:port] uri.port = call[:port]
uri.path = call[:path] uri.path = call[:path]
uri.query = call[:query] uri.query = generate_query(call[:query])
uri.to_s uri.to_s
rescue URI::Error rescue StandardError
'unknown' 'unknown'
end end
def generate_query(query_string)
query_string.is_a?(Hash) ? query_string.to_query : query_string.to_s
end
end end
end end
end end
...@@ -12,29 +12,29 @@ RSpec.describe Peek::Views::ExternalHttp, :request_store do ...@@ -12,29 +12,29 @@ RSpec.describe Peek::Views::ExternalHttp, :request_store do
end end
let(:event_1) do let(:event_1) do
double(:event, payload: { {
method: 'POST', code: "200", duration: 0.03, method: 'POST', code: "200", duration: 0.03,
scheme: 'https', host: 'gitlab.com', port: 80, path: '/api/v4/projects', scheme: 'https', host: 'gitlab.com', port: 80, path: '/api/v4/projects',
query: 'current=true' query: 'current=true'
}) }
end end
let(:event_2) do let(:event_2) do
double(:event, payload: { {
method: 'POST', duration: 1.3, method: 'POST', duration: 1.3,
scheme: 'http', host: 'gitlab.com', port: 80, path: '/api/v4/projects/2/issues', scheme: 'http', host: 'gitlab.com', port: 80, path: '/api/v4/projects/2/issues',
query: 'current=true', query: 'current=true',
exception_object: Net::ReadTimeout.new exception_object: Net::ReadTimeout.new
}) }
end end
let(:event_3) do let(:event_3) do
double(:event, payload: { {
method: 'GET', code: "301", duration: 0.005, method: 'GET', code: "301", duration: 0.005,
scheme: 'http', host: 'gitlab.com', port: 80, path: '/api/v4/projects/2', scheme: 'http', host: 'gitlab.com', port: 80, path: '/api/v4/projects/2',
query: 'current=true', query: 'current=true',
proxy_host: 'proxy.gitlab.com', proxy_port: 8080 proxy_host: 'proxy.gitlab.com', proxy_port: 8080
}) }
end end
it 'returns no results' do it 'returns no results' do
...@@ -44,9 +44,9 @@ RSpec.describe Peek::Views::ExternalHttp, :request_store do ...@@ -44,9 +44,9 @@ RSpec.describe Peek::Views::ExternalHttp, :request_store do
end end
it 'returns aggregated results' do it 'returns aggregated results' do
subscriber.request(event_1) subscriber.request(double(:event, payload: event_1))
subscriber.request(event_2) subscriber.request(double(:event, payload: event_2))
subscriber.request(event_3) subscriber.request(double(:event, payload: event_3))
results = subject.results results = subject.results
expect(results[:calls]).to eq(3) expect(results[:calls]).to eq(3)
...@@ -86,105 +86,129 @@ RSpec.describe Peek::Views::ExternalHttp, :request_store do ...@@ -86,105 +86,129 @@ RSpec.describe Peek::Views::ExternalHttp, :request_store do
end end
context 'when the host is in IPv4 format' do context 'when the host is in IPv4 format' do
before do
event_1[:host] = '1.2.3.4'
end
it 'displays IPv4 in the label' do it 'displays IPv4 in the label' do
subscriber.request( subscriber.request(double(:event, payload: event_1))
double(:event, payload: {
method: 'POST', code: "200", duration: 0.03, expect(subject.results[:details]).to contain_exactly(
scheme: 'https', host: '1.2.3.4', port: 80, path: '/api/v4/projects', a_hash_including(
query: 'current=true' duration: 30.0,
}) label: "POST https://1.2.3.4:80/api/v4/projects?current=true",
) code: "Response status: 200",
expect( proxy: nil,
subject.results[:details].map { |data| data.slice(:duration, :label, :code, :proxy, :error, :warnings) } error: nil,
).to match_array( warnings: []
[ )
{
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
end end
context 'when the host is in IPv6 foramat' do context 'when the host is in IPv6 foramat' do
before do
event_1[:host] = '2606:4700:90:0:f22e:fbec:5bed:a9b9'
end
it 'displays IPv6 in the label' do it 'displays IPv6 in the label' do
subscriber.request( subscriber.request(double(:event, payload: event_1))
double(:event, payload: {
method: 'POST', code: "200", duration: 0.03, expect(subject.results[:details]).to contain_exactly(
scheme: 'https', host: '2606:4700:90:0:f22e:fbec:5bed:a9b9', port: 80, path: '/api/v4/projects', a_hash_including(
query: 'current=true' 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: []
)
) )
expect( end
subject.results[:details].map { |data| data.slice(:duration, :label, :code, :proxy, :error, :warnings) } end
).to match_array(
[ context 'when the query is a hash' do
{ before do
duration: 30.0, event_1[:query] = { current: true, 'item1' => 'string', 'item2' => [1, 2] }
label: "POST https://[2606:4700:90:0:f22e:fbec:5bed:a9b9]:80/api/v4/projects?current=true", end
code: "Response status: 200",
proxy: nil, it 'converts query hash into a query string' do
error: nil, subscriber.request(double(:event, payload: event_1))
warnings: []
} expect(subject.results[:details]).to contain_exactly(
] a_hash_including(
duration: 30.0,
label: "POST https://gitlab.com:80/api/v4/projects?current=true&item1=string&item2%5B%5D=1&item2%5B%5D=2",
code: "Response status: 200",
proxy: nil,
error: nil,
warnings: []
)
) )
end end
end end
context 'when the host is invalid' do context 'when the host is invalid' do
before do
event_1[:host] = '!@#%!@#%!@#%'
end
it 'displays unknown in the label' do it 'displays unknown in the label' do
subscriber.request( subscriber.request(double(:event, payload: event_1))
double(:event, payload: {
method: 'POST', code: "200", duration: 0.03, expect(subject.results[:details]).to contain_exactly(
scheme: 'https', host: '!@#%!@#%!@#%', port: 80, path: '/api/v4/projects', a_hash_including(
query: 'current=true' duration: 30.0,
}) label: "POST unknown",
) code: "Response status: 200",
expect( proxy: nil,
subject.results[:details].map { |data| data.slice(:duration, :label, :code, :proxy, :error, :warnings) } error: nil,
).to match_array( warnings: []
[ )
{
duration: 30.0,
label: "POST unknown",
code: "Response status: 200",
proxy: nil,
error: nil,
warnings: []
}
]
) )
end end
end end
context 'when another URI component is invalid' do context 'when URI creation raises an URI::Error' do
before do
# This raises an URI::Error exception
event_1[:port] = 'invalid'
end
it 'displays unknown in the label' do it 'displays unknown in the label' do
subscriber.request( subscriber.request(double(:event, payload: event_1))
double(:event, payload: {
method: 'POST', code: "200", duration: 0.03, expect(subject.results[:details]).to contain_exactly(
scheme: 'https', host: 'invalid', port: 'invalid', path: '/api/v4/projects', a_hash_including(
query: 'current=true' duration: 30.0,
}) label: "POST unknown",
code: "Response status: 200",
proxy: nil,
error: nil,
warnings: []
)
) )
expect( end
subject.results[:details].map { |data| data.slice(:duration, :label, :code, :proxy, :error, :warnings) } end
).to match_array(
[ context 'when URI creation raises a StandardError exception' do
{ before do
duration: 30.0, # This raises a TypeError exception
label: "POST unknown", event_1[:scheme] = 1234
code: "Response status: 200", end
proxy: nil,
error: nil, it 'displays unknown in the label' do
warnings: [] subscriber.request(double(:event, payload: event_1))
}
] expect(subject.results[:details]).to contain_exactly(
a_hash_including(
duration: 30.0,
label: "POST unknown",
code: "Response status: 200",
proxy: nil,
error: nil,
warnings: []
)
) )
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment