Project

General

Profile

Feature #509 » test_cgi_modruby.rb

xibbar (Takeyuki FUJIOKA), 08/27/2008 10:56 PM

 
1
require 'test/unit'
2
require 'cgi'
3

    
4

    
5
class CGIModrubyTest < Test::Unit::TestCase
6

    
7

    
8
  def setup
9
    @environ = {
10
      'SERVER_PROTOCOL' => 'HTTP/1.1',
11
      'REQUEST_METHOD'  => 'GET',
12
      #'QUERY_STRING'    => 'a=foo&b=bar',
13
    }
14
    ENV.update(@environ)
15
    CGI.class_eval { const_set(:MOD_RUBY, true) }
16
    Apache._reset()
17
    #@cgi = CGI.new
18
    #@req = Apache.request
19
  end
20

    
21

    
22
  def teardown
23
    @environ.each do |key, val| ENV.delete(key) end
24
    CGI.class_eval { remove_const(:MOD_RUBY) }
25
  end
26

    
27

    
28
  def test_cgi_modruby_simple
29
    req = Apache.request
30
    cgi = CGI.new
31
    assert(req._setup_cgi_env_invoked?)
32
    assert(! req._send_http_header_invoked?)
33
    actual = cgi.header
34
    assert_equal('', actual)
35
    assert_equal('text/html', req.content_type)
36
    assert(req._send_http_header_invoked?)
37
  end
38

    
39

    
40
  def test_cgi_modruby_complex
41
    req = Apache.request
42
    cgi = CGI.new
43
    options = {
44
      'status'   => 'FORBIDDEN',
45
      'location' => 'http://www.example.com/',
46
      'type'     => 'image/gif',
47
      'content-encoding' => 'deflate',
48
      'cookie'   => [ CGI::Cookie.new('name1', 'abc', '123'),
49
                      CGI::Cookie.new('name'=>'name2', 'value'=>'value2', 'secure'=>true),
50
                    ],
51
    }
52
    assert(req._setup_cgi_env_invoked?)
53
    assert(! req._send_http_header_invoked?)
54
    actual = cgi.header(options)
55
    assert_equal('', actual)
56
    assert_equal('image/gif', req.content_type)
57
    assert_equal('403 Forbidden', req.status_line)
58
    assert_equal(403, req.status)
59
    assert_equal('deflate', req.content_encoding)
60
    assert_equal('http://www.example.com/', req.headers_out['location'])
61
    assert_equal(["name1=abc&123; path=", "name2=value2; path=; secure"],
62
                 req.headers_out['Set-Cookie'])
63
    assert(req._send_http_header_invoked?)
64
  end
65

    
66

    
67
  def test_cgi_modruby_location
68
    req = Apache.request
69
    cgi = CGI.new
70
    options = {
71
      'status'   => '200 OK',
72
      'location' => 'http://www.example.com/',
73
    }
74
    actual = cgi.header(options)
75
    assert_equal('200 OK', req.status_line)  # should be '302 Found' ?
76
    assert_equal(302, req.status)
77
    assert_equal('http://www.example.com/', req.headers_out['location'])
78
  end
79

    
80

    
81
  def test_cgi_modruby_requestparams
82
    req = Apache.request
83
    req.args = 'a=foo&b=bar'
84
    cgi = CGI.new
85
    assert_equal('foo', cgi['a'])
86
    assert_equal('bar', cgi['b'])
87
  end
88

    
89

    
90
  instance_methods.each do |method|
91
    private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']
92
  end if ENV['TEST']
93

    
94
end
95

    
96

    
97

    
98
## dummy class for mod_ruby
99
class Apache  #:nodoc:
100

    
101
  def self._reset
102
    @request = Request.new
103
  end
104

    
105
  def self.request
106
    return @request
107
  end
108

    
109
  class Request
110

    
111
    def initialize
112
      hash = {}
113
      def hash.add(name, value)
114
        (self[name] ||= []) << value
115
      end
116
      @headers_out  = hash
117
      @status_line  = nil
118
      @status       = nil
119
      @content_type = nil
120
      @content_encoding = nil
121
    end
122
    attr_accessor :headers_out, :status_line, :status, :content_type, :content_encoding
123

    
124
    attr_accessor :args
125
    #def args
126
    #  return ENV['QUERY_STRING']
127
    #end
128

    
129
    def send_http_header
130
      @http_header = '*invoked*'
131
    end
132
    def _send_http_header_invoked?
133
      @http_header ? true : false
134
    end
135

    
136
    def setup_cgi_env
137
      @cgi_env = '*invoked*'
138
    end
139
    def _setup_cgi_env_invoked?
140
      @cgi_env ? true : false
141
    end
142

    
143
  end
144

    
145
end