Permalink
Browse files

Added support for simpler matchers when in Rails and bumped to 0.0.2

  • Loading branch information...
1 parent c60c6d3 commit e58a3026e7460e759d9279b560cc45ecaf2c5abb @kaiwren kaiwren committed Apr 21, 2011
View
@@ -1,2 +1,3 @@
*.rbc
+*.gem
Gemfile.lock
View
@@ -1,4 +1,6 @@
-= RSpec HTTP
+= RSpec HTTP 0.0.2
+
+(c) Copyright 2010-2011 {C42 Engineering}[http://c42.in]. All Rights Reserved.
RSpec HTTP is a RSpec extension library that adds support for writing specs that cover HTTP based API (or more popularly, RESTful APIs).
@@ -16,4 +18,16 @@ This will make matchers such as the ones listed below available to you in your s
response.should be_http_unprocessable_entity
- response.should be_http_im_a_teapot
+ response.should be_http_im_a_teapot
+
+== Rails
+
+If you're using Rails (and implicitly, rspec-rails), the same matchers will also be available in your controller specs '''without''' the <code>http</code> namespace. In other words, in your controller specs you can do:
+
+response.should be_ok
+
+response.should be_created
+
+response.should be_unprocessable_entity
+
+response.should be_im_a_teapot
View
@@ -1,8 +1,11 @@
require 'rspec/core'
require 'rspec/http/status_codes'
+require 'rspec/http/response_code_matcher'
require 'rspec/http/response_code_matchers'
+require 'rspec/http/rails'# if Kernel.const_defined?('Rails')
+
RSpec::configure do |config|
config.include(RSpec::Http::ResponseCodeMatchers)
end
@@ -0,0 +1,2 @@
+require 'rspec/http/rails/response_code_matchers'
+require 'rspec/http/rails/controller_example_group'
@@ -0,0 +1,7 @@
+module RSpec
+ module Rails
+ module ControllerExampleGroup
+ include RSpec::Rails::ResponseCodeMatchers
+ end
+ end
+end
@@ -0,0 +1,11 @@
+module RSpec
+ module Rails
+ module ResponseCodeMatchers
+ RSpec::Http::StatusCodes.values.each do |code, status|
+ define_method("be_#{Http::StatusCodes.as_valid_method_name(code)}") do
+ RSpec::Http::ResponseCodeMatcher.new(code)
+ end
+ end
+ end
+ end
+end
@@ -0,0 +1,34 @@
+module RSpec
+ module Http
+ class ResponseCodeMatcher
+ def initialize(expected_code)
+ @expected_code = expected_code
+ end
+
+ def matches?(target)
+ @target = target
+ @target.code.to_i == @expected_code
+ end
+
+ def description
+ "Response code should be #{@expected_code}"
+ end
+
+ def failure_message
+ "Expected #{@target} to #{common_message}"
+ end
+
+ def negative_failure_message
+ "Expected #{@target} to not #{common_message}"
+ end
+
+ def common_message
+ message = "have a response code of #{@expected_code}, but got #{@target.code}"
+ if @target.code.to_i == 302 || @target.code.to_i == 201
+ message += " with a location of #{@target['Location'] || @target['location']}"
+ end
+ message
+ end
+ end
+ end
+end
@@ -1,51 +1,9 @@
module RSpec
module Http
module ResponseCodeMatchers
- def self.clean_up_status(message)
- message.gsub(/(\s|-)/, "_").gsub('\'', '').downcase.to_sym
- end
-
- def self.status_as_valid_method_name(look_up_code)
- (@status_codes ||= RSpec::Http::STATUS_CODES.inject({}) do |hash, (code, message)|
- hash[code] = clean_up_status(message)
- hash
- end.freeze)[look_up_code]
- end
-
- class HttpResponseCodeMatcher
- def initialize(expected_code)
- @expected_code = expected_code
- end
-
- def matches?(target)
- @target = target
- @target.code.to_i == @expected_code
- end
-
- def description
- "Response code should be #{@expected_code}"
- end
-
- def failure_message
- "Expected #{@target} to #{common_message}"
- end
-
- def negative_failure_message
- "Expected #{@target} to not #{common_message}"
- end
-
- def common_message
- message = "have a response code of #{@expected_code}, but got #{@target.code}"
- if @target.code.to_i == 302 || @target.code.to_i == 201
- message += " with a location of #{@target['Location'] || @target['location']}"
- end
- message
- end
- end
-
- RSpec::Http::STATUS_CODES.each do |code, status|
- define_method("be_http_#{status_as_valid_method_name(code)}") do
- HttpResponseCodeMatcher.new(code)
+ RSpec::Http::StatusCodes.values.each do |code, status|
+ define_method("be_http_#{Http::StatusCodes.as_valid_method_name(code)}") do
+ ResponseCodeMatcher.new(code)
end
end
end
@@ -1,59 +1,74 @@
module RSpec
module Http
- STATUS_CODES = {
- 100=>"Continue",
- 101=>"Switching Protocols",
- 102=>"Processing",
- 200=>"OK",
- 201=>"Created",
- 202=>"Accepted",
- 203=>"Non-Authoritative Information",
- 204=>"No Content",
- 205=>"Reset Content",
- 206=>"Partial Content",
- 207=>"Multi-Status",
- 226=>"IM Used",
- 300=>"Multiple Choices",
- 301=>"Moved Permanently",
- 302=>"Found",
- 303=>"See Other",
- 304=>"Not Modified",
- 305=>"Use Proxy",
- 306=>"Reserved",
- 307=>"Temporary Redirect",
- 400=>"Bad Request",
- 401=>"Unauthorized",
- 402=>"Payment Required",
- 403=>"Forbidden",
- 404=>"Not Found",
- 405=>"Method Not Allowed",
- 406=>"Not Acceptable",
- 407=>"Proxy Authentication Required",
- 408=>"Request Timeout",
- 409=>"Conflict",
- 410=>"Gone",
- 411=>"Length Required",
- 412=>"Precondition Failed",
- 413=>"Request Entity Too Large",
- 414=>"Request-URI Too Long",
- 415=>"Unsupported Media Type",
- 416=>"Requested Range Not Satisfiable",
- 417=>"Expectation Failed",
- 418 => "I'm A Teapot",
- 422=>"Unprocessable Entity",
- 423=>"Locked",
- 424=>"Failed Dependency",
- 426=>"Upgrade Required",
- 500=>"Internal Server Error",
- 501=>"Not Implemented",
- 502=>"Bad Gateway",
- 503=>"Service Unavailable",
- 504=>"Gateway Timeout",
- 505=>"HTTP Version Not Supported",
- 506=>"Variant Also Negotiates",
- 507=>"Insufficient Storage",
- 510=>"Not Extended"
- }.freeze
- end
+ module StatusCodes
+ def self.values
+ @values ||= {
+ 100=>"Continue",
+ 101=>"Switching Protocols",
+ 102=>"Processing",
+ 200=>"OK",
+ 201=>"Created",
+ 202=>"Accepted",
+ 203=>"Non-Authoritative Information",
+ 204=>"No Content",
+ 205=>"Reset Content",
+ 206=>"Partial Content",
+ 207=>"Multi-Status",
+ 226=>"IM Used",
+ 300=>"Multiple Choices",
+ 301=>"Moved Permanently",
+ 302=>"Found",
+ 303=>"See Other",
+ 304=>"Not Modified",
+ 305=>"Use Proxy",
+ 306=>"Reserved",
+ 307=>"Temporary Redirect",
+ 400=>"Bad Request",
+ 401=>"Unauthorized",
+ 402=>"Payment Required",
+ 403=>"Forbidden",
+ 404=>"Not Found",
+ 405=>"Method Not Allowed",
+ 406=>"Not Acceptable",
+ 407=>"Proxy Authentication Required",
+ 408=>"Request Timeout",
+ 409=>"Conflict",
+ 410=>"Gone",
+ 411=>"Length Required",
+ 412=>"Precondition Failed",
+ 413=>"Request Entity Too Large",
+ 414=>"Request-URI Too Long",
+ 415=>"Unsupported Media Type",
+ 416=>"Requested Range Not Satisfiable",
+ 417=>"Expectation Failed",
+ 418 => "I'm A Teapot",
+ 422=>"Unprocessable Entity",
+ 423=>"Locked",
+ 424=>"Failed Dependency",
+ 426=>"Upgrade Required",
+ 500=>"Internal Server Error",
+ 501=>"Not Implemented",
+ 502=>"Bad Gateway",
+ 503=>"Service Unavailable",
+ 504=>"Gateway Timeout",
+ 505=>"HTTP Version Not Supported",
+ 506=>"Variant Also Negotiates",
+ 507=>"Insufficient Storage",
+ 510=>"Not Extended"
+ }.freeze
+ end
+
+ def self.clean_up_status(message)
+ message.gsub(/(\s|-)/, "_").gsub('\'', '').downcase.to_sym
+ end
+
+ def self.as_valid_method_name(look_up_code)
+ (@status_codes ||= values.inject({}) do |hash, (code, message)|
+ hash[code] = clean_up_status(message)
+ hash
+ end.freeze)[look_up_code]
+ end
+ end
+ end
end
@@ -1,7 +1,7 @@
module RSpec # :nodoc:
module Http # :nodoc:
module Version # :nodoc:
- STRING = '0.0.1'
+ STRING = '0.0.2'
end
end
end
View
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
s.rubygems_version = "1.3.7"
- s.files = `git ls-files`.split("\n")
+ s.files = `git ls-files`.split("\n") - ['.gitignore']
s.test_files = `git ls-files -- {spec}/*`.split("\n")
s.extra_rdoc_files = [ "README.rdoc" ]
s.rdoc_options = ["--charset=UTF-8"]
@@ -8,32 +8,32 @@ module RSpec::Http
context 'status to matcher conversion' do
it "replaces spaces with underscores" do
- ResponseCodeMatchers::clean_up_status("Method Not Allowed").should eq(:method_not_allowed)
+ StatusCodes.clean_up_status("Method Not Allowed").should eq(:method_not_allowed)
end
it "downcases capital letters" do
- ResponseCodeMatchers::clean_up_status("IM Used").should eq(:im_used)
+ StatusCodes.clean_up_status("IM Used").should eq(:im_used)
end
it "removes apostrophes" do
- ResponseCodeMatchers::clean_up_status("I'm A Teapot").should eq(:im_a_teapot)
+ StatusCodes.clean_up_status("I'm A Teapot").should eq(:im_a_teapot)
end
it "replaces hyphens with underscores" do
- ResponseCodeMatchers::clean_up_status("Non-Authoritative Information").should eq(:non_authoritative_information)
+ StatusCodes.clean_up_status("Non-Authoritative Information").should eq(:non_authoritative_information)
end
end
context "matching codes" do
- STATUS_CODES.each do |code, status|
+ StatusCodes.values.each do |code, status|
it "understands if a response is of type #{status}" do
response.stub(:code).and_return(code.to_s)
- response.should send("be_http_#{ResponseCodeMatchers.status_as_valid_method_name(code)}")
+ response.should send("be_http_#{StatusCodes.as_valid_method_name(code)}")
end
it "understands if a response is not of type #{status}" do
response.stub(:code).and_return('0')
- response.should_not send("be_http_#{ResponseCodeMatchers.status_as_valid_method_name(code)}")
+ response.should_not send("be_http_#{StatusCodes.as_valid_method_name(code)}")
end
end

0 comments on commit e58a302

Please sign in to comment.