From d4e96c7b4428e4792232c09bd09644d5a4c76bb7 Mon Sep 17 00:00:00 2001 From: Sdogruyol Date: Mon, 8 Aug 2016 21:49:47 +0300 Subject: [PATCH] Add spec for send_file binary --- spec/helpers_spec.cr | 12 ++++++++++++ src/kemal/helpers/helpers.cr | 6 ++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/spec/helpers_spec.cr b/spec/helpers_spec.cr index df24ec3..e6de9d5 100644 --- a/spec/helpers_spec.cr +++ b/spec/helpers_spec.cr @@ -109,5 +109,17 @@ describe "Macros" do response.headers["Content-Type"].should eq("image/jpeg") response.headers["Content-Length"].should eq("20") end + + it "sends file with binary stream" do + get "/" do |env| + send_file env, "Serdar".to_slice + end + + request = HTTP::Request.new("GET", "/") + response = call_request_on_app(request) + response.status_code.should eq(200) + response.headers["Content-Type"].should eq("application/octet-stream") + response.headers["Content-Length"].should eq("6") + end end end diff --git a/src/kemal/helpers/helpers.cr b/src/kemal/helpers/helpers.cr index cc32386..6dba009 100644 --- a/src/kemal/helpers/helpers.cr +++ b/src/kemal/helpers/helpers.cr @@ -50,7 +50,7 @@ end # send_file env, "./path/to/file", "image/jpeg" def send_file(env, path : String, mime_type : String? = nil) file_path = File.expand_path(path, Dir.current) - mime_type = "application/octet-stream" unless mime_type + mime_type ||= "application/octet-stream" env.response.content_type = mime_type env.response.content_length = File.size(file_path) File.open(file_path) do |file| @@ -65,11 +65,9 @@ end # Optionally you can override the mime_type # # send_file env, data_slice, "image/jpeg" - def send_file(env, data : Slice(UInt8), mime_type : String? = nil) mime_type ||= "application/octet-stream" - env.response.content_type = mime_type env.response.content_length = data.bytesize env.response.write data -end +end \ No newline at end of file