assert-response 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +167 -0
- data/Rakefile +44 -0
- data/VERSION +1 -0
- data/lib/assert-response.rb +254 -0
- data/test/helper.rb +15 -0
- data/test/test_assert-response.rb +135 -0
- metadata +130 -0
data/.document
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Marc Rene Arns
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
= assert-response
|
2
|
+
== Description
|
3
|
+
|
4
|
+
Assert-methods (sugar) and a tiny DSL to facilitate testing of rack apps with {Rack::Test}.
|
5
|
+
The idea is that an error on the server side should be exposed to the tests as a real error (not hidden inside
|
6
|
+
last_response.errors). +assert-response+ raises the correct error and gives us the backtrace back in our tests.
|
7
|
+
|
8
|
+
Most of the time we check for the body of the response and then almost always a 404 - Not found is regarded as an error.
|
9
|
+
Further we mostly want to check against Strings that are contained in the response body, while in some cases we need Regexps.
|
10
|
+
|
11
|
+
So this methods and DSL makes the normal cases easier while making special cases possible.
|
12
|
+
|
13
|
+
To use it in your tests:
|
14
|
+
require 'rack/test'
|
15
|
+
require 'assert-reponse'
|
16
|
+
include Rack::Test::Methods
|
17
|
+
|
18
|
+
(for usage without the inclusion of Rack::Test::Methods, see {AssertResponse::Methods}
|
19
|
+
|
20
|
+
And then to test do something like this:
|
21
|
+
get '/myroute'
|
22
|
+
assert_response do
|
23
|
+
is_html # checks content-type
|
24
|
+
body 'my body' # same as 'body /my\ body/'
|
25
|
+
end
|
26
|
+
|
27
|
+
the same could be shorter:
|
28
|
+
get '/myroute'
|
29
|
+
assert_response do
|
30
|
+
html 'my body'
|
31
|
+
end
|
32
|
+
|
33
|
+
or even:
|
34
|
+
get '/myroute'
|
35
|
+
assert_response_html 'my body'
|
36
|
+
|
37
|
+
Predefined are the content types of html, css, js and json. But you may register your own by calling {AssertResponse.add_content_type}. Then you get all that assert_response_[your_content_type], assert_response_is_[your_content_type] and assert_response_not_found_[your_content_type] methods for granted.
|
38
|
+
|
39
|
+
== Installation
|
40
|
+
gem install assert-response
|
41
|
+
|
42
|
+
Usually +assert_response+ checks +last_response+, but you could also pass a response Object:
|
43
|
+
get '/a'
|
44
|
+
a = last_response
|
45
|
+
get '/b'
|
46
|
+
assert_response a do # checks /a
|
47
|
+
html 'body of a'
|
48
|
+
end
|
49
|
+
|
50
|
+
Inside the code block use the methods of {AssertResponse}.
|
51
|
+
|
52
|
+
== Usage
|
53
|
+
There are two ways to use assert-response: You could use the DSL or the assert_response_xxx methods.
|
54
|
+
=== DSL
|
55
|
+
The DSL mode is handy if you want to perform several tests against the same response.
|
56
|
+
You call +asser_response+ and give it a code block.
|
57
|
+
All methods of the DSL are methods of {AssertResponse} so have a look there.
|
58
|
+
get '/hello_world'
|
59
|
+
assert_response do
|
60
|
+
body '<body>body of hello_world</body>'
|
61
|
+
ok # same as "status 200"
|
62
|
+
is_html
|
63
|
+
end
|
64
|
+
=== assert_response_xxx methods
|
65
|
+
Some of the checks could be done simpler since some assertions imply other.
|
66
|
+
The code above could be simplified to
|
67
|
+
get '/hello_world'
|
68
|
+
assert_response_html '<body>body of hello_world</body>'
|
69
|
+
A call to assert_response_[method] will be forwarded to the according method of {AssertResponse}.
|
70
|
+
But it checks always +last_response+, so if you need to pass the response object you will have to use the DSL.
|
71
|
+
|
72
|
+
== Examples
|
73
|
+
|
74
|
+
=== HTML and other content
|
75
|
+
lets see what the following does
|
76
|
+
get '/myroute'
|
77
|
+
assert_response { html 'my body' }
|
78
|
+
or shorter:
|
79
|
+
get '/myroute'
|
80
|
+
assert_response_html 'my body' # with the cousins assert_response_css, assert_response_js and assert_response_json
|
81
|
+
|
82
|
+
* It checks if +"/myroute"+ has the +Content-Type+ +"text/html"+ and the +body+ matches /my\ body/.
|
83
|
+
* It also checks if +status+ of the response is +200+.
|
84
|
+
* If the +status+ of the response is +500+ the original error of the rack app is raised (with original message and backtrace!)
|
85
|
+
* If +status+ is +404+ an error of class {AssertResponse::Status404} is raised.
|
86
|
+
|
87
|
+
=== Redirects
|
88
|
+
To check if "/myredirect" returns +status+ 302 and the header +Location+ matches /\/mytarget/, try
|
89
|
+
get '/myredirect'
|
90
|
+
assert_response do
|
91
|
+
status 302
|
92
|
+
header 'Location', '/mytarget'
|
93
|
+
end
|
94
|
+
or shorter:
|
95
|
+
get '/myredirect'
|
96
|
+
assert_response_redirect '/mytarget'
|
97
|
+
|
98
|
+
|
99
|
+
=== Server Errors
|
100
|
+
Server errors should not happen, but if they do we want them to show up as errors in our test cases.
|
101
|
+
|
102
|
+
The following checks if the route +"/error"+ raises an error of class +RuntimeError+ with a message that matches +/my\ error\ message/+
|
103
|
+
get '/error'
|
104
|
+
assert_response { raises RuntimeError, "my error message" }
|
105
|
+
or shorter
|
106
|
+
get '/error'
|
107
|
+
assert_response_raises RuntimeError, "my error message"
|
108
|
+
|
109
|
+
If we are not interested in the error class or message we omit them.
|
110
|
+
get '/error'
|
111
|
+
assert_response_raises
|
112
|
+
|
113
|
+
=== 404 - not found
|
114
|
+
If we expect a status 404, we could do
|
115
|
+
get '/not-existant'
|
116
|
+
assert_response_status 404
|
117
|
+
or
|
118
|
+
get '/not-existant'
|
119
|
+
assert_response { not_found }
|
120
|
+
or
|
121
|
+
get '/not-existant'
|
122
|
+
assert_response_not_found
|
123
|
+
|
124
|
+
Most of the time we don't want a 404 response in our tests, so it's an error.
|
125
|
+
If we test for a body of a known content-type (eg. with assert_html, assert_js and friends) it is assumed that we don't want a 404 response, so an error will be thrown if we get some.
|
126
|
+
So to be able to check the body too to test a customer 404 page, we could do:
|
127
|
+
get '/not-existant'
|
128
|
+
assert_response do
|
129
|
+
not_found
|
130
|
+
header('Content-Type', 'text/html') # {AssertResponse#is_html} could not be used since it implies a status 200
|
131
|
+
body "Page could not be found" # {AssertResponse#html} could not be used since it implies a status 200
|
132
|
+
end
|
133
|
+
or
|
134
|
+
get '/not-existant'
|
135
|
+
assert_response_not_found_html "Page could not be found" # also 'assert_not_found_js' and friends
|
136
|
+
|
137
|
+
=== Custom Headers
|
138
|
+
get '/special'
|
139
|
+
assert_response_header 'my_header', 'my header value'
|
140
|
+
|
141
|
+
=== Custom Content-Types
|
142
|
+
get '/special'
|
143
|
+
assert_response_content_type 'my/contenttype'
|
144
|
+
|
145
|
+
or
|
146
|
+
AssertResponse.add_content_type :my_type, 'my/contenttype'
|
147
|
+
# and then
|
148
|
+
get '/special'
|
149
|
+
assert_response_my_type 'here the body of my type'
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
== Contributing to assert-response
|
154
|
+
|
155
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
156
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
157
|
+
* Fork the project
|
158
|
+
* Start a feature/bugfix branch
|
159
|
+
* Commit and push until you are happy with your contribution
|
160
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
161
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
162
|
+
|
163
|
+
== Copyright
|
164
|
+
|
165
|
+
Copyright (c) 2011 Marc Rene Arns. See LICENSE.txt for
|
166
|
+
further details.
|
167
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "assert-response"
|
16
|
+
gem.homepage = "http://github.com/metakeule/assert-response"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Assert-methods (sugar) and a tiny DSL to facilitate testing of rack apps with Rack::Test.}
|
19
|
+
#gem.description = %Q{TODO: longer description of your gem}
|
20
|
+
gem.email = "Base64.decode64('bGludXhAbWFyY3JlbmVhcm5zLmRl\n')"
|
21
|
+
gem.authors = ["Marc Rene Arns"]
|
22
|
+
gem.add_runtime_dependency 'rack-test'
|
23
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
24
|
+
end
|
25
|
+
Jeweler::RubygemsDotOrgTasks.new
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
Rake::TestTask.new(:test) do |test|
|
29
|
+
test.libs << 'lib' << 'test'
|
30
|
+
test.pattern = 'test/**/test_*.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
|
34
|
+
require 'rcov/rcovtask'
|
35
|
+
Rcov::RcovTask.new do |test|
|
36
|
+
test.libs << 'test'
|
37
|
+
test.pattern = 'test/**/test_*.rb'
|
38
|
+
test.verbose = true
|
39
|
+
end
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'yard'
|
44
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.3
|
@@ -0,0 +1,254 @@
|
|
1
|
+
class AssertResponse
|
2
|
+
class Status404 < Exception ; end
|
3
|
+
# for each content-type there is a method
|
4
|
+
# * +is_[content_type]+ that checks if the response has the content-type
|
5
|
+
# * +[content_type]+ that checks the content-type and matches the body against the given pattern and checks for status 200
|
6
|
+
# * +not_found_[content_type]+ that checks the content-type, matches the body against the pattern and checks for status 404
|
7
|
+
# see method_missing
|
8
|
+
CONTENT_TYPES = {}
|
9
|
+
CONTENT_TYPES[:html] = 'text/html'
|
10
|
+
CONTENT_TYPES[:css] = 'text/css'
|
11
|
+
CONTENT_TYPES[:js] = 'application/javascript'
|
12
|
+
CONTENT_TYPES[:json] = 'application/json'
|
13
|
+
CONTENT_TYPES[:text] = 'text/plain'
|
14
|
+
|
15
|
+
# Adds custom content_types to AssertResponse::CONTENT_TYPES. We would then have all the test methods for the content-type
|
16
|
+
#
|
17
|
+
# @param [Symbol] name name of the content_type
|
18
|
+
# @param [String] content_type content_type (mime type, eg. "text/html")
|
19
|
+
def AssertResponse.add_content_type(name, content_type)
|
20
|
+
CONTENT_TYPES.update name, content_type
|
21
|
+
end
|
22
|
+
|
23
|
+
# Creates a new {AssertResponse} Object. Usually you will want to +instance_exec+ some code on it.
|
24
|
+
#
|
25
|
+
# @param [Object] delegate_to the test object that has all the assert_xxx methods that we want to call
|
26
|
+
# @param [MockResponse, Response] response the response object that is checked
|
27
|
+
def initialize(delegate_to, response, &code)
|
28
|
+
@delegate_to = delegate_to
|
29
|
+
@response = response
|
30
|
+
@error = nil
|
31
|
+
check_for_error()
|
32
|
+
end
|
33
|
+
|
34
|
+
alias :_old_method_missing :method_missing
|
35
|
+
|
36
|
+
# handles is_[content_type], not_found_[content_type] and [content_type] methods
|
37
|
+
# Delegates unknown methods to +@delegate_to+
|
38
|
+
def method_missing(meth, *args, &code)
|
39
|
+
case meth.to_s
|
40
|
+
when /^(is|found)_(.+)$/ # is_[content_type] methods
|
41
|
+
if ctype = CONTENT_TYPES[$2.to_sym]
|
42
|
+
ok()
|
43
|
+
return content_type(ctype)
|
44
|
+
end
|
45
|
+
when /^not_found_(.+)$/ # not_found_[content_type] methods
|
46
|
+
if ctype = CONTENT_TYPES[$1.to_sym]
|
47
|
+
content_type ctype
|
48
|
+
return body(args.first, 404)
|
49
|
+
end
|
50
|
+
else
|
51
|
+
if ctype = CONTENT_TYPES[meth] # [content_type] methods
|
52
|
+
ok()
|
53
|
+
content_type ctype
|
54
|
+
return body(args.first)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
@delegate_to.send meth, *args, &code
|
58
|
+
end
|
59
|
+
|
60
|
+
# Checks if status is 200.
|
61
|
+
def ok()
|
62
|
+
status 200
|
63
|
+
end
|
64
|
+
|
65
|
+
# Checks if the status is +status+. For status other then 5xx and error is raised if the response has an error.
|
66
|
+
# For status other than 404 an error Status404 is thrown if response status is 404
|
67
|
+
#
|
68
|
+
# @params [Integer] status the expected status
|
69
|
+
def status(status)
|
70
|
+
raise_error() unless status =~ /^5/ or @response.errors.empty?
|
71
|
+
Kernel.raise(Status404, "not found") unless status == 404 or @response.status != 404
|
72
|
+
assert_equal(status, @response.status)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Checks if the response status is 404.
|
76
|
+
def not_found()
|
77
|
+
status 404
|
78
|
+
end
|
79
|
+
|
80
|
+
# Checks if the status is 302, and the header "Location" matches the +pattern+
|
81
|
+
#
|
82
|
+
# @param [String,Regexp] pattern the pattern to match against.
|
83
|
+
def redirect(pattern)
|
84
|
+
status 302
|
85
|
+
header 'Location', pattern
|
86
|
+
end
|
87
|
+
|
88
|
+
# Checks if the header +key+ exists and / or if it matches +pattern+.
|
89
|
+
#
|
90
|
+
# @param [String] key the key to look for
|
91
|
+
# @param [String,Regexp,:exists] pattern the pattern to match against. +:exists+ just checks for existance of +key+.
|
92
|
+
def header(key, pattern=:exists)
|
93
|
+
if pattern == :exists
|
94
|
+
assert !@response.headers[key].to_s.empty?, "Header '#{key}' not found"
|
95
|
+
else
|
96
|
+
assert_match normalize_pattern(pattern), @response.headers[key].to_s
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# Check if the +Content-Type+ matches +pattern+. Checks also if request is ok (see {#ok}).
|
101
|
+
#
|
102
|
+
# @param [String,Regexp] pattern the pattern to match against.
|
103
|
+
def content_type(pattern)
|
104
|
+
header 'Content-Type', pattern
|
105
|
+
end
|
106
|
+
|
107
|
+
# Check if body matches +pattern+ and status is +status+.
|
108
|
+
#
|
109
|
+
# @param [String,Regexp] pattern the pattern to match against.
|
110
|
+
# @params [Integer] status the expected status (default 200, see {#ok})
|
111
|
+
def body(pattern, status=200)
|
112
|
+
status(status)
|
113
|
+
assert_match normalize_pattern(pattern), @response.body
|
114
|
+
end
|
115
|
+
|
116
|
+
# Check if an error was raised.
|
117
|
+
#
|
118
|
+
# @param [Class] exception_class also check if error is_a? +exception_class+
|
119
|
+
# @param [String,Regexp] pattern also check if the error message matches +pattern+
|
120
|
+
def raises(exception_class=nil, pattern=nil)
|
121
|
+
assert_equal exception_class, @error.class if exception_class
|
122
|
+
assert_match normalize_pattern(pattern), @error.message if pattern
|
123
|
+
assert !@error.nil?
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
128
|
+
def normalize_pattern(pattern)
|
129
|
+
pattern.is_a?(Regexp) ? pattern : /#{Regexp.escape(pattern.to_s)}/
|
130
|
+
end
|
131
|
+
|
132
|
+
def check_for_error
|
133
|
+
normalize_error unless @response.errors.to_s.empty?
|
134
|
+
end
|
135
|
+
|
136
|
+
def normalize_error
|
137
|
+
message, backtrace = @response.errors.split "\n", 2
|
138
|
+
backtrace = backtrace.split
|
139
|
+
err_klass, message = message.split '-', 2
|
140
|
+
@error = Module.const_get(err_klass.strip.to_sym).new(message.strip)
|
141
|
+
@error.set_backtrace backtrace
|
142
|
+
@error
|
143
|
+
end
|
144
|
+
|
145
|
+
def raise_error()
|
146
|
+
Kernel.raise @error
|
147
|
+
end
|
148
|
+
|
149
|
+
# these methods are included in Rack::Test::Methods to be used in a Test Class
|
150
|
+
#
|
151
|
+
# call assert_response with a code block to use the DSL (methods from AssertResponse)
|
152
|
+
# or use a method like assert_response_xxx where xxx is the name of the method from AssertResponse you want to call
|
153
|
+
#
|
154
|
+
# *Test::Unit*
|
155
|
+
# simple
|
156
|
+
# require 'rack/test'
|
157
|
+
# require 'assert-response'
|
158
|
+
#
|
159
|
+
# class TestMe < Test::Unit::Testcase
|
160
|
+
# include Rack::Test::Methods
|
161
|
+
# def app
|
162
|
+
# MyApp
|
163
|
+
# end
|
164
|
+
#
|
165
|
+
# def test_something
|
166
|
+
# get '/works'
|
167
|
+
# assert_response_html "should really work"
|
168
|
+
# end
|
169
|
+
# end
|
170
|
+
#
|
171
|
+
# without including +Rack::Test::Methods+
|
172
|
+
#
|
173
|
+
# require 'rack/test'
|
174
|
+
# require 'assert-response'
|
175
|
+
#
|
176
|
+
# class AppWrapper
|
177
|
+
# include Rack::Test::Methods
|
178
|
+
# def app
|
179
|
+
# MyApp
|
180
|
+
# end
|
181
|
+
# end
|
182
|
+
#
|
183
|
+
# class TestMe < Test::Unit::Testcase
|
184
|
+
# include AssertResponse::Methods
|
185
|
+
#
|
186
|
+
# def test_something
|
187
|
+
# server = AppWrapper.new
|
188
|
+
# server.get '/works'
|
189
|
+
# assert_response server.last_response do
|
190
|
+
# html "should really work"
|
191
|
+
# end
|
192
|
+
# end
|
193
|
+
# end
|
194
|
+
#
|
195
|
+
# *Minitest*
|
196
|
+
# simple
|
197
|
+
# require 'rack/test'
|
198
|
+
# require 'assert-response'
|
199
|
+
# include Rack::Test::Methods
|
200
|
+
#
|
201
|
+
# # write your tests here
|
202
|
+
# it "should work" do
|
203
|
+
# get '/works'
|
204
|
+
# assert_response_html "should really work"
|
205
|
+
# end
|
206
|
+
#
|
207
|
+
# without including +Rack::Test::Methods+
|
208
|
+
# require 'rack/test'
|
209
|
+
# require 'assert-response'
|
210
|
+
#
|
211
|
+
# class AppWrapper
|
212
|
+
# include Rack::Test::Methods
|
213
|
+
# def app
|
214
|
+
# MyApp
|
215
|
+
# end
|
216
|
+
# end
|
217
|
+
#
|
218
|
+
# include AssertResponse::Methods
|
219
|
+
#
|
220
|
+
# describe "my app" do
|
221
|
+
# before do
|
222
|
+
# @app = AppWrapper.new
|
223
|
+
# end
|
224
|
+
#
|
225
|
+
# it "should work" do
|
226
|
+
# @app.get '/works'
|
227
|
+
# assert_response(@app.last_response) do
|
228
|
+
# html "should really work"
|
229
|
+
# end
|
230
|
+
# end
|
231
|
+
# end
|
232
|
+
module Methods
|
233
|
+
alias_method :__assert_response_method_missing, :method_missing
|
234
|
+
|
235
|
+
# route assert_response_ methods to AssertResponse
|
236
|
+
def method_missing(meth, *args, &code)
|
237
|
+
if meth.to_s =~ /^assert_response_(.+)$/
|
238
|
+
AssertResponse.new(self, last_response).send($1.to_sym, *args)
|
239
|
+
else
|
240
|
+
super
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
# creates an {AssertResponse} Object and +instance_exec+ the code (DSL) in it
|
245
|
+
def assert_response(response=last_response, &code)
|
246
|
+
file, line, rest = caller[0].split(':', 3)
|
247
|
+
AssertResponse.new(self, response).instance_exec(file, line.to_i, &code)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
module Rack::Test::Methods
|
253
|
+
include AssertResponse::Methods
|
254
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'minitest/spec'
|
12
|
+
require 'minitest/autorun'
|
13
|
+
require 'rack/test'
|
14
|
+
|
15
|
+
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'backports' if RUBY_VERSION =~ /1.8/
|
2
|
+
require_relative "helper.rb"
|
3
|
+
require_relative File.join("..", "lib", "assert-response.rb")
|
4
|
+
|
5
|
+
include Rack::Test::Methods
|
6
|
+
|
7
|
+
def handle_exception(env)
|
8
|
+
begin
|
9
|
+
yield
|
10
|
+
rescue Exception => err
|
11
|
+
env['rack.errors'] = StringIO.new("#{err.class} - #{err.message}\n#{err.backtrace.join("\n")}")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def tester
|
16
|
+
end
|
17
|
+
|
18
|
+
def app
|
19
|
+
proc { |env|
|
20
|
+
status = 200
|
21
|
+
headers = { 'Content-Type' => 'text/html' }
|
22
|
+
body = 'nothing to see here, move along'
|
23
|
+
case env['PATH_INFO']
|
24
|
+
when '/html'
|
25
|
+
body = '<body>my html</body>'
|
26
|
+
when '/js'
|
27
|
+
headers['Content-Type'] = 'application/javascript'
|
28
|
+
body = 'alert("hi");'
|
29
|
+
when '/css'
|
30
|
+
headers['Content-Type'] = 'text/css'
|
31
|
+
body = 'body{ color: black; }'
|
32
|
+
when '/json'
|
33
|
+
headers['Content-Type'] = 'application/json'
|
34
|
+
body = '{"response":"ok"}'
|
35
|
+
when '/text'
|
36
|
+
headers['Content-Type'] = 'text/plain'
|
37
|
+
body = 'foo-bar and others'
|
38
|
+
when '/error'
|
39
|
+
handle_exception(env) do
|
40
|
+
raise "my error"
|
41
|
+
end
|
42
|
+
status = 500
|
43
|
+
when '/redirect'
|
44
|
+
headers['Location'] = '/target'
|
45
|
+
status = 302
|
46
|
+
when '/arg_error'
|
47
|
+
handle_exception(env) do
|
48
|
+
tester 234
|
49
|
+
end
|
50
|
+
status = 500
|
51
|
+
when '/not_found'
|
52
|
+
status = 404
|
53
|
+
body = "<body>Not found</body>"
|
54
|
+
else
|
55
|
+
status = 404
|
56
|
+
end
|
57
|
+
[status, headers, [body]]
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "assert-response" do
|
62
|
+
it "should check json" do
|
63
|
+
get '/json'
|
64
|
+
assert_response_json /ok/
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should check html" do
|
68
|
+
get '/html'
|
69
|
+
assert_response_html '<body>'
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should check css" do
|
73
|
+
get '/css'
|
74
|
+
assert_response_css 'color'
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should check js" do
|
78
|
+
get '/js'
|
79
|
+
assert_response_js 'alert'
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should check text" do
|
83
|
+
get '/text'
|
84
|
+
assert_response do
|
85
|
+
is_text
|
86
|
+
body 'foo-bar'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should check redirect" do
|
91
|
+
get '/redirect'
|
92
|
+
assert_response_redirect '/target'
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should not find missing pages" do
|
96
|
+
get '/settings'
|
97
|
+
assert_response_not_found
|
98
|
+
end
|
99
|
+
|
100
|
+
it "check errors for some kind of sinatra errors" do
|
101
|
+
get '/error'
|
102
|
+
assert_response_raises
|
103
|
+
end
|
104
|
+
|
105
|
+
it "check errors for some other sinatra errors" do
|
106
|
+
get '/arg_error'
|
107
|
+
assert_response_raises ArgumentError
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should raise errors for some other sinatra errors" do
|
111
|
+
get '/error'
|
112
|
+
assert_raises RuntimeError do
|
113
|
+
assert_response_ok
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should raise errors for some other sinatra errors" do
|
118
|
+
get '/arg_error'
|
119
|
+
assert_raises ArgumentError do
|
120
|
+
assert_response_ok
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should raise error for not_found when body is expected" do
|
125
|
+
get '/not_there'
|
126
|
+
assert_raises AssertResponse::Status404 do
|
127
|
+
assert_response_html "hiho"
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should check not found html page" do
|
132
|
+
get '/not_found'
|
133
|
+
assert_response_not_found_html "<body>Not found</body>"
|
134
|
+
end
|
135
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: assert-response
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Marc Rene Arns
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-13 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack-test
|
16
|
+
requirement: &24401940 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *24401940
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: yard
|
27
|
+
requirement: &24401460 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *24401460
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
requirement: &24400980 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *24400980
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: jeweler
|
49
|
+
requirement: &24400500 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.5.2
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *24400500
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rcov
|
60
|
+
requirement: &24400020 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *24400020
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack-test
|
71
|
+
requirement: &24399540 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :runtime
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *24399540
|
80
|
+
description:
|
81
|
+
email: ! 'Base64.decode64(''bGludXhAbWFyY3JlbmVhcm5zLmRl
|
82
|
+
|
83
|
+
'')'
|
84
|
+
executables: []
|
85
|
+
extensions: []
|
86
|
+
extra_rdoc_files:
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.rdoc
|
89
|
+
files:
|
90
|
+
- .document
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.rdoc
|
94
|
+
- Rakefile
|
95
|
+
- VERSION
|
96
|
+
- lib/assert-response.rb
|
97
|
+
- test/helper.rb
|
98
|
+
- test/test_assert-response.rb
|
99
|
+
homepage: http://github.com/metakeule/assert-response
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
hash: 652417119344286028
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.6
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: Assert-methods (sugar) and a tiny DSL to facilitate testing of rack apps
|
127
|
+
with Rack::Test.
|
128
|
+
test_files:
|
129
|
+
- test/helper.rb
|
130
|
+
- test/test_assert-response.rb
|