root/lang/ruby/ssb/trunk/test/request_test.rb @ 2822

Revision 2822, 4.7 kB (checked in by ursm, 5 years ago)

lang/ruby/ssb: add test to [2795] (duplicate parameter keys)

  • Property svn:mime-type set to text/x-ruby; charset=utf-8
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Date Author Rev URL
Line 
1require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2require 'ssb/request'
3require 'webrick/cookie'
4require 'webrick/httputils'
5
6$SAFE = 1
7
8unit_tests do
9  def request_params
10    {
11      'ssb_q'.taint  => MockServer.uri.dup.taint,
12      'p1'.taint => WEBrick::HTTPUtils::FormData.new('foo'.taint),
13      'p2'.taint => WEBrick::HTTPUtils::FormData.new('bar'.taint),
14      'p3'.taint => WEBrick::HTTPUtils::FormData.new('bazz&hoge'.taint)
15    }
16  end
17
18  def cookie
19    ret = []
20    {
21      'homepage'.taint  => 'http://example.com/'.taint,
22      'mailaddr'.taint  => 'coji.mizo@example.com'.taint,
23      'useragent'.taint => 'DoCoMo/2.0 N902i(c100;TB;hid;icc)'.taint,
24      'uid'.taint       => 'NULLGWDOCOMO'.taint,
25      'hid'.taint       => 'hidhidhid'.taint,
26      'icc'.taint       => 'icciccicc'.taint,
27      'exheader'.taint  => 'X-Hoge: hoge'.taint,
28    }.each {|key, val|
29      ret.push WEBrick::Cookie.new(key, val)
30    }
31    ret
32  end
33
34  def setup
35    @http_proxy, @HTTP_PROXY = ENV['HTTP_PROXY'], ENV['http_proxy']
36    ENV['HTTP_PROXY'], ENV['http_proxy'] = nil, nil
37    @request = SSB::Request.new('GET', request_params, cookie)
38  end
39
40  def teardown
41    ENV['HTTP_PROXY'], ENV['http_proxy'] = @http_proxy, @HTTP_PROXY
42  end
43
44  test 'instance' do
45    assert_not_nil(@request)
46  end
47
48  test 'request_method' do
49    assert_instance_of(String, @request.method)
50    assert_equal(@request.method, 'GET')
51    assert(!@request.method.tainted?)
52  end
53
54  test 'request_uri_should_uri' do
55    assert_instance_of(URI::HTTP, @request.uri)
56  end
57
58  test 'request_uri_should_start_with_http' do
59    assert(@request.uri.to_s =~ /^http:\/\//)
60  end
61
62  test 'request_uri_should_encoded' do
63    assert_equal(@request.uri.to_s, "#{MockServer.uri}?p1=foo&p2=bar&p3=bazz%26hoge")
64  end
65
66  test 'request_uri_should_not_tainted' do
67    assert(!@request.uri.to_s.tainted?)
68  end
69
70  test 'request_post_params' do
71    assert_equal(@request.post_params, nil)
72  end
73
74  test 'request_term_should_not_nil' do
75    assert_not_nil(@request.term)
76  end
77
78  test 'request_term_keys_should_not_tainted_and_valid_value' do
79    assert_not_nil(@request.term)
80    test_keys = ['homepage', 'mailaddr', 'useragent', 'uid', 'hid', 'icc']
81    test_keys.each do |key|
82      assert_equal(@request.term[key.to_sym].to_s, cookie.find {|x| x.name == key }.value )
83      assert(!@request.term[key].tainted?)
84    end
85  end
86
87  test 'request_header_should_exist' do
88    assert_not_nil(@request.request_header)
89  end
90
91  test 'request_header_should_have_useragent' do
92    assert(@request.request_header.has_key?('User-Agent'))
93  end
94
95  test 'request_header_shuold_vaild_useragent_with_hid' do
96    assert_equal(@request.request_header['User-Agent'], 'DoCoMo/2.0 N902i(c100;TB;hidhidhid;icciccicc)')
97  end
98
99  test 'request_header_should_have_exheader' do
100    assert(@request.request_header.has_key?('X-Hoge'))
101  end
102
103  test 'test_request_header_shuold_valid_exheader' do
104    assert_equal(@request.request_header['X-Hoge'], 'hoge')
105  end
106
107  test 'test_request_should_success' do
108    mock_server = MockServer.new
109    assert(@request.execute)
110    mock_server.shutdown
111  end
112
113  test 'test_request_get' do
114    mock_server = MockServer.new
115    request = SSB::Request.new('GET'.taint, request_params, cookie)
116    response = request.execute
117    assert_instance_of(Net::HTTPOK, response)
118    assert(response.body =~ Regexp.new('It works by GET'))
119    mock_server.shutdown
120  end
121
122  test 'test_request_post' do
123    mock_server = MockServer.new
124    request = SSB::Request.new('POST'.taint, request_params, cookie)
125    response = request.execute
126    assert_instance_of(Net::HTTPOK, response)
127    assert(response.body =~ Regexp.new('It works by POST'))
128    mock_server.shutdown
129  end
130
131  test 'no proxy' do
132    http_class = @request.http_class
133    connection = http_class.new("http://www.google.com")
134    assert(!connection.proxy?)
135  end
136
137  test 'regular http proxy' do
138    http_class = @request.http_class("http://my.proxy:1234")
139    connection = http_class.new("http://www.google.com")
140    assert(connection.proxy?)
141    assert_equal(connection.proxy_port, 1234)
142    assert_equal(connection.proxy_address, "my.proxy")
143  end
144
145  test 'http proxy with authorization' do
146    http_class = @request.http_class("http://benjamin:secret999@my.proxy:1234")
147    connection = http_class.new("http://www.google.com")
148    assert(connection.proxy?)
149    assert_equal(connection.proxy_user, "benjamin")
150    assert_equal(connection.proxy_pass, "secret999")
151  end
152
153  test 'duplicate parameter keys' do
154    params = {
155      'ssb_q'.taint  => MockServer.uri.dup.taint,
156      'p'.taint => WEBrick::HTTPUtils::FormData.new('foo'.taint, 'bar'.taint)
157    }
158
159    request = SSB::Request.new('GET'.taint, params, cookie)
160    assert_equal("#{MockServer.uri}?p=foo&p=bar", request.uri.to_s)
161  end
162end
Note: See TracBrowser for help on using the browser.