-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#33] basic_logger using pretty inspect, printing exception message. …
…basic_logger unit tests. fixing credo warnings.
- Loading branch information
1 parent
54c6545
commit 21be1c0
Showing
7 changed files
with
75 additions
and
40 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
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
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
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,51 @@ | ||
defmodule Dayron.BasicLoggerTest do | ||
use ExUnit.Case, async: true | ||
alias Dayron.BasicLogger | ||
alias Dayron.Request | ||
alias Dayron.Response | ||
alias Dayron.ClientError | ||
import ExUnit.CaptureIO | ||
|
||
defp capture_log(fun) do | ||
data = capture_io(:user, fn -> | ||
Process.put(:capture_log, fun.()) | ||
Logger.flush() | ||
end) | ||
{Process.get(:capture_log), data} | ||
end | ||
|
||
setup do | ||
body = %{name: "Full Name", age: 30} | ||
options = [params: [{:q, "qu ery"}, {:page, 2}]] | ||
request = %Request{method: :get, url: "http://localhost/resources", body: body, headers: [access_token: "token"], options: options} | ||
{:ok, request: request} | ||
end | ||
|
||
test "logs a Response with success", %{request: request} do | ||
response = %Response{status_code: 200, elapsed_time: 28000} | ||
{:ok, output} = capture_log fn -> | ||
:ok = BasicLogger.log(request, response) | ||
end | ||
assert output =~ ~r/\[debug\] GET http\:\/\/localhost\/resources/ | ||
assert output =~ ~r/\[debug\] Response: 200 in 28ms/ | ||
end | ||
|
||
test "logs Response with error", %{request: request} do | ||
response = %Response{status_code: 500, elapsed_time: 5, body: "Internal Server Error"} | ||
{:ok, output} = capture_log fn -> | ||
:ok = BasicLogger.log(request, response) | ||
end | ||
assert output =~ ~r/\[error\] GET http\:\/\/localhost\/resources/ | ||
assert output =~ ~r/\[error\] Response: 500 in 5µs/ | ||
end | ||
|
||
test "logs ClientError exceptions", %{request: request} do | ||
response = %ClientError{reason: :timeout} | ||
{:ok, output} = capture_log fn -> | ||
:ok = BasicLogger.log(request, response) | ||
end | ||
|
||
assert output =~ ~r/\[error\] unexpected client error in request\:\n\nGET/ | ||
assert output =~ ~r/Reason\: \:timeout/ | ||
end | ||
end |