ruby-ajp 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,177 @@
1
+ require 'test/unit'
2
+ require 'stringio'
3
+ require File.dirname(__FILE__) + '/../../lib/net/ajp13'
4
+
5
+ class Net::AJP13::RequestTest < Test::Unit::TestCase
6
+ def setup
7
+ @req = Net::AJP13::Request.new('/')
8
+ end
9
+ def test_new
10
+ assert_kind_of Net::AJP13::Request, @req
11
+ assert_equal '/', @req.path
12
+
13
+ req_with_another_path = Net::AJP13::Request.new('/path/to/somewhere')
14
+ assert_equal '/path/to/somewhere', req_with_another_path.path
15
+ end
16
+
17
+ def test_header
18
+ assert_nil @req['accept-language']
19
+ assert_nil @req['content-type']
20
+ assert_nil @req['content-length']
21
+
22
+ @req['accept-laguage'] = 'ja,en_US,en'
23
+ assert_equal 'ja,en_US,en', @req['accept-laguage']
24
+ @req['AcCePt-lAgUaGe'] = 'fr,de,es'
25
+ assert_equal 'fr,de,es', @req['accept-laguage']
26
+ end
27
+
28
+ def test_attribute
29
+ assert_nil @req.get_attributes('query_string')
30
+ assert_nil @req.get_attributes('something-unrecognized')
31
+
32
+ @req.set_attribute('QUERY_STRING', "a=1&b=c")
33
+ assert_equal ["a=1&b=c"], @req.get_attributes('qUeRy_StRiNg')
34
+
35
+ @req.add_attribute('something-unrecognized', 'abcde')
36
+ @req.add_attribute('something-unrecognized', 'fghij')
37
+ assert_equal ['abcde', 'fghij'], @req.get_attributes('something-unrecognized')
38
+
39
+ @req.set_attribute('something-unrecognized', 'something else')
40
+ assert_equal ['something else'], @req.get_attributes('something-unrecognized')
41
+ end
42
+
43
+ def test_new
44
+ get = Net::AJP13::GetRequest.new('/path/to/somewhere',
45
+ 'Accept' => 'text/xml')
46
+ assert_equal 'GET', get.method
47
+ assert_equal '/path/to/somewhere', get.path
48
+ assert !get.request_body_permitted?
49
+ assert get.response_body_permitted?
50
+ assert_equal 'text/xml', get['accept']
51
+ assert_same false, get.is_ssl?
52
+
53
+ post = Net::AJP13::PostRequest.new('/path/to/somewhere/else',
54
+ 'Accept' => 'application/x-dvi')
55
+ assert_equal 'POST', post.method
56
+ assert_equal '/path/to/somewhere/else', post.path
57
+ assert post.request_body_permitted?
58
+ assert post.response_body_permitted?
59
+ assert_equal 'application/x-dvi', post['accept']
60
+
61
+ head = Net::AJP13::HeadRequest.new('/path/to/yet/another/place',
62
+ 'Accept' => 'application/x-pdf')
63
+ assert_equal 'HEAD', head.method
64
+ assert_equal '/path/to/yet/another/place', head.path
65
+ assert !head.request_body_permitted?
66
+ assert !head.response_body_permitted?
67
+ assert_equal 'application/x-pdf', head['accept']
68
+ end
69
+
70
+ def test_send_to
71
+ req = Net::AJP13::HeadRequest.new('/?q=something&a=1',
72
+ 'Host' => 'localhost')
73
+ req.protocol = 'HTTP/1.1'
74
+ req.remote_addr = '127.0.0.1'
75
+ req.remote_host = 'localhost'
76
+ req.server_name = 'localhost'
77
+ req.server_port = 80
78
+ io = StringIO.new
79
+ req.send_to(io)
80
+ io.rewind
81
+ assert_equal \
82
+ "\x12\x34\x00\x5c" + # payload
83
+ "\x02\x03" + # FORWARD_REQUEST HEAD
84
+ "\x00\x08HTTP/1.1\x00" + # protocol = HTTP/1.1
85
+ "\x00\x01/\x00" + # request_path = /
86
+ "\x00\x09127.0.0.1\x00" + # remote_addr = 127.0.0.1
87
+ "\x00\x09localhost\x00" + # remote_host = localhost
88
+ "\x00\x09localhost\x00" + # server_name = localhost
89
+ "\x00\x50" + # server_port = 80
90
+ "\x00" + # is_ssl = false
91
+ "\x00\x01" + # num_headers = 1
92
+ "\xA0\x0B" + # Host:
93
+ "\x00\x09localhost\0" + # localhost
94
+ "\x05" + # QUERY_STRING =
95
+ "\x00\x0Fq=something&a=1\x00"+# "q=something&a=1"
96
+ "\xFF" + # TERMINATOR
97
+ "",
98
+ io.read
99
+ end
100
+
101
+ def test_from_io
102
+ packet =
103
+ "\x12\x34\x00\x5c" + # payload
104
+ "\x02\x03" + # FORWARD_REQUEST HEAD
105
+ "\x00\x08HTTP/1.1\x00" + # protocol = HTTP/1.1
106
+ "\x00\x01/\x00" + # request_path = /
107
+ "\x00\x09127.0.0.1\x00" + # remote_addr = 127.0.0.1
108
+ "\x00\x09localhost\x00" + # remote_host = localhost
109
+ "\x00\x09localhost\x00" + # server_name = localhost
110
+ "\x00\x50" + # server_port = 80
111
+ "\x00" + # is_ssl = false
112
+ "\x00\x01" + # num_headers = 1
113
+ "\xA0\x0B" + # Host:
114
+ "\x00\x09localhost\x00" + # localhost
115
+ "\x05" + # QUERY_STRING =
116
+ "\x00\x0Fq=something&a=1\x00"+# "q=something&a=1"
117
+ "\xFF" + # TERMINATOR
118
+ ""
119
+ io = StringIO.new(packet)
120
+ assert !io.eof?
121
+ req = Net::AJP13::Request.from_io(io)
122
+ assert io.eof?
123
+ assert_equal "HEAD", req.method
124
+ assert_equal "/", req.path
125
+ assert_equal "127.0.0.1", req.remote_addr
126
+ assert_equal "localhost", req.remote_host
127
+ assert_equal "localhost", req.server_name
128
+ assert_equal 80, req.server_port
129
+ assert_same false, req.is_ssl?
130
+ assert_equal 1, req.length
131
+ assert_equal "localhost", req['host']
132
+
133
+ File.open(File.dirname(__FILE__) + '/data/ajp13request-data.1') {|f|
134
+ assert !f.eof?
135
+ req = Net::AJP13::Request.from_io(f)
136
+ assert f.eof?
137
+ assert_equal "GET", req.method
138
+ assert_equal "HTTP/1.1", req.protocol
139
+ assert_equal "/admin/", req.path
140
+ assert_equal "127.0.0.1", req.remote_addr
141
+ assert_nil req.remote_host
142
+ assert_equal "localhost", req.server_name
143
+ assert_equal 80, req.server_port
144
+ assert_same false, req.is_ssl?
145
+ assert_equal 11, req.length
146
+ req.each do |name, value|
147
+ case name.downcase
148
+ when 'user-agent'
149
+ assert_equal "Opera/8.51 (X11; Linux i686; U; ja)", value
150
+ when 'host'
151
+ assert_equal "localhost", value
152
+ when 'accept'
153
+ assert_equal "text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1", value
154
+ when 'accept-language'
155
+ assert_equal "ja,en;q=0.9", value
156
+ when 'accept-charset'
157
+ assert_equal "shift_jis, utf-8, utf-16, iso-8859-1;q=0.6, *;q=0.1", value
158
+ when 'accept-encoding'
159
+ assert_equal "deflate, gzip, x-gzip, identity, *;q=0", value
160
+ when 'cookie'
161
+ assert_block {
162
+ "JSESSIONID=54104A3A77560CEF8967D263F1A7193" == value or
163
+ "$Version=1" == value
164
+ }
165
+ when 'cache-control'
166
+ assert_equal "no-cache", value
167
+ when 'connection'
168
+ assert_equal "Keep-Alive, TE", value
169
+ when 'TE'
170
+ assert_equal "deflate, gzip, chunked, identity, trailers", value
171
+ when 'content-length'
172
+ assert_equal "0", value
173
+ end
174
+ end
175
+ }
176
+ end
177
+ end
@@ -0,0 +1,162 @@
1
+ require 'test/unit'
2
+ require 'stringio'
3
+ require File.dirname(__FILE__) + '/../../lib/net/ajp13'
4
+
5
+ class Net::AJP13::ResponseTest < Test::Unit::TestCase
6
+ def setup
7
+ @res = Net::AJP13::Response.new(301)
8
+ end
9
+
10
+ def test_new
11
+ assert_equal 301, @res.status
12
+ assert_equal "301", @res.code
13
+ assert_equal 'Moved Permanently', @res.message
14
+ assert_equal 'Moved Permanently', @res.reason_phrase
15
+ assert_same @res.message, @res.reason_phrase
16
+ assert_nil @res.body
17
+ assert_nil @res.body_stream
18
+ end
19
+
20
+ def test_from_io
21
+ packet =
22
+ "\x41\x42\x00\x2e" + # "AB" length
23
+ "\x04" + # SEND_HEADERS
24
+ "\x00\xC8\x00\x02OK\x00" + # 200 OK
25
+ "\x00\x02" + # num_headers = 2
26
+ "\xA0\x01" + # Content-Type:
27
+ "\x00\x18text/html;" +
28
+ " charset=UTF-8\x00" + # text/html; charset=UTF-8
29
+ "\xA0\x03" + # Content-Length:
30
+ "\x00\x0241\x00" + # 41
31
+ ""
32
+ io = StringIO.new(packet)
33
+ assert !io.eof?
34
+ res = Net::AJP13::Response.from_io(io)
35
+ assert io.eof?
36
+ assert_equal 200, res.status
37
+ assert_equal '200', res.code
38
+ assert_equal 'OK', res.message
39
+ assert !res.key?('Exprires')
40
+ assert_equal 'text/html; charset=UTF-8', res['content-type']
41
+ assert_equal '41', res['Content-Length']
42
+ assert_nil res.body
43
+ assert_nil res.body_stream
44
+
45
+ packet =
46
+ "\x41\x42\x00\x11" + # "AB" length
47
+ "\x04" + # SEND_HEADERS
48
+ "\x00\xC8\x00\x02OK\x00" + # 200 OK
49
+ "\x00\x01" + # num_headers = 1
50
+ "\xA0\x3c" + # UNRECOGNIZED HEADER CODE
51
+ "\x00\x0241\x00" + # 41
52
+ ""
53
+ assert_raise(Net::AJP13::AJPPacketError) {
54
+ res = Net::AJP13::Response.from_io(StringIO.new(packet))
55
+ }
56
+
57
+
58
+
59
+ # WebDAV response without body
60
+ File.open(File.dirname(__FILE__) + '/data/ajp13response-webdav.1') { |f|
61
+ assert !f.eof?
62
+ res = Net::AJP13::Response.from_io(f)
63
+ assert !f.eof? # because of END_RESPONSE packet
64
+
65
+ assert_equal 200, res.status
66
+ assert_equal "200", res.code
67
+ assert_equal "OK", res.reason_phrase
68
+ assert_equal "1,2", res['dav']
69
+ assert_equal "OPTIONS, GET, HEAD, POST, DELETE, TRACE, PROPPATCH, COPY, MOVE, LOCK, UNLOCK, PROPFIND", res['ALLOW']
70
+ assert_equal "DAV", res['MS-Author-Via']
71
+ assert_equal 0, res.content_length
72
+ assert_nil res['no-such-header']
73
+ assert_nil res.body
74
+ assert_nil res.body_stream
75
+ }
76
+
77
+
78
+ # WebDAV response with body
79
+ File.open(File.dirname(__FILE__) + '/data/ajp13response-webdav.2') { |f|
80
+ assert !f.eof?
81
+ res = Net::AJP13::Response.from_io(f)
82
+ assert !f.eof? # because of BODY_CHUNK packets and END_RESPONSE packet.
83
+
84
+ assert_equal 207, res.status
85
+ assert_equal 'Multi-Status', res.message
86
+ assert_equal 'text/xml;charset=UTF-8', res['content-type']
87
+ assert_equal '390', res['content-length']
88
+ assert_equal 390, res.content_length
89
+ assert_nil res['no-such-header']
90
+ assert_nil res.body
91
+ assert_nil res.body_stream
92
+ #assert_equal %Q(<?xml version="1.0" encoding="utf-8" ?>\n<multistatus xmlns="DAV:"><response><href>/webdav/</href>\n<propstat><prop><resourcetype><collection/></resourcetype>\n</prop>\n<status>HTTP/1.1 200 OK</status>\n</propstat>\n<propstat><prop><getcontentlength/><getlastmodified/><executable/><checked-in/><checked-out/></prop>\n<status>HTTP/1.1 404 Not Found</status>\n</propstat>\n</response>\n</multistatus>\n), res.body
93
+ }
94
+ end
95
+
96
+
97
+ def test_message
98
+ res = Net::AJP13::Response.new(500, :reason_phrase => 'Internal Servlet Container Error :P')
99
+ assert_equal "Internal Servlet Container Error :P", res.message
100
+ assert_equal 500, res.status
101
+ assert_equal "500", res.code
102
+ end
103
+
104
+ def test_body
105
+ assert_nil @res.body
106
+ @res.body = "test body"
107
+ assert_equal "test body", @res.body
108
+ end
109
+
110
+ def test_body_stream
111
+ assert_nil @res.body
112
+ assert_nil @res.body_stream
113
+ @res.body_stream = io = StringIO.new('test body stream')
114
+ assert_same io, @res.body_stream
115
+ assert_nil @res.body
116
+ end
117
+
118
+ def test_send_to
119
+ res = Net::AJP13::Response.new(200, :reason_phrase => 'Yeah!')
120
+ res['content-length'] = '30'
121
+
122
+ io = StringIO.new
123
+ res.send_to(io)
124
+ io.rewind
125
+ assert_equal "" +
126
+ "\x41\x42\x00\x14" + # "AB" length
127
+ "\x04" + # SEND_HEADERS
128
+ "\x00\xC8\x00\x05Yeah!\x00" +# 200 Yeah!
129
+ "\x00\x01" + # num_headers = 1
130
+ "\xA0\x03" + # Content-Length
131
+ "\x00\x0230\x00" + # 30
132
+ "",
133
+ io.read
134
+ end
135
+
136
+ def test_send_to_and_from_io
137
+ res = Net::AJP13::Response.new(500, :reason_phrase => 'Something in the Servlet is wrong')
138
+ res['content-length'] = '50'
139
+ res['X-Framework'] = 'No such Framework T_T'
140
+ res['ConTent-tYpe'] = 'text/html; charset=BIG-5'
141
+ res['Content-Language'] = 'zh-CN'
142
+ res['Servlet-Engine'] = 'No such engine :P'
143
+
144
+ io = StringIO.new
145
+ res.send_to(io)
146
+ io.rewind
147
+
148
+ assert !io.eof?
149
+ res = Net::AJP13::Response.from_io(io)
150
+ assert io.eof?
151
+ assert_equal 500, res.status
152
+ assert_equal '500', res.code
153
+ assert_equal '50', res['Content-Length']
154
+ assert_equal 50, res.content_length
155
+ assert_equal 'zh-CN', res['Content-Language']
156
+ assert_equal 'text/html; charset=BIG-5', res['Content-Type']
157
+ assert_equal 'No such engine :P', res['Servlet-Engine']
158
+ assert_equal 'No such Framework T_T', res['X-Framework']
159
+ assert_nil res['No-Such-Header']
160
+ end
161
+ end
162
+