Project

General

Profile

Feature #9020 ยป patch.diff

timcraft (Tim Craft), 12/23/2013 11:26 PM

View differences:

lib/net/http/responses.rb
267 267
  }
268 268
end
269 269

  
270
# :startdoc:
270
class Net::HTTPResponse
271
  def continue?; code == "100".freeze end
272
  def switch_protocol?; code == "101".freeze end
273

  
274
  def ok?; code == "200".freeze end
275
  def created?; code == "201".freeze end
276
  def accepted?; code == "202".freeze end
277
  def non_authoritative_information?; code == "203".freeze end
278
  def no_content?; code == "204".freeze end
279
  def reset_content?; code == "205".freeze end
280
  def partial_content?; code == "206".freeze end
281
  def multi_status?; code == "207".freeze end
282
  def im_used?; code == "226".freeze end
283

  
284
  def multiple_choices?; code == "300".freeze end
285
  def moved_permanently?; code == "301".freeze end
286
  def found?; code == "302".freeze end
287
  def see_other?; code == "303".freeze end
288
  def not_modified?; code == "304".freeze end
289
  def use_proxy?; code == "305".freeze end
290
  def temporary_redirect?; code == "307".freeze end
291

  
292
  def bad_request?; code == "400".freeze end
293
  def unauthorized?; code == "401".freeze end
294
  def payment_required?; code == "402".freeze end
295
  def forbidden?; code == "403".freeze end
296
  def not_found?; code == "404".freeze end
297
  def method_not_allowed?; code == "405".freeze end
298
  def not_acceptable?; code == "406".freeze end
299
  def proxy_authentication_required?; code == "407".freeze end
300
  def request_timeout?; code == "408".freeze end
301
  def conflict?; code == "409".freeze end
302
  def gone?; code == "410".freeze end
303
  def length_required?; code == "411".freeze end
304
  def precondition_failed?; code == "412".freeze end
305
  def request_entity_too_large?; code == "413".freeze end
306
  def request_uri_too_long?; code == "414".freeze end
307
  def unsupported_media_type?; code == "415".freeze end
308
  def requested_range_not_satisfiable?; code == "416".freeze end
309
  def expectation_failed?; code == "417".freeze end
310
  def unprocessable_entity?; code == "422".freeze end
311
  def locked?; code == "423".freeze end
312
  def failed_dependency?; code == "424".freeze end
313
  def upgrade_required?; code == "426".freeze end
314
  def precondition_required?; code == "428".freeze end
315
  def too_many_requests?; code == "429".freeze end
316
  def request_header_fields_too_large?; code == "431".freeze end
271 317

  
318
  def internal_server_error?; code == "500".freeze end
319
  def not_implemented?; code == "501".freeze end
320
  def bad_gateway?; code == "502".freeze end
321
  def service_unavailable?; code == "503".freeze end
322
  def gateway_timeout?; code == "504".freeze end
323
  def version_not_supported?; code == "505".freeze end
324
  def insufficient_storage?; code == "507".freeze end
325
  def network_authentication_required?; code == "511".freeze end
326

  
327
  def informational?; code.to_i.between?(100, 199) end
328
  def successful?; code.to_i.between?(200, 299) end
329
  def redirection?; code.to_i.between?(300, 399) end
330
  def client_error?; code.to_i.between?(400, 499) end
331
  def server_error?; code.to_i.between?(500, 599) end
332
end
333

  
334
# :startdoc:
test/net/http/test_httpresponses.rb
21 21
      assert(klass < group, "#{klass.name} (#{code}) must inherit from #{group.name}")
22 22
    }
23 23
  end
24

  
25
  def test_status_code_methods
26
    response = Net::HTTPResponse.new('1.1', '200', 'OK')
27

  
28
    assert response.ok?
29
    assert response.successful?
30
    assert !response.informational?
31
    assert !response.redirection?
32
    assert !response.client_error?
33
    assert !response.server_error?
34

  
35
    response = Net::HTTPResponse.new('1.1', '400', 'Bad Request')
36

  
37
    assert response.bad_request?
38
    assert response.client_error?
39
    assert !response.informational?
40
    assert !response.successful?
41
    assert !response.redirection?
42
    assert !response.server_error?
43
  end
24 44
end