2006-06-30 16:42:12 -04:00
|
|
|
# Copyright (c) 2005 Zed A. Shaw
|
|
|
|
# You can redistribute it and/or modify it under the same terms as Ruby.
|
|
|
|
#
|
|
|
|
# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
|
|
|
|
# for more information.
|
2006-05-21 10:46:42 -04:00
|
|
|
|
2007-10-20 19:15:19 -04:00
|
|
|
require 'test/testhelp'
|
2006-01-28 14:03:53 -05:00
|
|
|
|
|
|
|
include Mongrel
|
|
|
|
|
|
|
|
class URIClassifierTest < Test::Unit::TestCase
|
|
|
|
|
|
|
|
def test_uri_finding
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier = URIClassifier.new
|
|
|
|
uri_classifier.register("/test", 1)
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
script_name, path_info, value = uri_classifier.resolve("/test")
|
|
|
|
assert value
|
|
|
|
assert_equal 1, value
|
|
|
|
assert_equal "/test", script_name
|
2006-01-28 14:03:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_uri_prefix_ops
|
|
|
|
test = "/pre/fix/test"
|
|
|
|
prefix = "/pre"
|
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier = URIClassifier.new
|
|
|
|
uri_classifier.register(prefix,1)
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
script_name, path_info, value = uri_classifier.resolve(prefix)
|
|
|
|
script_name, path_info, value = uri_classifier.resolve(test)
|
2007-10-19 05:27:33 -04:00
|
|
|
assert value
|
2007-10-19 05:26:50 -04:00
|
|
|
assert_equal prefix, script_name
|
|
|
|
assert_equal test[script_name.length .. -1], path_info
|
2006-02-28 00:17:23 -05:00
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
assert uri_classifier.inspect
|
|
|
|
assert_equal prefix, uri_classifier.uris[0]
|
2006-01-28 14:03:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_not_finding
|
|
|
|
test = "/cant/find/me"
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier = URIClassifier.new
|
|
|
|
uri_classifier.register(test, 1)
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
script_name, path_info, value = uri_classifier.resolve("/nope/not/here")
|
|
|
|
assert_nil script_name
|
|
|
|
assert_nil path_info
|
|
|
|
assert_nil value
|
2006-01-28 14:03:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_exceptions
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier = URIClassifier.new
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier.register("test", 1)
|
2006-01-28 14:03:53 -05:00
|
|
|
|
|
|
|
failed = false
|
|
|
|
begin
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier.register("test", 1)
|
2006-01-28 14:03:53 -05:00
|
|
|
rescue => e
|
|
|
|
failed = true
|
|
|
|
end
|
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
assert failed
|
2006-01-28 14:03:53 -05:00
|
|
|
|
|
|
|
failed = false
|
|
|
|
begin
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier.register("", 1)
|
2006-01-28 14:03:53 -05:00
|
|
|
rescue => e
|
|
|
|
failed = true
|
|
|
|
end
|
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
assert failed
|
2006-01-28 14:03:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_register_unregister
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier = URIClassifier.new
|
2006-01-28 14:03:53 -05:00
|
|
|
|
|
|
|
100.times do
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier.register("stuff", 1)
|
|
|
|
value = uri_classifier.unregister("stuff")
|
|
|
|
assert_equal 1, value
|
2006-01-28 14:03:53 -05:00
|
|
|
end
|
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier.register("things",1)
|
|
|
|
script_name, path_info, value = uri_classifier.resolve("things")
|
|
|
|
assert_equal 1, value
|
2006-01-28 14:03:53 -05:00
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier.unregister("things")
|
|
|
|
script_name, path_info, value = uri_classifier.resolve("things")
|
|
|
|
assert_nil value
|
2006-01-28 14:03:53 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2006-02-02 01:53:32 -05:00
|
|
|
def test_uri_branching
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier = URIClassifier.new
|
|
|
|
uri_classifier.register("/test", 1)
|
|
|
|
uri_classifier.register("/test/this",2)
|
2006-02-02 01:53:32 -05:00
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
script_name, path_info, handler = uri_classifier.resolve("/test")
|
|
|
|
script_name, path_info, handler = uri_classifier.resolve("/test/that")
|
|
|
|
assert_equal "/test", script_name, "failed to properly find script off branch portion of uri"
|
|
|
|
assert_equal "/that", path_info
|
|
|
|
assert_equal 1, handler, "wrong result for branching uri"
|
2006-02-02 01:53:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_all_prefixing
|
|
|
|
tests = ["/test","/test/that","/test/this"]
|
|
|
|
uri = "/test/this/that"
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier = URIClassifier.new
|
2006-02-02 01:53:32 -05:00
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
current = ""
|
2006-02-02 01:53:32 -05:00
|
|
|
uri.each_byte do |c|
|
2007-10-19 05:26:50 -04:00
|
|
|
current << c.chr
|
|
|
|
uri_classifier.register(current, c)
|
2006-02-02 01:53:32 -05:00
|
|
|
end
|
2007-10-21 22:58:02 -04:00
|
|
|
|
2006-02-02 01:53:32 -05:00
|
|
|
|
2007-10-20 23:00:43 -04:00
|
|
|
# Try to resolve everything with no asserts as a fuzzing
|
2006-02-02 01:53:32 -05:00
|
|
|
tests.each do |prefix|
|
2007-10-19 05:26:50 -04:00
|
|
|
current = ""
|
2006-02-02 01:53:32 -05:00
|
|
|
prefix.each_byte do |c|
|
2007-10-19 05:26:50 -04:00
|
|
|
current << c.chr
|
|
|
|
script_name, path_info, handler = uri_classifier.resolve(current)
|
|
|
|
assert script_name
|
|
|
|
assert path_info
|
|
|
|
assert handler
|
2006-02-02 01:53:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-10-20 23:00:43 -04:00
|
|
|
# Assert that we find stuff
|
2006-02-02 01:53:32 -05:00
|
|
|
tests.each do |t|
|
2007-10-19 05:26:50 -04:00
|
|
|
script_name, path_info, handler = uri_classifier.resolve(t)
|
|
|
|
assert handler
|
2006-02-02 01:53:32 -05:00
|
|
|
end
|
|
|
|
|
2007-10-20 23:00:43 -04:00
|
|
|
# Assert we don't find stuff
|
2007-10-19 05:26:50 -04:00
|
|
|
script_name, path_info, handler = uri_classifier.resolve("chicken")
|
|
|
|
assert_nil handler
|
|
|
|
assert_nil script_name
|
|
|
|
assert_nil path_info
|
2006-02-02 01:53:32 -05:00
|
|
|
end
|
2006-02-09 21:38:18 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Verifies that a root mounted ("/") handler resolves
|
|
|
|
# such that path info matches the original URI.
|
2007-10-20 23:00:43 -04:00
|
|
|
# This is needed to accommodate real usage of handlers.
|
2006-02-09 21:38:18 -05:00
|
|
|
def test_root_mounted
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier = URIClassifier.new
|
2006-02-09 21:38:18 -05:00
|
|
|
root = "/"
|
|
|
|
path = "/this/is/a/test"
|
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier.register(root, 1)
|
2006-02-09 21:38:18 -05:00
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
script_name, path_info, handler = uri_classifier.resolve(root)
|
|
|
|
assert_equal 1, handler
|
|
|
|
assert_equal root, path_info
|
|
|
|
assert_equal root, script_name
|
2006-02-09 21:38:18 -05:00
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
script_name, path_info, handler = uri_classifier.resolve(path)
|
|
|
|
assert_equal path, path_info
|
|
|
|
assert_equal root, script_name
|
|
|
|
assert_equal 1, handler
|
2006-02-09 21:38:18 -05:00
|
|
|
end
|
2006-02-28 00:17:23 -05:00
|
|
|
|
2007-09-13 00:00:39 -04:00
|
|
|
# Verifies that a root mounted ("/") handler
|
|
|
|
# is the default point, doesn't matter the order we use
|
2007-10-20 23:00:43 -04:00
|
|
|
# to register the URIs
|
2007-09-13 00:00:39 -04:00
|
|
|
def test_classifier_order
|
|
|
|
tests = ["/before", "/way_past"]
|
|
|
|
root = "/"
|
|
|
|
path = "/path"
|
|
|
|
|
2007-10-19 05:26:50 -04:00
|
|
|
uri_classifier = URIClassifier.new
|
|
|
|
uri_classifier.register(path, 1)
|
|
|
|
uri_classifier.register(root, 2)
|
2007-09-13 00:00:39 -04:00
|
|
|
|
|
|
|
tests.each do |uri|
|
2007-10-19 05:26:50 -04:00
|
|
|
script_name, path_info, handler = uri_classifier.resolve(uri)
|
2007-10-21 00:13:37 -04:00
|
|
|
assert_equal root, script_name, "#{uri} did not resolve to #{root}"
|
2007-10-19 05:26:50 -04:00
|
|
|
assert_equal uri, path_info
|
|
|
|
assert_equal 2, handler
|
2007-09-13 00:00:39 -04:00
|
|
|
end
|
|
|
|
end
|
2006-02-28 00:17:23 -05:00
|
|
|
|
2006-01-28 14:03:53 -05:00
|
|
|
end
|
|
|
|
|