killer_rspec_rack 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ require 'killer_rspec_rack/macros'
2
+ require 'killer_rspec_rack/matchers'
3
+
4
+ module KillerRspecRack
5
+ def self.included base
6
+ base.extend KillerRspecRack::Macros
7
+ base.send :include, KillerRspecRack::Matchers
8
+ end
9
+ end
@@ -0,0 +1,29 @@
1
+ module KillerRspecRack
2
+ module Macros
3
+ def the_response_body_of url, options = {:method => :get}, &assertion
4
+ decoders = Hash.new(lambda { |body| body })
5
+ decoders.merge!({"application/json" => lambda { |body| ActiveSupport::JSON.decode(body) }})
6
+
7
+ context "the response body of #{url}" do
8
+ subject do
9
+ self.send(options[:method], url)
10
+ decoder = decoders[last_response.headers["Content-Type"]]
11
+ decoder.call(last_response.body)
12
+ end
13
+
14
+ self.instance_eval &assertion
15
+ end
16
+ end
17
+
18
+ def the_response_of url, options = {:method => :get}, &assertion
19
+ context "the response of #{url}" do
20
+ subject do
21
+ self.send(options[:method], url)
22
+ last_response
23
+ end
24
+
25
+ self.instance_eval &assertion
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,84 @@
1
+ module KillerRspecRack
2
+ module Matchers
3
+ def self.define_matcher(name, &declarations)
4
+ define_method name do |*expected|
5
+ return RSpec::Matchers::Matcher.new(name, *expected, &declarations) if defined? RSpec
6
+ return Spec::Matchers::Matcher.new(name, *expected, &declarations) if defined? Spec
7
+ end
8
+ end
9
+
10
+ define_matcher :be_ok_and_json do
11
+ match do |response|
12
+ response.ok? && KillerRspecRack::Matchers.has_header?(response, "Content-Type", "application/json")
13
+ end
14
+
15
+ failure_message_for_should do |response|
16
+ "expected response to be ok and json, but was '#{response.ok? ? "OK" : "NOT OK"}' and '#{response.headers['Content-Type']}"
17
+ end
18
+
19
+ failure_message_for_should_not do |response|
20
+ "expected response to be not ok and not json, but was '#{response.ok? ? "OK" : "NOT OK"}' and '#{response.headers['Content-Type']}"
21
+ end
22
+ end
23
+
24
+ define_matcher :be_unauthorized do
25
+ match do |response|
26
+ response.status == 401
27
+ end
28
+
29
+ failure_message_for_should do |response|
30
+ "expected response to be (401 Unauthorized), but was #{response.status}"
31
+ end
32
+
33
+ failure_message_for_should_not do |response|
34
+ "expected response not to be (401 Unauthorized), but was"
35
+ end
36
+ end
37
+
38
+ def self.has_header? response, header, value
39
+ response.headers[header] == value.to_s
40
+ end
41
+
42
+ define_matcher :have_header do |header, value|
43
+ match do |response|
44
+ KillerRspecRack::Matchers.has_header? response, header, value
45
+ end
46
+
47
+ failure_message_for_should do |response|
48
+ "expected response to have header '#{header}=>#{value}', but did not"
49
+ end
50
+
51
+ failure_message_for_should_not do |response|
52
+ "expected response not to have header '#{header}=>#{value}', but did"
53
+ end
54
+ end
55
+
56
+ define_matcher :have_content_type do |value|
57
+ match do |response|
58
+ KillerRspecRack::Matchers.has_header? response, "Content-Type", value
59
+ end
60
+
61
+ failure_message_for_should do |response|
62
+ "expected response to have header 'Content-Type=>#{value}', but did not"
63
+ end
64
+
65
+ failure_message_for_should_not do |response|
66
+ "expected response to not have header 'Content-Type=>#{value}', but did"
67
+ end
68
+ end
69
+
70
+ define_matcher :have_content_length do |value|
71
+ match do |response|
72
+ KillerRspecRack::Matchers.has_header? response, "Content-Length", value
73
+ end
74
+
75
+ failure_message_for_should do |response|
76
+ "expected response to have header 'Content-Length=>#{value}', but did not"
77
+ end
78
+
79
+ failure_message_for_should_not do |response|
80
+ "expected response to not have header 'Content-Length=>#{value}', but did"
81
+ end
82
+ end
83
+ end
84
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: killer_rspec_rack
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Andrew Kiellor
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-15 00:00:00 +11:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: "\n A collection of RSpec macros and matchers for testing\n Rack based Ruby applications apps.\n "
23
+ email: akiellor@gmail.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/killer_rspec_rack.rb
32
+ - lib/killer_rspec_rack/macros.rb
33
+ - lib/killer_rspec_rack/matchers.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/akiellor/killer_rspec_rack
36
+ licenses: []
37
+
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ hash: 3
49
+ segments:
50
+ - 0
51
+ version: "0"
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements:
62
+ - rspec v1.3, or greater
63
+ rubyforge_project:
64
+ rubygems_version: 1.4.2
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: RSpec macros and matchers for testing Rack apps.
68
+ test_files: []
69
+