Dont need rails adapter anymore, rack handles it
[unicorn.git] / test / unit / test_uriclassifier.rb
bloba4380651201a1001c20cb79aa8fdcf9dac78dd9c
1 # Copyright (c) 2005 Zed A. Shaw 
2 # You can redistribute it and/or modify it under the same terms as Ruby.
4 # Additional work donated by contributors.  See http://mongrel.rubyforge.org/attributions.html 
5 # for more information.
7 require 'test/test_helper'
9 include Mongrel
11 class URIClassifierTest < Test::Unit::TestCase
13   def test_uri_finding
14     uri_classifier = URIClassifier.new
15     uri_classifier.register("/test", 1)
16     
17     script_name, path_info, value = uri_classifier.resolve("/test")
18     assert_equal 1, value
19     assert_equal "/test", script_name
20   end
21   
22   def test_root_handler_only
23     uri_classifier = URIClassifier.new
24     uri_classifier.register("/", 1)
25     
26     script_name, path_info, value = uri_classifier.resolve("/test")
27     assert_equal 1, value
28     assert_equal "/", script_name 
29     assert_equal "/test", path_info
30   end
32   def test_uri_prefix_ops
33     test = "/pre/fix/test"
34     prefix = "/pre"
36     uri_classifier = URIClassifier.new
37     uri_classifier.register(prefix,1)
39     script_name, path_info, value = uri_classifier.resolve(prefix)
40     script_name, path_info, value = uri_classifier.resolve(test)
41     assert_equal 1, value
42     assert_equal prefix, script_name
43     assert_equal test[script_name.length .. -1], path_info
45     assert uri_classifier.inspect
46     assert_equal prefix, uri_classifier.uris[0]
47   end
49   def test_not_finding
50     test = "/cant/find/me"
51     uri_classifier = URIClassifier.new
52     uri_classifier.register(test, 1)
54     script_name, path_info, value = uri_classifier.resolve("/nope/not/here")
55     assert_nil script_name
56     assert_nil path_info
57     assert_nil value
58   end
60   def test_exceptions
61     uri_classifier = URIClassifier.new
63     uri_classifier.register("/test", 1)
64     
65     failed = false
66     begin 
67       uri_classifier.register("/test", 1)
68     rescue => e
69       failed = true
70     end
72     assert failed
74     failed = false
75     begin
76       uri_classifier.register("", 1)
77     rescue => e
78       failed = true
79     end
81     assert failed
82   end
85   def test_register_unregister
86     uri_classifier = URIClassifier.new
87     
88     100.times do
89       uri_classifier.register("/stuff", 1)
90       value = uri_classifier.unregister("/stuff")
91       assert_equal 1, value
92     end
94     uri_classifier.register("/things",1)
95     script_name, path_info, value = uri_classifier.resolve("/things")
96     assert_equal 1, value
98     uri_classifier.unregister("/things")
99     script_name, path_info, value = uri_classifier.resolve("/things")
100     assert_nil value
102   end
105   def test_uri_branching
106     uri_classifier = URIClassifier.new
107     uri_classifier.register("/test", 1)
108     uri_classifier.register("/test/this",2)
109   
110     script_name, path_info, handler = uri_classifier.resolve("/test")
111     script_name, path_info, handler = uri_classifier.resolve("/test/that")
112     assert_equal "/test", script_name, "failed to properly find script off branch portion of uri"
113     assert_equal "/that", path_info
114     assert_equal 1, handler, "wrong result for branching uri"
115   end
117   def test_all_prefixing
118     tests = ["/test","/test/that","/test/this"]
119     uri = "/test/this/that"
120     uri_classifier = URIClassifier.new
121     
122     current = ""
123     uri.each_byte do |c|
124       current << c.chr
125       uri_classifier.register(current, c)
126     end
127     
129     # Try to resolve everything with no asserts as a fuzzing
130     tests.each do |prefix|
131       current = ""
132       prefix.each_byte do |c|
133         current << c.chr
134         script_name, path_info, handler = uri_classifier.resolve(current)
135         assert script_name
136         assert path_info
137         assert handler
138       end
139     end
141     # Assert that we find stuff
142     tests.each do |t|
143       script_name, path_info, handler = uri_classifier.resolve(t)
144       assert handler
145     end
147     # Assert we don't find stuff
148     script_name, path_info, handler = uri_classifier.resolve("chicken")
149     assert_nil handler
150     assert_nil script_name
151     assert_nil path_info
152   end
155   # Verifies that a root mounted ("/") handler resolves
156   # such that path info matches the original URI.
157   # This is needed to accommodate real usage of handlers.
158   def test_root_mounted
159     uri_classifier = URIClassifier.new
160     root = "/"
161     path = "/this/is/a/test"
163     uri_classifier.register(root, 1)
165     script_name, path_info, handler = uri_classifier.resolve(root)
166     assert_equal 1, handler
167     assert_equal root, path_info
168     assert_equal root, script_name
170     script_name, path_info, handler = uri_classifier.resolve(path)
171     assert_equal path, path_info
172     assert_equal root, script_name
173     assert_equal 1, handler
174   end
176   # Verifies that a root mounted ("/") handler
177   # is the default point, doesn't matter the order we use
178   # to register the URIs
179   def test_classifier_order
180     tests = ["/before", "/way_past"]
181     root = "/"
182     path = "/path"
184     uri_classifier = URIClassifier.new
185     uri_classifier.register(path, 1)
186     uri_classifier.register(root, 2)
188     tests.each do |uri|
189       script_name, path_info, handler = uri_classifier.resolve(uri)
190       assert_equal root, script_name, "#{uri} did not resolve to #{root}"
191       assert_equal uri, path_info
192       assert_equal 2, handler
193     end
194   end
195   
196   if ENV['BENCHMARK']
197     # Eventually we will have a suite of benchmarks instead of lamely installing a test
198     
199     def test_benchmark    
201       # This URI set should favor a TST. Both versions increase linearly until you hit 14 
202       # URIs, then the TST flattens out.
203       @uris = %w(
204         / 
205         /dag /dig /digbark /dog /dogbark /dog/bark /dug /dugbarking /puppy 
206         /c /cat /cat/tree /cat/tree/mulberry /cats /cot /cot/tree/mulberry /kitty /kittycat
207 #        /eag /eig /eigbark /eog /eogbark /eog/bark /eug /eugbarking /iuppy 
208 #        /f /fat /fat/tree /fat/tree/mulberry /fats /fot /fot/tree/mulberry /jitty /jittyfat
209 #        /gag /gig /gigbark /gog /gogbark /gog/bark /gug /gugbarking /kuppy 
210 #        /h /hat /hat/tree /hat/tree/mulberry /hats /hot /hot/tree/mulberry /litty /littyhat
211 #        /ceag /ceig /ceigbark /ceog /ceogbark /ceog/cbark /ceug /ceugbarking /ciuppy 
212 #        /cf /cfat /cfat/ctree /cfat/ctree/cmulberry /cfats /cfot /cfot/ctree/cmulberry /cjitty /cjittyfat
213 #        /cgag /cgig /cgigbark /cgog /cgogbark /cgog/cbark /cgug /cgugbarking /ckuppy 
214 #        /ch /chat /chat/ctree /chat/ctree/cmulberry /chats /chot /chot/ctree/cmulberry /citty /cittyhat
215       )
216       
217       @requests = %w(
218         /
219         /dig
220         /digging
221         /dogging
222         /dogbarking/
223         /puppy/barking
224         /c
225         /cat
226         /cat/shrub
227         /cat/tree
228         /cat/tree/maple
229         /cat/tree/mulberry/tree
230         /cat/tree/oak
231         /cats/
232         /cats/tree
233         /cod
234         /zebra
235       )
236     
237       @classifier = URIClassifier.new
238       @uris.each do |uri|
239         @classifier.register(uri, 1)
240       end
241       
242       puts "#{@uris.size} URIs / #{@requests.size * 10000} requests"
243   
244       Benchmark.bm do |x|
245         x.report do
246   #        require 'ruby-prof'
247   #        profile = RubyProf.profile do
248             10000.times do
249               @requests.each do |request|
250                 @classifier.resolve(request)
251               end
252             end
253   #        end
254   #        File.open("profile.html", 'w') { |file| RubyProf::GraphHtmlPrinter.new(profile).print(file, 0) }
255         end
256       end          
257     end
258   end
259