rack-test-rest 0.4.6 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -126,5 +126,5 @@ that a single index operation may return (defaults to 100).
126
126
 
127
127
  # Copyright
128
128
 
129
- Copyright (c) 2011 Joseph Ruscio. See LICENSE.txt for
129
+ Copyright (c) 2011-2013 Joseph Ruscio. See LICENSE.txt for
130
130
  further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.6
1
+ 0.5.0
@@ -1,5 +1,3 @@
1
- require 'pp'
2
-
3
1
  module Rack
4
2
  module Test
5
3
  module Rest
@@ -9,8 +7,7 @@ module Rack
9
7
  end
10
8
 
11
9
  def handle_error_code(code)
12
- assert_equal code, last_response.status,
13
- "Expected #{code}, got #{last_response.status} - body #{last_response.body.empty? ? "empty" : last_response.body.pretty_inspect.chomp}"
10
+ assert_status_code(code)
14
11
 
15
12
  if @rack_test_rest[:debug]
16
13
  puts "Status: #{last_response.status}" if @rack_test_rest[:debug]
@@ -18,7 +15,7 @@ module Rack
18
15
  puts last_response.headers.inspect
19
16
  puts "Body: #{last_response.body}" if @rack_test_rest[:debug]
20
17
  end
21
- assert_content_type_is_json(last_response)
18
+ assert_content_type_is_json
22
19
 
23
20
  if last_response.headers['Content-Length'].to_i > 0
24
21
  JSON.parse(last_response.body)
@@ -27,6 +24,16 @@ module Rack
27
24
  end
28
25
  end
29
26
 
27
+ # remove library lines from call stack so error is reported
28
+ # where the call to rack-test-rest is being made
29
+ def with_clean_backtraces
30
+ yield
31
+ rescue MiniTest::Assertion => error
32
+ cleaned = error.backtrace.reject {|l| l.index('rack-test-rest/lib')}
33
+ error.set_backtrace(cleaned)
34
+ raise
35
+ end
36
+
30
37
  def create_resource(params={})
31
38
  expected_code = params[:code]
32
39
  params.delete :code
@@ -34,18 +41,22 @@ module Rack
34
41
  puts "Posting to: '#{resource_uri}#{@rack_test_rest[:extension]}'" if @rack_test_rest[:debug]
35
42
  post "#{resource_uri}#{@rack_test_rest[:extension]}", params
36
43
 
37
- return handle_error_code(expected_code) if expected_code
44
+ with_clean_backtraces do
38
45
 
39
- if @rack_test_rest[:debug]
40
- puts "#{last_response.status}: #{last_response.body}"
41
- puts last_response.original_headers["Location"]
42
- end
46
+ return handle_error_code(expected_code) if expected_code
43
47
 
44
- assert_equal(201, last_response.status)
45
- assert_content_type_is_json(last_response)
48
+ if @rack_test_rest[:debug]
49
+ puts "#{last_response.status}: #{last_response.body}"
50
+ puts last_response.original_headers["Location"]
51
+ end
52
+
53
+ assert_status_code(201)
54
+ assert_content_type_is_json
55
+
56
+ if @rack_test_rest[:location]
57
+ assert last_response.original_headers["Location"] =~ @rack_test_rest[:location]
58
+ end
46
59
 
47
- if @rack_test_rest[:location]
48
- assert last_response.original_headers["Location"] =~ @rack_test_rest[:location]
49
60
  end
50
61
 
51
62
  last_response.original_headers["Location"]
@@ -63,18 +74,22 @@ module Rack
63
74
  uri = "#{resource_uri}#{@rack_test_rest[:extension]}"
64
75
  end
65
76
 
66
- puts "GET #{uri} #{params.pretty_inspect}" if @rack_test_rest[:debug]
77
+ puts "GET #{uri} #{params.inspect}" if @rack_test_rest[:debug]
67
78
  get uri, params
68
79
 
69
- return handle_error_code(expected_code) if expected_code
80
+ with_clean_backtraces do
70
81
 
71
- if @rack_test_rest[:debug]
72
- puts "Code: #{last_response.status}"
73
- puts "Body: #{last_response.body}"
74
- end
82
+ return handle_error_code(expected_code) if expected_code
75
83
 
