Module: Rack::Test::Rest
- Defined in:
- lib/rack-test-rest.rb,
lib/rack-test-rest/version.rb
Constant Summary collapse
- VERSION =
'0.6.0'
Instance Method Summary collapse
- #create_resource(params = {}) ⇒ Object
- #delete_resource(params = {}) ⇒ Object
- #handle_error_code(code) ⇒ Object
- #paginate_resource(opts = {}) ⇒ Object
- #read_resource(params = {}) ⇒ Object
- #resource_uri ⇒ Object
- #update_resource(params = {}) ⇒ Object
-
#with_clean_backtraces ⇒ Object
remove library lines from call stack so error is reported where the call to rack-test-rest is being made.
Instance Method Details
#create_resource(params = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rack-test-rest.rb', line 39 def create_resource(params={}) expected_code = params[:code] params.delete :code puts "Posting to: '#{resource_uri}#{@rack_test_rest[:extension]}'" if @rack_test_rest[:debug] post "#{resource_uri}#{@rack_test_rest[:extension]}", params with_clean_backtraces do return handle_error_code(expected_code) if expected_code if @rack_test_rest[:debug] puts "#{last_response.status}: #{last_response.body}" puts last_response.original_headers["Location"] end assert_status_code(201) assert_content_type_is_json if @rack_test_rest[:location] assert last_response.original_headers["Location"] =~ @rack_test_rest[:location], "Response location header '%s' does not match RegExp '%s'" % [last_response.original_headers["Location"], @rack_test_rest[:location]] end end last_response.original_headers["Location"] end |
#delete_resource(params = {}) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/rack-test-rest.rb', line 119 def delete_resource(params={}) delete "#{resource_uri}/#{params[:id]}#{@rack_test_rest[:extension]}" with_clean_backtraces do return handle_error_code(params[:code]) if params[:code] assert_status_code(204) end end |
#handle_error_code(code) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rack-test-rest.rb', line 11 def handle_error_code(code) assert_status_code(code) if @rack_test_rest[:debug] puts "Status: #{last_response.status}" if @rack_test_rest[:debug] puts "Headers:" puts last_response.headers.inspect puts "Body: #{last_response.body}" if @rack_test_rest[:debug] end assert_content_type_is_json if last_response.headers['Content-Length'].to_i > 0 JSON.parse(last_response.body) else nil end end |
#paginate_resource(opts = {}) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/rack-test-rest.rb', line 128 def paginate_resource(opts={}) count = opts[:count] ? opts[:count] : 512 max = opts[:max_length] ? opts[:max_length] : 100 existing_resource_count = opts[:existing_resource_count] || 0 #populate the DB 0.upto(count - 1) do |id| if opts[:do_create] == false yield(id) else create_resource(yield(id)) end end total = count + existing_resource_count retrieved = 0 offset = 0 while retrieved < total # Get a random number from 1-100 length = rand(max - 1) + 1 expected_length = (length > (total - retrieved)) ? (total - retrieved) : length if @rack_test_rest[:debug] puts "Requesting offset='#{offset}', length='#{length}'" puts "Expecting '#{expected_length}'" end pg_resp = read_resource(:offset => offset, :length => length) with_clean_backtraces do puts "Received #{pg_resp[@rack_test_rest[:resource]].count} records" if @rack_test_rest[:debug] assert_equal(expected_length, pg_resp[@rack_test_rest[:resource]].count) puts "Found #{pg_resp["query"]["found"]} records" if @rack_test_rest[:debug] assert_equal(total, pg_resp["query"]["found"]) assert_equal(total, pg_resp["query"]["total"]) assert_equal(expected_length, pg_resp["query"]["length"]) assert_equal(offset, pg_resp["query"]["offset"]) retrieved += expected_length offset = retrieved end end end |
#read_resource(params = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rack-test-rest.rb', line 69 def read_resource(params={}) expected_code = params[:code] params.delete :code if params[:id] id = params[:id] params.delete(:id) uri = "#{resource_uri}/#{id}#{@rack_test_rest[:extension]}" else uri = "#{resource_uri}#{@rack_test_rest[:extension]}" end puts "GET #{uri} #{params.inspect}" if @rack_test_rest[:debug] get uri, params with_clean_backtraces do return handle_error_code(expected_code) if expected_code if @rack_test_rest[:debug] puts "Code: #{last_response.status}" puts "Body: #{last_response.body}" end assert_status_code(200) assert_content_type_is_json end JSON.parse(last_response.body) end |
#resource_uri ⇒ Object
7 8 9 |
# File 'lib/rack-test-rest.rb', line 7 def resource_uri "#{@rack_test_rest[:root_uri]}/#{@rack_test_rest[:resource]}" end |
#update_resource(params = {}) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rack-test-rest.rb', line 101 def update_resource(params={}) expected_code = params[:code] params.delete :code id = params[:id] params.delete(:id) puts "Attempting to update #{id} with #{params.inspect}" if @rack_test_rest[:debug] put "#{resource_uri}/#{id}#{@rack_test_rest[:extension]}", params with_clean_backtraces do return handle_error_code(expected_code) if expected_code puts "#{last_response.status}: #{last_response.body}" if @rack_test_rest[:debug] assert_status_code(204) end end |
#with_clean_backtraces ⇒ Object
remove library lines from call stack so error is reported where the call to rack-test-rest is being made
31 32 33 34 35 36 37 |
# File 'lib/rack-test-rest.rb', line 31 def with_clean_backtraces yield rescue MiniTest::Assertion => error cleaned = error.backtrace.reject {|l| l.index(/rack-test-rest[-.0-9]{0,}\/lib/)} error.set_backtrace(cleaned) raise end |