4 class MapControllerTest < ActionDispatch::IntegrationTest
7 @badbigbbox = %w[-0.1,-0.1,1.1,1.1 10,10,11,11]
8 @badmalformedbbox = %w[-0.1 hello
10 @badlatmixedbbox = %w[0,0.1,0.1,0 -0.1,80,0.1,70 0.24,54.34,0.25,54.33]
11 @badlonmixedbbox = %w[80,-0.1,70,0.1 54.34,0.24,54.33,0.25]
12 # @badlatlonoutboundsbbox = %w{ 191,-0.1,193,0.1 -190.1,89.9,-190,90 }
13 @goodbbox = %w[-0.1,-0.1,0.1,0.1 51.1,-0.1,51.2,0
14 -0.1,%20-0.1,%200.1,%200.1 -0.1edcd,-0.1d,0.1,0.1 -0.1E,-0.1E,0.1S,0.1N S0.1,W0.1,N0.1,E0.1]
15 # That last item in the goodbbox really shouldn't be there, as the API should
16 # really reject it, however this is to test to see if the api changes.
20 # test all routes which lead to this controller
23 { :path => "/api/0.6/map", :method => :get },
24 { :controller => "api/map", :action => "index" }
27 { :path => "/api/0.6/map.json", :method => :get },
28 { :controller => "api/map", :action => "index", :format => "json" }
33 # test http accept headers
34 def test_http_accept_header
37 minlon = node.lon - 0.1
38 minlat = node.lat - 0.1
39 maxlon = node.lon + 0.1
40 maxlat = node.lat + 0.1
41 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
43 # Accept: XML format -> use XML
44 accept_header = accept_format_header("text/xml")
45 get map_path(:bbox => bbox), :headers => accept_header
46 assert_response :success, "Expected success with the map call"
47 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
49 # Accept: Any format -> use XML
50 accept_header = accept_format_header("*/*")
51 get map_path(:bbox => bbox), :headers => accept_header
52 assert_response :success, "Expected success with the map call"
53 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
55 # Accept: Any format, .json URL suffix -> use json
56 accept_header = accept_format_header("*/*")
57 get map_path(:bbox => bbox, :format => "json"), :headers => accept_header
58 assert_response :success, "Expected success with the map call"
59 assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
61 # Accept: Firefox header -> use XML
62 accept_header = accept_format_header("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
63 get map_path(:bbox => bbox), :headers => accept_header
64 assert_response :success, "Expected success with the map call"
65 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
67 # Accept: JOSM header text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 -> use XML
68 # Note: JOSM's header does not comply with RFC 7231, section 5.3.1
69 accept_header = accept_format_header("text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2")
70 get map_path(:bbox => bbox), :headers => accept_header
71 assert_response :success, "Expected success with the map call"
72 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
74 # Accept: text/plain, */* -> use XML
75 accept_header = accept_format_header("text/plain, */*")
76 get map_path(:bbox => bbox), :headers => accept_header
77 assert_response :success, "Expected success with the map call"
78 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
80 # Accept: text/* -> use XML
81 accept_header = accept_format_header("text/*")
82 get map_path(:bbox => bbox), :headers => accept_header
83 assert_response :success, "Expected success with the map call"
84 assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
86 # Accept: json, */* format -> use json
87 accept_header = accept_format_header("application/json, */*")
88 get map_path(:bbox => bbox), :headers => accept_header
89 assert_response :success, "Expected success with the map call"
90 assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
92 # Accept: json format -> use json
93 accept_header = accept_format_header("application/json")
94 get map_path(:bbox => bbox), :headers => accept_header
95 assert_response :success, "Expected success with the map call"
96 assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
98 # text/json is in invalid format, return HTTP 406 Not acceptable
99 accept_header = accept_format_header("text/json")
100 get map_path(:bbox => bbox), :headers => accept_header
101 assert_response :not_acceptable, "text/json should fail"
103 # image/jpeg is a format which we don't support, return HTTP 406 Not acceptable
104 accept_header = accept_format_header("image/jpeg")
105 get map_path(:bbox => bbox), :headers => accept_header
106 assert_response :not_acceptable, "text/json should fail"
109 # -------------------------------------
110 # Test reading a bounding box.
111 # -------------------------------------
114 node = create(:node, :lat => 7, :lon => 7)
115 tag = create(:node_tag, :node => node)
116 way1 = create(:way_node, :node => node).way
117 way2 = create(:way_node, :node => node).way
118 relation = create(:relation_member, :member => node).relation
120 # Need to split the min/max lat/lon out into their own variables here
121 # so that we can test they are returned later.
122 minlon = node.lon - 0.1
123 minlat = node.lat - 0.1
124 maxlon = node.lon + 0.1
125 maxlat = node.lat + 0.1
126 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
127 get map_path(:bbox => bbox)
129 print @request.to_yaml
132 assert_response :success, "Expected success with the map call"
133 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
134 assert_select "bounds[minlon='#{format('%<lon>.7f', :lon => minlon)}']" \
135 "[minlat='#{format('%<lat>.7f', :lat => minlat)}']" \
136 "[maxlon='#{format('%<lon>.7f', :lon => maxlon)}']" \
137 "[maxlat='#{format('%<lat>.7f', :lat => maxlat)}']", :count => 1
138 assert_select "node[id='#{node.id}']" \
139 "[lat='#{format('%<lat>.7f', :lat => node.lat)}']" \
140 "[lon='#{format('%<lon>.7f', :lon => node.lon)}']" \
141 "[version='#{node.version}']" \
142 "[changeset='#{node.changeset_id}']" \
143 "[visible='#{node.visible}']" \
144 "[timestamp='#{node.timestamp.xmlschema}']", :count => 1 do
145 # This should really be more generic
146 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
148 assert_select "way", :count => 2
149 assert_select "way[id='#{way1.id}']", :count => 1
150 assert_select "way[id='#{way2.id}']", :count => 1
151 assert_select "relation", :count => 1
152 assert_select "relation[id='#{relation.id}']", :count => 1
157 node = create(:node, :lat => 7, :lon => 7)
158 tag = create(:node_tag, :node => node)
159 way1 = create(:way_node, :node => node).way
160 way2 = create(:way_node, :node => node).way
161 relation = create(:relation_member, :member => node).relation
163 # Need to split the min/max lat/lon out into their own variables here
164 # so that we can test they are returned later.
165 minlon = node.lon - 0.1
166 minlat = node.lat - 0.1
167 maxlon = node.lon + 0.1
168 maxlat = node.lat + 0.1
169 bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
170 get map_path(:bbox => bbox, :format => "json")
172 print @request.to_yaml
175 assert_response :success, "Expected success with the map call"
176 js = ActiveSupport::JSON.decode(@response.body)
179 assert_equal Settings.api_version, js["version"]
180 assert_equal Settings.generator, js["generator"]
181 assert_equal GeoRecord::Coord.new(minlon), js["bounds"]["minlon"]
182 assert_equal GeoRecord::Coord.new(minlat), js["bounds"]["minlat"]
183 assert_equal GeoRecord::Coord.new(maxlon), js["bounds"]["maxlon"]
184 assert_equal GeoRecord::Coord.new(maxlat), js["bounds"]["maxlat"]
186 result_nodes = js["elements"].select { |a| a["type"] == "node" }
187 .select { |a| a["id"] == node.id }
188 .select { |a| a["lat"] == GeoRecord::Coord.new(node.lat) }
189 .select { |a| a["lon"] == GeoRecord::Coord.new(node.lon) }
190 .select { |a| a["version"] == node.version }
191 .select { |a| a["changeset"] == node.changeset_id }
192 .select { |a| a["timestamp"] == node.timestamp.xmlschema }
193 assert_equal(1, result_nodes.count)
194 result_node = result_nodes.first
196 assert_equal result_node["tags"], tag.k => tag.v
197 assert_equal 2, (js["elements"].count { |a| a["type"] == "way" })
198 assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way1.id })
199 assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way2.id })
200 assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" })
201 assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" && a["id"] == relation.id })
204 # This differs from the above test in that we are making the bbox exactly
205 # the same as the node we are looking at
206 def test_map_inclusive
207 node = create(:node, :lat => 7, :lon => 7)
208 tag = create(:node_tag, :node => node)
209 way1 = create(:way_node, :node => node).way
210 way2 = create(:way_node, :node => node).way
211 relation = create(:relation_member, :member => node).relation
213 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
214 get map_path(:bbox => bbox)
215 assert_response :success, "The map call should have succeeded"
216 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
217 assert_select "bounds[minlon='#{node.lon}']" \
218 "[minlat='#{node.lat}']" \
219 "[maxlon='#{node.lon}']" \
220 "[maxlat='#{node.lat}']", :count => 1
221 assert_select "node[id='#{node.id}']" \
222 "[lat='#{format('%<lat>.7f', :lat => node.lat)}']" \
223 "[lon='#{format('%<lon>.7f', :lon => node.lon)}']" \
224 "[version='#{node.version}']" \
225 "[changeset='#{node.changeset_id}']" \
226 "[visible='#{node.visible}']" \
227 "[timestamp='#{node.timestamp.xmlschema}']", :count => 1 do
228 # This should really be more generic
229 assert_select "tag[k='#{tag.k}'][v='#{tag.v}']"
231 assert_select "way", :count => 2
232 assert_select "way[id='#{way1.id}']", :count => 1
233 assert_select "way[id='#{way2.id}']", :count => 1
234 assert_select "relation", :count => 1
235 assert_select "relation[id='#{relation.id}']", :count => 1
239 def test_map_complete_way
240 node = create(:node, :lat => 7, :lon => 7)
241 # create a couple of nodes well outside of the bbox
242 node2 = create(:node, :lat => 45, :lon => 45)
243 node3 = create(:node, :lat => 10, :lon => 10)
244 way1 = create(:way_node, :node => node).way
245 create(:way_node, :way => way1, :node => node2, :sequence_id => 2)
246 way2 = create(:way_node, :node => node).way
247 create(:way_node, :way => way2, :node => node3, :sequence_id => 2)
248 relation = create(:relation_member, :member => way1).relation
250 bbox = "#{node.lon},#{node.lat},#{node.lon},#{node.lat}"
251 get map_path(:bbox => bbox)
252 assert_response :success, "The map call should have succeeded"
253 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
254 assert_select "bounds[minlon='#{node.lon}'][minlat='#{node.lat}'][maxlon='#{node.lon}'][maxlat='#{node.lat}']", :count => 1
255 assert_select "node", :count => 3
256 assert_select "node[id='#{node.id}']", :count => 1
257 assert_select "node[id='#{node2.id}']", :count => 1
258 assert_select "node[id='#{node3.id}']", :count => 1
259 assert_select "way", :count => 2
260 assert_select "way[id='#{way1.id}']", :count => 1
261 assert_select "way[id='#{way2.id}']", :count => 1
262 assert_select "relation", :count => 1
263 assert_select "relation[id='#{relation.id}']", :count => 1
268 get map_path(:bbox => "179.998,89.998,179.999.1,89.999")
269 assert_response :success, "The map call should have succeeded"
270 assert_select "osm[version='#{Settings.api_version}'][generator='#{Settings.generator}']", :count => 1 do
271 assert_select "bounds[minlon='179.9980000'][minlat='89.9980000'][maxlon='179.9990000'][maxlat='89.9990000']", :count => 1
272 assert_select "node", :count => 0
273 assert_select "way", :count => 0
274 assert_select "relation", :count => 0
278 def test_map_without_bbox
280 assert_response :bad_request
281 assert_equal "The parameter bbox is required", @response.body, "A bbox param was expected"
284 def test_bbox_too_big
285 @badbigbbox.each do |bbox|
286 get map_path(:bbox => bbox)
287 assert_response :bad_request, "The bbox:#{bbox} was expected to be too big"
288 assert_equal "The maximum bbox size is #{Settings.max_request_area}, and your request was too large. Either request a smaller area, or use planet.osm", @response.body, "bbox: #{bbox}"
292 def test_bbox_malformed
293 @badmalformedbbox.each do |bbox|
294 get map_path(:bbox => bbox)
295 assert_response :bad_request, "The bbox:#{bbox} was expected to be malformed"
296 assert_equal "The parameter bbox must be of the form min_lon,min_lat,max_lon,max_lat", @response.body, "bbox: #{bbox}"
300 def test_bbox_lon_mixedup
301 @badlonmixedbbox.each do |bbox|
302 get map_path(:bbox => bbox)
303 assert_response :bad_request, "The bbox:#{bbox} was expected to have the longitude mixed up"
304 assert_equal "The minimum longitude must be less than the maximum longitude, but it wasn't", @response.body, "bbox: #{bbox}"
308 def test_bbox_lat_mixedup
309 @badlatmixedbbox.each do |bbox|
310 get map_path(:bbox => bbox)
311 assert_response :bad_request, "The bbox:#{bbox} was expected to have the latitude mixed up"
312 assert_equal "The minimum latitude must be less than the maximum latitude, but it wasn't", @response.body, "bbox: #{bbox}"
316 # We can't actually get an out of bounds error, as the bbox is sanitised.
317 # def test_latlon_outofbounds
318 # @badlatlonoutboundsbbox.each do |bbox|
319 # [ "trackpoints", "map" ].each do |tq|
320 # get tq, :bbox => bbox
321 # #print @request.to_yaml
322 # assert_response :bad_request, "The bbox #{bbox} was expected to be out of range"
323 # assert_equal "The latitudes must be between -90 an 90, and longitudes between -180 and 180", @response.body, "bbox: #{bbox}"