76
- assert_content_type_is_json(last_response)
77
- assert_equal(200, last_response.status)
84
+ if @rack_test_rest[:debug]
85
+ puts "Code: #{last_response.status}"
86
+ puts "Body: #{last_response.body}"
87
+ end
88
+
89
+ assert_status_code(200)
90
+ assert_content_type_is_json
91
+
92
+ end
78
93
 
79
94
  JSON.parse(last_response.body)
80
95
  end
@@ -86,23 +101,24 @@ module Rack
86
101
  id = params[:id]
87
102
  params.delete(:id)
88
103
 
89
- puts "Attempting to update #{id} with #{params.pretty_inspect}" if @rack_test_rest[:debug]
104
+ puts "Attempting to update #{id} with #{params.inspect}" if @rack_test_rest[:debug]
90
105
 
91
106
  put "#{resource_uri}/#{id}#{@rack_test_rest[:extension]}", params
92
107
 
93
- return handle_error_code(expected_code) if expected_code
94
-
95
- puts "#{last_response.status}: #{last_response.body}" if @rack_test_rest[:debug]
96
-
97
- assert_equal(204, last_response.status)
108
+ with_clean_backtraces do
109
+ return handle_error_code(expected_code) if expected_code
110
+ puts "#{last_response.status}: #{last_response.body}" if @rack_test_rest[:debug]
111
+ assert_status_code(204)
112
+ end
98
113
  end
99
114
 
100
115
  def delete_resource(params={})
101
116
  delete "#{resource_uri}/#{params[:id]}#{@rack_test_rest[:extension]}"
102
117
 
103
- return handle_error_code(params[:code]) if params[:code]
104
-
105
- assert_equal(204, last_response.status)
118
+ with_clean_backtraces do
119
+ return handle_error_code(params[:code]) if params[:code]
120
+ assert_status_code(204)
121
+ end
106
122
  end
107
123
 
108
124
  def paginate_resource(params={})
@@ -135,27 +151,34 @@ module Rack
135
151
 
136
152
  pg_resp = read_resource(:offset => offset, :length => length)
137
153
 
138
- puts "Received #{pg_resp[@rack_test_rest[:resource]].count} records" if @rack_test_rest[:debug]
139
- assert_equal(expected_length, pg_resp[@rack_test_rest[:resource]].count)
154
+ with_clean_backtraces do
155
+ puts "Received #{pg_resp[@rack_test_rest[:resource]].count} records" if @rack_test_rest[:debug]
156
+ assert_equal(expected_length, pg_resp[@rack_test_rest[:resource]].count)
140
157
 
141
- puts "Found #{pg_resp["query"]["found"]} records" if @rack_test_rest[:debug]
142
- assert_equal(count, pg_resp["query"]["found"])
158
+ puts "Found #{pg_resp["query"]["found"]} records" if @rack_test_rest[:debug]
159
+ assert_equal(count, pg_resp["query"]["found"])
143
160
 
144
- assert_equal(count, pg_resp["query"]["total"])
145
- assert_equal(expected_length, pg_resp["query"]["length"])
146
- assert_equal(offset, pg_resp["query"]["offset"])
161
+ assert_equal(count, pg_resp["query"]["total"])
162
+ assert_equal(expected_length, pg_resp["query"]["length"])
163
+ assert_equal(offset, pg_resp["query"]["offset"])
147
164
 
148
- retrieved += expected_length
149
- offset = retrieved
165
+ retrieved += expected_length
166
+ offset = retrieved
167
+ end
150
168
  end
151
169
  end
152
170
 
153
171
  private
154
172
 
155
- def assert_content_type_is_json(response)
173
+ def assert_content_type_is_json(response=last_response)
156
174
  # ignore character sets when evaluating content type
157
175
  content_type = response.headers['Content-Type'].split(';')[0].strip.downcase
158
- assert_equal 'application/json', content_type
176
+ assert_equal 'application/json', content_type, 'Expected content type to be json'
177
+ end
178
+
179
+ def assert_status_code(code, response=last_response)
180
+ assert_equal code, response.status,
181
+ "Expected status #{code}, but got a #{last_response.status}.\nBody: #{last_response.body.empty? ? "empty" : last_response.body.inspect.chomp}"
159
182
  end
160
183
 
161
184
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "rack-test-rest"
8
- s.version = "0.4.6"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Joseph Ruscio"]
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.6
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  segments:
112
112
  - 0
113
- hash: -1783318799475097374
113
+ hash: -506413952214297023
114
114
  required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  none: false
116
116
  requirements: