spec_static.rb 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. require 'rack/static'
  2. require 'rack/mock'
  3. class DummyApp
  4. def call(env)
  5. [200, {}, ["Hello World"]]
  6. end
  7. end
  8. describe Rack::Static do
  9. root = File.expand_path(File.dirname(__FILE__))
  10. OPTIONS = {:urls => ["/cgi"], :root => root}
  11. STATIC_OPTIONS = {:urls => [""], :root => root, :index => 'static/index.html'}
  12. HASH_OPTIONS = {:urls => {"/cgi/sekret" => 'cgi/test'}, :root => root}
  13. @request = Rack::MockRequest.new(Rack::Static.new(DummyApp.new, OPTIONS))
  14. @static_request = Rack::MockRequest.new(Rack::Static.new(DummyApp.new, STATIC_OPTIONS))
  15. @hash_request = Rack::MockRequest.new(Rack::Static.new(DummyApp.new, HASH_OPTIONS))
  16. it "serves files" do
  17. res = @request.get("/cgi/test")
  18. res.should.be.ok
  19. res.body.should =~ /ruby/
  20. end
  21. it "404s if url root is known but it can't find the file" do
  22. res = @request.get("/cgi/foo")
  23. res.should.be.not_found
  24. end
  25. it "calls down the chain if url root is not known" do
  26. res = @request.get("/something/else")
  27. res.should.be.ok
  28. res.body.should == "Hello World"
  29. end
  30. it "calls index file when requesting root" do
  31. res = @static_request.get("/")
  32. res.should.be.ok
  33. res.body.should =~ /index!/
  34. end
  35. it "doesn't call index file if :index option was omitted" do
  36. res = @request.get("/")
  37. res.body.should == "Hello World"
  38. end
  39. it "serves hidden files" do
  40. res = @hash_request.get("/cgi/sekret")
  41. res.should.be.ok
  42. res.body.should =~ /ruby/
  43. end
  44. it "calls down the chain if the URI is not specified" do
  45. res = @hash_request.get("/something/else")
  46. res.should.be.ok
  47. res.body.should == "Hello World"
  48. end
  49. it "supports serving fixed cache-control" do
  50. opts = OPTIONS.merge(:cache_control => 'public')
  51. request = Rack::MockRequest.new(Rack::Static.new(DummyApp.new, opts))
  52. res = request.get("/cgi/test")
  53. res.should.be.ok
  54. res.headers['Cache-Control'].should == 'public'
  55. end
  56. end