1 require File.dirname(__FILE__) + '/../test_helper'
5 class AmfControllerTest < ActionController::TestCase
8 # this should be what AMF controller returns when the bbox of a request
9 # is invalid or too large.
10 BOUNDARY_ERROR = [-2,"Sorry - I can't get the map for that area."]
14 id = current_ways(:visible_way).id
15 amf_content "getway", "/1", [id]
17 assert_response :success
19 assert_equal amf_result("/1")[0], id
22 def test_getway_invisible
23 # check an invisible way
24 id = current_ways(:invisible_way).id
25 amf_content "getway", "/1", [id]
27 assert_response :success
29 way = amf_result("/1")
30 assert_equal way[0], id
31 assert way[1].empty? and way[2].empty?
34 def test_getway_nonexistent
35 # check chat a non-existent way is not returned
36 amf_content "getway", "/1", [0]
38 assert_response :success
40 way = amf_result("/1")
41 assert_equal way[0], 0
42 assert way[1].empty? and way[2].empty?
46 node = current_nodes(:used_node_1)
51 amf_content "whichways", "/1", [minlon, minlat, maxlon, maxlat]
53 assert_response :success
56 # check contents of message
58 assert_equal 0, map[0]
59 assert_equal Array, map[1].class
60 assert map[1].include?(current_ways(:used_way).id)
61 assert !map[1].include?(current_ways(:invisible_way).id)
65 # checks that too-large a bounding box will not be served.
66 def test_whichways_toobig
67 bbox = [-0.1,-0.1,1.1,1.1]
68 check_bboxes_are_bad [bbox] do |map|
69 assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
74 # checks that an invalid bounding box will not be served. in this case
75 # one with max < min latitudes.
76 def test_whichways_badlat
77 bboxes = [[0,0.1,0.1,0], [-0.1,80,0.1,70], [0.24,54.34,0.25,54.33]]
78 check_bboxes_are_bad bboxes do |map|
79 assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
84 # same as test_whichways_badlat, but for longitudes
85 def test_whichways_badlon
86 bboxes = [[80,-0.1,70,0.1], [54.34,0.24,54.33,0.25]]
87 check_bboxes_are_bad bboxes do |map|
88 assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
92 def test_whichways_deleted
93 node = current_nodes(:used_node_1)
98 amf_content "whichways_deleted", "/1", [minlon, minlat, maxlon, maxlat]
100 assert_response :success
103 # check contents of message
104 map = amf_result "/1"
105 assert_equal 0, map[0]
106 assert_equal Array, map[1].class
107 assert map[1].include?(current_ways(:used_way).id)
108 assert !map[1].include?(current_ways(:invisible_way).id)
111 def test_whichways_deleted_toobig
112 bbox = [-0.1,-0.1,1.1,1.1]
113 amf_content "whichways_deleted", "/1", bbox
115 assert_response :success
118 map = amf_result "/1"
119 assert_equal BOUNDARY_ERROR, map, "AMF controller should have returned an error."
123 id = current_relations(:visible_relation).id
124 amf_content "getrelation", "/1", [id]
126 assert_response :success
128 assert_equal amf_result("/1")[0], id
131 def test_getrelation_invisible
132 id = current_relations(:invisible_relation).id
133 amf_content "getrelation", "/1", [id]
135 assert_response :success
137 rel = amf_result("/1")
138 assert_equal rel[0], id
139 assert rel[1].empty? and rel[2].empty?
142 def test_getrelation_nonexistent
144 amf_content "getrelation", "/1", [id]
146 assert_response :success
148 rel = amf_result("/1")
149 assert_equal rel[0], id
150 assert rel[1].empty? and rel[2].empty?
154 # try to get the last visible version (specified by <0) (should be current version)
155 latest = current_ways(:way_with_versions)
156 # try to get version 1
157 v1 = ways(:way_with_versions_v1)
158 {latest => -1, v1 => v1.version}.each do |way, v|
159 amf_content "getway_old", "/1", [way.id, v]
161 assert_response :success
163 returned_way = amf_result("/1")
164 assert_equal returned_way[1], way.id
165 assert_equal returned_way[4], way.version
169 def test_getway_old_nonexistent
170 # try to get the last version+10 (shoudn't exist)
171 latest = current_ways(:way_with_versions)
172 # try to get last visible version of non-existent way
173 # try to get specific version of non-existent way
174 {nil => -1, nil => 1, latest => latest.version + 10}.each do |way, v|
175 amf_content "getway_old", "/1", [way.nil? ? 0 : way.id, v]
177 assert_response :success
179 returned_way = amf_result("/1")
180 assert returned_way[2].empty?
181 assert returned_way[3].empty?
182 assert returned_way[4] < 0
186 def test_getway_history
187 latest = current_ways(:way_with_versions)
188 amf_content "getway_history", "/1", [latest.id]
190 assert_response :success
192 history = amf_result("/1")
194 # ['way',wayid,history]
195 assert_equal history[0], 'way'
196 assert_equal history[1], latest.id
197 assert_equal history[2].first[0], latest.version
198 assert_equal history[2].last[0], ways(:way_with_versions_v1).version
201 def test_getway_history_nonexistent
202 amf_content "getway_history", "/1", [0]
204 assert_response :success
206 history = amf_result("/1")
208 # ['way',wayid,history]
209 assert_equal history[0], 'way'
210 assert_equal history[1], 0
211 assert history[2].empty?
214 def test_getnode_history
215 latest = current_nodes(:node_with_versions)
216 amf_content "getnode_history", "/1", [latest.id]
218 assert_response :success
220 history = amf_result("/1")
222 # ['node',nodeid,history]
223 assert_equal history[0], 'node'
224 assert_equal history[1], latest.id
225 assert_equal history[2].first[0], latest.timestamp.to_i
226 assert_equal history[2].last[0], nodes(:node_with_versions_v1).timestamp.to_i
229 def test_getnode_history_nonexistent
230 amf_content "getnode_history", "/1", [0]
232 assert_response :success
234 history = amf_result("/1")
236 # ['node',nodeid,history]
237 assert_equal history[0], 'node'
238 assert_equal history[1], 0
239 assert history[2].empty?
243 # ************************************************************
244 # AMF Helper functions
246 # Get the result record for the specified ID
247 # It's an assertion FAIL if the record does not exist
249 assert @amf_result.has_key?("#{ref}/onResult")
250 @amf_result["#{ref}/onResult"]
253 # Encode the AMF message to invoke "target" with parameters as
254 # the passed data. The ref is used to retrieve the results.
255 def amf_content(target, ref, data)
258 c.write 0.chr+0.chr # version 0
259 c.write 0.chr+0.chr # n headers
260 c.write a.chr+b.chr # n bodies
261 c.write AMF.encodestring(target)
262 c.write AMF.encodestring(ref)
263 c.write [-1].pack("N")
264 c.write AMF.encodevalue(data)
266 @request.env["RAW_POST_DATA"] = c.string
269 # Parses the @response object as an AMF messsage.
270 # The result is a hash of message_ref => data.
271 # The attribute @amf_result is initialised to this hash.
272 def amf_parse_response
273 if @response.body.class.to_s == 'Proc'
275 @response.body.call @response, res
276 req = StringIO.new(res.string)
278 req = StringIO.new(@response.body)
280 req.read(2) # version
282 # parse through any headers
283 headers=AMF.getint(req) # Read number of headers
284 headers.times do # Read each header
285 name=AMF.getstring(req) # |
286 req.getc # | skip boolean
287 value=AMF.getvalue(req) # |
290 # parse through responses
292 bodies=AMF.getint(req) # Read number of bodies
293 bodies.times do # Read each body
294 message=AMF.getstring(req) # | get message name
295 index=AMF.getstring(req) # | get index in response sequence
296 bytes=AMF.getlong(req) # | get total size in bytes
297 args=AMF.getvalue(req) # | get response (probably an array)
298 results[message] = args
300 @amf_result = results
305 # given an array of bounding boxes (each an array of 4 floats), call the
306 # AMF "whichways" controller for each and pass the result back to the
307 # caller's block for assertion testing.
308 def check_bboxes_are_bad(bboxes)
309 bboxes.each do |bbox|
310 amf_content "whichways", "/1", bbox
312 assert_response :success
315 # pass the response back to the caller's block to be tested
316 # against what the caller expected.
317 map = amf_result "/1"