Merge pull request #2485 from mmd-osm/patch/json2

JSON output nodes, ways, relations, map
This commit is contained in:
Andy Allan 2020-02-26 15:55:48 +01:00 committed by GitHub
commit 73c95847a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 566 additions and 13 deletions

View file

@ -23,6 +23,87 @@ module Api
{ :path => "/api/0.6/map", :method => :get },
{ :controller => "api/map", :action => "index" }
)
assert_routing(
{ :path => "/api/0.6/map.json", :method => :get },
{ :controller => "api/map", :action => "index", :format => "json" }
)
end
##
# test http accept headers
def test_http_accept_header
node = create(:node)
minlon = node.lon - 0.1
minlat = node.lat - 0.1
maxlon = node.lon + 0.1
maxlat = node.lat + 0.1
bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
# Accept: XML format -> use XML
http_accept_format("text/xml")
get :index, :params => { :bbox => bbox }
assert_response :success, "Expected success with the map call"
assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
# Accept: Any format -> use XML
http_accept_format("*/*")
get :index, :params => { :bbox => bbox }
assert_response :success, "Expected success with the map call"
assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
# Accept: Any format, .json URL suffix -> use json
http_accept_format("*/*")
get :index, :params => { :bbox => bbox, :format => "json" }
assert_response :success, "Expected success with the map call"
assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
# Accept: Firefox header -> use XML
http_accept_format("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
get :index, :params => { :bbox => bbox }
assert_response :success, "Expected success with the map call"
assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
# Accept: JOSM header text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 -> use XML
# Note: JOSM's header does not comply with RFC 7231, section 5.3.1
http_accept_format("text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2")
get :index, :params => { :bbox => bbox }
assert_response :success, "Expected success with the map call"
assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
# Accept: text/plain, */* -> use XML
http_accept_format("text/plain, */*")
get :index, :params => { :bbox => bbox }
assert_response :success, "Expected success with the map call"
assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
# Accept: text/* -> use XML
http_accept_format("text/*")
get :index, :params => { :bbox => bbox }
assert_response :success, "Expected success with the map call"
assert_equal "application/xml; charset=utf-8", @response.header["Content-Type"]
# Accept: json, */* format -> use json
http_accept_format("application/json, */*")
get :index, :params => { :bbox => bbox }
assert_response :success, "Expected success with the map call"
assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
# Accept: json format -> use json
http_accept_format("application/json")
get :index, :params => { :bbox => bbox }
assert_response :success, "Expected success with the map call"
assert_equal "application/json; charset=utf-8", @response.header["Content-Type"]
# text/json is in invalid format, return HTTP 406 Not acceptable
http_accept_format("text/json")
get :index, :params => { :bbox => bbox }
assert_response :not_acceptable, "text/json should fail"
# image/jpeg is a format which we don't support, return HTTP 406 Not acceptable
http_accept_format("image/jpeg")
get :index, :params => { :bbox => bbox }
assert_response :not_acceptable, "text/json should fail"
end
# -------------------------------------
@ -63,6 +144,54 @@ module Api
end
end
def test_map_json
node = create(:node, :lat => 7, :lon => 7)
tag = create(:node_tag, :node => node)
way1 = create(:way_node, :node => node).way
way2 = create(:way_node, :node => node).way
relation = create(:relation_member, :member => node).relation
# Need to split the min/max lat/lon out into their own variables here
# so that we can test they are returned later.
minlon = node.lon - 0.1
minlat = node.lat - 0.1
maxlon = node.lon + 0.1
maxlat = node.lat + 0.1
bbox = "#{minlon},#{minlat},#{maxlon},#{maxlat}"
get :index, :params => { :bbox => bbox, :format => "json" }
if $VERBOSE
print @request.to_yaml
print @response.body
end
assert_response :success, "Expected success with the map call"
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js
assert_equal Settings.api_version, js["version"]
assert_equal Settings.generator, js["generator"]
assert_equal GeoRecord::Coord.new(minlon), js["bounds"]["minlon"]
assert_equal GeoRecord::Coord.new(minlat), js["bounds"]["minlat"]
assert_equal GeoRecord::Coord.new(maxlon), js["bounds"]["maxlon"]
assert_equal GeoRecord::Coord.new(maxlat), js["bounds"]["maxlat"]
result_nodes = js["elements"].select { |a| a["type"] == "node" }
.select { |a| a["id"] == node.id }
.select { |a| a["lat"] == GeoRecord::Coord.new(node.lat) }
.select { |a| a["lon"] == GeoRecord::Coord.new(node.lon) }
.select { |a| a["version"] == node.version }
.select { |a| a["changeset"] == node.changeset_id }
.select { |a| a["timestamp"] == node.timestamp.xmlschema }
assert_equal result_nodes.count, 1
result_node = result_nodes.first
assert_equal result_node["tags"], tag.k => tag.v
assert_equal 2, (js["elements"].count { |a| a["type"] == "way" })
assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way1.id })
assert_equal 1, (js["elements"].count { |a| a["type"] == "way" && a["id"] == way2.id })
assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" })
assert_equal 1, (js["elements"].count { |a| a["type"] == "relation" && a["id"] == relation.id })
end
# This differs from the above test in that we are making the bbox exactly
# the same as the node we are looking at
def test_map_inclusive

