Skip to content

Commit

Permalink
Merge pull request #759 from carlfin/logging-instrumentor-queries-as-…
Browse files Browse the repository at this point in the history
…hash

[fix] logging behavior for query hashes
  • Loading branch information
geemus committed Sep 2, 2021
2 parents 4075233 + bb90e2c commit 973e581
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/excon/instrumentors/logging_instrumentor.rb
Expand Up @@ -28,7 +28,7 @@ def self.instrument(name, params = {})
info << "?"

if params[:query].is_a?(Hash)
info << params.to_a.map{ |key,value| "#{key}=#{value}" }.join('&')
info << params[:query].to_a.map { |key,value| "#{key}=#{value}" }.join('&')
else
info << params[:query]
end
Expand Down
34 changes: 32 additions & 2 deletions tests/instrumentors/logging_instrumentor_tests.rb
@@ -1,12 +1,13 @@
require 'logger'
require 'tempfile'

Shindo.tests('logging instrumentor') do
env_init

tests("connection logger").returns(true) do
Excon.stub({:method => :get}, {body: 'body', status: 200})

log_path = "/tmp/excon_#{Time.now.to_i}.txt"
log_path = Tempfile.create.path
logger = Logger.new(log_path)
# omit datetime to simplify test matcher
logger.formatter = proc do |severity, datetime, progname, msg|
Expand All @@ -19,13 +20,42 @@
logger: logger,
mock: true
)

response = connection.request(method: :get, path: '/logger')
File.readlines(log_path)[1..2] == [

File.readlines(log_path) == [
"request: http://127.0.0.1/logger\n",
"response: body\n"
]
end

tests("connection logger with query as hash").returns(true) do
Excon.stub({:method => :get}, {body: 'body', status: 200})

log_path = Tempfile.create.path
logger = Logger.new(log_path)
# omit datetime to simplify test matcher
logger.formatter = proc do |severity, datetime, progname, msg|
"#{msg}\n"
end

connection = Excon.new(
'http://127.0.0.1:9292',
instrumentor: Excon::LoggingInstrumentor,
logger: logger,
mock: true
)
response = connection.request(
method: :get,
path: '/logger',
query: {test: 'test'}
)
File.readlines(log_path) == [
"request: http://127.0.0.1/logger?test=test\n",
"response: body\n"
]
end

Excon.stubs.clear
env_restore
end

0 comments on commit 973e581

Please sign in to comment.