+
+ def test_getway_old
+ # try to get the last visible version (specified by <0) (should be current version)
+ latest = current_ways(:way_with_versions)
+ # try to get version 1
+ v1 = ways(:way_with_versions_v1)
+ {latest => -1, v1 => v1.version}.each do |way, v|
+ amf_content "getway_old", "/1", [way.id, v]
+ post :amf_read
+ assert_response :success
+ amf_parse_response
+ returned_way = amf_result("/1")
+ assert_equal returned_way[1], way.id
+ assert_equal returned_way[4], way.version
+ end
+ end
+
+ def test_getway_old_nonexistent
+ # try to get the last version+10 (shoudn't exist)
+ latest = current_ways(:way_with_versions)
+ # try to get last visible version of non-existent way
+ # try to get specific version of non-existent way
+ {nil => -1, nil => 1, latest => latest.version + 10}.each do |way, v|
+ amf_content "getway_old", "/1", [way.nil? ? 0 : way.id, v]
+ post :amf_read
+ assert_response :success
+ amf_parse_response
+ returned_way = amf_result("/1")
+ assert returned_way[2].empty?
+ assert returned_way[3].empty?
+ assert returned_way[4] < 0
+ end
+ end
+
+ def test_getway_history
+ latest = current_ways(:way_with_versions)
+ amf_content "getway_history", "/1", [latest.id]
+ post :amf_read
+ assert_response :success
+ amf_parse_response
+ history = amf_result("/1")
+
+ # ['way',wayid,history]
+ assert_equal history[0], 'way'
+ assert_equal history[1], latest.id
+ assert_equal history[2].first[0], latest.version
+ assert_equal history[2].last[0], ways(:way_with_versions_v1).version
+ end
+
+ def test_getway_history_nonexistent
+ amf_content "getway_history", "/1", [0]
+ post :amf_read
+ assert_response :success
+ amf_parse_response
+ history = amf_result("/1")
+
+ # ['way',wayid,history]
+ assert_equal history[0], 'way'
+ assert_equal history[1], 0
+ assert history[2].empty?
+ end
+
+ def test_getnode_history
+ latest = current_nodes(:node_with_versions)
+ amf_content "getnode_history", "/1", [latest.id]
+ post :amf_read
+ assert_response :success
+ amf_parse_response
+ history = amf_result("/1")
+
+ # ['node',nodeid,history]
+ assert_equal history[0], 'node'
+ assert_equal history[1], latest.id
+ assert_equal history[2].first[0], latest.timestamp.to_i
+ assert_equal history[2].last[0], nodes(:node_with_versions_v1).timestamp.to_i
+ end
+
+ def test_getnode_history_nonexistent
+ amf_content "getnode_history", "/1", [0]
+ post :amf_read
+ assert_response :success
+ amf_parse_response
+ history = amf_result("/1")
+
+ # ['node',nodeid,history]
+ assert_equal history[0], 'node'
+ assert_equal history[1], 0
+ assert history[2].empty?
+ end
+
+
+ # ************************************************************
+ # AMF Helper functions
+
+ # Get the result record for the specified ID
+ # It's an assertion FAIL if the record does not exist
+ def amf_result ref
+ assert @amf_result.has_key?("#{ref}/onResult")
+ @amf_result["#{ref}/onResult"]
+ end
+
+ # Encode the AMF message to invoke "target" with parameters as
+ # the passed data. The ref is used to retrieve the results.
+ def amf_content(target, ref, data)
+ a,b=1.divmod(256)
+ c = StringIO.new()
+ c.write 0.chr+0.chr # version 0
+ c.write 0.chr+0.chr # n headers
+ c.write a.chr+b.chr # n bodies
+ c.write AMF.encodestring(target)
+ c.write AMF.encodestring(ref)
+ c.write [-1].pack("N")
+ c.write AMF.encodevalue(data)
+
+ @request.env["RAW_POST_DATA"] = c.string
+ end
+
+ # Parses the @response object as an AMF messsage.
+ # The result is a hash of message_ref => data.
+ # The attribute @amf_result is initialised to this hash.
+ def amf_parse_response
+ if @response.body.class.to_s == 'Proc'
+ res = StringIO.new()
+ @response.body.call @response, res
+ req = StringIO.new(res.string)
+ else
+ req = StringIO.new(@response.body)
+ end
+ req.read(2) # version
+
+ # parse through any headers
+ headers=AMF.getint(req) # Read number of headers
+ headers.times do # Read each header
+ name=AMF.getstring(req) # |
+ req.getc # | skip boolean
+ value=AMF.getvalue(req) # |
+ end
+
+ # parse through responses
+ results = {}
+ bodies=AMF.getint(req) # Read number of bodies
+ bodies.times do # Read each body
+ message=AMF.getstring(req) # | get message name
+ index=AMF.getstring(req) # | get index in response sequence
+ bytes=AMF.getlong(req) # | get total size in bytes
+ args=AMF.getvalue(req) # | get response (probably an array)
+ results[message] = args
+ end
+ @amf_result = results
+ results
+ end
+