rspec-http 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/CHANGELOG +3 -0
- data/Gemfile +11 -0
- data/LICENCE +7 -0
- data/README.rdoc +19 -0
- data/Rakefile +11 -0
- data/lib/rspec/http.rb +8 -0
- data/lib/rspec/http/response_code_matchers.rb +53 -0
- data/lib/rspec/http/status_codes.rb +59 -0
- data/lib/rspec/http/version.rb +7 -0
- data/rspec-http.gemspec +25 -0
- data/spec/rspec/http/response_code_matchers_spec.rb +57 -0
- data/spec/spec_helper.rb +1 -0
- metadata +79 -0
data/.gitignore
ADDED
data/CHANGELOG
ADDED
data/Gemfile
ADDED
data/LICENCE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright 2011 C42 Engineering India Pvt. Ltd.
|
2
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
3
|
+
you may not use this file except in compliance with the License.
|
4
|
+
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
5
|
+
Unless required by applicable law or agreed to in writing, software distributed under the License
|
6
|
+
is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
7
|
+
See the License for the specific language governing permissions and limitations under the License.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= RSpec HTTP
|
2
|
+
|
3
|
+
RSpec HTTP is a RSpec extension library that adds support for writing specs that cover HTTP based API (or more popularly, RESTful APIs).
|
4
|
+
|
5
|
+
To use this library, first add the rspec-http gem to your Gemfile like so:
|
6
|
+
gem 'rspec-http', '~> 0.0.1'
|
7
|
+
|
8
|
+
Then add the following line to your spec_helper.rb:
|
9
|
+
require 'rspec/http'
|
10
|
+
|
11
|
+
This will make matchers such as the ones listed below available to you in your specs.
|
12
|
+
|
13
|
+
response.should be_http_ok
|
14
|
+
|
15
|
+
response.should be_http_created
|
16
|
+
|
17
|
+
response.should be_http_unprocessable_entity
|
18
|
+
|
19
|
+
response.should be_http_im_a_teapot
|
data/Rakefile
ADDED
data/lib/rspec/http.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Http
|
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)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module RSpec
|
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
|
58
|
+
end
|
59
|
+
|
data/rspec-http.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
3
|
+
require "rspec/http/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rspec-http"
|
7
|
+
s.version = RSpec::Http::Version::STRING
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Sidu Ponnappa", "Niranjan Paranjape"]
|
10
|
+
s.email = "ckponnappa@gmail.com"
|
11
|
+
s.homepage = "http://c42.in/open_source"
|
12
|
+
s.summary = "RSpec HTTP is an extension library that makes it easier to write specs for HTTP/REST APIs"
|
13
|
+
s.description = "RSpec HTTP is an extension library that makes it easier to write specs for HTTP/REST APIs"
|
14
|
+
|
15
|
+
s.rubygems_version = "1.3.7"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
19
|
+
s.extra_rdoc_files = [ "README.rdoc" ]
|
20
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
21
|
+
s.require_path = "lib"
|
22
|
+
|
23
|
+
s.add_runtime_dependency "rspec", "~> 2.0"
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
module RSpec::Http
|
4
|
+
describe 'API matchers' do
|
5
|
+
include ResponseCodeMatchers
|
6
|
+
|
7
|
+
let(:response) { mock('HTTP Response') }
|
8
|
+
|
9
|
+
context 'status to matcher conversion' do
|
10
|
+
it "replaces spaces with underscores" do
|
11
|
+
ResponseCodeMatchers::clean_up_status("Method Not Allowed").should eq(:method_not_allowed)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "downcases capital letters" do
|
15
|
+
ResponseCodeMatchers::clean_up_status("IM Used").should eq(:im_used)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "removes apostrophes" do
|
19
|
+
ResponseCodeMatchers::clean_up_status("I'm A Teapot").should eq(:im_a_teapot)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "replaces hyphens with underscores" do
|
23
|
+
ResponseCodeMatchers::clean_up_status("Non-Authoritative Information").should eq(:non_authoritative_information)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "matching codes" do
|
28
|
+
STATUS_CODES.each do |code, status|
|
29
|
+
it "understands if a response is of type #{status}" do
|
30
|
+
response.stub(:code).and_return(code.to_s)
|
31
|
+
response.should send("be_http_#{ResponseCodeMatchers.status_as_valid_method_name(code)}")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "understands if a response is not of type #{status}" do
|
35
|
+
response.stub(:code).and_return('0')
|
36
|
+
response.should_not send("be_http_#{ResponseCodeMatchers.status_as_valid_method_name(code)}")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "where the value of the location header field can be important" do
|
41
|
+
before :each do
|
42
|
+
response.stub(:[]).with('Location').and_return('http://test.server')
|
43
|
+
end
|
44
|
+
|
45
|
+
it "response of type created" do
|
46
|
+
response.stub(:code).and_return('201')
|
47
|
+
expect{ response.should be_http_ok }.to raise_error(/with a location of http:\/\/test\.server$/)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "response of type redirect" do
|
51
|
+
response.stub(:code).and_return('302')
|
52
|
+
expect{ response.should be_http_ok }.to raise_error(/with a location of http:\/\/test\.server$/)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rspec/http'
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-http
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sidu Ponnappa
|
9
|
+
- Niranjan Paranjape
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2011-04-06 00:00:00 +05:30
|
15
|
+
default_executable:
|
16
|
+
dependencies:
|
17
|
+
- !ruby/object:Gem::Dependency
|
18
|
+
name: rspec
|
19
|
+
prerelease: false
|
20
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
21
|
+
none: false
|
22
|
+
requirements:
|
23
|
+
- - ~>
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: "2.0"
|
26
|
+
type: :runtime
|
27
|
+
version_requirements: *id001
|
28
|
+
description: RSpec HTTP is an extension library that makes it easier to write specs for HTTP/REST APIs
|
29
|
+
email: ckponnappa@gmail.com
|
30
|
+
executables: []
|
31
|
+
|
32
|
+
extensions: []
|
33
|
+
|
34
|
+
extra_rdoc_files:
|
35
|
+
- README.rdoc
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- CHANGELOG
|
39
|
+
- Gemfile
|
40
|
+
- LICENCE
|
41
|
+
- README.rdoc
|
42
|
+
- Rakefile
|
43
|
+
- lib/rspec/http.rb
|
44
|
+
- lib/rspec/http/response_code_matchers.rb
|
45
|
+
- lib/rspec/http/status_codes.rb
|
46
|
+
- lib/rspec/http/version.rb
|
47
|
+
- rspec-http.gemspec
|
48
|
+
- spec/rspec/http/response_code_matchers_spec.rb
|
49
|
+
- spec/spec_helper.rb
|
50
|
+
has_rdoc: true
|
51
|
+
homepage: http://c42.in/open_source
|
52
|
+
licenses: []
|
53
|
+
|
54
|
+
post_install_message:
|
55
|
+
rdoc_options:
|
56
|
+
- --charset=UTF-8
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.6.2
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: RSpec HTTP is an extension library that makes it easier to write specs for HTTP/REST APIs
|
78
|
+
test_files: []
|
79
|
+
|