]> git.openstreetmap.org Git - rails.git/blob - test/controllers/amf_controller_test.rb
Fix most auto-correctable rubocop issues
[rails.git] / test / controllers / amf_controller_test.rb
1 require 'test_helper'
2 require 'stringio'
3 include Potlatch
4
5 class AmfControllerTest < ActionController::TestCase
6   api_fixtures
7
8   ##
9   # test all routes which lead to this controller
10   def test_routes
11     assert_routing(
12       { :path => "/api/0.6/amf/read", :method => :post },
13       { :controller => "amf", :action => "amf_read" }
14     )
15     assert_routing(
16       { :path => "/api/0.6/amf/write", :method => :post },
17       { :controller => "amf", :action => "amf_write" }
18     )
19   end
20
21   def test_getway
22     # check a visible way
23     id = current_ways(:visible_way).id
24     amf_content "getway", "/1", [id]
25     post :amf_read
26     assert_response :success
27     amf_parse_response
28     way = amf_result("/1")
29     assert_equal 0, way[0]
30     assert_equal "", way[1]
31     assert_equal id, way[2]
32     assert_equal 1, way[3].length
33     assert_equal 3, way[3][0][2]
34     assert_equal 1, way[5]
35     assert_equal 2, way[6]
36   end
37
38   def test_getway_invisible
39     # check an invisible way
40     id = current_ways(:invisible_way).id
41     amf_content "getway", "/1", [id]
42     post :amf_read
43     assert_response :success
44     amf_parse_response
45     way = amf_result("/1")
46     assert_equal -4, way[0], -4
47     assert_equal "way", way[1]
48     assert_equal id, way[2]
49     assert(way[3].nil?) && way[4].nil? && way[5].nil? && way[6].nil?
50   end
51
52   def test_getway_with_versions
53     # check a way with multiple versions
54     id = current_ways(:way_with_versions).id
55     amf_content "getway", "/1", [id]
56     post :amf_read
57     assert_response :success
58     amf_parse_response
59     way = amf_result("/1")
60     assert_equal 0, way[0]
61     assert_equal "", way[1]
62     assert_equal id, way[2]
63     assert_equal 1, way[3].length
64     assert_equal 15, way[3][0][2]
65     assert_equal 4, way[5]
66     assert_equal 2, way[6]
67   end
68
69   def test_getway_with_duplicate_nodes
70     # check a way with duplicate nodes
71     id = current_ways(:way_with_duplicate_nodes).id
72     amf_content "getway", "/1", [id]
73     post :amf_read
74     assert_response :success
75     amf_parse_response
76     way = amf_result("/1")
77     assert_equal 0, way[0]
78     assert_equal "", way[1]
79     assert_equal id, way[2]
80     assert_equal 2, way[3].length
81     assert_equal 4, way[3][0][2]
82     assert_equal 4, way[3][1][2]
83     assert_equal 1, way[5]
84     assert_equal 2, way[6]
85   end
86
87   def test_getway_with_multiple_nodes
88     # check a way with multiple nodes
89     id = current_ways(:way_with_multiple_nodes).id
90     amf_content "getway", "/1", [id]
91     post :amf_read
92     assert_response :success
93     amf_parse_response
94     way = amf_result("/1")
95     assert_equal 0, way[0]
96     assert_equal "", way[1]
97     assert_equal id, way[2]
98     assert_equal 3, way[3].length
99     assert_equal 4, way[3][0][2]
100     assert_equal 15, way[3][1][2]
101     assert_equal 6, way[3][2][2]
102     assert_equal 2, way[5]
103     assert_equal 2, way[6]
104   end
105
106   def test_getway_nonexistent
107     # check chat a non-existent way is not returned
108     amf_content "getway", "/1", [0]
109     post :amf_read
110     assert_response :success
111     amf_parse_response
112     way = amf_result("/1")
113     assert_equal -4, way[0]
114     assert_equal "way", way[1]
115     assert_equal 0, way[2]
116     assert(way[3].nil?) && way[4].nil? && way[5].nil? && way[6].nil?
117   end
118
119   def test_whichways
120     node = current_nodes(:used_node_1)
121     minlon = node.lon - 0.1
122     minlat = node.lat - 0.1
123     maxlon = node.lon + 0.1
124     maxlat = node.lat + 0.1
125     amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
126     post :amf_read
127     assert_response :success
128     amf_parse_response
129
130     # check contents of message
131     map = amf_result "/1"
132     assert_equal 0, map[0], 'map error code should be 0'
133     assert_equal "", map[1], 'map error text should be empty'
134
135     # check the formatting of the message
136     assert_equal 5, map.length, 'map should have length 5'
137     assert_equal Array, map[2].class, 'map "ways" element should be an array'
138     assert_equal Array, map[3].class, 'map "nodes" element should be an array'
139     assert_equal Array, map[4].class, 'map "relations" element should be an array'
140     map[2].each do |w|
141       assert_equal 2, w.length, 'way should be (id, version) pair'
142       assert w[0] == w[0].floor, 'way ID should be an integer'
143       assert w[1] == w[1].floor, 'way version should be an integer'
144     end
145
146     map[3].each do |n|
147       assert_equal 5, w.length, 'node should be (id, lat, lon, [tags], version) tuple'
148       assert n[0] == n[0].floor, 'node ID should be an integer'
149       assert n[1] >= minlat - 0.01, 'node lat should be greater than min'
150       assert n[1] <= maxlat - 0.01, 'node lat should be less than max'
151       assert n[2] >= minlon - 0.01, 'node lon should be greater than min'
152       assert n[2] <= maxlon - 0.01, 'node lon should be less than max'
153       assert_equal Array, a[3].class, 'node tags should be array'
154       assert n[4] == n[4].floor, 'node version should be an integer'
155     end
156
157     map[4].each do |r|
158       assert_equal 2, r.length, 'relation should be (id, version) pair'
159       assert r[0] == r[0].floor, 'relation ID should be an integer'
160       assert r[1] == r[1].floor, 'relation version should be an integer'
161     end
162
163     # TODO: looks like amf_controller changed since this test was written
164     # so someone who knows what they're doing should check this!
165     ways = map[2].collect { |x| x[0] }
166     assert ways.include?(current_ways(:used_way).id),
167            "map should include used way"
168     assert !ways.include?(current_ways(:invisible_way).id),
169            'map should not include deleted way'
170   end
171
172   ##
173   # checks that too-large a bounding box will not be served.
174   def test_whichways_toobig
175     bbox = [-0.1, -0.1, 1.1, 1.1]
176     check_bboxes_are_bad [bbox] do |map, _bbox|
177       assert_boundary_error map, " The server said: The maximum bbox size is 0.25, and your request was too large. Either request a smaller area, or use planet.osm"
178     end
179   end
180
181   ##
182   # checks that an invalid bounding box will not be served. in this case
183   # one with max < min latitudes.
184   #
185   # NOTE: the controller expands the bbox by 0.01 in each direction!
186   def test_whichways_badlat
187     bboxes = [[0, 0.1, 0.1, 0], [-0.1, 80, 0.1, 70], [0.24, 54.35, 0.25, 54.33]]
188     check_bboxes_are_bad bboxes do |map, bbox|
189       assert_boundary_error map, " The server said: The minimum latitude must be less than the maximum latitude, but it wasn't", bbox.inspect
190     end
191   end
192
193   ##
194   # same as test_whichways_badlat, but for longitudes
195   #
196   # NOTE: the controller expands the bbox by 0.01 in each direction!
197   def test_whichways_badlon
198     bboxes = [[80, -0.1, 70, 0.1], [54.35, 0.24, 54.33, 0.25]]
199     check_bboxes_are_bad bboxes do |map, bbox|
200       assert_boundary_error map, " The server said: The minimum longitude must be less than the maximum longitude, but it wasn't", bbox.inspect
201     end
202   end
203
204   def test_whichways_deleted
205     node = current_nodes(:used_node_1)
206     minlon = node.lon - 0.1
207     minlat = node.lat - 0.1
208     maxlon = node.lon + 0.1
209     maxlat = node.lat + 0.1
210     amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
211     post :amf_read
212     assert_response :success
213     amf_parse_response
214
215     # check contents of message
216     map = amf_result "/1"
217     assert_equal 0, map[0], 'first map element should be 0'
218     assert_equal "", map[1], 'second map element should be an empty string'
219     assert_equal Array, map[2].class, 'third map element should be an array'
220     # TODO: looks like amf_controller changed since this test was written
221     # so someone who knows what they're doing should check this!
222     assert !map[2].include?(current_ways(:used_way).id),
223            "map should not include used way"
224     assert map[2].include?(current_ways(:invisible_way).id),
225            'map should include deleted way'
226   end
227
228   def test_whichways_deleted_toobig
229     bbox = [-0.1, -0.1, 1.1, 1.1]
230     amf_content "whichways_deleted", "/1", bbox
231     post :amf_read
232     assert_response :success
233     amf_parse_response
234
235     map = amf_result "/1"
236     assert_deleted_boundary_error map, " The server said: The maximum bbox size is 0.25, and your request was too large. Either request a smaller area, or use planet.osm"
237   end
238
239   def test_getrelation
240     id = current_relations(:visible_relation).id
241     amf_content "getrelation", "/1", [id]
242     post :amf_read
243     assert_response :success
244     amf_parse_response
245     rel = amf_result("/1")
246     assert_equal rel[0], 0
247     assert_equal rel[2], id
248   end
249
250   def test_getrelation_invisible
251     id = current_relations(:invisible_relation).id
252     amf_content "getrelation", "/1", [id]
253     post :amf_read
254     assert_response :success
255     amf_parse_response
256     rel = amf_result("/1")
257     assert_equal rel[0], -4
258     assert_equal rel[1], "relation"
259     assert_equal rel[2], id
260     assert(rel[3].nil?) && rel[4].nil?
261   end
262
263   def test_getrelation_nonexistent
264     id = 0
265     amf_content "getrelation", "/1", [id]
266     post :amf_read
267     assert_response :success
268     amf_parse_response
269     rel = amf_result("/1")
270     assert_equal rel[0], -4
271     assert_equal rel[1], "relation"
272     assert_equal rel[2], id
273     assert(rel[3].nil?) && rel[4].nil?
274   end
275
276   def test_getway_old
277     # try to get the last visible version (specified by <0) (should be current version)
278     latest = current_ways(:way_with_versions)
279     # NOTE: looks from the API changes that this now expects a timestamp
280     # instead of a version number...
281     # try to get version 1
282     v1 = ways(:way_with_versions_v1)
283     { latest.id => '',
284       v1.way_id => v1.timestamp.strftime("%d %b %Y, %H:%M:%S")
285     }.each do |id, t|
286       amf_content "getway_old", "/1", [id, t]
287       post :amf_read
288       assert_response :success
289       amf_parse_response
290       returned_way = amf_result("/1")
291       assert_equal 0, returned_way[0]
292       assert_equal id, returned_way[2]
293       # API returns the *latest* version, even for old ways...
294       assert_equal latest.version, returned_way[5]
295     end
296   end
297
298   ##
299   # test that the server doesn't fall over when rubbish is passed
300   # into the method args.
301   def test_getway_old_invalid
302     way_id = current_ways(:way_with_versions).id
303     { "foo"  => "bar",
304       way_id => "not a date",
305       way_id => "2009-03-25 00:00:00", # <- wrong format
306       way_id => "0 Jan 2009 00:00:00", # <- invalid date
307       -1     => "1 Jan 2009 00:00:00"  # <- invalid ID
308     }.each do |id, t|
309       amf_content "getway_old", "/1", [id, t]
310       post :amf_read
311       assert_response :success
312       amf_parse_response
313       returned_way = amf_result("/1")
314       assert_equal -1, returned_way[0]
315       assert returned_way[3].nil?
316       assert returned_way[4].nil?
317       assert returned_way[5].nil?
318     end
319   end
320
321   def test_getway_old_nonexistent
322     # try to get the last version+10 (shoudn't exist)
323     v1 = ways(:way_with_versions_v1)
324     # try to get last visible version of non-existent way
325     # try to get specific version of non-existent way
326     [[0, ''],
327      [0, '1 Jan 1970, 00:00:00'],
328      [v1.way_id, (v1.timestamp - 10).strftime("%d %b %Y, %H:%M:%S")]
329     ].each do |id, t|
330       amf_content "getway_old", "/1", [id, t]
331       post :amf_read
332       assert_response :success
333       amf_parse_response
334       returned_way = amf_result("/1")
335       assert_equal -1, returned_way[0]
336       assert returned_way[3].nil?
337       assert returned_way[4].nil?
338       assert returned_way[5].nil?
339     end
340   end
341
342   def test_getway_history
343     latest = current_ways(:way_with_versions)
344     oldest = ways(:way_with_versions_v1)
345
346     amf_content "getway_history", "/1", [latest.id]
347     post :amf_read
348     assert_response :success
349     amf_parse_response
350     history = amf_result("/1")
351
352     # ['way',wayid,history]
353     assert_equal 'way', history[0]
354     assert_equal latest.id, history[1]
355     # We use dates rather than version numbers here, because you might
356     # have moved a node within a way (i.e. way version not incremented).
357     # The timestamp is +1 because we say "give me the revision of 15:33:02",
358     # but that might actually include changes at 15:33:02.457.
359     assert_equal (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].first[0]
360     assert_equal (oldest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"), history[2].last[0]
361   end
362
363   def test_getway_history_nonexistent
364     amf_content "getway_history", "/1", [0]
365     post :amf_read
366     assert_response :success
367     amf_parse_response
368     history = amf_result("/1")
369
370     # ['way',wayid,history]
371     assert_equal history[0], 'way'
372     assert_equal history[1], 0
373     assert history[2].empty?
374   end
375
376   def test_getnode_history
377     latest = current_nodes(:node_with_versions)
378     amf_content "getnode_history", "/1", [latest.id]
379     post :amf_read
380     assert_response :success
381     amf_parse_response
382     history = amf_result("/1")
383
384     # ['node',nodeid,history]
385     # note that (as per getway_history) we actually round up
386     # to the next second
387     assert_equal history[0], 'node',
388                  'first element should be "node"'
389     assert_equal history[1], latest.id,
390                  'second element should be the input node ID'
391     assert_equal history[2].first[0],
392                  (latest.timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
393                  'first element in third element (array) should be the latest version'
394     assert_equal history[2].last[0],
395                  (nodes(:node_with_versions_v1).timestamp + 1).strftime("%d %b %Y, %H:%M:%S"),
396                  'last element in third element (array) should be the initial version'
397   end
398
399   def test_getnode_history_nonexistent
400     amf_content "getnode_history", "/1", [0]
401     post :amf_read
402     assert_response :success
403     amf_parse_response
404     history = amf_result("/1")
405
406     # ['node',nodeid,history]
407     assert_equal history[0], 'node'
408     assert_equal history[1], 0
409     assert history[2].empty?
410   end
411
412   # ************************************************************
413   # AMF Write tests
414   def test_putpoi_update_valid
415     nd = current_nodes(:visible_node)
416     cs_id = changesets(:public_user_first_change).id
417     amf_content "putpoi", "/1", ["test@example.com:test", cs_id, nd.version, nd.id, nd.lon, nd.lat, nd.tags, nd.visible]
418     post :amf_write
419     assert_response :success
420     amf_parse_response
421     result = amf_result("/1")
422
423     assert_equal 0, result[0]
424     assert_equal "", result[1]
425     assert_equal nd.id, result[2]
426     assert_equal nd.id, result[3]
427     assert_equal nd.version + 1, result[4]
428
429     # Now try to update again, with a different lat/lon, using the updated version number
430     lat = nd.lat + 0.1
431     lon = nd.lon - 0.1
432     amf_content "putpoi", "/2", ["test@example.com:test", cs_id, nd.version + 1, nd.id, lon, lat, nd.tags, nd.visible]
433     post :amf_write
434     assert_response :success
435     amf_parse_response
436     result = amf_result("/2")
437
438     assert_equal 0, result[0]
439     assert_equal "", result[1]
440     assert_equal nd.id, result[2]
441     assert_equal nd.id, result[3]
442     assert_equal nd.version + 2, result[4]
443   end
444
445   # Check that we can create a no valid poi
446   # Using similar method for the node controller test
447   def test_putpoi_create_valid
448     # This node has no tags
449     nd = Node.new
450     # create a node with random lat/lon
451     lat = rand(100) - 50 + rand
452     lon = rand(100) - 50 + rand
453     # normal user has a changeset open
454     changeset = changesets(:public_user_first_change)
455
456     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, {}, nil]
457     post :amf_write
458     assert_response :success
459     amf_parse_response
460     result = amf_result("/1")
461
462     # check the array returned by the amf
463     assert_equal 5, result.size
464     assert_equal 0, result[0], "expected to get the status ok from the amf"
465     assert_equal 0, result[2], "The old id should be 0"
466     assert result[3] > 0, "The new id should be greater than 0"
467     assert_equal 1, result[4], "The new version should be 1"
468
469     # Finally check that the node that was saved has saved the data correctly
470     # in both the current and history tables
471     # First check the current table
472     current_node = Node.find(result[3].to_i)
473     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
474     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
475     assert_equal 0, current_node.tags.size, "There seems to be a tag that has been added to the node"
476     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
477     # Now check the history table
478     historic_nodes = Node.where(:id => result[3])
479     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
480     first_historic_node = historic_nodes.first
481     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
482     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
483     assert_equal 0, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
484     assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
485
486     ####
487     # This node has some tags
488     tnd = Node.new
489     # create a node with random lat/lon
490     lat = rand(100) - 50 + rand
491     lon = rand(100) - 50 + rand
492     # normal user has a changeset open
493     changeset = changesets(:public_user_first_change)
494
495     amf_content "putpoi", "/2", ["test@example.com:test", changeset.id, nil, nil, lon, lat, { "key" => "value", "ping" => "pong" }, nil]
496     post :amf_write
497     assert_response :success
498     amf_parse_response
499     result = amf_result("/2")
500
501     # check the array returned by the amf
502     assert_equal 5, result.size
503     assert_equal 0, result[0], "Expected to get the status ok in the amf"
504     assert_equal 0, result[2], "The old id should be 0"
505     assert result[3] > 0, "The new id should be greater than 0"
506     assert_equal 1, result[4], "The new version should be 1"
507
508     # Finally check that the node that was saved has saved the data correctly
509     # in both the current and history tables
510     # First check the current table
511     current_node = Node.find(result[3].to_i)
512     assert_in_delta lat, current_node.lat, 0.00001, "The latitude was not retreieved correctly"
513     assert_in_delta lon, current_node.lon, 0.00001, "The longitude was not retreived correctly"
514     assert_equal 2, current_node.tags.size, "There seems to be a tag that has been added to the node"
515     assert_equal({ "key" => "value", "ping" => "pong" }, current_node.tags, "tags are different")
516     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
517     # Now check the history table
518     historic_nodes = Node.where(:id => result[3])
519     assert_equal 1, historic_nodes.size, "There should only be one historic node created"
520     first_historic_node = historic_nodes.first
521     assert_in_delta lat, first_historic_node.lat, 0.00001, "The latitude was not retreived correctly"
522     assert_in_delta lon, first_historic_node.lon, 0.00001, "The longitude was not retreuved correctly"
523     assert_equal 2, first_historic_node.tags.size, "There seems to be a tag that have been attached to this node"
524     assert_equal({ "key" => "value", "ping" => "pong" }, first_historic_node.tags, "tags are different")
525     assert_equal result[4], first_historic_node.version, "The version returned, is different to the one returned by the amf"
526   end
527
528   # try creating a POI with rubbish in the tags
529   def test_putpoi_create_with_control_chars
530     # This node has no tags
531     nd = Node.new
532     # create a node with random lat/lon
533     lat = rand(100) - 50 + rand
534     lon = rand(100) - 50 + rand
535     # normal user has a changeset open
536     changeset = changesets(:public_user_first_change)
537
538     mostly_invalid = (0..31).to_a.map(&:chr).join
539     tags = { "something" => "foo#{mostly_invalid}bar" }
540
541     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
542     post :amf_write
543     assert_response :success
544     amf_parse_response
545     result = amf_result("/1")
546
547     # check the array returned by the amf
548     assert_equal 5, result.size
549     assert_equal 0, result[0], "Expected to get the status ok in the amf"
550     assert_equal 0, result[2], "The old id should be 0"
551     assert result[3] > 0, "The new id should be greater than 0"
552     assert_equal 1, result[4], "The new version should be 1"
553
554     # Finally check that the node that was saved has saved the data correctly
555     # in both the current and history tables
556     # First check the current table
557     current_node = Node.find(result[3].to_i)
558     assert_equal 1, current_node.tags.size, "There seems to be a tag that has been added to the node"
559     assert_equal({ "something" => "foo\t\n\rbar" }, current_node.tags, "tags were not fixed correctly")
560     assert_equal result[4], current_node.version, "The version returned, is different to the one returned by the amf"
561   end
562
563   # try creating a POI with rubbish in the tags
564   def test_putpoi_create_with_invalid_utf8
565     # This node has no tags
566     nd = Node.new
567     # create a node with random lat/lon
568     lat = rand(100) - 50 + rand
569     lon = rand(100) - 50 + rand
570     # normal user has a changeset open
571     changeset = changesets(:public_user_first_change)
572
573     invalid = "\xc0\xc0"
574     tags = { "something" => "foo#{invalid}bar" }
575
576     amf_content "putpoi", "/1", ["test@example.com:test", changeset.id, nil, nil, lon, lat, tags, nil]
577     post :amf_write
578     assert_response :success
579     amf_parse_response
580     result = amf_result("/1")
581
582     assert_equal 2, result.size
583     assert_equal -1, result[0], "Expected to get the status FAIL in the amf"
584     assert_equal "One of the tags is invalid. Linux users may need to upgrade to Flash Player 10.1.", result[1]
585   end
586
587   def test_putpoi_delete_valid
588   end
589
590   def test_putpoi_delete_already_deleted
591   end
592
593   def test_putpoi_delete_not_found
594   end
595
596   def test_putpoi_invalid_latlon
597   end
598
599   def test_startchangeset_invalid_xmlchar_comment
600     invalid = "\035\022"
601     comment = "foo#{invalid}bar"
602
603     amf_content "startchangeset", "/1", ["test@example.com:test", {}, nil, comment, 1]
604     post :amf_write
605     assert_response :success
606     amf_parse_response
607     result = amf_result("/1")
608
609     assert_equal 3, result.size, result.inspect
610     assert_equal 0, result[0]
611     new_cs_id = result[2].to_i
612
613     cs = Changeset.find(new_cs_id)
614     assert_equal "foobar", cs.tags["comment"]
615   end
616
617   # ************************************************************
618   # AMF Helper functions
619
620   # Get the result record for the specified ID
621   # It's an assertion FAIL if the record does not exist
622   def amf_result(ref)
623     assert @amf_result.key?("#{ref}/onResult")
624     @amf_result["#{ref}/onResult"]
625   end
626
627   # Encode the AMF message to invoke "target" with parameters as
628   # the passed data. The ref is used to retrieve the results.
629   def amf_content(target, ref, data)
630     a, b = 1.divmod(256)
631     c = StringIO.new
632     c.write 0.chr + 0.chr   # version 0
633     c.write 0.chr + 0.chr   # n headers
634     c.write a.chr + b.chr   # n bodies
635     c.write AMF.encodestring(target)
636     c.write AMF.encodestring(ref)
637     c.write [-1].pack("N")
638     c.write AMF.encodevalue(data)
639
640     @request.env["RAW_POST_DATA"] = c.string
641   end
642
643   # Parses the @response object as an AMF messsage.
644   # The result is a hash of message_ref => data.
645   # The attribute @amf_result is initialised to this hash.
646   def amf_parse_response
647     req = StringIO.new(@response.body)
648
649     req.read(2)   # version
650
651     # parse through any headers
652     headers = AMF.getint(req)                                   # Read number of headers
653     headers.times do                                            # Read each header
654       name = AMF.getstring(req)                         #  |
655       req.getc                                                          #  | skip boolean
656       value = AMF.getvalue(req)                         #  |
657     end
658
659     # parse through responses
660     results = {}
661     bodies = AMF.getint(req)                                    # Read number of bodies
662     bodies.times do                                                     # Read each body
663       message = AMF.getstring(req)                      #  | get message name
664       index = AMF.getstring(req)                                #  | get index in response sequence
665       bytes = AMF.getlong(req)                          #  | get total size in bytes
666       args = AMF.getvalue(req)                          #  | get response (probably an array)
667       results[message] = args
668     end
669     @amf_result = results
670     results
671   end
672
673   ##
674   # given an array of bounding boxes (each an array of 4 floats), call the
675   # AMF "whichways" controller for each and pass the result back to the
676   # caller's block for assertion testing.
677   def check_bboxes_are_bad(bboxes)
678     bboxes.each do |bbox|
679       amf_content "whichways", "/1", bbox
680       post :amf_read
681       assert_response :success
682       amf_parse_response
683
684       # pass the response back to the caller's block to be tested
685       # against what the caller expected.
686       map = amf_result "/1"
687       yield map, bbox
688     end
689   end
690
691   # this should be what AMF controller returns when the bbox of a
692   # whichways request is invalid or too large.
693   def assert_boundary_error(map, msg = nil, error_hint = nil)
694     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
695     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
696   end
697
698   # this should be what AMF controller returns when the bbox of a
699   # whichways_deleted request is invalid or too large.
700   def assert_deleted_boundary_error(map, msg = nil, error_hint = nil)
701     expected_map = [-2, "Sorry - I can't get the map for that area.#{msg}"]
702     assert_equal expected_map, map, "AMF controller should have returned an error. (#{error_hint})"
703   end
704 end