From bbeb35af1218e29c27190b66830ab5eae6333e94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Rie=C3=9F?= Date: Thu, 2 Sep 2021 16:03:49 +0200 Subject: [PATCH 1/3] [fix] logging behavior for query hashes * we must log only the query when testing for the query as hash * see modified behavior inside Excon::Instrumentors::LoggingInstrumentor * added very simple test --- .../instrumentors/logging_instrumentor.rb | 2 +- .../logging_instrumentor_tests.rb | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/excon/instrumentors/logging_instrumentor.rb b/lib/excon/instrumentors/logging_instrumentor.rb index d1c48a10..9a68080e 100644 --- a/lib/excon/instrumentors/logging_instrumentor.rb +++ b/lib/excon/instrumentors/logging_instrumentor.rb @@ -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 diff --git a/tests/instrumentors/logging_instrumentor_tests.rb b/tests/instrumentors/logging_instrumentor_tests.rb index 16251829..6d90bb1e 100644 --- a/tests/instrumentors/logging_instrumentor_tests.rb +++ b/tests/instrumentors/logging_instrumentor_tests.rb @@ -26,6 +26,33 @@ ] end + tests("connection logger with query as hash").returns(true) do + Excon.stub({:method => :get}, {body: 'body'}, status: 200}) + + log_path = "/tmp/excon_#{Time.now.to_i}.txt" + 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)[1..2] == [ + "request: http://127.0.0.1/logger?test=test\n", + "response: body\n" + ] + end + Excon.stubs.clear env_restore end From 61ee867357e35adea606b8fdc04a374dee9ce24e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Rie=C3=9F?= Date: Thu, 2 Sep 2021 18:08:22 +0200 Subject: [PATCH 2/3] [fix] typo/syntax error on test code --- tests/instrumentors/logging_instrumentor_tests.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/instrumentors/logging_instrumentor_tests.rb b/tests/instrumentors/logging_instrumentor_tests.rb index 6d90bb1e..3c984a72 100644 --- a/tests/instrumentors/logging_instrumentor_tests.rb +++ b/tests/instrumentors/logging_instrumentor_tests.rb @@ -27,7 +27,7 @@ end tests("connection logger with query as hash").returns(true) do - Excon.stub({:method => :get}, {body: 'body'}, status: 200}) + Excon.stub({:method => :get}, {body: 'body', status: 200}) log_path = "/tmp/excon_#{Time.now.to_i}.txt" logger = Logger.new(log_path) From bb90e2c4d878599fe639aa756a1ebe5691d35f2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Rie=C3=9F?= Date: Thu, 2 Sep 2021 20:05:02 +0200 Subject: [PATCH 3/3] [fix] untangle logfile names for tests --- tests/instrumentors/logging_instrumentor_tests.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/instrumentors/logging_instrumentor_tests.rb b/tests/instrumentors/logging_instrumentor_tests.rb index 3c984a72..128d4c65 100644 --- a/tests/instrumentors/logging_instrumentor_tests.rb +++ b/tests/instrumentors/logging_instrumentor_tests.rb @@ -1,4 +1,5 @@ require 'logger' +require 'tempfile' Shindo.tests('logging instrumentor') do env_init @@ -6,7 +7,7 @@ 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| @@ -19,8 +20,10 @@ 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" ] @@ -29,7 +32,7 @@ tests("connection logger with query as hash").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| @@ -47,7 +50,7 @@ path: '/logger', query: {test: 'test'} ) - File.readlines(log_path)[1..2] == [ + File.readlines(log_path) == [ "request: http://127.0.0.1/logger?test=test\n", "response: body\n" ]