Module: Rack::Test::Rest

Defined in:
lib/rack-test-rest.rb,
lib/rack-test-rest/version.rb

Constant Summary collapse

VERSION =
'0.7.0'

Instance Method Summary collapse

Instance Method Details

#create_resource(params = {}) ⇒ Object

create a new instance of the given resource, expecting a 201 unless the :code option is specified.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rack-test-rest.rb', line 17

def create_resource(params={})
  expected_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

#create_resource_invalid(opts) ⇒ Object

create resource, but expect a 400 - helper for the common case of testing invalid parameters for creating your resource.



49
50
51
# File 'lib/rack-test-rest.rb', line 49

def create_resource_invalid(opts)
  create_resource({code: 400}.merge(opts))
end

#delete_resource(params = {}) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/rack-test-rest.rb', line 105

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

#paginate_resource(opts = {}) ⇒ Object

Create a set number of the resource and test pagination up to that number.



117
118
119
120
121
122
123
124
125
126
127
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
# File 'lib/rack-test-rest.rb', line 117

def paginate_resource(opts={})
  count       = opts.fetch(:count, 512)
  do_create   = opts.fetch(:do_create, true)
  max         = opts.fetch(:max_length, 100)
  read_params = opts.fetch(:read_params, {})
  start_count = opts.fetch(:existing_resource_count, 0)

  #populate the DB
  0.upto(count - 1) do |id|
    if do_create
      create_resource(yield(id))
    else
      yield(id)
    end
  end

  total = count + start_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

    get_params = read_params.merge(offset: offset, length: length)
    pg_resp = read_resource(get_params)

    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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rack-test-rest.rb', line 53

def read_resource(params={})
  id = params.delete(:id)
  expected_code = params.delete(:code)

  if 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_uriObject

defines expected resource-to-uri scheme. overload this in your class if needed.



10
11
12
# File 'lib/rack-test-rest.rb', line 10

def resource_uri
  "#{@rack_test_rest[:root_uri]}/#{@rack_test_rest[:resource]}"
end

#update_resource(params = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rack-test-rest.rb', line 83

def update_resource(params={})
  id = params.delete(:id)
  expected_code = params.delete(:code)

  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

#update_resource_invalid(opts) ⇒ Object

update resource, but expect a 400 - helper for the common case of testing invalid parameters for updating your resource.



101
102
103
# File 'lib/rack-test-rest.rb', line 101

def update_resource_invalid(opts)
  update_resource({code: 400}.merge(opts))
end