diff --git a/lib/net/http/responses.rb b/lib/net/http/responses.rb index bc7642c..a17c859 100644 --- a/lib/net/http/responses.rb +++ b/lib/net/http/responses.rb @@ -267,5 +267,68 @@ class Net::HTTPResponse } end -# :startdoc: +class Net::HTTPResponse + def continue?; code == "100".freeze end + def switch_protocol?; code == "101".freeze end + + def ok?; code == "200".freeze end + def created?; code == "201".freeze end + def accepted?; code == "202".freeze end + def non_authoritative_information?; code == "203".freeze end + def no_content?; code == "204".freeze end + def reset_content?; code == "205".freeze end + def partial_content?; code == "206".freeze end + def multi_status?; code == "207".freeze end + def im_used?; code == "226".freeze end + + def multiple_choices?; code == "300".freeze end + def moved_permanently?; code == "301".freeze end + def found?; code == "302".freeze end + def see_other?; code == "303".freeze end + def not_modified?; code == "304".freeze end + def use_proxy?; code == "305".freeze end + def temporary_redirect?; code == "307".freeze end + + def bad_request?; code == "400".freeze end + def unauthorized?; code == "401".freeze end + def payment_required?; code == "402".freeze end + def forbidden?; code == "403".freeze end + def not_found?; code == "404".freeze end + def method_not_allowed?; code == "405".freeze end + def not_acceptable?; code == "406".freeze end + def proxy_authentication_required?; code == "407".freeze end + def request_timeout?; code == "408".freeze end + def conflict?; code == "409".freeze end + def gone?; code == "410".freeze end + def length_required?; code == "411".freeze end + def precondition_failed?; code == "412".freeze end + def request_entity_too_large?; code == "413".freeze end + def request_uri_too_long?; code == "414".freeze end + def unsupported_media_type?; code == "415".freeze end + def requested_range_not_satisfiable?; code == "416".freeze end + def expectation_failed?; code == "417".freeze end + def unprocessable_entity?; code == "422".freeze end + def locked?; code == "423".freeze end + def failed_dependency?; code == "424".freeze end + def upgrade_required?; code == "426".freeze end + def precondition_required?; code == "428".freeze end + def too_many_requests?; code == "429".freeze end + def request_header_fields_too_large?; code == "431".freeze end + def internal_server_error?; code == "500".freeze end + def not_implemented?; code == "501".freeze end + def bad_gateway?; code == "502".freeze end + def service_unavailable?; code == "503".freeze end + def gateway_timeout?; code == "504".freeze end + def version_not_supported?; code == "505".freeze end + def insufficient_storage?; code == "507".freeze end + def network_authentication_required?; code == "511".freeze end + + def informational?; code.to_i.between?(100, 199) end + def successful?; code.to_i.between?(200, 299) end + def redirection?; code.to_i.between?(300, 399) end + def client_error?; code.to_i.between?(400, 499) end + def server_error?; code.to_i.between?(500, 599) end +end + +# :startdoc: diff --git a/test/net/http/test_httpresponses.rb b/test/net/http/test_httpresponses.rb index bf7fbee..8ee0008 100644 --- a/test/net/http/test_httpresponses.rb +++ b/test/net/http/test_httpresponses.rb @@ -21,4 +21,24 @@ class HTTPResponsesTest < Test::Unit::TestCase assert(klass < group, "#{klass.name} (#{code}) must inherit from #{group.name}") } end + + def test_status_code_methods + response = Net::HTTPResponse.new('1.1', '200', 'OK') + + assert response.ok? + assert response.successful? + assert !response.informational? + assert !response.redirection? + assert !response.client_error? + assert !response.server_error? + + response = Net::HTTPResponse.new('1.1', '400', 'Bad Request') + + assert response.bad_request? + assert response.client_error? + assert !response.informational? + assert !response.successful? + assert !response.redirection? + assert !response.server_error? + end end