Sha256: 32a67fd69c44e115a47d1329594f18f8b1a345fd4f6f419067017e4c84f7671d

Contents?: true

Size: 1.74 KB

Versions: 49

Compression:

Stored size: 1.74 KB

Contents

require "spec_helper"

if defined?(::Rack)
  describe Timber::Integrations::Rack::HTTPContext do
    let(:time) { Time.utc(2016, 9, 1, 12, 0, 0) }
    let(:io) { StringIO.new }
    let(:logger) do
      logger = Timber::Logger.new(io)
      logger.level = ::Logger::INFO
      logger
    end

    around(:each) do |example|
      class RackHttpController < ActionController::Base
        layout nil

        def index
          Thread.current[:_timber_context_snapshot] = Timber::CurrentContext.instance.snapshot
          render json: {}
        end

        def method_for_action(action_name)
          action_name
        end
      end

      ::RailsApp.routes.draw do
        get '/rack_http' => 'rack_http#index'
      end

      with_rails_logger(logger) do
        Timecop.freeze(time) { example.run }
      end

      Object.send(:remove_const, :RackHttpController)
    end

    describe "#process" do
      it "should set the context" do
        allow(Benchmark).to receive(:ms).and_return(1).and_yield

        dispatch_rails_request("/rack_http")
        http_context = Thread.current[:_timber_context_snapshot][:http]

        expect(http_context).to eq({:method=>"GET", :path=>"/rack_http", :remote_addr=>"123.456.789.10", :request_id=>"unique-request-id-1234"})

        lines = clean_lines(io.string.split("\n"))
        expect(lines.length).to eq(3)
        lines.each do |line|
          expect(line).to include("\"http\":{\"method\":\"GET\",\"path\":\"/rack_http\",\"remote_addr\":\"123.456.789.10\",\"request_id\":\"unique-request-id-1234\"}")
        end
      end
    end

    # Remove blank lines since Rails does this to space out requests in the logs
    def clean_lines(lines)
      lines.select { |line| !line.start_with?(" @metadat") }
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
timber-2.1.0.rc6 spec/timber/integrations/rack/http_context_spec.rb
timber-2.1.0.rc5 spec/timber/integrations/rack/http_context_spec.rb
timber-2.1.0.rc4 spec/timber/integrations/rack/http_context_spec.rb
timber-2.1.0.rc3 spec/timber/integrations/rack/http_context_spec.rb
timber-2.1.0.rc2 spec/timber/integrations/rack/http_context_spec.rb
timber-2.1.0.rc1 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.24 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.23 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.22 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.21 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.20 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.19 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.17 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.16 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.15 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.14 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.12 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.11 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.10 spec/timber/integrations/rack/http_context_spec.rb
timber-2.0.9 spec/timber/integrations/rack/http_context_spec.rb