From 2653c689a6ce44736330ce56415b678045205a72 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Mon, 5 Jul 2010 20:16:38 +0900 Subject: [PATCH 1/3] make all tests work on Ruby 1.9.1. Timeout::Error is subclass of RuntimeError not SignalException on Ruby 1.9.1. --- lib/openid/fetchers.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/openid/fetchers.rb b/lib/openid/fetchers.rb index 22c87ac3..d3dfa72d 100644 --- a/lib/openid/fetchers.rb +++ b/lib/openid/fetchers.rb @@ -205,6 +205,8 @@ def fetch(url, body=nil, headers=nil, redirect_limit=REDIRECT_LIMIT) conn.request_post(url.request_uri, body, headers) end } + rescue Timeout::Error => why + raise FetchingError, "Error fetching #{url}: #{why}" rescue RuntimeError => why raise why rescue OpenSSL::SSL::SSLError => why @@ -212,8 +214,6 @@ def fetch(url, body=nil, headers=nil, redirect_limit=REDIRECT_LIMIT) rescue FetchingError => why raise why rescue Exception => why - # Things we've caught here include a Timeout::Error, which descends - # from SignalException. raise FetchingError, "Error fetching #{url}: #{why}" end From 61398ec068e0d3035e383b45571222c284f812aa Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Mon, 5 Jul 2010 20:32:03 +0900 Subject: [PATCH 2/3] support encoding introduced since Ruby 1.9. --- lib/openid/fetchers.rb | 20 ++++++++++++++++++++ test/test_fetchers.rb | 29 ++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/openid/fetchers.rb b/lib/openid/fetchers.rb index d3dfa72d..a7dff2c6 100644 --- a/lib/openid/fetchers.rb +++ b/lib/openid/fetchers.rb @@ -205,6 +205,7 @@ def fetch(url, body=nil, headers=nil, redirect_limit=REDIRECT_LIMIT) conn.request_post(url.request_uri, body, headers) end } + setup_encoding(response) rescue Timeout::Error => why raise FetchingError, "Error fetching #{url}: #{why}" rescue RuntimeError => why @@ -234,5 +235,24 @@ def fetch(url, body=nil, headers=nil, redirect_limit=REDIRECT_LIMIT) return HTTPResponse._from_net_response(response, unparsed_url) end end + + private + def setup_encoding(response) + return unless defined?(::Encoding::ASCII_8BIT) + charset = response.type_params["charset"] + return if charset.nil? + encoding = nil + begin + encoding = Encoding.find(charset) + rescue ArgumentError + end + encoding ||= Encoding::ASCII_8BIT + body = response.body + if body.respond_to?(:force_encoding) + body.force_encoding(encoding) + else + body.set_encoding(encoding) + end + end end end diff --git a/test/test_fetchers.rb b/test/test_fetchers.rb index 7130da55..6b033314 100644 --- a/test/test_fetchers.rb +++ b/test/test_fetchers.rb @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + require 'test/unit' require 'net/http' require 'webrick' @@ -112,7 +114,22 @@ def _redirect_loop assert_block("Fetched too many times.") { @_redirect_counter < 10 } } end - + + UTF8_PAGE_CONTENT = <<-EOHTML + + UTF-8 + こんにちは + +EOHTML + def _utf8_page + lambda { |req, resp| + resp['Content-Type'] = "text/html; charset=utf-8" + body = UTF8_PAGE_CONTENT.dup + body.force_encoding("ASCII-8BIT") if body.respond_to?(:force_encoding) + resp.body = body + } + end + def setup @fetcher = OpenID::StandardFetcher.new @logfile = StringIO.new @@ -138,6 +155,7 @@ def setup } @server.mount_proc('/post', _require_post) @server.mount_proc('/redirect_loop', _redirect_loop) + @server.mount_proc('/utf8_page', _utf8_page) @server.start } @uri = _uri_build @@ -211,6 +229,15 @@ def test_redirect_limit } end + def test_utf8_page + uri = _uri_build('/utf8_page') + response = @fetcher.fetch(uri) + assert_equal(UTF8_PAGE_CONTENT, response.body) + if response.body.respond_to?(:encoding) + assert_equal(Encoding::UTF_8, response.body.encoding) + end + end + def test_cases for path, expected_code, expected_url in @@cases uri = _uri_build(path) From b7d4fd7baa8fe04a0d6ac02244618b2db1bf8361 Mon Sep 17 00:00:00 2001 From: Kouhei Sutou Date: Mon, 5 Jul 2010 20:42:52 +0900 Subject: [PATCH 3/3] add a test for discovering from UTF-8 HTML. --- test/data/test_discover/openid_utf8.html | 11 +++++++++++ test/test_discover.rb | 14 ++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 test/data/test_discover/openid_utf8.html diff --git a/test/data/test_discover/openid_utf8.html b/test/data/test_discover/openid_utf8.html new file mode 100644 index 00000000..c98001af --- /dev/null +++ b/test/data/test_discover/openid_utf8.html @@ -0,0 +1,11 @@ + + + + Identity Page for Smoker + + + + +

こんにちは

+ + diff --git a/test/test_discover.rb b/test/test_discover.rb index 13b115ce..71511c3e 100644 --- a/test/test_discover.rb +++ b/test/test_discover.rb @@ -369,6 +369,20 @@ def test_html1And2 } end + def test_html_utf8 + utf8_html = read_data_file('test_discover/openid_utf8.html', false) + utf8_html.force_encoding("UTF-8") if utf8_html.respond_to?(:force_encoding) + services = _discover('text/html', utf8_html, 1) + + _checkService(services[0], + "http://www.myopenid.com/server", + @id_url, + 'http://smoker.myopenid.com/', + nil, + ['1.1'], + false) + end + def test_yadisEmpty services = _discover('application/xrds+xml', read_data_file('test_discover/yadis_0entries.xml', false),