webrick 1.3.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of webrick might be problematic. Click here for more details.

Files changed (63) hide show
  1. data/README.txt +21 -0
  2. data/lib/webrick.rb +227 -0
  3. data/lib/webrick/accesslog.rb +151 -0
  4. data/lib/webrick/cgi.rb +260 -0
  5. data/lib/webrick/compat.rb +35 -0
  6. data/lib/webrick/config.rb +121 -0
  7. data/lib/webrick/cookie.rb +110 -0
  8. data/lib/webrick/htmlutils.rb +28 -0
  9. data/lib/webrick/httpauth.rb +95 -0
  10. data/lib/webrick/httpauth/authenticator.rb +112 -0
  11. data/lib/webrick/httpauth/basicauth.rb +108 -0
  12. data/lib/webrick/httpauth/digestauth.rb +392 -0
  13. data/lib/webrick/httpauth/htdigest.rb +128 -0
  14. data/lib/webrick/httpauth/htgroup.rb +93 -0
  15. data/lib/webrick/httpauth/htpasswd.rb +121 -0
  16. data/lib/webrick/httpauth/userdb.rb +52 -0
  17. data/lib/webrick/httpproxy.rb +305 -0
  18. data/lib/webrick/httprequest.rb +461 -0
  19. data/lib/webrick/httpresponse.rb +399 -0
  20. data/lib/webrick/https.rb +64 -0
  21. data/lib/webrick/httpserver.rb +264 -0
  22. data/lib/webrick/httpservlet.rb +22 -0
  23. data/lib/webrick/httpservlet/abstract.rb +153 -0
  24. data/lib/webrick/httpservlet/cgi_runner.rb +46 -0
  25. data/lib/webrick/httpservlet/cgihandler.rb +108 -0
  26. data/lib/webrick/httpservlet/erbhandler.rb +87 -0
  27. data/lib/webrick/httpservlet/filehandler.rb +470 -0
  28. data/lib/webrick/httpservlet/prochandler.rb +33 -0
  29. data/lib/webrick/httpstatus.rb +184 -0
  30. data/lib/webrick/httputils.rb +394 -0
  31. data/lib/webrick/httpversion.rb +49 -0
  32. data/lib/webrick/log.rb +136 -0
  33. data/lib/webrick/server.rb +218 -0
  34. data/lib/webrick/ssl.rb +127 -0
  35. data/lib/webrick/utils.rb +241 -0
  36. data/lib/webrick/version.rb +13 -0
  37. data/sample/webrick/demo-app.rb +66 -0
  38. data/sample/webrick/demo-multipart.cgi +12 -0
  39. data/sample/webrick/demo-servlet.rb +6 -0
  40. data/sample/webrick/demo-urlencoded.cgi +12 -0
  41. data/sample/webrick/hello.cgi +11 -0
  42. data/sample/webrick/hello.rb +8 -0
  43. data/sample/webrick/httpd.rb +23 -0
  44. data/sample/webrick/httpproxy.rb +25 -0
  45. data/sample/webrick/httpsd.rb +33 -0
  46. data/test/openssl/utils.rb +313 -0
  47. data/test/ruby/envutil.rb +208 -0
  48. data/test/webrick/test_cgi.rb +134 -0
  49. data/test/webrick/test_cookie.rb +131 -0
  50. data/test/webrick/test_filehandler.rb +285 -0
  51. data/test/webrick/test_httpauth.rb +167 -0
  52. data/test/webrick/test_httpproxy.rb +282 -0
  53. data/test/webrick/test_httprequest.rb +411 -0
  54. data/test/webrick/test_httpresponse.rb +49 -0
  55. data/test/webrick/test_httpserver.rb +305 -0
  56. data/test/webrick/test_httputils.rb +96 -0
  57. data/test/webrick/test_httpversion.rb +40 -0
  58. data/test/webrick/test_server.rb +67 -0
  59. data/test/webrick/test_utils.rb +64 -0
  60. data/test/webrick/utils.rb +58 -0
  61. data/test/webrick/webrick.cgi +36 -0
  62. data/test/webrick/webrick_long_filename.cgi +36 -0
  63. metadata +106 -0