View file

@ -13,6 +13,10 @@ module Api
{ :path => "/api/0.6/node/1", :method => :get },
{ :controller => "api/nodes", :action => "show", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/node/1.json", :method => :get },
{ :controller => "api/nodes", :action => "show", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/node/1", :method => :put },
{ :controller => "api/nodes", :action => "update", :id => "1" }
@ -25,6 +29,10 @@ module Api
{ :path => "/api/0.6/nodes", :method => :get },
{ :controller => "api/nodes", :action => "index" }
)
assert_routing(
{ :path => "/api/0.6/nodes.json", :method => :get },
{ :controller => "api/nodes", :action => "index", :format => "json" }
)
end
def test_create
@ -464,6 +472,19 @@ module Api
assert_select "node[id='#{node5.id}'][visible='false']", :count => 1
end
# test a working call with json format
get :index, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id}", :format => "json" }
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js
assert_equal 5, js["elements"].count
assert_equal 5, (js["elements"].count { |a| a["type"] == "node" })
assert_equal 1, (js["elements"].count { |a| a["id"] == node1.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == node2.id && a["visible"] == false })
assert_equal 1, (js["elements"].count { |a| a["id"] == node3.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == node4.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == node5.id && a["visible"] == false })
# check error when a non-existent node is included
get :index, :params => { :nodes => "#{node1.id},#{node2.id},#{node3.id},#{node4.id},#{node5.id},0" }
assert_response :not_found

View file

