require "spec_helper" describe ExceptionNotifier::IkachanNotifier do let(:notifier) { ExceptionNotifier::IkachanNotifier.new(options) } describe "initial options" do let(:options) do { base_url: 'ikachan.udzura.jp', channel: '#udzura', message_format: '%{class}: %{message}' } end describe ":base_url" do it "should set client's base_url" do expect(notifier.client.base_url).to eq("http://ikachan.udzura.jp/") end end describe "message with invalid request key" do let(:options) do { base_url: 'ikachan.udzura.jp', channel: '#udzura', message_format: '%{request_noattr}' } end it "should just raise error" do expect { notifier }.to raise_error(RuntimeError, "Parameter name request_noattr is unavailable") end end end describe "#build_message and #message" do before do require 'rack/request' stub_request(:post, "http://ikachan.udzura.jp/join") stub_request(:post, "http://ikachan.udzura.jp/notice") end let(:exception) { StandardError.new("Hello, exception!")} let(:options) do { base_url: 'ikachan.udzura.jp', channel: '#udzura', }.merge(extra_options) end describe "message with request info" do let(:extra_options) do {message_format: '%{request_path_info}'} end it "should include request's path info" do notifier.build_message(exception, {env: {'PATH_INFO' => '/foo/bar'}}) expect(notifier.message).to eq("/foo/bar") end it "should not raise error without env" do expect { notifier.build_message(exception) }.not_to raise_error expect(notifier.message).to eq("") end end describe "message with more request info" do let(:env_sane) do {"HTTP_HOST" => "example.udzura.jp:80", "rack.url_scheme" => 'http', "PATH_INFO" => "/hello.html"} end let(:extra_options) do {message_format: '%{request_url} / ssl? = %{request_ssl?}'} end it do notifier.build_message(exception, {env: env_sane}) expect(notifier.message).to eq("http://example.udzura.jp/hello.html / ssl? = false") end end describe "message modifiers" do before { notifier.build_message(exception, {env: {'PATH_INFO' => '/foo/bar'}}) } let(:default_message) do ExceptionNotifier::IkachanNotifier::DEFAULT_FORMAT % { class: "StandardError", message: "Hello, exception!", occurred: '' } end describe ':message_prefix' do let(:extra_options) do { message_prefix: 'hello - ' } end it do expect(notifier.message).to eq('hello - ' + default_message) end end describe ':message_suffix' do let(:extra_options) do { message_suffix: ' - world' } end it do expect(notifier.message).to eq(default_message + ' - world') end end describe ':message_nocolor' do let(:extra_options) do { message_nocolor: true } end it do expect(notifier.message).to eq("[ERROR] StandardError - Hello, exception!, ") end end end end describe "#call" do let(:options) do { base_url: 'ikachan.udzura.jp', channel: '#udzura', message_format: message_format } end let(:exception) { StandardError.new("Hello, exception!")} describe 'single line' do let(:message_format){ '%{class}: %{message}' } it "should notice message to ikachan" do stub_join = stub_request(:post, "http://ikachan.udzura.jp/join"). with(body: {"channel" => "#udzura"}) stub_notice = stub_request(:post, "http://ikachan.udzura.jp/notice"). with(body: {"channel" => "#udzura", "message" => "StandardError: Hello, exception!"}) notifier.call(exception, {}) stub_join.should have_been_requested.once stub_notice.should have_been_requested.once end end describe 'multiple line' do let(:message_format){ <<-EOFormat } %{class} %{message} extra info EOFormat it "should notice message to ikachan" do stub_join = stub_request(:post, "http://ikachan.udzura.jp/join"). with(body: {"channel" => "#udzura"}) stub_notice = stub_request(:post, "http://ikachan.udzura.jp/notice"). with(body: {"channel" => "#udzura", "message" => an_instance_of(String)}) notifier.call(exception, {}) stub_join.should have_been_requested.once stub_notice.should have_been_requested.times(3) end end end end