@@ -0,0 +1,49 @@
1
+ require "webrick"
2
+ require "minitest/autorun"
3
+
4
+ module WEBrick
5
+ class TestHTTPResponse < MiniTest::Unit::TestCase
6
+ class FakeLogger
7
+ attr_reader :messages
8
+
9
+ def initialize
10
+ @messages = []
11
+ end
12
+
13
+ def warn msg
14
+ @messages << msg
15
+ end
16
+ end
17
+
18
+ attr_reader :config, :logger, :res
19
+
20
+ def setup
21
+ super
22
+ @logger = FakeLogger.new
23
+ @config = Config::HTTP
24
+ @config[:Logger] = logger
25
+ @res = HTTPResponse.new config
26
+ @res.keep_alive = true
27
+ end
28
+
29
+ def test_304_does_not_log_warning
30
+ res.status = 304
31
+ res.setup_header
32
+ assert_equal 0, logger.messages.length
33
+ end
34
+
35
+ def test_204_does_not_log_warning
36
+ res.status = 204
37
+ res.setup_header
38
+
39
+ assert_equal 0, logger.messages.length
40
+ end
41
+
42
+ def test_1xx_does_not_log_warnings
43
+ res.status = 105
44
+ res.setup_header
45
+
46
+ assert_equal 0, logger.messages.length
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,305 @@
1
+ require "test/unit"
2
+ require "net/http"
3
+ require "webrick"
4
+ require_relative "utils"
5
+
6
+ class TestWEBrickHTTPServer < Test::Unit::TestCase
7
+ def test_mount
8
+ httpd = WEBrick::HTTPServer.new(
9
+ :Logger => WEBrick::Log.new(TestWEBrick::NullWriter),
10
+ :DoNotListen=>true
11
+ )
12
+ httpd.mount("/", :Root)
13
+ httpd.mount("/foo", :Foo)
14
+ httpd.mount("/foo/bar", :Bar, :bar1)
15
+ httpd.mount("/foo/bar/baz", :Baz, :baz1, :baz2)
16
+
17
+ serv, opts, script_name, path_info = httpd.search_servlet("/")
18
+ assert_equal(:Root, serv)
19
+ assert_equal([], opts)
20
+ assert_equal(script_name, "")
21
+ assert_equal(path_info, "/")
22
+
23
+ serv, opts, script_name, path_info = httpd.search_servlet("/sub")
24
+ assert_equal(:Root, serv)
25
+ assert_equal([], opts)
26
+ assert_equal(script_name, "")
27
+ assert_equal(path_info, "/sub")
28
+
29
+ serv, opts, script_name, path_info = httpd.search_servlet("/sub/")
30
+ assert_equal(:Root, serv)
31
+ assert_equal([], opts)
32
+ assert_equal(script_name, "")
33
+ assert_equal(path_info, "/sub/")
34
+
35
+ serv, opts, script_name, path_info = httpd.search_servlet("/foo")
36
+ assert_equal(:Foo, serv)
37
+ assert_equal([], opts)
38
+ assert_equal(script_name, "/foo")
39
+ assert_equal(path_info, "")
40
+
41
+ serv, opts, script_name, path_info = httpd.search_servlet("/foo/")
42
+ assert_equal(:Foo, serv)
43
+ assert_equal([], opts)
44
+ assert_equal(script_name, "/foo")
45
+ assert_equal(path_info, "/")
46
+
47
+ serv, opts, script_name, path_info = httpd.search_servlet("/foo/sub")
48
+ assert_equal(:Foo, serv)
49
+ assert_equal([], opts)
50
+ assert_equal(script_name, "/foo")
51
+ assert_equal(path_info, "/sub")
52
+
53
+ serv, opts, script_name, path_info = httpd.search_servlet("/foo/bar")
54
+ assert_equal(:Bar, serv)
55
+ assert_equal([:bar1], opts)
56
+ assert_equal(script_name, "/foo/bar")
57
+ assert_equal(path_info, "")
58
+
59
+ serv, opts, script_name, path_info = httpd.search_servlet("/foo/bar/baz")
60
+ assert_equal(:Baz, serv)
61
+ assert_equal([:baz1, :baz2], opts)
62
+ assert_equal(script_name, "/foo/bar/baz")
63
+ assert_equal(path_info, "")
64
+ end
65
+
66
+ class Req
67
+ attr_reader :port, :host
68
+ def initialize(addr, port, host)
69
+ @addr, @port, @host = addr, port, host
70
+ end
71
+ def addr
72
+ [0,0,0,@addr]
73
+ end
74
+ end
75
+
76
+ def httpd(addr, port, host, ali)
77
+ config ={
78
+ :Logger => WEBrick::Log.new(TestWEBrick::NullWriter),
79
+ :DoNotListen => true,
80
+ :BindAddress => addr,
81
+ :Port => port,
82
+ :ServerName => host,
83
+ :ServerAlias => ali,
84
+ }
85
+ return WEBrick::HTTPServer.new(config)
86
+ end
87
+
88
+ def assert_eql?(v1, v2)
89
+ assert_equal(v1.object_id, v2.object_id)
90
+ end
91
+
92
+ def test_lookup_server
93
+ addr1 = "192.168.100.1"
94
+ addr2 = "192.168.100.2"
95
+ addrz = "192.168.100.254"
96
+ local = "127.0.0.1"
97
+ port1 = 80
98
+ port2 = 8080
99
+ port3 = 10080
100
+ portz = 32767
101
+ name1 = "www.example.com"
102
+ name2 = "www2.example.com"
103
+ name3 = "www3.example.com"
104
+ namea = "www.example.co.jp"
105
+ nameb = "www.example.jp"
106
+ namec = "www2.example.co.jp"
107
+ named = "www2.example.jp"
108
+ namez = "foobar.example.com"
109
+ alias1 = [namea, nameb]
110
+ alias2 = [namec, named]
111
+
112
+ host1 = httpd(nil, port1, name1, nil)
113
+ hosts = [
114
+ host2 = httpd(addr1, port1, name1, nil),
115
+ host3 = httpd(addr1, port1, name2, alias1),
116
+ host4 = httpd(addr1, port2, name1, nil),
117
+ host5 = httpd(addr1, port2, name2, alias1),
118
+ host6 = httpd(addr1, port2, name3, alias2),
119
+ host7 = httpd(addr2, nil, name1, nil),
120
+ host8 = httpd(addr2, nil, name2, alias1),
121
+ host9 = httpd(addr2, nil, name3, alias2),
122
+ host10 = httpd(local, nil, nil, nil),
123
+ host11 = httpd(nil, port3, nil, nil),
124
+ ].sort_by{ rand }
125
+ hosts.each{|h| host1.virtual_host(h) }
126
+
127
+ # connect to addr1
128
+ assert_eql?(host2, host1.lookup_server(Req.new(addr1, port1, name1)))
129
+ assert_eql?(host3, host1.lookup_server(Req.new(addr1, port1, name2)))
130
+ assert_eql?(host3, host1.lookup_server(Req.new(addr1, port1, namea)))
131
+ assert_eql?(host3, host1.lookup_server(Req.new(addr1, port1, nameb)))
132
+ assert_eql?(nil, host1.lookup_server(Req.new(addr1, port1, namez)))
133
+ assert_eql?(host4, host1.lookup_server(Req.new(addr1, port2, name1)))
134
+ assert_eql?(host5, host1.lookup_server(Req.new(addr1, port2, name2)))
135
+ assert_eql?(host5, host1.lookup_server(Req.new(addr1, port2, namea)))
136
+ assert_eql?(host5, host1.lookup_server(Req.new(addr1, port2, nameb)))
137
+ assert_eql?(nil, host1.lookup_server(Req.new(addr1, port2, namez)))
138
+ assert_eql?(host11, host1.lookup_server(Req.new(addr1, port3, name1)))
139
+ assert_eql?(host11, host1.lookup_server(Req.new(addr1, port3, name2)))
140
+ assert_eql?(host11, host1.lookup_server(Req.new(addr1, port3, namea)))
141
+ assert_eql?(host11, host1.lookup_server(Req.new(addr1, port3, nameb)))
142
+ assert_eql?(host11, host1.lookup_server(Req.new(addr1, port3, namez)))
143
+ assert_eql?(nil, host1.lookup_server(Req.new(addr1, portz, name1)))
144
+ assert_eql?(nil, host1.lookup_server(Req.new(addr1, portz, name2)))
145
+ assert_eql?(nil, host1.lookup_server(Req.new(addr1, portz, namea)))
146
+ assert_eql?(nil, host1.lookup_server(Req.new(addr1, portz, nameb)))
147
+ assert_eql?(nil, host1.lookup_server(Req.new(addr1, portz, namez)))
148
+
149
+ # connect to addr2
150
+ assert_eql?(host7, host1.lookup_server(Req.new(addr2, port1, name1)))
151
+ assert_eql?(host8, host1.lookup_server(Req.new(addr2, port1, name2)))
152
+ assert_eql?(host8, host1.lookup_server(Req.new(addr2, port1, namea)))
153
+ assert_eql?(host8, host1.lookup_server(Req.new(addr2, port1, nameb)))
154
+ assert_eql?(nil, host1.lookup_server(Req.new(addr2, port1, namez)))
155
+ assert_eql?(host7, host1.lookup_server(Req.new(addr2, port2, name1)))
156
+ assert_eql?(host8, host1.lookup_server(Req.new(addr2, port2, name2)))
157
+ assert_eql?(host8, host1.lookup_server(Req.new(addr2, port2, namea)))
158
+ assert_eql?(host8, host1.lookup_server(Req.new(addr2, port2, nameb)))
159
+ assert_eql?(nil, host1.lookup_server(Req.new(addr2, port2, namez)))
160
+ assert_eql?(host7, host1.lookup_server(Req.new(addr2, port3, name1)))
161
+ assert_eql?(host8, host1.lookup_server(Req.new(addr2, port3, name2)))
162
+ assert_eql?(host8, host1.lookup_server(Req.new(addr2, port3, namea)))
163
+ assert_eql?(host8, host1.lookup_server(Req.new(addr2, port3, nameb)))
164
+ assert_eql?(host11, host1.lookup_server(Req.new(addr2, port3, namez)))
165
+ assert_eql?(host7, host1.lookup_server(Req.new(addr2, portz, name1)))
166
+ assert_eql?(host8, host1.lookup_server(Req.new(addr2, portz, name2)))
167
+ assert_eql?(host8, host1.lookup_server(Req.new(addr2, portz, namea)))
168
+ assert_eql?(host8, host1.lookup_server(Req.new(addr2, portz, nameb)))
169
+ assert_eql?(nil, host1.lookup_server(Req.new(addr2, portz, namez)))
170
+
171
+ # connect to addrz
172
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, port1, name1)))
173
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, port1, name2)))
174
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, port1, namea)))
175
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, port1, nameb)))
176
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, port1, namez)))
177
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, port2, name1)))
178
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, port2, name2)))
179
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, port2, namea)))
180
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, port2, nameb)))
181
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, port2, namez)))
182
+ assert_eql?(host11, host1.lookup_server(Req.new(addrz, port3, name1)))
183
+ assert_eql?(host11, host1.lookup_server(Req.new(addrz, port3, name2)))
184
+ assert_eql?(host11, host1.lookup_server(Req.new(addrz, port3, namea)))
185
+ assert_eql?(host11, host1.lookup_server(Req.new(addrz, port3, nameb)))
186
+ assert_eql?(host11, host1.lookup_server(Req.new(addrz, port3, namez)))
187
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, portz, name1)))
188
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, portz, name2)))
189
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, portz, namea)))
190
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, portz, nameb)))
191
+ assert_eql?(nil, host1.lookup_server(Req.new(addrz, portz, namez)))
192
+
193
+ # connect to localhost
194
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port1, name1)))
195
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port1, name2)))
196
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port1, namea)))
197
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port1, nameb)))
198
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port1, namez)))
199
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port2, name1)))
200
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port2, name2)))
201
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port2, namea)))
202
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port2, nameb)))
203
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port2, namez)))
204
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port3, name1)))
205
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port3, name2)))
206
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port3, namea)))
207
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port3, nameb)))
208
+ assert_eql?(host10, host1.lookup_server(Req.new(local, port3, namez)))
209
+ assert_eql?(host10, host1.lookup_server(Req.new(local, portz, name1)))
210
+ assert_eql?(host10, host1.lookup_server(Req.new(local, portz, name2)))
211
+ assert_eql?(host10, host1.lookup_server(Req.new(local, portz, namea)))
212
+ assert_eql?(host10, host1.lookup_server(Req.new(local, portz, nameb)))
213
+ assert_eql?(host10, host1.lookup_server(Req.new(local, portz, namez)))
214
+ end
215
+
216
+ def test_callbacks
217
+ accepted = started = stopped = 0
218
+ requested0 = requested1 = 0
219
+ config = {
220
+ :ServerName => "localhost",
221
+ :AcceptCallback => Proc.new{ accepted += 1 },
222
+ :StartCallback => Proc.new{ started += 1 },
223
+ :StopCallback => Proc.new{ stopped += 1 },
224
+ :RequestCallback => Proc.new{|req, res| requested0 += 1 },
225
+ }
226
+ TestWEBrick.start_httpserver(config){|server, addr, port, log|
227
+ vhost_config = {
228
+ :ServerName => "myhostname",
229
+ :BindAddress => addr,
230
+ :Port => port,
231
+ :DoNotListen => true,
232
+ :Logger => WEBrick::Log.new(TestWEBrick::NullWriter),
233
+ :AccessLog => [],
234
+ :RequestCallback => Proc.new{|req, res| requested1 += 1 },
235
+ }
236
+ server.virtual_host(WEBrick::HTTPServer.new(vhost_config))
237
+
238
+ true while server.status != :Running
239
+ assert_equal(started, 1, log.call)
240
+ assert_equal(stopped, 0, log.call)
241
+ assert_equal(accepted, 0, log.call)
242
+
243
+ http = Net::HTTP.new(addr, port)
244
+ req = Net::HTTP::Get.new("/")
245
+ req["Host"] = "myhostname:#{port}"
246
+ http.request(req){|res| assert_equal("404", res.code, log.call)}
247
+ http.request(req){|res| assert_equal("404", res.code, log.call)}
248
+ http.request(req){|res| assert_equal("404", res.code, log.call)}
249
+ req["Host"] = "localhost:#{port}"
250
+ http.request(req){|res| assert_equal("404", res.code, log.call)}
251
+ http.request(req){|res| assert_equal("404", res.code, log.call)}
252
+ http.request(req){|res| assert_equal("404", res.code, log.call)}
253
+ assert_equal(6, accepted, log.call)
254
+ assert_equal(3, requested0, log.call)
255
+ assert_equal(3, requested1, log.call)
256
+ }
257
+ assert_equal(started, 1)
258
+ assert_equal(stopped, 1)
259
+ end
260
+
261
+ def test_response_io_without_chunked_set
262
+ config = {
263
+ :ServerName => "localhost"
264
+ }
265
+ TestWEBrick.start_httpserver(config){|server, addr, port, log|
266
+ server.mount_proc("/", lambda { |req, res|
267
+ r,w = IO.pipe
268
+ # Test for not setting chunked...
269
+ # res.chunked = true
270
+ res.body = r
271
+ w << "foo"
272
+ w.close
273
+ })
274
+ Thread.pass while server.status != :Running
275
+ http = Net::HTTP.new(addr, port)
276
+ req = Net::HTTP::Get.new("/")
277
+ req['Connection'] = 'Keep-Alive'
278
+ begin
279
+ timeout(2) do
280
+ http.request(req){|res| assert_equal("foo", res.body) }
281
+ end
282
+ rescue Timeout::Error
283
+ flunk('corrupted reponse')
284
+ end
285
+ }
286
+ end
287
+
288
+ def test_request_handler_callback_is_deprecated
289
+ requested = 0
290
+ config = {
291
+ :ServerName => "localhost",
292
+ :RequestHandler => Proc.new{|req, res| requested += 1 },
293
+ }
294
+ TestWEBrick.start_httpserver(config){|server, addr, port, log|
295
+ true while server.status != :Running
296
+
297
+ http = Net::HTTP.new(addr, port)
298
+ req = Net::HTTP::Get.new("/")
299
+ req["Host"] = "localhost:#{port}"
300
+ http.request(req){|res| assert_equal("404", res.code, log.call)}
301
+ assert_match(%r{:RequestHandler is deprecated, please use :RequestCallback$}, log.call, log.call)
302
+ }
303
+ assert_equal(requested, 1)
304
+ end
305
+ end
@@ -0,0 +1,96 @@
1
+ require "test/unit"
2
+ require "webrick/httputils"
3
+
4
+ class TestWEBrickHTTPUtils < Test::Unit::TestCase
5
+ include WEBrick::HTTPUtils
6
+
7
+ def test_normilize_path
8
+ assert_equal("/foo", normalize_path("/foo"))
9
+ assert_equal("/foo/bar/", normalize_path("/foo/bar/"))
10
+
11
+ assert_equal("/", normalize_path("/foo/../"))
12
+ assert_equal("/", normalize_path("/foo/.."))
13
+ assert_equal("/", normalize_path("/foo/bar/../../"))
14
+ assert_equal("/", normalize_path("/foo/bar/../.."))
15
+ assert_equal("/", normalize_path("/foo/bar/../.."))
16
+ assert_equal("/baz", normalize_path("/foo/bar/../../baz"))
17
+ assert_equal("/baz", normalize_path("/foo/../bar/../baz"))
18
+ assert_equal("/baz/", normalize_path("/foo/../bar/../baz/"))
19
+ assert_equal("/...", normalize_path("/bar/../..."))
20
+ assert_equal("/.../", normalize_path("/bar/../.../"))
21
+
22
+ assert_equal("/foo/", normalize_path("/foo/./"))
23
+ assert_equal("/foo/", normalize_path("/foo/."))
24
+ assert_equal("/foo/", normalize_path("/foo/././"))
25
+ assert_equal("/foo/", normalize_path("/foo/./."))
26
+ assert_equal("/foo/bar", normalize_path("/foo/./bar"))
27
+ assert_equal("/foo/bar/", normalize_path("/foo/./bar/."))
28
+ assert_equal("/foo/bar/", normalize_path("/./././foo/./bar/."))
29
+
30
+ assert_equal("/foo/bar/", normalize_path("//foo///.//bar/.///.//"))
31
+ assert_equal("/", normalize_path("//foo///..///bar/.///..//.//"))
32
+
33
+ assert_raise(RuntimeError){ normalize_path("foo/bar") }
34
+ assert_raise(RuntimeError){ normalize_path("..") }
35
+ assert_raise(RuntimeError){ normalize_path("/..") }
36
+ assert_raise(RuntimeError){ normalize_path("/./..") }
37
+ assert_raise(RuntimeError){ normalize_path("/./../") }
38
+ assert_raise(RuntimeError){ normalize_path("/./../..") }
39
+ assert_raise(RuntimeError){ normalize_path("/./../../") }
40
+ assert_raise(RuntimeError){ normalize_path("/./../") }
41
+ assert_raise(RuntimeError){ normalize_path("/../..") }
42
+ assert_raise(RuntimeError){ normalize_path("/../../") }
43
+ assert_raise(RuntimeError){ normalize_path("/../../..") }
44
+ assert_raise(RuntimeError){ normalize_path("/../../../") }
45
+ assert_raise(RuntimeError){ normalize_path("/../foo/../") }
46
+ assert_raise(RuntimeError){ normalize_path("/../foo/../../") }
47
+ assert_raise(RuntimeError){ normalize_path("/foo/bar/../../../../") }
48
+ assert_raise(RuntimeError){ normalize_path("/foo/../bar/../../") }
49
+ assert_raise(RuntimeError){ normalize_path("/./../bar/") }
50
+ assert_raise(RuntimeError){ normalize_path("/./../") }
51
+ end
52
+
53
+ def test_split_header_value
54
+ assert_equal(['foo', 'bar'], split_header_value('foo, bar'))
55
+ assert_equal(['"foo"', 'bar'], split_header_value('"foo", bar'))
56
+ assert_equal(['foo', '"bar"'], split_header_value('foo, "bar"'))
57
+ assert_equal(['*'], split_header_value('*'))
58
+ assert_equal(['W/"xyzzy"', 'W/"r2d2xxxx"', 'W/"c3piozzzz"'],
59
+ split_header_value('W/"xyzzy", W/"r2d2xxxx", W/"c3piozzzz"'))
60
+ end
61
+
62
+ def test_escape
63
+ assert_equal("/foo/bar", escape("/foo/bar"))
64
+ assert_equal("/~foo/bar", escape("/~foo/bar"))
65
+ assert_equal("/~foo%20bar", escape("/~foo bar"))
66
+ assert_equal("/~foo%20bar", escape("/~foo bar"))
67
+ assert_equal("/~foo%09bar", escape("/~foo\tbar"))
68
+ assert_equal("/~foo+bar", escape("/~foo+bar"))
69
+ end
70
+
71
+ def test_escape_form
72
+ assert_equal("%2Ffoo%2Fbar", escape_form("/foo/bar"))
73
+ assert_equal("%2F~foo%2Fbar", escape_form("/~foo/bar"))
74
+ assert_equal("%2F~foo+bar", escape_form("/~foo bar"))
75
+ assert_equal("%2F~foo+%2B+bar", escape_form("/~foo + bar"))
76
+ end
77
+
78
+ def test_unescape
79
+ assert_equal("/foo/bar", unescape("%2ffoo%2fbar"))
80
+ assert_equal("/~foo/bar", unescape("/%7efoo/bar"))
81
+ assert_equal("/~foo/bar", unescape("%2f%7efoo%2fbar"))
82
+ assert_equal("/~foo+bar", unescape("/%7efoo+bar"))
83
+ end
84
+
85
+ def test_unescape_form
86
+ assert_equal("//foo/bar", unescape_form("/%2Ffoo/bar"))
87
+ assert_equal("//foo/bar baz", unescape_form("/%2Ffoo/bar+baz"))
88
+ assert_equal("/~foo/bar baz", unescape_form("/%7Efoo/bar+baz"))
89
+ end
90
+
91
+ def test_escape_path
92
+ assert_equal("/foo/bar", escape_path("/foo/bar"))
93
+ assert_equal("/foo/bar/", escape_path("/foo/bar/"))
94
+ assert_equal("/%25foo/bar/", escape_path("/%foo/bar/"))
95
+ end
96
+ end