Skip to content

Commit

Permalink
const.rb - Update Puma::HTTP_STATUS_CODES (#3162)
Browse files Browse the repository at this point in the history
* const.rb - Update Puma::HTTP_STATUS_CODES

Used code included in comments

* Update test_puma_server.rb
  • Loading branch information
MSP-Greg committed May 23, 2023
1 parent d244dc7 commit 2bb9143
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
9 changes: 5 additions & 4 deletions lib/puma/const.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class UnsupportedOption < RuntimeError
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
103 => 'Early Hints',
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
Expand Down Expand Up @@ -49,16 +50,16 @@ class UnsupportedOption < RuntimeError
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Payload Too Large',
413 => 'Content Too Large',
414 => 'URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Range Not Satisfiable',
417 => 'Expectation Failed',
418 => 'I\'m A Teapot',
421 => 'Misdirected Request',
422 => 'Unprocessable Entity',
422 => 'Unprocessable Content',
423 => 'Locked',
424 => 'Failed Dependency',
425 => 'Too Early',
426 => 'Upgrade Required',
428 => 'Precondition Required',
429 => 'Too Many Requests',
Expand All @@ -73,7 +74,7 @@ class UnsupportedOption < RuntimeError
506 => 'Variant Also Negotiates',
507 => 'Insufficient Storage',
508 => 'Loop Detected',
510 => 'Not Extended',
510 => 'Not Extended (OBSOLETED)',
511 => 'Network Authentication Required'
}.freeze

Expand Down
32 changes: 22 additions & 10 deletions test/test_puma_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def message; "no backtrace error"; end
class TestPumaServer < Minitest::Test
parallelize_me!

STATUS_CODES = ::Puma::HTTP_STATUS_CODES

def setup
@host = "127.0.0.1"

Expand Down Expand Up @@ -387,7 +389,8 @@ def test_request_payload_too_large

data = sock.gets

assert_equal "HTTP/1.1 413 Payload Too Large\r\n", data
# Content Too Large
assert_equal "HTTP/1.1 413 #{STATUS_CODES[413]}\r\n", data
end

def test_http_11_keep_alive_with_large_payload
Expand All @@ -397,7 +400,8 @@ def test_http_11_keep_alive_with_large_payload
sock << "hello world foo bar"
h = header sock

assert_equal ["HTTP/1.1 413 Payload Too Large", "Content-Length: 17"], h
# Content Too Large
assert_equal ["HTTP/1.1 413 #{STATUS_CODES[413]}", "Content-Length: 17"], h

end

Expand Down Expand Up @@ -478,7 +482,8 @@ def test_lowlevel_error_message

data = send_http_and_sysread "GET / HTTP/1.0\r\n\r\n"

assert_includes data, 'HTTP/1.0 500 Internal Server Error'
# Internal Server Error
assert_includes data, "HTTP/1.0 500 #{STATUS_CODES[500]}"
assert_match(/Puma caught this error: Oh no an error.*\(NoMethodError\).*test\/test_puma_server.rb/m, data)
end

Expand All @@ -488,7 +493,8 @@ def test_lowlevel_error_message_without_backtrace
end

data = send_http_and_sysread "GET / HTTP/1.1\r\n\r\n"
assert_includes data, 'HTTP/1.1 500 Internal Server Error'
# Internal Server Error
assert_includes data, "HTTP/1.1 500 #{STATUS_CODES[500]}"
assert_includes data, 'Puma caught this error: no backtrace error (WithoutBacktraceError)'
assert_includes data, '<no backtrace available>'
end
Expand Down Expand Up @@ -591,7 +597,8 @@ def test_timeout_in_data_phase(**options)

data = sock.gets

assert_equal "HTTP/1.1 408 Request Timeout\r\n", data
# Request Timeout
assert_equal "HTTP/1.1 408 #{STATUS_CODES[408]}\r\n", data
end

def test_timeout_data_no_queue
Expand Down Expand Up @@ -652,7 +659,8 @@ def test_http_11_keep_alive_without_body

h = header sock

assert_equal ["HTTP/1.1 204 No Content"], h
# No Content
assert_equal ["HTTP/1.1 204 #{STATUS_CODES[204]}"], h
end

def test_http_11_close_without_body
Expand All @@ -662,7 +670,8 @@ def test_http_11_close_without_body

h = header sock

assert_equal ["HTTP/1.1 204 No Content", "Connection: close"], h
# No Content
assert_equal ["HTTP/1.1 204 #{STATUS_CODES[204]}", "Connection: close"], h
end

def test_http_10_keep_alive_with_body
Expand Down Expand Up @@ -1547,21 +1556,24 @@ def test_empty_body_array_content_length_0
server_run { |env| [404, {'Content-Length' => '0'}, []] }

resp = send_http_and_sysread "GET / HTTP/1.1\r\n\r\n"
assert_equal "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n", resp
# Not Found
assert_equal "HTTP/1.1 404 #{STATUS_CODES[404]}\r\nContent-Length: 0\r\n\r\n", resp
end

def test_empty_body_array_no_content_length
server_run { |env| [404, {}, []] }

resp = send_http_and_sysread "GET / HTTP/1.1\r\n\r\n"
assert_equal "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n", resp
# Not Found
assert_equal "HTTP/1.1 404 #{STATUS_CODES[404]}\r\nContent-Length: 0\r\n\r\n", resp
end

def test_empty_body_enum
server_run { |env| [404, {}, [].to_enum] }

resp = send_http_and_sysread "GET / HTTP/1.1\r\n\r\n"
assert_equal "HTTP/1.1 404 Not Found\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\n", resp
# Not Found
assert_equal "HTTP/1.1 404 #{STATUS_CODES[404]}\r\nTransfer-Encoding: chunked\r\n\r\n0\r\n\r\n", resp
end

def test_form_data_encoding_windows_bom
Expand Down

0 comments on commit 2bb9143

Please sign in to comment.