File: test_mechanize_form_encoding.rb

package info (click to toggle)
ruby-mechanize 2.7.6-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,480 kB
  • sloc: ruby: 11,380; makefile: 5; sh: 4
file content (109 lines) | stat: -rw-r--r-- 3,246 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# coding: utf-8
require 'mechanize/test_case'

class TestMechanizeFormEncoding < Mechanize::TestCase

  # See also: tests of Util.from_native_charset
  # Encoding test should do with non-utf-8 characters

  INPUTTED_VALUE = "ใƒ†ใ‚นใƒˆ" # "test" in Japanese UTF-8 encoding
  CONTENT_ENCODING = 'Shift_JIS' # one of Japanese encoding
  encoded_value = "\x83\x65\x83\x58\x83\x67".force_encoding(::Encoding::SHIFT_JIS) # "test" in Japanese Shift_JIS encoding
  EXPECTED_QUERY = "first_name=#{CGI.escape(encoded_value)}&first_name=&gender=&green%5Beggs%5D="

  ENCODING_ERRORS = [EncodingError, Encoding::ConverterNotFoundError] # and so on

  ENCODING_LOG_MESSAGE = /INFO -- : form encoding: Shift_JIS/
  INVALID_ENCODING = 'UTF-eight'

  def set_form_with_encoding(enc)
    page = @mech.get("http://localhost/form_set_fields.html")
    form = page.forms.first
    form.encoding = enc
    form['first_name'] = INPUTTED_VALUE
    form
  end

  def test_form_encoding_returns_accept_charset
    page = @mech.get("http://localhost/rails_3_encoding_hack_form_test.html")
    form = page.forms.first
    accept_charset = form.form_node['accept-charset']

    assert accept_charset
    assert_equal accept_charset, form.encoding
    refute_equal page.encoding, form.encoding
  end

  def test_form_encoding_returns_page_encoding_when_no_accept_charset
    page = @mech.get("http://localhost/form_set_fields.html")
    form = page.forms.first
    accept_charset = form.form_node['accept-charset']

    assert_nil accept_charset
    refute_equal accept_charset, form.encoding
    assert_equal page.encoding, form.encoding
  end

  def test_form_encoding_equals_sets_new_encoding
    page = @mech.get("http://localhost/form_set_fields.html")
    form = page.forms.first

    refute_equal CONTENT_ENCODING, form.encoding

    form.encoding = CONTENT_ENCODING

    assert_equal CONTENT_ENCODING, form.encoding
  end

  def test_form_encoding_returns_nil_when_no_page_in_initialize
    # this sequence is seen at Mechanize#post(url, query_hash)

    node = {}
    # Create a fake form
    class << node
      def search(*args); []; end
    end
    node['method'] = 'POST'
    node['enctype'] = 'application/x-www-form-urlencoded'
    form = Mechanize::Form.new(node)

    assert_nil form.encoding
  end

  def test_post_form_with_form_encoding
    form = set_form_with_encoding CONTENT_ENCODING
    form.submit

    # we can not use "links.find{|l| l.text == 'key:val'}" assertion here
    # because the link text encoding is always UTF-8 regaredless of html encoding
    assert EXPECTED_QUERY, @mech.page.at('div#query').inner_text
  end

  def test_post_form_with_problematic_encoding
    form = set_form_with_encoding INVALID_ENCODING

    assert_raises(*ENCODING_ERRORS){ form.submit }
  end

  def test_form_ignore_encoding_error_is_true
    form = set_form_with_encoding INVALID_ENCODING
    form.ignore_encoding_error = true

    form.submit

    # HACK no assertions
  end

  def test_post_form_logs_form_encoding
    sio = StringIO.new
    @mech.log = Logger.new(sio)
    @mech.log.level = Logger::INFO

    form = set_form_with_encoding CONTENT_ENCODING
    form.submit

    assert_match ENCODING_LOG_MESSAGE, sio.string

    @mech.log = nil
  end
end