Announcing ‘responsalizr’ – test HTTP Response Codes in Ruby

Get it here:

gem install responsalizr

Responsalizr is a very small and very simple gem I’ve written that extends the standard ruby Net::HTTPResponse class with a few methods to allow for idiomatic testing of HTTP response codes. The methods added are:

  1. A numeric response code comparer:
    • code?(integer) #eg: code?(200), code?(404)
  2. Response type name comparers:
    • ok? – corresponds to code 200
    • not_found? – corresponds to code 404
    • moved_permanently? – corresponds to a 301
    • etc…

    The response type name compare methods are derived from the subclasses of Net::HTTPResponse; you can find a table with their names at the bottom of this post.

Here is an example rspec script that demonstrates what you can do with responsalizr. Each test does the same thing twice; once doing a numeric response code compare, the other doing a response type name compare:


require 'rubygems'
require 'responsalizr'

include Responsalizr

describe "Google" do
  it "should respond to the uk homepage with a 200 or an ok" do
    Response.from("http://www.google.co.uk").should be_ok
    Response.from("http://www.google.co.uk").should be_a_code 200
  end

  it "should respond with a 404 or not found" do
    Response.from("http://www.google.com/bing.aspx").should be_not_found
    Response.from("http://www.google.com/bing.aspx").should be_a_code 404
  end

  it "should respond with a 302 or a moved permanently" do
    Response.from("http://finance.google.com").should be_moved_permanently
    Response.from("http://finance.google.com").should be_a_code 301
  end

  it "should be testable through a proxy" do
    Response.from("http://www.google.co.uk", {:proxy_host => "63.223.106.54", :port => 80}).should be_ok
    Response.from("http://www.google.co.uk", {:proxy_host => "63.223.106.54", :port => 80}).should be_a_code(200)
  end
end

Chose whichever way you like (codes/names) and – I’ve only used both above for demonstration purposes.

So, if all you’re doing is testing HTTP responses for their code (whether you want to do that with codes – 404 – or names – not_found) then responsalizr is the thing for you.

About the specific response name methods and their respective codes, here’s a table that summarizes them:

Response Type Name Response Code
information? 1xx
continue? 100
switch_protocol? 101
success? 2xx
ok? 200
created? 201
accepted? 202
non_authoritative_information? 203
no_content? 204
reset_content? 205
partial_content? 206
redirection? 3xx
multiple_choice? 300
moved_permanently? 301
found? 302
see_other? 303
not_modified? 304
use_proxy? 305
temporary_redirect? 307
client_error? 4xx
bad_request? 400
unauthorized? 401
payment_required? 402
forbidden? 403
not_found? 404
method_not_allowed? 405
not_acceptable? 406
proxy_authentication_required? 407
request_time_out? 408
conflict? 409
gone? 410
length_required? 411
precondition_failed? 412
request_entity_too_large? 413
request_uri_too_long? 414
unsupported_media_type? 415
requested_range_not_satisfiable? 416
expectation_failed? 417
server_error? 5xx
internal_server_error? 500
not_implemented? 501
bad_gateway? 502
service_unavailable? 503
gateway_time_out? 504
version_not_supported? 505
unknown_response? xxx

Because of the rspec (and cucumber) predicate magic, you can use, eg, the bad_gateway?, the forbidden? and the internal_server_error? method as follows:


...should be_a_bad_gateway
...should be_forbidden
...should be_an_internal_server_error

Happy HTTP Response Code testing!