From 7bd91e5a11b8d994ba6f601522d6ce85e97158b2 Mon Sep 17 00:00:00 2001 From: Damir Makhmutov Date: Sun, 10 Sep 2017 17:45:45 +0300 Subject: [PATCH] Fix specs --- Gemfile | 2 ++ lib/rack/logstash.rb | 10 +++++----- spec/rack_logstash_spec.rb | 30 ++++++++++++------------------ spec/spec_helper.rb | 3 ++- 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Gemfile b/Gemfile index e088013..17aa1b3 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ source 'https://rubygems.org/' +gem 'byebug' + gemspec diff --git a/lib/rack/logstash.rb b/lib/rack/logstash.rb index 3c8fdd1..1e6a86d 100644 --- a/lib/rack/logstash.rb +++ b/lib/rack/logstash.rb @@ -70,11 +70,11 @@ def exception_log_entry(env, ex) req = Rack::Request.new(env) common_entry_fields(env).tap do |e| - e['message'] = "#{req.ip} " + - "#{req.request_method} " + - "#{req.path_info_and_query_string} " + - "#{req.server_protocol} " + - "=> #{ex.message} (#{ex.class})" + e['message'] = "#{req.ip} " + + "#{req.request_method} " + + "#{req.path_info_and_query_string} " + + "#{req.server_protocol} " + + "=> #{ex.message} (#{ex.class})" e['exception'] = { 'class' => ex.class, diff --git a/spec/rack_logstash_spec.rb b/spec/rack_logstash_spec.rb index 3444fd1..63b83a4 100644 --- a/spec/rack_logstash_spec.rb +++ b/spec/rack_logstash_spec.rb @@ -22,28 +22,22 @@ # This... is evil let(:log_entry) { {} } - before :each do - expect(Rack::Logstash::Transport). - to receive(:new). - with('tcp://logstash:5151'). + before do + expect(Rack::Logstash::Transport).to receive(:new).with('tcp://logstash:5151'). and_return(mock_transport) expect(mock_transport).to receive(:send) do |s| - log_entry.replace(JSON.parse(s)) + log_entry.replace(s) end - allow(Socket). - to receive(:gethostname). - and_return("server.example.com") + allow(Socket).to receive(:gethostname).and_return("server.example.com") end let(:response) { [200, [], ["OK"]] } context "a simple request" do - before :each do - expect(Time). - to receive(:now). - with(no_args). + before do + expect(Time).to receive(:now).with(no_args). and_return( Time.at(1234567890).utc, Time.at(1234567890.01).utc @@ -77,7 +71,7 @@ end it "logs the timestamp" do - expect(log_entry['timestamp']).to eq(Time.now.strftime("13/Feb/2009:23:31:30 +0000")) + expect(log_entry['timestamp']).to eq(Time.now.strftime("2009-02-13T23:31:30.000Z")) end it "logs the verb" do @@ -153,13 +147,13 @@ end end - context "with tags" do + context "with service" do let(:app) do url = logstash_url res = response Rack::Builder.new do - use Rack::Logstash, url, :tags => ["foo", "bar"] + use Rack::Logstash, url, service: 'simple_service' run proc { |env| res } end @@ -173,8 +167,8 @@ expect(log_entry).to_not be_empty end - it "sends the tags" do - expect(log_entry['tags']).to eq(["foo", "bar"]) + it "sends the service" do + expect(log_entry['service']).to eq('simple_service') end end @@ -212,7 +206,7 @@ end it "sends an exception class" do - expect(log_entry['exception']['class']).to eq("RuntimeError") + expect(log_entry['exception']['class']).to eq(RuntimeError) end it "sends an exception message" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a52ddb1..74bbccb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,10 +5,11 @@ Bundler.setup(:default, :development) require 'rspec/core' require 'rspec/mocks' + require 'byebug' RSpec.configure do |config| config.fail_fast = true -# config.full_backtrace = true + config.full_backtrace = true config.expect_with :rspec do |c| c.syntax = :expect