Skip to content

Commit

Permalink
feat: add Action#header method
Browse files Browse the repository at this point in the history
Closes #42
  • Loading branch information
vladfaust committed Aug 25, 2018
1 parent a640570 commit a75e9b1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
20 changes: 18 additions & 2 deletions spec/action_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module Prism::Action
end

it "sets content type header" do
response.content_type.should eq("application/json")
response.headers["Content-Type"].should eq "application/json; charset=utf-8"
end
end

Expand All @@ -85,7 +85,7 @@ module Prism::Action
end

it "sets content type header" do
response.content_type.should eq("application/json")
response.headers["Content-Type"].should eq "application/json; charset=utf-8"
end
end

Expand Down Expand Up @@ -199,4 +199,20 @@ module Prism::Action
CallbacksAction.buffer.should eq ["before", "around_before", "call", "around_after", "after"]
end
end

struct HeaderAction
include Prism::Action

def call
header("Custom", "42")
end
end

describe HeaderAction do
response = handle_request(HeaderAction)

it do
response.headers["Custom"].should eq "42"
end
end
end
15 changes: 13 additions & 2 deletions src/prism/action.cr
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ module Prism
context.response.status_code = value
end

# Set HTTP header.
#
# ```
# def call
# header("Content-Type", "application/json")
# end
# ```
def header(name, value)
context.response.headers[name] = value
end

private CONTENT_TYPE_TEXT = "text/html; charset=utf-8"

{% begin %}
Expand All @@ -133,7 +144,7 @@ module Prism
# end
# ```
def text(value)
context.response.content_type = CONTENT_TYPE_TEXT
header("Content-Type", CONTENT_TYPE_TEXT)
context.response.print(value)
end

Expand Down Expand Up @@ -163,7 +174,7 @@ module Prism
# end
# ```
def json(value)
context.response.content_type = CONTENT_TYPE_JSON
header("Content-Type", CONTENT_TYPE_JSON)
value.to_json(context.response)
context.response.close
end
Expand Down

0 comments on commit a75e9b1

Please sign in to comment.