PageRenderTime 52ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_rack_handler.rb

https://github.com/puma/puma
Ruby | 270 lines | 211 code | 58 blank | 1 comment | 2 complexity | 28c8447d536a9ea8a1a09f2e5d654f6a MD5 | raw file
  1. require_relative "helper"
  2. require "rack/handler/puma"
  3. class TestHandlerGetStrSym < Minitest::Test
  4. def test_handler
  5. handler = Rack::Handler.get(:puma)
  6. assert_equal Rack::Handler::Puma, handler
  7. handler = Rack::Handler.get('Puma')
  8. assert_equal Rack::Handler::Puma, handler
  9. end
  10. end
  11. class TestPathHandler < Minitest::Test
  12. def app
  13. Proc.new {|env| @input = env; [200, {}, ["hello world"]]}
  14. end
  15. def setup
  16. @input = nil
  17. end
  18. def in_handler(app, options = {})
  19. options[:Port] ||= 0
  20. options[:Silent] = true
  21. @launcher = nil
  22. thread = Thread.new do
  23. Rack::Handler::Puma.run(app, **options) do |s, p|
  24. @launcher = s
  25. end
  26. end
  27. # Wait for launcher to boot
  28. Timeout.timeout(10) do
  29. sleep 0.5 until @launcher
  30. end
  31. sleep 1.5 unless Puma::IS_MRI
  32. yield @launcher
  33. ensure
  34. @launcher.stop if @launcher
  35. thread.join if thread
  36. end
  37. def test_handler_boots
  38. host = '127.0.0.1'
  39. port = UniquePort.call
  40. opts = { Host: host, Port: port }
  41. in_handler(app, opts) do |launcher|
  42. hit(["http://#{host}:#{port}/test"])
  43. assert_equal("/test", @input["PATH_INFO"])
  44. end
  45. end
  46. end
  47. class TestUserSuppliedOptionsPortIsSet < Minitest::Test
  48. def setup
  49. @options = {}
  50. @options[:user_supplied_options] = [:Port]
  51. end
  52. def test_port_wins_over_config
  53. user_port = 5001
  54. file_port = 6001
  55. Dir.mktmpdir do |d|
  56. Dir.chdir(d) do
  57. FileUtils.mkdir("config")
  58. File.open("config/puma.rb", "w") { |f| f << "port #{file_port}" }
  59. @options[:Port] = user_port
  60. conf = Rack::Handler::Puma.config(->{}, @options)
  61. conf.load
  62. assert_equal ["tcp://0.0.0.0:#{user_port}"], conf.options[:binds]
  63. end
  64. end
  65. end
  66. end
  67. class TestUserSuppliedOptionsHostIsSet < Minitest::Test
  68. def setup
  69. @options = {}
  70. @options[:user_supplied_options] = [:Host]
  71. end
  72. def test_host_uses_supplied_port_default
  73. user_port = rand(1000..9999)
  74. user_host = "123.456.789"
  75. @options[:Host] = user_host
  76. @options[:Port] = user_port
  77. conf = Rack::Handler::Puma.config(->{}, @options)
  78. conf.load
  79. assert_equal ["tcp://#{user_host}:#{user_port}"], conf.options[:binds]
  80. end
  81. def test_ipv6_host_supplied_port_default
  82. @options[:Host] = "::1"
  83. conf = Rack::Handler::Puma.config(->{}, @options)
  84. conf.load
  85. assert_equal ["tcp://[::1]:9292"], conf.options[:binds]
  86. end
  87. end
  88. class TestUserSuppliedOptionsIsEmpty < Minitest::Test
  89. def setup
  90. @options = {}
  91. @options[:user_supplied_options] = []
  92. end
  93. def test_config_file_wins_over_port
  94. user_port = 5001
  95. file_port = 6001
  96. Dir.mktmpdir do |d|
  97. Dir.chdir(d) do
  98. FileUtils.mkdir("config")
  99. File.open("config/puma.rb", "w") { |f| f << "port #{file_port}" }
  100. @options[:Port] = user_port
  101. conf = Rack::Handler::Puma.config(->{}, @options)
  102. conf.load
  103. assert_equal ["tcp://0.0.0.0:#{file_port}"], conf.options[:binds]
  104. end
  105. end
  106. end
  107. def test_default_host_when_using_config_file
  108. user_port = 5001
  109. file_port = 6001
  110. Dir.mktmpdir do |d|
  111. Dir.chdir(d) do
  112. FileUtils.mkdir("config")
  113. File.open("config/puma.rb", "w") { |f| f << "port #{file_port}" }
  114. @options[:Host] = "localhost"
  115. @options[:Port] = user_port
  116. conf = Rack::Handler::Puma.config(->{}, @options)
  117. conf.load
  118. assert_equal ["tcp://localhost:#{file_port}"], conf.options[:binds]
  119. end
  120. end
  121. end
  122. def test_default_host_when_using_config_file_with_explicit_host
  123. user_port = 5001
  124. file_port = 6001
  125. Dir.mktmpdir do |d|
  126. Dir.chdir(d) do
  127. FileUtils.mkdir("config")
  128. File.open("config/puma.rb", "w") { |f| f << "port #{file_port}, '1.2.3.4'" }
  129. @options[:Host] = "localhost"
  130. @options[:Port] = user_port
  131. conf = Rack::Handler::Puma.config(->{}, @options)
  132. conf.load
  133. assert_equal ["tcp://1.2.3.4:#{file_port}"], conf.options[:binds]
  134. end
  135. end
  136. end
  137. end
  138. class TestUserSuppliedOptionsIsNotPresent < Minitest::Test
  139. def setup
  140. @options = {}
  141. end
  142. def test_default_port_when_no_config_file
  143. conf = Rack::Handler::Puma.config(->{}, @options)
  144. conf.load
  145. assert_equal ["tcp://0.0.0.0:9292"], conf.options[:binds]
  146. end
  147. def test_config_wins_over_default
  148. file_port = 6001
  149. Dir.mktmpdir do |d|
  150. Dir.chdir(d) do
  151. FileUtils.mkdir("config")
  152. File.open("config/puma.rb", "w") { |f| f << "port #{file_port}" }
  153. conf = Rack::Handler::Puma.config(->{}, @options)
  154. conf.load
  155. assert_equal ["tcp://0.0.0.0:#{file_port}"], conf.options[:binds]
  156. end
  157. end
  158. end
  159. def test_user_port_wins_over_default_when_user_supplied_is_blank
  160. user_port = 5001
  161. @options[:user_supplied_options] = []
  162. @options[:Port] = user_port
  163. conf = Rack::Handler::Puma.config(->{}, @options)
  164. conf.load
  165. assert_equal ["tcp://0.0.0.0:#{user_port}"], conf.options[:binds]
  166. end
  167. def test_user_port_wins_over_default
  168. user_port = 5001
  169. @options[:Port] = user_port
  170. conf = Rack::Handler::Puma.config(->{}, @options)
  171. conf.load
  172. assert_equal ["tcp://0.0.0.0:#{user_port}"], conf.options[:binds]
  173. end
  174. def test_user_port_wins_over_config
  175. user_port = 5001
  176. file_port = 6001
  177. Dir.mktmpdir do |d|
  178. Dir.chdir(d) do
  179. FileUtils.mkdir("config")
  180. File.open("config/puma.rb", "w") { |f| f << "port #{file_port}" }
  181. @options[:Port] = user_port
  182. conf = Rack::Handler::Puma.config(->{}, @options)
  183. conf.load
  184. assert_equal ["tcp://0.0.0.0:#{user_port}"], conf.options[:binds]
  185. end
  186. end
  187. end
  188. def test_default_log_request_when_no_config_file
  189. conf = Rack::Handler::Puma.config(->{}, @options)
  190. conf.load
  191. assert_equal false, conf.options[:log_requests]
  192. end
  193. def test_file_log_requests_wins_over_default_config
  194. file_log_requests_config = true
  195. @options[:config_files] = [
  196. 'test/config/t1_conf.rb'
  197. ]
  198. conf = Rack::Handler::Puma.config(->{}, @options)
  199. conf.load
  200. assert_equal file_log_requests_config, conf.options[:log_requests]
  201. end
  202. def test_user_log_requests_wins_over_file_config
  203. user_log_requests_config = false
  204. @options[:log_requests] = user_log_requests_config
  205. @options[:config_files] = [
  206. 'test/config/t1_conf.rb'
  207. ]
  208. conf = Rack::Handler::Puma.config(->{}, @options)
  209. conf.load
  210. assert_equal user_log_requests_config, conf.options[:log_requests]
  211. end
  212. end