2009-02-03 19:50:22 -05:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2009-01-19 20:58:26 -05:00
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class ResponseTest < Minitest::Test
|
Add default_content_type setting
Historically, Sinatra::Response defaults to a text/html Content-Type.
However, in practice, Sinatra immediately clears this attribute after
instantiating a new Sinatra::Response object, so this default is some-
what suspect. Let's break this behavior and have Sinatra::Response
be Content-Type-less by default, and update the tests to reflect this.
Next, let's introduce a new default_content_type setting that will be
applied (instead of text/html) if the Content-Type is not set before the
response is finalized. This will be great for e.g. JSON API developers.
Let's also make it nil-able to indicate that a default Content-Type
should never be applied.
Wherever Sinatra is emitting HTML, e.g. in error pages, force the
Content-Type to text/html.
Finally, clean up the error-handling logic to behave as follows:
* Set the X-Cascade header as early as possible.
* If an error block matches and returns a value, stop processing and
return that value.
* If we have a not found or bad request error, inspect the exception (if
any) for an error message and present it as the response body if it
exists, or present a default error message.
The remaining logic is the same otherwise. This should make error
handlers simpler to write and behave more consistently by not polluting
the body with a default message when none may be necessary.
2017-01-23 17:36:00 -05:00
|
|
|
setup { @response = Sinatra::Response.new([], 200, { 'Content-Type' => 'text/html' }) }
|
2009-01-19 20:58:26 -05:00
|
|
|
|
2012-12-12 16:27:03 -05:00
|
|
|
def assert_same_body(a, b)
|
2013-03-10 08:08:47 -04:00
|
|
|
assert_equal a.to_enum(:each).to_a, b.to_enum(:each).to_a
|
2012-12-12 16:27:03 -05:00
|
|
|
end
|
|
|
|
|
2009-01-19 20:58:26 -05:00
|
|
|
it "initializes with 200, text/html, and empty body" do
|
|
|
|
assert_equal 200, @response.status
|
|
|
|
assert_equal 'text/html', @response['Content-Type']
|
|
|
|
assert_equal [], @response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses case insensitive headers' do
|
|
|
|
@response['content-type'] = 'application/foo'
|
|
|
|
assert_equal 'application/foo', @response['Content-Type']
|
|
|
|
assert_equal 'application/foo', @response['CONTENT-TYPE']
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'writes to body' do
|
|
|
|
@response.body = 'Hello'
|
|
|
|
@response.write ' World'
|
2011-05-13 04:35:17 -04:00
|
|
|
assert_equal 'Hello World', @response.body.join
|
2009-01-19 20:58:26 -05:00
|
|
|
end
|
|
|
|
|
2018-02-21 11:02:05 -05:00
|
|
|
[204, 304].each do |status_code|
|
2009-01-19 20:58:26 -05:00
|
|
|
it "removes the Content-Type header and body when response status is #{status_code}" do
|
|
|
|
@response.status = status_code
|
|
|
|
@response.body = ['Hello World']
|
|
|
|
assert_equal [status_code, {}, []], @response.finish
|
|
|
|
end
|
|
|
|
end
|
2009-02-03 19:50:22 -05:00
|
|
|
|
2016-06-19 09:03:03 -04:00
|
|
|
[200, 201, 202, 301, 302, 400, 401, 403, 404, 500].each do |status_code|
|
|
|
|
it "will not removes the Content-Type header and body when response status
|
|
|
|
is #{status_code}" do
|
|
|
|
@response.status = status_code
|
|
|
|
@response.body = ['Hello World']
|
|
|
|
assert_equal [
|
|
|
|
status_code,
|
|
|
|
{ 'Content-Type' => 'text/html', 'Content-Length' => '11' },
|
|
|
|
['Hello World']
|
|
|
|
], @response.finish
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-03 19:50:22 -05:00
|
|
|
it 'Calculates the Content-Length using the bytesize of the body' do
|
|
|
|
@response.body = ['Hello', 'World!', '✈']
|
2013-07-20 15:30:36 -04:00
|
|
|
_, headers, body = @response.finish
|
2009-02-03 19:50:22 -05:00
|
|
|
assert_equal '14', headers['Content-Length']
|
2012-12-12 16:27:03 -05:00
|
|
|
assert_same_body @response.body, body
|
2009-02-03 19:50:22 -05:00
|
|
|
end
|
2011-05-13 05:00:05 -04:00
|
|
|
|
2011-07-12 04:41:26 -04:00
|
|
|
it 'does not call #to_ary or #inject on the body' do
|
|
|
|
object = Object.new
|
|
|
|
def object.inject(*) fail 'called' end
|
|
|
|
def object.to_ary(*) fail 'called' end
|
|
|
|
def object.each(*) end
|
|
|
|
@response.body = object
|
|
|
|
assert @response.finish
|
|
|
|
end
|
|
|
|
|
2011-05-13 05:00:05 -04:00
|
|
|
it 'does not nest a Sinatra::Response' do
|
|
|
|
@response.body = Sinatra::Response.new ["foo"]
|
2012-12-12 16:27:03 -05:00
|
|
|
assert_same_body @response.body, ["foo"]
|
2011-05-13 05:00:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not nest a Rack::Response' do
|
|
|
|
@response.body = Rack::Response.new ["foo"]
|
2012-12-12 16:27:03 -05:00
|
|
|
assert_same_body @response.body, ["foo"]
|
2011-05-13 05:00:05 -04:00
|
|
|
end
|
2009-01-19 20:58:26 -05:00
|
|
|
end
|