/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

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