rack-test-rest 0.7.1 → 0.8.0
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.
- data/Rakefile +1 -1
- data/lib/rack-test-rest.rb +17 -7
- data/lib/rack-test-rest/version.rb +1 -1
- data/test/fixtures/sample_app.rb +32 -0
- data/test/rack_test_rest_test.rb +94 -0
- data/test/test_helper.rb +12 -0
- metadata +9 -7
- data/test/helper.rb +0 -15
- data/test/test_rack-test-rest.rb +0 -7
data/Rakefile
CHANGED
data/lib/rack-test-rest.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'json'
|
1
2
|
require 'rack-test-rest/version'
|
2
3
|
|
3
4
|
module Rack
|
@@ -15,7 +16,7 @@ module Rack
|
|
15
16
|
# unless the :code option is specified.
|
16
17
|
#
|
17
18
|
def create_resource(params={})
|
18
|
-
expected_code = params
|
19
|
+
id, expected_code, params = _rtr_prepare_params(params)
|
19
20
|
|
20
21
|
puts "Posting to: '#{resource_uri}#{@rack_test_rest[:extension]}'" if @rack_test_rest[:debug]
|
21
22
|
post "#{resource_uri}#{@rack_test_rest[:extension]}", params
|
@@ -51,8 +52,7 @@ module Rack
|
|
51
52
|
end
|
52
53
|
|
53
54
|
def read_resource(params={})
|
54
|
-
id = params
|
55
|
-
expected_code = params.delete(:code)
|
55
|
+
id, expected_code, params = _rtr_prepare_params(params)
|
56
56
|
|
57
57
|
if id
|
58
58
|
uri = "#{resource_uri}/#{id}#{@rack_test_rest[:extension]}"
|
@@ -81,8 +81,7 @@ module Rack
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def update_resource(params={})
|
84
|
-
id = params
|
85
|
-
expected_code = params.delete(:code)
|
84
|
+
id, expected_code, params = _rtr_prepare_params(params)
|
86
85
|
|
87
86
|
puts "Attempting to update #{id} with #{params.inspect}" if @rack_test_rest[:debug]
|
88
87
|
|
@@ -103,10 +102,11 @@ module Rack
|
|
103
102
|
end
|
104
103
|
|
105
104
|
def delete_resource(params={})
|
106
|
-
|
105
|
+
id, code, params = _rtr_prepare_params(params)
|
106
|
+
delete "#{resource_uri}/#{id}#{@rack_test_rest[:extension]}"
|
107
107
|
|
108
108
|
with_clean_backtraces do
|
109
|
-
return handle_error_code(
|
109
|
+
return handle_error_code(code) if code
|
110
110
|
assert_status_code(204)
|
111
111
|
end
|
112
112
|
end
|
@@ -167,6 +167,16 @@ module Rack
|
|
167
167
|
|
168
168
|
private
|
169
169
|
|
170
|
+
# split out common arguments & protect payload to ensure
|
171
|
+
# we don't modify it by reference
|
172
|
+
def _rtr_prepare_params(opts)
|
173
|
+
# symbolize all keys & ensure we don't affect original object
|
174
|
+
params = opts.each_with_object({}) { |(k,v),memo| memo[k.to_sym] = v }
|
175
|
+
id = params.delete(:id)
|
176
|
+
code = params.delete(:code)
|
177
|
+
[id, code, params]
|
178
|
+
end
|
179
|
+
|
170
180
|
def assert_content_type_is_json(response=last_response)
|
171
181
|
# ignore character sets when evaluating content type
|
172
182
|
content_type = response.headers['Content-Type'].split(';')[0].strip.downcase
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module Rack::Test::Rest
|
4
|
+
|
5
|
+
class SampleApp < Sinatra::Base
|
6
|
+
|
7
|
+
get '/v1/users' do
|
8
|
+
headers 'Content-Type' => 'application/json'
|
9
|
+
body '{}'
|
10
|
+
end
|
11
|
+
|
12
|
+
post '/v1/users' do
|
13
|
+
headers 'Content-Type' => 'application/json'
|
14
|
+
body '{}'
|
15
|
+
end
|
16
|
+
|
17
|
+
get '/v1/users/:id' do
|
18
|
+
headers 'Content-Type' => 'application/json'
|
19
|
+
body '{}'
|
20
|
+
end
|
21
|
+
|
22
|
+
put '/v1/users/:id' do
|
23
|
+
status 204
|
24
|
+
end
|
25
|
+
|
26
|
+
delete '/v1/users/:id' do
|
27
|
+
status 204
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestRackTestRest < Minitest::Test
|
4
|
+
include Rack::Test::Methods
|
5
|
+
include Rack::Test::Rest
|
6
|
+
|
7
|
+
def app
|
8
|
+
SampleApp
|
9
|
+
end
|
10
|
+
|
11
|
+
def setup
|
12
|
+
@rack_test_rest = {
|
13
|
+
#debug: true
|
14
|
+
root_uri: '/v1',
|
15
|
+
resource: 'users'
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_read_resource
|
20
|
+
read_resource(id: 15)
|
21
|
+
|
22
|
+
# request
|
23
|
+
assert last_request.get?
|
24
|
+
assert_equal '/v1/users/15', last_request.path
|
25
|
+
|
26
|
+
# response
|
27
|
+
assert last_response.ok?
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_read_should_not_modify_payload
|
31
|
+
payload = {id: 21, foo: 'bar', boom: 'baz'}
|
32
|
+
original = payload.dup
|
33
|
+
|
34
|
+
read_resource(payload)
|
35
|
+
assert_equal original, payload
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_update_resource
|
39
|
+
touched = Time.now
|
40
|
+
update_resource(id: 12, email: 'user@test.com', touched_at: touched)
|
41
|
+
|
42
|
+
# request
|
43
|
+
assert last_request.put?
|
44
|
+
assert_equal '/v1/users/12', last_request.path
|
45
|
+
assert_equal 'user@test.com', last_request.params['email']
|
46
|
+
assert_equal touched.to_s, last_request.params['touched_at']
|
47
|
+
|
48
|
+
# response
|
49
|
+
assert_equal 204, last_response.status
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_update_should_not_modify_payload
|
53
|
+
payload = {id: 21, foo: 'bar', boom: 'baz'}
|
54
|
+
original = payload.dup
|
55
|
+
|
56
|
+
update_resource(payload)
|
57
|
+
assert_equal original, payload
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_delete_resource
|
61
|
+
delete_resource(id: 21)
|
62
|
+
|
63
|
+
# request
|
64
|
+
assert last_request.delete?
|
65
|
+
assert_equal '/v1/users/21', last_request.path
|
66
|
+
|
67
|
+
# response
|
68
|
+
assert_equal 204, last_response.status
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_delete_should_not_modify_payload
|
72
|
+
payload = {id: 21, foo: 'bar', boom: 'baz'}
|
73
|
+
original = payload.dup
|
74
|
+
|
75
|
+
delete_resource(payload)
|
76
|
+
assert_equal original, payload
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_should_accept_string_params
|
80
|
+
# param parsing is DRYed up now, so testing one route
|
81
|
+
# should be adequate
|
82
|
+
|
83
|
+
update_resource('id' => 34, 'email' => 'fred@idk.com')
|
84
|
+
|
85
|
+
# request
|
86
|
+
assert last_request.put?
|
87
|
+
assert_equal '/v1/users/34', last_request.path
|
88
|
+
assert_equal 'fred@idk.com', last_request.params['email']
|
89
|
+
|
90
|
+
# response
|
91
|
+
assert_equal 204, last_response.status
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-test-rest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-02-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -41,8 +41,9 @@ files:
|
|
41
41
|
- LICENSE.txt
|
42
42
|
- Rakefile
|
43
43
|
- README.md
|
44
|
-
- test/
|
45
|
-
- test/
|
44
|
+
- test/fixtures/sample_app.rb
|
45
|
+
- test/rack_test_rest_test.rb
|
46
|
+
- test/test_helper.rb
|
46
47
|
homepage: https://github.com/josephruscio/rack-test-rest
|
47
48
|
licenses:
|
48
49
|
- MIT
|
@@ -58,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
59
|
version: '0'
|
59
60
|
segments:
|
60
61
|
- 0
|
61
|
-
hash:
|
62
|
+
hash: 2737779017982661905
|
62
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
64
|
none: false
|
64
65
|
requirements:
|
@@ -72,5 +73,6 @@ signing_key:
|
|
72
73
|
specification_version: 3
|
73
74
|
summary: Easy testing of RESTful API's with rack-test and Test::Unit.
|
74
75
|
test_files:
|
75
|
-
- test/
|
76
|
-
- test/
|
76
|
+
- test/fixtures/sample_app.rb
|
77
|
+
- test/rack_test_rest_test.rb
|
78
|
+
- test/test_helper.rb
|
data/test/helper.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'bundler/setup'
|
3
|
-
rescue Bundler::BundlerError => e
|
4
|
-
$stderr.puts e.message
|
5
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
6
|
-
exit e.status_code
|
7
|
-
end
|
8
|
-
require 'test/unit'
|
9
|
-
|
10
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
11
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
12
|
-
require 'rack-test-rest'
|
13
|
-
|
14
|
-
class Test::Unit::TestCase
|
15
|
-
end
|