Merge remote branch 'e2/fix_ruby_encoding_warnings'

This commit is contained in:
Jonas Nicklas 2010-08-20 14:38:10 +02:00
commit 0d6ccfcb64
2 changed files with 58 additions and 5 deletions

View File

@ -91,7 +91,7 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
native.xpath(locator).map { |n| self.class.new(driver, n) }
end
private
private
# a reference to the select node if this is an option node
def select_node
@ -155,7 +155,7 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
self[:enctype] == "multipart/form-data"
end
private
private
def method
self[:method] =~ /post/i ? :post : :get
@ -192,7 +192,7 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
def process(method, path, attributes = {})
return if path.gsub(/^#{request_path}/, '') =~ /^#/
send(method, path, attributes, env)
send(method, to_binary(path), to_binary( attributes ), env)
follow_redirects!
end
@ -208,9 +208,23 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
response.status
end
def to_binary(object)
return object unless Kernel.const_defined?(:Encoding)
if object.respond_to?(:force_encoding)
object.dup.force_encoding(Encoding::ASCII_8BIT)
elsif object.respond_to?(:each_pair) #Hash
{}.tap { |x| object.each_pair {|k,v| x[to_binary(k)] = to_binary(v) } }
elsif object.respond_to?(:each) #Array
object.map{|x| to_binary(x)}
else
object
end
end
def submit(method, path, attributes)
path = request_path if not path or path.empty?
send(method, path, attributes, env)
send(method, to_binary(path), to_binary(attributes), env)
follow_redirects!
end
@ -243,7 +257,7 @@ class Capybara::Driver::RackTest < Capybara::Driver::Base
raise Capybara::InfiniteRedirectError, "redirected more than 5 times, check for infinite redirects." if response.redirect?
end
private
private
def reset_cache
@body = nil

View File

@ -1,5 +1,21 @@
# encoding: utf-8
require File.expand_path('../spec_helper', File.dirname(__FILE__))
require 'stringio'
def capture(*streams)
streams.map! { |stream| stream.to_s }
begin
result = StringIO.new
streams.each { |stream| eval "$#{stream} = result" }
yield
ensure
streams.each { |stream| eval("$#{stream} = #{stream.upcase}") }
end
result.string
end
describe Capybara::Driver::RackTest do
before do
@driver = Capybara::Driver::RackTest.new(TestApp)
@ -11,6 +27,29 @@ describe Capybara::Driver::RackTest do
end.should raise_error(ArgumentError)
end
if '1.9'.respond_to?(:encode)
describe "with non-binary parameters" do
it "should convert attribute values to binary" do
output = capture(:stderr) {
@driver.visit('/mypage', :param => 'µ')
}.should_not =~ %r{warning: regexp match /.../n against to UTF-8 string}
end
it "should convert attribute with Array to binary" do
output = capture(:stderr) {
@driver.visit('/mypage', :param => ['µ'])
}.should_not =~ %r{warning: regexp match /.../n against to UTF-8 string}
end
it "should convert path to binary" do
output = capture(:stderr) {
@driver.visit('/mypage'.encode('utf-8'))
}.should_not =~ %r{warning: regexp match /.../n against to UTF-8 string}
end
end
end
it_should_behave_like "driver"
it_should_behave_like "driver with header support"
it_should_behave_like "driver with status code support"