2023-02-12 18:09:35 +00:00
|
|
|
require_relative 'test_helper'
|
2008-12-13 21:06:02 +00:00
|
|
|
|
2019-02-01 03:32:45 +00:00
|
|
|
class ThirdPartyError < RuntimeError
|
|
|
|
def http_status; 400 end
|
|
|
|
end
|
|
|
|
|
2015-01-10 19:30:47 +00:00
|
|
|
class ResultTest < Minitest::Test
|
2008-12-13 21:06:02 +00:00
|
|
|
it "sets response.body when result is a String" do
|
2012-05-21 21:21:59 +00:00
|
|
|
mock_app { get('/') { 'Hello World' } }
|
2008-12-13 21:06:02 +00:00
|
|
|
|
|
|
|
get '/'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets response.body when result is an Array of Strings" do
|
2012-05-21 21:21:59 +00:00
|
|
|
mock_app { get('/') { ['Hello', 'World'] } }
|
2008-12-13 21:06:02 +00:00
|
|
|
|
|
|
|
get '/'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'HelloWorld', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets response.body when result responds to #each" do
|
2012-05-21 21:21:59 +00:00
|
|
|
mock_app do
|
|
|
|
get('/') do
|
2008-12-13 21:06:02 +00:00
|
|
|
res = lambda { 'Hello World' }
|
|
|
|
def res.each ; yield call ; end
|
2012-05-21 21:21:59 +00:00
|
|
|
return res
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
2012-05-21 21:21:59 +00:00
|
|
|
end
|
2008-12-13 21:06:02 +00:00
|
|
|
|
|
|
|
get '/'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets response.body to [] when result is nil" do
|
2012-05-21 21:21:59 +00:00
|
|
|
mock_app { get( '/') { nil } }
|
2008-12-13 21:06:02 +00:00
|
|
|
|
|
|
|
get '/'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert ok?
|
|
|
|
assert_equal '', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets status, headers, and body when result is a Rack response tuple" do
|
|
|
|
mock_app {
|
2012-05-21 21:21:59 +00:00
|
|
|
get('/') { [203, {'Content-Type' => 'foo/bar'}, 'Hello World'] }
|
2008-12-13 21:06:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get '/'
|
2011-08-17 12:02:05 +00:00
|
|
|
assert_equal 203, status
|
2009-01-14 22:00:26 +00:00
|
|
|
assert_equal 'foo/bar', response['Content-Type']
|
|
|
|
assert_equal 'Hello World', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
|
|
|
|
2008-12-21 10:11:25 +00:00
|
|
|
it "sets status and body when result is a two-tuple" do
|
2012-05-21 21:21:59 +00:00
|
|
|
mock_app { get('/') { [409, 'formula of'] } }
|
2008-12-21 10:11:25 +00:00
|
|
|
|
|
|
|
get '/'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert_equal 409, status
|
|
|
|
assert_equal 'formula of', body
|
2008-12-21 10:11:25 +00:00
|
|
|
end
|
|
|
|
|
2011-06-04 16:24:42 +00:00
|
|
|
it "raises a ArgumentError when result is a non two or three tuple Array" do
|
2009-01-20 01:58:26 +00:00
|
|
|
mock_app {
|
2012-05-21 21:21:59 +00:00
|
|
|
get('/') { [409, 'formula of', 'something else', 'even more'] }
|
2009-01-20 01:58:26 +00:00
|
|
|
}
|
|
|
|
|
2015-01-10 19:30:47 +00:00
|
|
|
assert_raises(ArgumentError) { get '/' }
|
2009-01-20 01:58:26 +00:00
|
|
|
end
|
|
|
|
|
2018-11-07 08:00:02 +00:00
|
|
|
it "sets status when result is a Integer status code" do
|
2012-05-21 21:21:59 +00:00
|
|
|
mock_app { get('/') { 205 } }
|
2008-12-13 21:06:02 +00:00
|
|
|
|
|
|
|
get '/'
|
2009-01-14 22:00:26 +00:00
|
|
|
assert_equal 205, status
|
|
|
|
assert_equal '', body
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|
2019-02-01 03:32:45 +00:00
|
|
|
|
|
|
|
it "sets status to 500 when raised error is not Sinatra::Error" do
|
|
|
|
mock_app do
|
|
|
|
set :raise_errors, false
|
|
|
|
get('/') { raise ThirdPartyError }
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/'
|
|
|
|
assert_equal 500, status
|
|
|
|
assert_equal '<h1>Internal Server Error</h1>', body
|
|
|
|
end
|
2008-12-13 21:06:02 +00:00
|
|
|
end
|