Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion spec/lucky/text_response_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ require "../spec_helper"

include ContextHelper

# Monkey patch HTTP::Server::Response to allow accessing the response body directly.
class HTTP::Server::Response
getter body_io : IO = IO::Memory.new

def write(slice : Bytes) : Nil
@body_io.write slice

previous_def
end
end

describe Lucky::TextResponse do
describe "#print" do
context "flash" do
Expand Down Expand Up @@ -169,7 +180,7 @@ describe Lucky::TextResponse do
context = build_context("HEAD")
print_response_with_body(context, "Body", status: nil)
context.request.method.should eq "HEAD"
context.request.body.to_s.should eq ""
context.response.body_io.to_s.should eq("")
context.response.status_code.should eq 200
end
end
Expand Down
8 changes: 7 additions & 1 deletion src/lucky/text_response.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class Lucky::TextResponse < Lucky::Response
context.response.content_type = content_type
context.response.status_code = status
gzip if should_gzip?
context.response.print body
context.response.print(body) if should_print?
rescue e : IO::Error
Lucky::Log.error(exception: e) { "Broken Pipe: Maybe the client navigated away?" }
end

def status : Int
Expand All @@ -49,6 +51,10 @@ class Lucky::TextResponse < Lucky::Response
{% end %}
end

private def should_print? : Bool
context.request.method.downcase != "head"
end

private def write_flash : Nil
context.session.set(
Lucky::FlashStore::SESSION_KEY,
Expand Down