Sha256: 7f52ed927602b4c896f1bd24e955bea2ecd251b225452fb4ac232da0ba4351c2

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require 'stringio'
require 'logger'

RSpec.describe Faraday::Response::Logger do
  let(:string_io) { StringIO.new }
  let(:logger) { Logger.new(string_io) }
  let(:conn) do
    rubbles = %w(Barney Betty)
    logger_options = {
      bodies: true,
      formatter: FinAppsCore::Logging::ContenTypeFormatter
    }

    Faraday.new do |b|
      b.response :logger, logger, logger_options
      b.adapter :test do |stubs|
        stubs.get('/text') { [200, {'Content-Type' => 'text/plain'}, 'hello'] }
        stubs.get('/json') { [200, {'Content-Type' => 'application/json'}, rubbles] }
        stubs.get('/pdf') { [200, {'Content-Type' => 'application/pdf'}, 'binary-here'] }
      end
    end
  end

  before do
    logger.level = Logger::DEBUG
  end

  context 'when content type is text/plain' do
    before { conn.get '/text' }

    it 'logs response body' do
      expect(string_io.string).to match('hello')
    end
  end

  context 'when content type is application/json' do
    before { conn.get '/json' }

    it 'logs response body' do
      expect(string_io.string).to match('[\"Barney\", \"Betty\"]')
    end
  end

  context 'when content type is something else' do
    before { conn.get '/pdf' }

    it 'does not log response body' do
      expect(string_io.string).not_to match('binary-here')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
finapps_core-6.0.2 spec/logging/conten_type_formatter_spec.rb
finapps_core-6.0.1 spec/logging/conten_type_formatter_spec.rb