spec_sendfile.rb 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. require 'rack/sendfile'
  2. require 'rack/mock'
  3. describe Rack::File do
  4. should "respond to #to_path" do
  5. Rack::File.new(Dir.pwd).should.respond_to :to_path
  6. end
  7. end
  8. describe Rack::Sendfile do
  9. def sendfile_body
  10. res = ['Hello World']
  11. def res.to_path ; "/tmp/hello.txt" ; end
  12. res
  13. end
  14. def simple_app(body=sendfile_body)
  15. lambda { |env| [200, {'Content-Type' => 'text/plain'}, body] }
  16. end
  17. def sendfile_app(body=sendfile_body)
  18. Rack::Sendfile.new(simple_app(body))
  19. end
  20. @request = Rack::MockRequest.new(sendfile_app)
  21. def request(headers={})
  22. yield @request.get('/', headers)
  23. end
  24. it "does nothing when no X-Sendfile-Type header present" do
  25. request do |response|
  26. response.should.be.ok
  27. response.body.should.equal 'Hello World'
  28. response.headers.should.not.include 'X-Sendfile'
  29. end
  30. end
  31. it "sets X-Sendfile response header and discards body" do
  32. request 'HTTP_X_SENDFILE_TYPE' => 'X-Sendfile' do |response|
  33. response.should.be.ok
  34. response.body.should.be.empty
  35. response.headers['Content-Length'].should == '0'
  36. response.headers['X-Sendfile'].should.equal '/tmp/hello.txt'
  37. end
  38. end
  39. it "sets X-Lighttpd-Send-File response header and discards body" do
  40. request 'HTTP_X_SENDFILE_TYPE' => 'X-Lighttpd-Send-File' do |response|
  41. response.should.be.ok
  42. response.body.should.be.empty
  43. response.headers['Content-Length'].should == '0'
  44. response.headers['X-Lighttpd-Send-File'].should.equal '/tmp/hello.txt'
  45. end
  46. end
  47. it "sets X-Accel-Redirect response header and discards body" do
  48. headers = {
  49. 'HTTP_X_SENDFILE_TYPE' => 'X-Accel-Redirect',
  50. 'HTTP_X_ACCEL_MAPPING' => '/tmp/=/foo/bar/'
  51. }
  52. request headers do |response|
  53. response.should.be.ok
  54. response.body.should.be.empty
  55. response.headers['Content-Length'].should == '0'
  56. response.headers['X-Accel-Redirect'].should.equal '/foo/bar/hello.txt'
  57. end
  58. end
  59. it 'writes to rack.error when no X-Accel-Mapping is specified' do
  60. request 'HTTP_X_SENDFILE_TYPE' => 'X-Accel-Redirect' do |response|
  61. response.should.be.ok
  62. response.body.should.equal 'Hello World'
  63. response.headers.should.not.include 'X-Accel-Redirect'
  64. response.errors.should.include 'X-Accel-Mapping'
  65. end
  66. end
  67. it 'does nothing when body does not respond to #to_path' do
  68. @request = Rack::MockRequest.new(sendfile_app(['Not a file...']))
  69. request 'HTTP_X_SENDFILE_TYPE' => 'X-Sendfile' do |response|
  70. response.body.should.equal 'Not a file...'
  71. response.headers.should.not.include 'X-Sendfile'
  72. end
  73. end
  74. end