@ -17,6 +17,14 @@ module Api
{ :path => "/api/0.6/node/1/2", :method => :get },
{ :controller => "api/old_nodes", :action => "version", :id => "1", :version => "2" }
)
assert_routing(
{ :path => "/api/0.6/node/1/history.json", :method => :get },
{ :controller => "api/old_nodes", :action => "history", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/node/1/2.json", :method => :get },
{ :controller => "api/old_nodes", :action => "version", :id => "1", :version => "2", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/node/1/2/redact", :method => :post },
{ :controller => "api/old_nodes", :action => "redact", :id => "1", :version => "2" }

View file

@ -13,6 +13,14 @@ module Api
{ :path => "/api/0.6/relation/1/2", :method => :get },
{ :controller => "api/old_relations", :action => "version", :id => "1", :version => "2" }
)
assert_routing(
{ :path => "/api/0.6/relation/1/history.json", :method => :get },
{ :controller => "api/old_relations", :action => "history", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/relation/1/2.json", :method => :get },
{ :controller => "api/old_relations", :action => "version", :id => "1", :version => "2", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/relation/1/2/redact", :method => :post },
{ :controller => "api/old_relations", :action => "redact", :id => "1", :version => "2" }

View file

@ -13,6 +13,14 @@ module Api
{ :path => "/api/0.6/way/1/2", :method => :get },
{ :controller => "api/old_ways", :action => "version", :id => "1", :version => "2" }
)
assert_routing(
{ :path => "/api/0.6/way/1/history.json", :method => :get },
{ :controller => "api/old_ways", :action => "history", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/way/1/2.json", :method => :get },
{ :controller => "api/old_ways", :action => "version", :id => "1", :version => "2", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/way/1/2/redact", :method => :post },
{ :controller => "api/old_ways", :action => "redact", :id => "1", :version => "2" }

View file

@ -13,10 +13,18 @@ module Api
{ :path => "/api/0.6/relation/1/full", :method => :get },
{ :controller => "api/relations", :action => "full", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/relation/1/full.json", :method => :get },
{ :controller => "api/relations", :action => "full", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/relation/1", :method => :get },
{ :controller => "api/relations", :action => "show", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/relation/1.json", :method => :get },
{ :controller => "api/relations", :action => "show", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/relation/1", :method => :put },
{ :controller => "api/relations", :action => "update", :id => "1" }
@ -29,6 +37,10 @@ module Api
{ :path => "/api/0.6/relations", :method => :get },
{ :controller => "api/relations", :action => "index" }
)
assert_routing(
{ :path => "/api/0.6/relations.json", :method => :get },
{ :controller => "api/relations", :action => "index", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/node/1/relations", :method => :get },
@ -42,6 +54,18 @@ module Api
{ :path => "/api/0.6/relation/1/relations", :method => :get },
{ :controller => "api/relations", :action => "relations_for_relation", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/node/1/relations.json", :method => :get },
{ :controller => "api/relations", :action => "relations_for_node", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/way/1/relations.json", :method => :get },
{ :controller => "api/relations", :action => "relations_for_way", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/relation/1/relations.json", :method => :get },
{ :controller => "api/relations", :action => "relations_for_relation", :id => "1", :format => "json" }
)
end
# -------------------------------------
@ -187,6 +211,18 @@ module Api
assert_select "relation[id='#{relation4.id}'][visible='true']", :count => 1
end
# test a working call with json format
get :index, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id}", :format => "json" }
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js
assert_equal 4, js["elements"].count
assert_equal 4, (js["elements"].count { |a| a["type"] == "relation" })
assert_equal 1, (js["elements"].count { |a| a["id"] == relation1.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == relation2.id && a["visible"] == false })
assert_equal 1, (js["elements"].count { |a| a["id"] == relation3.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == relation4.id && a["visible"].nil? })
# check error when a non-existent relation is included
get :index, :params => { :relations => "#{relation1.id},#{relation2.id},#{relation3.id},#{relation4.id},0" }
assert_response :not_found

View file

@ -13,10 +13,18 @@ module Api
{ :path => "/api/0.6/way/1/full", :method => :get },
{ :controller => "api/ways", :action => "full", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/way/1/full.json", :method => :get },
{ :controller => "api/ways", :action => "full", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/way/1", :method => :get },
{ :controller => "api/ways", :action => "show", :id => "1" }
)
assert_routing(
{ :path => "/api/0.6/way/1.json", :method => :get },
{ :controller => "api/ways", :action => "show", :id => "1", :format => "json" }
)
assert_routing(
{ :path => "/api/0.6/way/1", :method => :put },
{ :controller => "api/ways", :action => "update", :id => "1" }
@ -29,6 +37,10 @@ module Api
{ :path => "/api/0.6/ways", :method => :get },
{ :controller => "api/ways", :action => "index" }
)
assert_routing(
{ :path => "/api/0.6/ways.json", :method => :get },
{ :controller => "api/ways", :action => "index", :format => "json" }
)
end
# -------------------------------------
@ -104,6 +116,18 @@ module Api
assert_select "way[id='#{way4.id}'][visible='true']", :count => 1
end
# test a working call with json format
get :index, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id}", :format => "json" }
js = ActiveSupport::JSON.decode(@response.body)
assert_not_nil js
assert_equal 4, js["elements"].count
assert_equal 4, (js["elements"].count { |a| a["type"] == "way" })
assert_equal 1, (js["elements"].count { |a| a["id"] == way1.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == way2.id && a["visible"] == false })
assert_equal 1, (js["elements"].count { |a| a["id"] == way3.id && a["visible"].nil? })
assert_equal 1, (js["elements"].count { |a| a["id"] == way4.id && a["visible"].nil? })
# check error when a non-existent way is included
get :index, :params => { :ways => "#{way1.id},#{way2.id},#{way3.id},#{way4.id},0" }
assert_response :not_found

View file

@ -115,6 +115,12 @@ module ActiveSupport
@request.env["HTTP_AUTHORIZATION"] = format("Basic %{auth}", :auth => Base64.encode64("#{user}:#{pass}"))
end
##
# set request header for HTTP Accept
def http_accept_format(format)
@request.env["HTTP_ACCEPT"] = format
end
##
# set request readers to ask for a particular error format
def error_format(format)