-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#255 - Improve and extend development log entries
- Loading branch information
Showing
14 changed files
with
362 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require "./spec_helper" | ||
|
||
describe Marten::Core::DebugModeLoggable do | ||
describe "::debug_mode_debug_log" do | ||
it "logs a debug message when the debug mode is enabled" do | ||
Log.capture do |logs| | ||
with_overridden_setting("debug", true) do | ||
Marten::Core::DebugModeLoggable.debug_mode_debug_log("Test message") | ||
end | ||
|
||
logs.check(:debug, /Test message/) | ||
end | ||
end | ||
|
||
it "does not log a debug message when the debug mode is not enabled" do | ||
Log.capture do |logs| | ||
with_overridden_setting("debug", false) do | ||
Marten::Core::DebugModeLoggable.debug_mode_debug_log("Test message") | ||
end | ||
|
||
logs.empty | ||
end | ||
end | ||
end | ||
|
||
describe "::debug_mode_info_log" do | ||
it "logs an info message when the debug mode is enabled" do | ||
Log.capture do |logs| | ||
with_overridden_setting("debug", true) do | ||
Marten::Core::DebugModeLoggable.debug_mode_info_log("Test message") | ||
end | ||
|
||
logs.check(:info, /Test message/) | ||
end | ||
end | ||
|
||
it "does not log an info message when the debug mode is not enabled" do | ||
Log.capture do |logs| | ||
with_overridden_setting("debug", false) do | ||
Marten::Core::DebugModeLoggable.debug_mode_info_log("Test message") | ||
end | ||
|
||
logs.empty | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
require "./spec_helper" | ||
|
||
describe Marten::Server::Handlers::DebugLogger do | ||
describe "#call" do | ||
it "generates the expected log entries before and after the request completion" do | ||
output_io = IO::Memory.new | ||
|
||
ctx = HTTP::Server::Context.new( | ||
request: ::HTTP::Request.new( | ||
method: "GET", | ||
resource: "/foo/bar", | ||
headers: HTTP::Headers{"Host" => "example.com", "Accept-Language" => "FR,en;q=0.5"} | ||
), | ||
response: ::HTTP::Server::Response.new(io: IO::Memory.new) | ||
) | ||
|
||
handler = Marten::Server::Handlers::DebugLogger.new | ||
handler.next = HTTP::Handler::HandlerProc.new do |handler_ctx| | ||
handler_ctx.response.output = output_io | ||
handler_ctx.response.print("It works") | ||
end | ||
|
||
Log.capture do |logs| | ||
with_overridden_setting("debug", true) do | ||
handler.call(ctx) | ||
end | ||
|
||
logs.check(:info, /Started \"GET \/foo\/bar\"/i) | ||
logs.next(:info, /Completed with \"200\"/i) | ||
end | ||
end | ||
|
||
it "generates additional log entries when data is present" do | ||
output_io = IO::Memory.new | ||
|
||
ctx = HTTP::Server::Context.new( | ||
request: ::HTTP::Request.new( | ||
method: "GET", | ||
resource: "/foo/bar", | ||
headers: HTTP::Headers{"Host" => "example.com", "Content-Type" => "application/x-www-form-urlencoded"}, | ||
body: "foo=bar&test=xyz" | ||
), | ||
response: ::HTTP::Server::Response.new(io: IO::Memory.new) | ||
) | ||
|
||
handler = Marten::Server::Handlers::DebugLogger.new | ||
handler.next = HTTP::Handler::HandlerProc.new do |handler_ctx| | ||
handler_ctx.response.output = output_io | ||
handler_ctx.response.print("It works") | ||
end | ||
|
||
Log.capture do |logs| | ||
with_overridden_setting("debug", true) do | ||
handler.call(ctx) | ||
end | ||
|
||
logs.check(:info, /Started \"GET \/foo\/bar\"/i) | ||
logs.next(:info, /Data: {\"foo\" => \[\"bar\"\], \"test\" => \[\"xyz\"\]}/i) | ||
logs.next(:info, /Completed with \"200\"/i) | ||
end | ||
end | ||
|
||
it "generates additional log entries when query params are present" do | ||
output_io = IO::Memory.new | ||
|
||
ctx = HTTP::Server::Context.new( | ||
request: ::HTTP::Request.new( | ||
method: "GET", | ||
resource: "/foo/bar?foo=bar&test=xyz", | ||
headers: HTTP::Headers{"Host" => "example.com"}, | ||
), | ||
response: ::HTTP::Server::Response.new(io: IO::Memory.new) | ||
) | ||
|
||
handler = Marten::Server::Handlers::DebugLogger.new | ||
handler.next = HTTP::Handler::HandlerProc.new do |handler_ctx| | ||
handler_ctx.response.output = output_io | ||
handler_ctx.response.print("It works") | ||
end | ||
|
||
Log.capture do |logs| | ||
with_overridden_setting("debug", true) do | ||
handler.call(ctx) | ||
end | ||
|
||
logs.check(:info, /Started \"GET \/foo\/bar\"/i) | ||
logs.next(:info, /Query params: {\"foo\" => \[\"bar\"\], \"test\" => \[\"xyz\"\]}/i) | ||
logs.next(:info, /Completed with \"200\"/i) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require "./spec_helper" | ||
|
||
describe Marten::Server do | ||
describe "#handlers" do | ||
it "returns the expected handlers in non-debug mode" do | ||
with_overridden_setting("debug", false) do | ||
Marten::Server.handlers.size.should eq 5 | ||
|
||
Marten::Server.handlers[0].should be_a HTTP::ErrorHandler | ||
Marten::Server.handlers[1].should be_a Marten::Server::Handlers::Logger | ||
Marten::Server.handlers[2].should be_a Marten::Server::Handlers::Error | ||
Marten::Server.handlers[3].should be_a Marten::Server::Handlers::Middleware | ||
Marten::Server.handlers[4].should be_a Marten::Server::Handlers::Routing | ||
end | ||
end | ||
|
||
it "returns the expected handlers in debug mode" do | ||
with_overridden_setting("debug", true) do | ||
Marten::Server.handlers.size.should eq 5 | ||
|
||
Marten::Server.handlers[0].should be_a HTTP::ErrorHandler | ||
Marten::Server.handlers[1].should be_a Marten::Server::Handlers::DebugLogger | ||
Marten::Server.handlers[2].should be_a Marten::Server::Handlers::Error | ||
Marten::Server.handlers[3].should be_a Marten::Server::Handlers::Middleware | ||
Marten::Server.handlers[4].should be_a Marten::Server::Handlers::Routing | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
ENV["MARTEN_ENV"] = "test" | ||
|
||
require "json" | ||
require "log/spec" | ||
require "spec" | ||
require "timecop" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.