rack-test-rest 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +3 -1
- data/lib/rack-test-rest.rb +70 -49
- data/lib/rack-test-rest/version.rb +1 -1
- metadata +9 -16
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 259bc58c5eed3c443e4066daec756cc850d4d56b
|
4
|
+
data.tar.gz: 4339f92a6292a5d48333994d5690b95d63b877ec
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d2fbaaffc53a8693852b7192d8b4ea786add56bb0317ea17e52c9eb9f945de982830bbe211cc18be33af5256b53257d9686ebcd09ce699f695ff3efa0076bc12
|
7
|
+
data.tar.gz: b7494c03b1c68e62dd241493e93019dba081704d5d256436012501972204a7cab9b09ee0de3072bda4cbe1eacc711dae2146cbe6f278d04026acbcc68dc37855
|
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/rack-test-rest.png)](http://badge.fury.io/rb/rack-test-rest)
|
2
|
+
|
1
3
|
`rack-test-rest` is an extension to `rack-test` that when combined with
|
2
4
|
`Test::Unit` simplifies the process of unit testing properly
|
3
5
|
designed RESTful API's.
|
@@ -126,5 +128,5 @@ that a single index operation may return (defaults to 100).
|
|
126
128
|
|
127
129
|
# Copyright
|
128
130
|
|
129
|
-
Copyright (c) 2011-
|
131
|
+
Copyright (c) 2011-2014 Joseph Ruscio. See LICENSE.txt for
|
130
132
|
further details.
|
data/lib/rack-test-rest.rb
CHANGED
@@ -4,41 +4,18 @@ module Rack
|
|
4
4
|
module Test
|
5
5
|
module Rest
|
6
6
|
|
7
|
+
# defines expected resource-to-uri scheme. overload this in your class
|
8
|
+
# if needed.
|
9
|
+
#
|
7
10
|
def resource_uri
|
8
11
|
"#{@rack_test_rest[:root_uri]}/#{@rack_test_rest[:resource]}"
|
9
12
|
end
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
if @rack_test_rest[:debug]
|
15
|
-
puts "Status: #{last_response.status}" if @rack_test_rest[:debug]
|
16
|
-
puts "Headers:"
|
17
|
-
puts last_response.headers.inspect
|
18
|
-
puts "Body: #{last_response.body}" if @rack_test_rest[:debug]
|
19
|
-
end
|
20
|
-
assert_content_type_is_json
|
21
|
-
|
22
|
-
if last_response.headers['Content-Length'].to_i > 0
|
23
|
-
JSON.parse(last_response.body)
|
24
|
-
else
|
25
|
-
nil
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
# remove library lines from call stack so error is reported
|
30
|
-
# where the call to rack-test-rest is being made
|
31
|
-
def with_clean_backtraces
|
32
|
-
yield
|
33
|
-
rescue MiniTest::Assertion => error
|
34
|
-
cleaned = error.backtrace.reject {|l| l.index(/rack-test-rest[-.0-9]{0,}\/lib/)}
|
35
|
-
error.set_backtrace(cleaned)
|
36
|
-
raise
|
37
|
-
end
|
38
|
-
|
14
|
+
# create a new instance of the given resource, expecting a 201
|
15
|
+
# unless the :code option is specified.
|
16
|
+
#
|
39
17
|
def create_resource(params={})
|
40
|
-
expected_code = params
|
41
|
-
params.delete :code
|
18
|
+
expected_code = params.delete(:code)
|
42
19
|
|
43
20
|
puts "Posting to: '#{resource_uri}#{@rack_test_rest[:extension]}'" if @rack_test_rest[:debug]
|
44
21
|
post "#{resource_uri}#{@rack_test_rest[:extension]}", params
|
@@ -66,13 +43,18 @@ module Rack
|
|
66
43
|
last_response.original_headers["Location"]
|
67
44
|
end
|
68
45
|
|
46
|
+
# create resource, but expect a 400 - helper for the common case
|
47
|
+
# of testing invalid parameters for creating your resource.
|
48
|
+
#
|
49
|
+
def create_resource_invalid(opts)
|
50
|
+
create_resource({code: 400}.merge(opts))
|
51
|
+
end
|
52
|
+
|
69
53
|
def read_resource(params={})
|
70
|
-
|
71
|
-
params.delete
|
54
|
+
id = params.delete(:id)
|
55
|
+
expected_code = params.delete(:code)
|
72
56
|
|
73
|
-
if
|
74
|
-
id = params[:id]
|
75
|
-
params.delete(:id)
|
57
|
+
if id
|
76
58
|
uri = "#{resource_uri}/#{id}#{@rack_test_rest[:extension]}"
|
77
59
|
else
|
78
60
|
uri = "#{resource_uri}#{@rack_test_rest[:extension]}"
|
@@ -99,11 +81,8 @@ module Rack
|
|
99
81
|
end
|
100
82
|
|
101
83
|
def update_resource(params={})
|
102
|
-
|
103
|
-
params.delete
|
104
|
-
|
105
|
-
id = params[:id]
|
106
|
-
params.delete(:id)
|
84
|
+
id = params.delete(:id)
|
85
|
+
expected_code = params.delete(:code)
|
107
86
|
|
108
87
|
puts "Attempting to update #{id} with #{params.inspect}" if @rack_test_rest[:debug]
|
109
88
|
|
@@ -116,6 +95,13 @@ module Rack
|
|
116
95
|
end
|
117
96
|
end
|
118
97
|
|
98
|
+
# update resource, but expect a 400 - helper for the common case
|
99
|
+
# of testing invalid parameters for updating your resource.
|
100
|
+
#
|
101
|
+
def update_resource_invalid(opts)
|
102
|
+
update_resource({code: 400}.merge(opts))
|
103
|
+
end
|
104
|
+
|
119
105
|
def delete_resource(params={})
|
120
106
|
delete "#{resource_uri}/#{params[:id]}#{@rack_test_rest[:extension]}"
|
121
107
|
|
@@ -125,22 +111,26 @@ module Rack
|
|
125
111
|
end
|
126
112
|
end
|
127
113
|
|
114
|
+
# Create a set number of the resource and test pagination up
|
115
|
+
# to that number.
|
116
|
+
#
|
128
117
|
def paginate_resource(opts={})
|
129
|
-
|
130
|
-
|
131
|
-
max
|
132
|
-
|
118
|
+
count = opts.fetch(:count, 512)
|
119
|
+
do_create = opts.fetch(:do_create, true)
|
120
|
+
max = opts.fetch(:max_length, 100)
|
121
|
+
read_params = opts.fetch(:read_params, {})
|
122
|
+
start_count = opts.fetch(:existing_resource_count, 0)
|
133
123
|
|
134
124
|
#populate the DB
|
135
125
|
0.upto(count - 1) do |id|
|
136
|
-
if
|
137
|
-
yield(id)
|
138
|
-
else
|
126
|
+
if do_create
|
139
127
|
create_resource(yield(id))
|
128
|
+
else
|
129
|
+
yield(id)
|
140
130
|
end
|
141
131
|
end
|
142
132
|
|
143
|
-
total = count +
|
133
|
+
total = count + start_count
|
144
134
|
retrieved = 0
|
145
135
|
offset = 0
|
146
136
|
|
@@ -155,7 +145,8 @@ module Rack
|
|
155
145
|
puts "Expecting '#{expected_length}'"
|
156
146
|
end
|
157
147
|
|
158
|
-
|
148
|
+
get_params = read_params.merge(offset: offset, length: length)
|
149
|
+
pg_resp = read_resource(get_params)
|
159
150
|
|
160
151
|
with_clean_backtraces do
|
161
152
|
puts "Received #{pg_resp[@rack_test_rest[:resource]].count} records" if @rack_test_rest[:debug]
|
@@ -187,6 +178,36 @@ module Rack
|
|
187
178
|
"Expected status #{code}, but got a #{last_response.status}.\nBody: #{last_response.body.empty? ? "empty" : last_response.body.inspect.chomp}"
|
188
179
|
end
|
189
180
|
|
181
|
+
def handle_error_code(code)
|
182
|
+
assert_status_code(code)
|
183
|
+
|
184
|
+
if @rack_test_rest[:debug]
|
185
|
+
puts "Status: #{last_response.status}"
|
186
|
+
puts "Headers:"
|
187
|
+
puts last_response.headers.inspect
|
188
|
+
puts "Body: #{last_response.body}"
|
189
|
+
end
|
190
|
+
assert_content_type_is_json
|
191
|
+
|
192
|
+
if last_response.headers['Content-Length'].to_i > 0
|
193
|
+
JSON.parse(last_response.body)
|
194
|
+
else
|
195
|
+
nil
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
# remove library lines from call stack so error is reported
|
200
|
+
# where the call to rack-test-rest is being made
|
201
|
+
def with_clean_backtraces
|
202
|
+
yield
|
203
|
+
rescue MiniTest::Assertion => error
|
204
|
+
cleaned = error.backtrace.reject do |line|
|
205
|
+
line.index(/rack-test-rest[-.0-9]{0,}\/lib/)
|
206
|
+
end
|
207
|
+
error.set_backtrace(cleaned)
|
208
|
+
raise
|
209
|
+
end
|
210
|
+
|
190
211
|
end
|
191
212
|
end
|
192
213
|
end
|
metadata
CHANGED
@@ -1,30 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-test-rest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.7.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Joseph Ruscio
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-01-22 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rake
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - '>='
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '0'
|
30
27
|
description: Extends rack-test to simplifies the process of unit testing properly
|
@@ -46,30 +43,26 @@ files:
|
|
46
43
|
homepage: https://github.com/josephruscio/rack-test-rest
|
47
44
|
licenses:
|
48
45
|
- MIT
|
46
|
+
metadata: {}
|
49
47
|
post_install_message:
|
50
48
|
rdoc_options: []
|
51
49
|
require_paths:
|
52
50
|
- lib
|
53
51
|
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
-
none: false
|
55
52
|
requirements:
|
56
|
-
- -
|
53
|
+
- - '>='
|
57
54
|
- !ruby/object:Gem::Version
|
58
55
|
version: '0'
|
59
|
-
segments:
|
60
|
-
- 0
|
61
|
-
hash: -3995603219145648516
|
62
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
-
none: false
|
64
57
|
requirements:
|
65
|
-
- -
|
58
|
+
- - '>='
|
66
59
|
- !ruby/object:Gem::Version
|
67
60
|
version: '0'
|
68
61
|
requirements: []
|
69
62
|
rubyforge_project:
|
70
|
-
rubygems_version:
|
63
|
+
rubygems_version: 2.0.3
|
71
64
|
signing_key:
|
72
|
-
specification_version:
|
65
|
+
specification_version: 4
|
73
66
|
summary: Easy testing of RESTful API's with rack-test and Test::Unit.
|
74
67
|
test_files:
|
75
68
|
- test/helper.rb
|