PageRenderTime 23ms CodeModel.GetById 1ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/bundle/jruby/2.1/gems/rack-1.5.2/test/spec_directory.rb

https://github.com/delowong/logstash
Ruby | 88 lines | 74 code | 14 blank | 0 comment | 2 complexity | 07d8cf678cee14fc43376319eafc4e63 MD5 | raw file
 1require 'rack/directory'
 2require 'rack/lint'
 3require 'rack/mock'
 4
 5describe Rack::Directory do
 6  DOCROOT = File.expand_path(File.dirname(__FILE__)) unless defined? DOCROOT
 7  FILE_CATCH = proc{|env| [200, {'Content-Type'=>'text/plain', "Content-Length" => "7"}, ['passed!']] }
 8  app = Rack::Lint.new(Rack::Directory.new(DOCROOT, FILE_CATCH))
 9
10  should "serve directory indices" do
11    res = Rack::MockRequest.new(Rack::Lint.new(app)).
12      get("/cgi/")
13
14    res.should.be.ok
15    res.should =~ /<html><head>/
16  end
17
18  should "pass to app if file found" do
19    res = Rack::MockRequest.new(Rack::Lint.new(app)).
20      get("/cgi/test")
21
22    res.should.be.ok
23    res.should =~ /passed!/
24  end
25
26  should "serve uri with URL encoded filenames" do
27    res = Rack::MockRequest.new(Rack::Lint.new(app)).
28      get("/%63%67%69/") # "/cgi/test"
29
30    res.should.be.ok
31    res.should =~ /<html><head>/
32
33    res = Rack::MockRequest.new(Rack::Lint.new(app)).
34      get("/cgi/%74%65%73%74") # "/cgi/test"
35
36    res.should.be.ok
37    res.should =~ /passed!/
38  end
39
40  should "not allow directory traversal" do
41    res = Rack::MockRequest.new(Rack::Lint.new(app)).
42      get("/cgi/../test")
43
44    res.should.be.forbidden
45
46    res = Rack::MockRequest.new(Rack::Lint.new(app)).
47      get("/cgi/%2E%2E/test")
48
49    res.should.be.forbidden
50  end
51
52  should "404 if it can't find the file" do
53    res = Rack::MockRequest.new(Rack::Lint.new(app)).
54      get("/cgi/blubb")
55
56    res.should.be.not_found
57  end
58
59  should "uri escape path parts" do # #265, properly escape file names
60    mr = Rack::MockRequest.new(Rack::Lint.new(app))
61
62    res = mr.get("/cgi/test%2bdirectory")
63
64    res.should.be.ok
65    res.body.should =~ %r[/cgi/test%2Bdirectory/test%2Bfile]
66
67    res = mr.get("/cgi/test%2bdirectory/test%2bfile")
68    res.should.be.ok
69  end
70
71  should "correctly escape script name" do
72    app2 = Rack::Builder.new do
73      map '/script-path' do
74        run app
75      end
76    end
77
78    mr = Rack::MockRequest.new(Rack::Lint.new(app2))
79
80    res = mr.get("/script-path/cgi/test%2bdirectory")
81
82    res.should.be.ok
83    res.body.should =~ %r[/script-path/cgi/test%2Bdirectory/test%2Bfile]
84
85    res = mr.get("/script-path/cgi/test%2bdirectory/test%2bfile")
86    res.should.be.ok
87  end
88end