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
3 changes: 3 additions & 0 deletions lib/lotus/action.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'lotus/action/configurable'
require 'lotus/action/rack'
require 'lotus/action/http'
require 'lotus/action/mime'
require 'lotus/action/redirect'
require 'lotus/action/exposable'
Expand Down Expand Up @@ -36,6 +37,7 @@ module Action
#
# @see Lotus::Action::Rack
# @see Lotus::Action::Mime
# @see Lotus::Action::Http
# @see Lotus::Action::Redirect
# @see Lotus::Action::Exposable
# @see Lotus::Action::Throwable
Expand All @@ -47,6 +49,7 @@ def self.included(base)
base.class_eval do
include Rack
include Mime
include Http
include Redirect
include Exposable
include Throwable
Expand Down
17 changes: 17 additions & 0 deletions lib/lotus/action/http.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Lotus
module Action
# Manages HTTP headers and body
module Http

# Ensures that there's no Content-Type if the response code requires
# no body
def finish
super
if requires_no_body?
headers.delete(Mime::CONTENT_TYPE)
end
end

end
end
end
5 changes: 5 additions & 0 deletions lib/lotus/action/rack.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'securerandom'
require 'lotus/http/status'

module Lotus
module Action
Expand Down Expand Up @@ -93,6 +94,10 @@ def use(middleware)

protected

def requires_no_body?
Lotus::Http::Status.requires_no_body?(@_status)
end

# Gets the headers from the response
#
# @return [Hash] the HTTP headers from the response
Expand Down
2 changes: 1 addition & 1 deletion lib/lotus/action/throwable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def handle_exception(exception)
# @see Lotus::Action::Throwable#handle_exception
# @see Lotus::Http::Status:ALL
def halt(code = nil)
status(*Http::Status.for_code(code)) if code
status(*Lotus::Http::Status.for_code(code)) if code
throw :halt
end

Expand Down
16 changes: 16 additions & 0 deletions lib/lotus/http/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class Status
599 => 'Network connect timeout error'
}).freeze

# Status codes that by RFC must not include a message body
#
# @api private
WITHOUT_BODY = Set.new((100..199).to_a << 204 << 205 << 301 << 302 << 304).freeze

# Return a status for the given code
#
# @param code [Fixnum] a valid HTTP code
Expand All @@ -45,6 +50,17 @@ class Status
def self.for_code(code)
ALL.assoc(code)
end

# Checks if the given code by RFC must not include a message body
#
# @param code [Fixnum] a valid HTTP code
# @return [Boolean] true if the code requires no body
#
# @since 0.1.0
# @api private
def self.requires_no_body?(code)
WITHOUT_BODY.include?(code)
end
end
end
end
18 changes: 17 additions & 1 deletion test/integration/mime_type_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
get '/configuration', to: 'mimes#configuration'
get '/accept', to: 'mimes#accept'
get '/restricted', to: 'mimes#restricted'
get '/latin', to: 'mimes#latin'
get '/latin', to: 'mimes#latin'
get '/nocontent', to: 'mimes#no_content'
end

module Mimes
Expand Down Expand Up @@ -71,6 +72,14 @@ class Restricted
def call(params)
end
end

class NoContent
include Lotus::Action

def call(params)
self.status = 204
end
end
end

describe 'Content type' do
Expand Down Expand Up @@ -102,6 +111,13 @@ def call(params)
response.body.must_equal 'html'
end

it 'does not produce a "Content-Type" header when the request has a 204 No Content status' do
response = @app.get('/nocontent')
response.headers['Content-Type'].must_be_nil
response.body.must_equal ''
end


describe 'when Accept is sent' do
it 'sets "Content-Type" header according to "Accept"' do
response = @app.get('/', 'HTTP_ACCEPT' => '*/*')
Expand Down
2 changes: 1 addition & 1 deletion test/redirect_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
response = action.call({})

response[0].must_equal(302)
response[1].must_equal({ 'Content-Type' => 'application/octet-stream; charset=utf-8', 'Location' => '/destination' })
response[1].must_equal({ 'Location' => '/destination' })
end

it 'redirects with custom status code' do
Expand Down