Skip to content

Commit

Permalink
Fix logging feature specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ixti committed Oct 15, 2018
1 parent d774dd7 commit 347a0ff
Showing 1 changed file with 35 additions and 42 deletions.
77 changes: 35 additions & 42 deletions spec/lib/http/features/logging_spec.rb
@@ -1,74 +1,67 @@
# frozen_string_literal: true

require "logger"

RSpec.describe HTTP::Features::Logging do
subject(:feature) { HTTP::Features::Logging.new(:logger => logger) }
let(:logger) { TestLogger.new }
subject(:feature) do
logger = Logger.new(logdev)
logger.formatter = ->(severity, _, _, message) do
format("** %s **\n%s\n", severity, message)
end

described_class.new(:logger => logger)
end

let(:logdev) { StringIO.new }

describe "logging the request" do
let(:request) do
HTTP::Request.new(
:verb => :post,
:uri => "https://example.com/",
:headers => {:accept => "application/json"},
:body => '{"hello": "world!"}'
:verb => :post,
:uri => "https://example.com/",
:headers => {:accept => "application/json"},
:body => '{"hello": "world!"}'
)
end

it "should log the request" do
feature.wrap_request(request)

expect(logger.output).to eq(
[
"> POST https://example.com/",
<<~REQ.strip
Accept: application/json
Host: example.com
User-Agent: http.rb/4.0.0.dev
expect(logdev.string).to eq <<~OUTPUT
** INFO **
> POST https://example.com/
** DEBUG **
Accept: application/json
Host: example.com
User-Agent: http.rb/#{HTTP::VERSION}
{"hello": "world!"}
REQ
]
)
{"hello": "world!"}
OUTPUT
end
end

describe "logging the response" do
let(:response) do
HTTP::Response.new(
:version => "1.1",
:uri => "https://example.com",
:status => 200,
:uri => "https://example.com",
:status => 200,
:headers => {:content_type => "application/json"},
:body => '{"success": true}'
:body => '{"success": true}'
)
end

it "should log the response" do
feature.wrap_response(response)

expect(logger.output).to eq(
[
"< 200 OK",
<<~REQ.strip
Content-Type: application/json
{"success": true}
REQ
]
)
end
end

class TestLogger
attr_reader :output
def initialize
@output = []
end
expect(logdev.string).to eq <<~OUTPUT
** INFO **
< 200 OK
** DEBUG **
Content-Type: application/json
%w[fatal error warn info debug].each do |level|
define_method(level.to_sym) do |*args, &block|
@output << (block ? block.call : args)
end
{"success": true}
OUTPUT
end
end
end

0 comments on commit 347a0ff

Please sign in to comment.