Permalink
Browse files

tests

  • Loading branch information...
1 parent 8170415 commit 43c33f1f82af9729d7a49e3f96c3563c2c293938 @vast committed Jul 15, 2009
Showing with 85 additions and 0 deletions.
  1. +30 −0 lib/sinatra/x_send_file.rb
  2. +22 −0 test/sinatra_app.rb
  3. +33 −0 test/sinatra_x_send_file_test.rb
View
@@ -0,0 +1,30 @@
+require 'sinatra/base'
+module Sinatra
+
+ module XSendFile
+ def x_send_file(path, opts = {})
+ stat = File.stat(path)
+
+ content_type media_type(opts[:type]) ||
+ media_type(File.extname(path)) ||
+ response['Content-Type'] ||
+ 'application/octet-stream'
+
+ if opts[:disposition] == 'attachment' || opts[:filename]
+ attachment opts[:filename] || path
+ elsif opts[:disposition] == 'inline'
+ response['Content-Disposition'] = 'inline'
+ end
+
+ header_key = opts[:header] || (Sinatra::Application.respond_to?(:xsf_header) && Sinatra::Application.xsf_header) ||
+ 'X-SendFile'
+ response[header_key] = path
+
+ halt
+ rescue Errno::ENOENT
+ not_found
+ end
+ end
+
+ helpers XSendFile
+end
View
@@ -0,0 +1,22 @@
+require 'rubygems'
+require 'sinatra'
+require 'lib/sinatra/x_send_file'
+
+
+set :xsf_header, 'x-test-send-file'
+
+get '/' do
+ x_send_file(__FILE__)
+end
+
+get '/nginx' do
+ x_send_file(__FILE__, :header => 'X-Accel-Redirect')
+end
+
+get '/lighty1.4' do
+ x_send_file(__FILE__, :header => 'X-LIGHTTPD-send-file')
+end
+
+get '/xsendfile' do
+ x_send_file(__FILE__, :header => 'X-SendFile')
+end
@@ -0,0 +1,33 @@
+require 'sinatra_app'
+require 'test/unit'
+require 'rack/test'
+
+set :environment, :test
+
+class SinatraXSendFileTest < Test::Unit::TestCase
+ include Rack::Test::Methods
+
+ def app
+ Sinatra::Application
+ end
+
+ def test_x_send_file
+ get '/xsendfile'
+ assert_not_nil last_response.headers['X-SendFile']
+ end
+
+ def test_nginx_x_accel_redirect
+ get '/nginx'
+ assert_not_nil last_response.headers['X-Accel-Redirect']
+ end
+
+ def test_lighty1_4_send_file
+ get '/lighty1.4'
+ assert_not_nil last_response.headers['X-LIGHTTPD-send-file']
+ end
+
+ def test_header_global_override
+ get '/'
+ assert_not_nil last_response.headers['x-test-send-file']
+ end
+end

0 comments on commit 43c33f1

Please sign in to comment.