rspec-http 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,6 @@
1
- = RSpec HTTP
1
+ = RSpec HTTP 0.0.2
2
+
3
+ (c) Copyright 2010-2011 {C42 Engineering}[http://c42.in]. All Rights Reserved.
2
4
 
3
5
  RSpec HTTP is a RSpec extension library that adds support for writing specs that cover HTTP based API (or more popularly, RESTful APIs).
4
6
 
@@ -16,4 +18,16 @@ This will make matchers such as the ones listed below available to you in your s
16
18
 
17
19
  response.should be_http_unprocessable_entity
18
20
 
19
- response.should be_http_im_a_teapot
21
+ response.should be_http_im_a_teapot
22
+
23
+ == Rails
24
+
25
+ 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:
26
+
27
+ response.should be_ok
28
+
29
+ response.should be_created
30
+
31
+ response.should be_unprocessable_entity
32
+
33
+ response.should be_im_a_teapot
@@ -1,8 +1,11 @@
1
1
  require 'rspec/core'
2
2
 
3
3
  require 'rspec/http/status_codes'
4
+ require 'rspec/http/response_code_matcher'
4
5
  require 'rspec/http/response_code_matchers'
5
6
 
7
+ require 'rspec/http/rails'# if Kernel.const_defined?('Rails')
8
+
6
9
  RSpec::configure do |config|
7
10
  config.include(RSpec::Http::ResponseCodeMatchers)
8
11
  end
@@ -0,0 +1,2 @@
1
+ require 'rspec/http/rails/response_code_matchers'
2
+ require 'rspec/http/rails/controller_example_group'
@@ -0,0 +1,7 @@
1
+ module RSpec
2
+ module Rails
3
+ module ControllerExampleGroup
4
+ include RSpec::Rails::ResponseCodeMatchers
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module RSpec
2
+ module Rails
3
+ module ResponseCodeMatchers
4
+ RSpec::Http::StatusCodes.values.each do |code, status|
5
+ define_method("be_#{Http::StatusCodes.as_valid_method_name(code)}") do
6
+ RSpec::Http::ResponseCodeMatcher.new(code)
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,34 @@
1
+ module RSpec
2
+ module Http
3
+ class ResponseCodeMatcher
4
+ def initialize(expected_code)
5
+ @expected_code = expected_code
6
+ end
7
+
8
+ def matches?(target)
9
+ @target = target
10
+ @target.code.to_i == @expected_code
11
+ end
12
+
13
+ def description
14
+ "Response code should be #{@expected_code}"
15
+ end
16
+
17
+ def failure_message
18
+ "Expected #{@target} to #{common_message}"
19
+ end
20
+
21
+ def negative_failure_message
22
+ "Expected #{@target} to not #{common_message}"
23
+ end
24
+
25
+ def common_message
26
+ message = "have a response code of #{@expected_code}, but got #{@target.code}"
27
+ if @target.code.to_i == 302 || @target.code.to_i == 201
28
+ message += " with a location of #{@target['Location'] || @target['location']}"
29
+ end
30
+ message
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,51 +1,9 @@
1
1
  module RSpec
2
2
  module Http
3
3
  module ResponseCodeMatchers
4
- def self.clean_up_status(message)
5
- message.gsub(/(\s|-)/, "_").gsub('\'', '').downcase.to_sym
6
- end
7
-
8
- def self.status_as_valid_method_name(look_up_code)
9
- (@status_codes ||= RSpec::Http::STATUS_CODES.inject({}) do |hash, (code, message)|
10
- hash[code] = clean_up_status(message)
11
- hash
12
- end.freeze)[look_up_code]
13
- end
14
-
15
- class HttpResponseCodeMatcher
16
- def initialize(expected_code)
17
- @expected_code = expected_code
18
- end
19
-
20
- def matches?(target)
21
- @target = target
22
- @target.code.to_i == @expected_code
23
- end
24
-
25
- def description
26
- "Response code should be #{@expected_code}"
27
- end
28
-
29
- def failure_message
30
- "Expected #{@target} to #{common_message}"
31
- end
32
-
33
- def negative_failure_message
34
- "Expected #{@target} to not #{common_message}"
35
- end
36
-
37
- def common_message
38
- message = "have a response code of #{@expected_code}, but got #{@target.code}"
39
- if @target.code.to_i == 302 || @target.code.to_i == 201
40
- message += " with a location of #{@target['Location'] || @target['location']}"
41
- end
42
- message
43
- end
44
- end
45
-
46
- RSpec::Http::STATUS_CODES.each do |code, status|
47
- define_method("be_http_#{status_as_valid_method_name(code)}") do
48
- HttpResponseCodeMatcher.new(code)
4
+ RSpec::Http::StatusCodes.values.each do |code, status|
5
+ define_method("be_http_#{Http::StatusCodes.as_valid_method_name(code)}") do
6
+ ResponseCodeMatcher.new(code)
49
7
  end
50
8
  end
51
9
  end
@@ -1,59 +1,74 @@
1
1
  module RSpec
2
2
  module Http
3
- STATUS_CODES = {
4
- 100=>"Continue",
5
- 101=>"Switching Protocols",
6
- 102=>"Processing",
7
- 200=>"OK",
8
- 201=>"Created",
9
- 202=>"Accepted",
10
- 203=>"Non-Authoritative Information",
11
- 204=>"No Content",
12
- 205=>"Reset Content",
13
- 206=>"Partial Content",
14
- 207=>"Multi-Status",
15
- 226=>"IM Used",
16
- 300=>"Multiple Choices",
17
- 301=>"Moved Permanently",
18
- 302=>"Found",
19
- 303=>"See Other",
20
- 304=>"Not Modified",
21
- 305=>"Use Proxy",
22
- 306=>"Reserved",
23
- 307=>"Temporary Redirect",
24
- 400=>"Bad Request",
25
- 401=>"Unauthorized",
26
- 402=>"Payment Required",
27
- 403=>"Forbidden",
28
- 404=>"Not Found",
29
- 405=>"Method Not Allowed",
30
- 406=>"Not Acceptable",
31
- 407=>"Proxy Authentication Required",
32
- 408=>"Request Timeout",
33
- 409=>"Conflict",
34
- 410=>"Gone",
35
- 411=>"Length Required",
36
- 412=>"Precondition Failed",
37
- 413=>"Request Entity Too Large",
38
- 414=>"Request-URI Too Long",
39
- 415=>"Unsupported Media Type",
40
- 416=>"Requested Range Not Satisfiable",
41
- 417=>"Expectation Failed",
42
- 418 => "I'm A Teapot",
43
- 422=>"Unprocessable Entity",
44
- 423=>"Locked",
45
- 424=>"Failed Dependency",
46
- 426=>"Upgrade Required",
47
- 500=>"Internal Server Error",
48
- 501=>"Not Implemented",
49
- 502=>"Bad Gateway",
50
- 503=>"Service Unavailable",
51
- 504=>"Gateway Timeout",
52
- 505=>"HTTP Version Not Supported",
53
- 506=>"Variant Also Negotiates",
54
- 507=>"Insufficient Storage",
55
- 510=>"Not Extended"
56
- }.freeze
57
- end
3
+ module StatusCodes
4
+ def self.values
5
+ @values ||= {
6
+ 100=>"Continue",
7
+ 101=>"Switching Protocols",
8
+ 102=>"Processing",
9
+ 200=>"OK",
10
+ 201=>"Created",
11
+ 202=>"Accepted",
12
+ 203=>"Non-Authoritative Information",
13
+ 204=>"No Content",
14
+ 205=>"Reset Content",
15
+ 206=>"Partial Content",
16
+ 207=>"Multi-Status",
17
+ 226=>"IM Used",
18
+ 300=>"Multiple Choices",
19
+ 301=>"Moved Permanently",
20
+ 302=>"Found",
21
+ 303=>"See Other",
22
+ 304=>"Not Modified",
23
+ 305=>"Use Proxy",
24
+ 306=>"Reserved",
25
+ 307=>"Temporary Redirect",
26
+ 400=>"Bad Request",
27
+ 401=>"Unauthorized",
28
+ 402=>"Payment Required",
29
+ 403=>"Forbidden",
30
+ 404=>"Not Found",
31
+ 405=>"Method Not Allowed",
32
+ 406=>"Not Acceptable",
33
+ 407=>"Proxy Authentication Required",
34
+ 408=>"Request Timeout",
35
+ 409=>"Conflict",
36
+ 410=>"Gone",
37
+ 411=>"Length Required",
38
+ 412=>"Precondition Failed",
39
+ 413=>"Request Entity Too Large",
40
+ 414=>"Request-URI Too Long",
41
+ 415=>"Unsupported Media Type",
42
+ 416=>"Requested Range Not Satisfiable",
43
+ 417=>"Expectation Failed",
44
+ 418 => "I'm A Teapot",
45
+ 422=>"Unprocessable Entity",
46
+ 423=>"Locked",
47
+ 424=>"Failed Dependency",
48
+ 426=>"Upgrade Required",
49
+ 500=>"Internal Server Error",
50
+ 501=>"Not Implemented",
51
+ 502=>"Bad Gateway",
52
+ 503=>"Service Unavailable",
53
+ 504=>"Gateway Timeout",
54
+ 505=>"HTTP Version Not Supported",
55
+ 506=>"Variant Also Negotiates",
56
+ 507=>"Insufficient Storage",
57
+ 510=>"Not Extended"
58
+ }.freeze
59
+ end
60
+
61
+ def self.clean_up_status(message)
62
+ message.gsub(/(\s|-)/, "_").gsub('\'', '').downcase.to_sym
63
+ end
64
+
65
+ def self.as_valid_method_name(look_up_code)
66
+ (@status_codes ||= values.inject({}) do |hash, (code, message)|
67
+ hash[code] = clean_up_status(message)
68
+ hash
69
+ end.freeze)[look_up_code]
70
+ end
71
+ end
72
+ end
58
73
  end
59
74
 
@@ -1,7 +1,7 @@
1
1
  module RSpec # :nodoc:
2
2
  module Http # :nodoc:
3
3
  module Version # :nodoc:
4
- STRING = '0.0.1'
4
+ STRING = '0.0.2'
5
5
  end
6
6
  end
7
7
  end
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
14
14
 
15
15
  s.rubygems_version = "1.3.7"
16
16
 
17
- s.files = `git ls-files`.split("\n")
17
+ s.files = `git ls-files`.split("\n") - ['.gitignore']
18
18
  s.test_files = `git ls-files -- {spec}/*`.split("\n")
19
19
  s.extra_rdoc_files = [ "README.rdoc" ]
20
20
  s.rdoc_options = ["--charset=UTF-8"]
@@ -8,32 +8,32 @@ module RSpec::Http
8
8
 
9
9
  context 'status to matcher conversion' do
10
10
  it "replaces spaces with underscores" do
11
- ResponseCodeMatchers::clean_up_status("Method Not Allowed").should eq(:method_not_allowed)
11
+ StatusCodes.clean_up_status("Method Not Allowed").should eq(:method_not_allowed)
12
12
  end
13
13
 
14
14
  it "downcases capital letters" do
15
- ResponseCodeMatchers::clean_up_status("IM Used").should eq(:im_used)
15
+ StatusCodes.clean_up_status("IM Used").should eq(:im_used)
16
16
  end
17
17
 
18
18
  it "removes apostrophes" do
19
- ResponseCodeMatchers::clean_up_status("I'm A Teapot").should eq(:im_a_teapot)
19
+ StatusCodes.clean_up_status("I'm A Teapot").should eq(:im_a_teapot)
20
20
  end
21
21
 
22
22
  it "replaces hyphens with underscores" do
23
- ResponseCodeMatchers::clean_up_status("Non-Authoritative Information").should eq(:non_authoritative_information)
23
+ StatusCodes.clean_up_status("Non-Authoritative Information").should eq(:non_authoritative_information)
24
24
  end
25
25
  end
26
26
 
27
27
  context "matching codes" do
28
- STATUS_CODES.each do |code, status|
28
+ StatusCodes.values.each do |code, status|
29
29
  it "understands if a response is of type #{status}" do
30
30
  response.stub(:code).and_return(code.to_s)
31
- response.should send("be_http_#{ResponseCodeMatchers.status_as_valid_method_name(code)}")
31
+ response.should send("be_http_#{StatusCodes.as_valid_method_name(code)}")
32
32
  end
33
33
 
34
34
  it "understands if a response is not of type #{status}" do
35
35
  response.stub(:code).and_return('0')
36
- response.should_not send("be_http_#{ResponseCodeMatchers.status_as_valid_method_name(code)}")
36
+ response.should_not send("be_http_#{StatusCodes.as_valid_method_name(code)}")
37
37
  end
38
38
  end
39
39
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: rspec-http
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Sidu Ponnappa
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2011-04-06 00:00:00 +05:30
14
+ date: 2011-04-21 00:00:00 +05:30
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -34,13 +34,16 @@ extensions: []
34
34
  extra_rdoc_files:
35
35
  - README.rdoc
36
36
  files:
37
- - .gitignore
38
37
  - CHANGELOG
39
38
  - Gemfile
40
39
  - LICENCE
41
40
  - README.rdoc
42
41
  - Rakefile
43
42
  - lib/rspec/http.rb
43
+ - lib/rspec/http/rails.rb
44
+ - lib/rspec/http/rails/controller_example_group.rb
45
+ - lib/rspec/http/rails/response_code_matchers.rb
46
+ - lib/rspec/http/response_code_matcher.rb
44
47
  - lib/rspec/http/response_code_matchers.rb
45
48
  - lib/rspec/http/status_codes.rb
46
49
  - lib/rspec/http/version.rb
data/.gitignore DELETED
@@ -1,2 +0,0 @@
1
- *.rbc
2
- Gemfile.lock