require 'spec_helper' require 'stringio' require 'logger' describe DefaultLogger do def conn(logger, logger_options={}) rubbles = ['Barney', 'Betty', 'Bam Bam'] Faraday.new do |b| b.use DefaultLogger, logger: @logger b.adapter :test do |stubs| stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] } stubs.post('/ohai') { [200, {'Content-Type' => 'text/html'}, 'fred'] } stubs.get('/ohno') { [200, {'Content-Type' => 'text/html'}, 'wilma'] } stubs.post('/ohyes') { [200, {'Content-Type' => 'text/html'}, 'pebbles'] } stubs.get('/rubbles') { [200, {'Content-Type' => 'application/json'}, rubbles] } end end end before do @io = StringIO.new @logger = Logger.new(@io) @logger.level = Logger::DEBUG @conn = conn(@logger) @resp = @conn.get '/hello', nil, :accept => 'text/html' end it "still returns output" do expect(@resp.body).to eq 'hello' end it "logss method and url" do expect(@io.string).to match 'Started GET request to:' end it "logss request headers" do expect(@io.string).to match %("Accept":"text/html") end it "logs response headers" do expect(@io.string).to match %("Content-Type":"text/html") end it "logs request body" do app = conn(@logger, :bodies => { :request => true }) app.post '/ohyes', 'name=Tamago', :accept => 'text/html' expect(@io.string).to match %(name=Tamago) end it "logs response body" do app = conn(@logger, :bodies => { :response => true }) app.get '/ohno', :accept => 'text/html' expect(@io.string).to match %(wilma) end it "logs request and response body" do app = conn(@logger, :bodies => true) app.post '/ohyes', 'name=Ebi', :accept => 'text/html' expect(@io.string).to match %(name=Ebi) expect(@io.string).to match %(pebbles) end it "logs response body object" do app = conn(@logger, :bodies => true) app.get '/rubbles', nil, :accept => 'text/html' expect(@io.string).to match %([\"Barney\", \"Betty\", \"Bam Bam\"]\n) end it 'obfuscates the token in the header' do app = conn(@logger, :bodies => true) @conn.post '/ohai', 'name=Toro', :accept => 'text/html', :authorization => 'Bearer 1234567890' expect(@io.string).to_not match %(1234567890) end end