1# frozen_string_literal: true
2require 'test/unit'
3require 'cgi'
4require_relative 'update_env'
5
6
7class CGIModrubyTest < Test::Unit::TestCase
8  include UpdateEnv
9
10
11  def setup
12    @environ = {}
13    update_env(
14      'SERVER_PROTOCOL' => 'HTTP/1.1',
15      'REQUEST_METHOD'  => 'GET',
16      #'QUERY_STRING'    => 'a=foo&b=bar',
17    )
18    CGI.class_eval { const_set(:MOD_RUBY, true) }
19    Apache._reset()
20    #@cgi = CGI.new
21    #@req = Apache.request
22  end
23
24
25  def teardown
26    ENV.update(@environ)
27    CGI.class_eval { remove_const(:MOD_RUBY) }
28  end
29
30
31  def test_cgi_modruby_simple
32    req = Apache.request
33    cgi = CGI.new
34    assert(req._setup_cgi_env_invoked?)
35    assert(! req._send_http_header_invoked?)
36    actual = cgi.http_header
37    assert_equal('', actual)
38    assert_equal('text/html', req.content_type)
39    assert(req._send_http_header_invoked?)
40  end
41
42
43  def test_cgi_modruby_complex
44    req = Apache.request
45    cgi = CGI.new
46    options = {
47      'status'   => 'FORBIDDEN',
48      'location' => 'http://www.example.com/',
49      'type'     => 'image/gif',
50      'content-encoding' => 'deflate',
51      'cookie'   => [ CGI::Cookie.new('name1', 'abc', '123'),
52                      CGI::Cookie.new('name'=>'name2', 'value'=>'value2', 'secure'=>true),
53                    ],
54    }
55    assert(req._setup_cgi_env_invoked?)
56    assert(! req._send_http_header_invoked?)
57    actual = cgi.http_header(options)
58    assert_equal('', actual)
59    assert_equal('image/gif', req.content_type)
60    assert_equal('403 Forbidden', req.status_line)
61    assert_equal(403, req.status)
62    assert_equal('deflate', req.content_encoding)
63    assert_equal('http://www.example.com/', req.headers_out['location'])
64    assert_equal(["name1=abc&123; path=", "name2=value2; path=; secure"],
65                 req.headers_out['Set-Cookie'])
66    assert(req._send_http_header_invoked?)
67  end
68
69
70  def test_cgi_modruby_location
71    req = Apache.request
72    cgi = CGI.new
73    options = {
74      'status'   => '200 OK',
75      'location' => 'http://www.example.com/',
76    }
77    cgi.http_header(options)
78    assert_equal('200 OK', req.status_line)  # should be '302 Found' ?
79    assert_equal(302, req.status)
80    assert_equal('http://www.example.com/', req.headers_out['location'])
81  end
82
83
84  def test_cgi_modruby_requestparams
85    req = Apache.request
86    req.args = 'a=foo&b=bar'
87    cgi = CGI.new
88    assert_equal('foo', cgi['a'])
89    assert_equal('bar', cgi['b'])
90  end
91
92
93  instance_methods.each do |method|
94    private method if method =~ /^test_(.*)/ && $1 != ENV['TEST']
95  end if ENV['TEST']
96
97end
98
99
100
101## dummy class for mod_ruby
102class Apache  #:nodoc:
103
104  def self._reset
105    @request = Request.new
106  end
107
108  def self.request
109    return @request
110  end
111
112  class Request
113
114    def initialize
115      hash = {}
116      def hash.add(name, value)
117        (self[name] ||= []) << value
118      end
119      @http_header = nil
120      @headers_out  = hash
121      @status_line  = nil
122      @status       = nil
123      @content_type = nil
124      @content_encoding = nil
125    end
126    attr_accessor :headers_out, :status_line, :status, :content_type, :content_encoding
127
128    attr_accessor :args
129    #def args
130    #  return ENV['QUERY_STRING']
131    #end
132
133    def send_http_header
134      @http_header = '*invoked*'
135    end
136    def _send_http_header_invoked?
137      @http_header ? true : false
138    end
139
140    def setup_cgi_env
141      @cgi_env = '*invoked*'
142    end
143    def _setup_cgi_env_invoked?
144      @cgi_env ? true : false
145    end
146
147  end
148
149end
150