test/stripe/util_test.rb in stripe-3.2.0 vs test/stripe/util_test.rb in stripe-3.3.0

- old
+ new

@@ -166,16 +166,20 @@ context ".log_*" do setup do @old_log_level = Stripe.log_level Stripe.log_level = nil + @old_stderr = $stderr + $stderr = StringIO.new + @old_stdout = $stdout $stdout = StringIO.new end teardown do Stripe.log_level = @old_log_level + $stderr = @old_stderr $stdout = @old_stdout end context ".log_debug" do should "not log if logging is disabled" do @@ -187,17 +191,48 @@ Stripe.log_level = Stripe::LEVEL_DEBUG Util.log_debug("foo") assert_equal "message=foo level=debug \n", $stdout.string end + should "not log if level set to error" do + Stripe.log_level = Stripe::LEVEL_ERROR + Util.log_debug("foo") + assert_equal "", $stdout.string + end + should "not log if level set to info" do Stripe.log_level = Stripe::LEVEL_INFO Util.log_debug("foo") assert_equal "", $stdout.string end end + context ".log_error" do + should "not log if logging is disabled" do + Util.log_error("foo") + assert_equal "", $stdout.string + end + + should "log if level set to debug" do + Stripe.log_level = Stripe::LEVEL_DEBUG + Util.log_error("foo") + assert_equal "message=foo level=error \n", $stderr.string + end + + should "log if level set to error" do + Stripe.log_level = Stripe::LEVEL_ERROR + Util.log_error("foo") + assert_equal "message=foo level=error \n", $stderr.string + end + + should "log if level set to info" do + Stripe.log_level = Stripe::LEVEL_INFO + Util.log_error("foo") + assert_equal "message=foo level=error \n", $stderr.string + end + end + context ".log_info" do should "not log if logging is disabled" do Util.log_info("foo") assert_equal "", $stdout.string end @@ -206,18 +241,60 @@ Stripe.log_level = Stripe::LEVEL_DEBUG Util.log_info("foo") assert_equal "message=foo level=info \n", $stdout.string end + should "not log if level set to error" do + Stripe.log_level = Stripe::LEVEL_ERROR + Util.log_debug("foo") + assert_equal "", $stdout.string + end + should "log if level set to info" do Stripe.log_level = Stripe::LEVEL_INFO Util.log_info("foo") assert_equal "message=foo level=info \n", $stdout.string end end end + context ".log_* with a logger" do + setup do + @out = StringIO.new + logger = ::Logger.new(@out) + + # Set a really simple formatter to make matching output as easy as + # possible. + logger.formatter = proc { |_severity, _datetime, _progname, message| + message + } + + Stripe.logger = logger + end + + context ".log_debug" do + should "log to the logger" do + Util.log_debug("foo") + assert_equal "message=foo ", @out.string + end + end + + context ".log_error" do + should "log to the logger" do + Util.log_error("foo") + assert_equal "message=foo ", @out.string + end + end + + context ".log_info" do + should "log to the logger" do + Util.log_info("foo") + assert_equal "message=foo ", @out.string + end + end + end + context ".normalize_headers" do should "normalize the format of a header key" do assert_equal({ "Request-Id" => nil }, Util.normalize_headers({ "Request-Id" => nil })) assert_equal({ "Request-Id" => nil }, @@ -253,10 +330,18 @@ should "not colorize otherwise" do assert_equal "foo", Util.send(:colorize, "foo", :green, false) end end + context ".level_name" do + should "convert levels to names" do + assert_equal "debug", Util.send(:level_name, LEVEL_DEBUG) + assert_equal "error", Util.send(:level_name, LEVEL_ERROR) + assert_equal "info", Util.send(:level_name, LEVEL_INFO) + end + end + context ".log_internal" do should "log in a terminal friendly way" do out = StringIO.new # Sketchy as anything, but saves us from pulling in a mocking library. @@ -265,19 +350,35 @@ out.instance_eval do def isatty; true; end end Util.send(:log_internal, "message", { foo: "bar" }, - color: :green, level: Stripe::LEVEL_DEBUG, out: out) + color: :green, level: Stripe::LEVEL_DEBUG, logger: nil, out: out) assert_equal "\e[0;32;49mDEBU\e[0m message \e[0;32;49mfoo\e[0m=bar\n", out.string end should "log in a data friendly way" do out = StringIO.new Util.send(:log_internal, "message", { foo: "bar" }, - color: :green, level: Stripe::LEVEL_DEBUG, out: out) + color: :green, level: Stripe::LEVEL_DEBUG, logger: nil, out: out) assert_equal "message=message level=debug foo=bar\n", + out.string + end + + should "log to a logger if set" do + out = StringIO.new + logger = ::Logger.new(out) + + # Set a really simple formatter to make matching output as easy as + # possible. + logger.formatter = proc { |_severity, _datetime, _progname, message| + message + } + + Util.send(:log_internal, "message", { foo: "bar" }, + color: :green, level: Stripe::LEVEL_DEBUG, logger: logger, out: nil) + assert_equal "message=message foo=bar", out.string end end context ".wrap_logfmt_value" do