From a9c493cddeff172891679d40f86d27d33691d011 Mon Sep 17 00:00:00 2001 From: ocha Date: Sun, 15 Mar 2015 11:02:20 +0900 Subject: [PATCH] =?UTF-8?q?UrlFetchTitle:=20=E3=82=BF=E3=82=A4=E3=83=A0?= =?UTF-8?q?=E3=82=A2=E3=82=A6=E3=83=88=E3=82=A8=E3=83=A9=E3=83=BC=E8=A1=A8?= =?UTF-8?q?=E7=A4=BA=E3=82=92=E5=AE=9F=E8=A3=85=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs #5 --- lib/rgrb/plugin/url_fetch_title/generator.rb | 3 +++ .../rgrb/plugin/url_fetch_title/generator_spec.rb | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/lib/rgrb/plugin/url_fetch_title/generator.rb b/lib/rgrb/plugin/url_fetch_title/generator.rb index d419439b..261f8646 100644 --- a/lib/rgrb/plugin/url_fetch_title/generator.rb +++ b/lib/rgrb/plugin/url_fetch_title/generator.rb @@ -2,6 +2,7 @@ require 'uri' require 'socket' +require 'net/http' require 'mechanize' require 'active_support' require 'active_support/core_ext/numeric/conversions' @@ -117,6 +118,8 @@ def fetch_title(url) end rescue SocketError body = '(サーバーに接続できませんでした)' + rescue Net::HTTP::Persistent::Error + body = '(タイムアウト)' rescue Mechanize::ResponseCodeError => response_code_error response_code = response_code_error.response_code.to_i reason_phrase = diff --git a/spec/rgrb/plugin/url_fetch_title/generator_spec.rb b/spec/rgrb/plugin/url_fetch_title/generator_spec.rb index 1e809acf..d35c09e1 100644 --- a/spec/rgrb/plugin/url_fetch_title/generator_spec.rb +++ b/spec/rgrb/plugin/url_fetch_title/generator_spec.rb @@ -111,6 +111,21 @@ end end + context 'タイムアウトした場合' do + let(:url) { 'http://example.net/very_very_heavy' } + + before do + stub_request(:get, url).to_timeout + end + + subject { generator.fetch_title(url) } + it 'タイムアウトしたことを示すエラーメッセージを返す' do + expect(subject).to eq( + "#{default_prefix}(タイムアウト)#{default_suffix}" + ) + end + end + context '401 Unauthorized' do let(:url) { 'http://example.net/need_login' }