]> git.openstreetmap.org Git - rails.git/blobdiff - test/controllers/api/map_controller_test.rb
Added Accept header unit tests
[rails.git] / test / controllers / api / map_controller_test.rb
index 5df2dc6a3cabb30c1e58d13e6463a933566566ca..c5392c723e776b54f03cae162065035c615d0c0e 100644 (file)
@@ -29,6 +29,78 @@ module Api
       )
     end
 
+    ##
+    # test http accept headers
+    def test_http_accept_header
+      node = create(:node, :lat => 7, :lon => 7)
+
+      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, ActionController::UnknownFormat error is expected
+      http_accept_format("text/json")
+      get :index, :params => { :bbox => bbox }
+      assert_response :internal_server_error, "text/json should fail"
+    end
+
     # -------------------------------------
     # Test reading a bounding box.
     # -------------------------------------
@@ -92,15 +164,15 @@ module Api
 
       assert_equal Settings.api_version, js["version"]
       assert_equal Settings.generator, js["generator"]
-      assert_equal "#{format('%.7f', minlon)}", js["bounds"]["minlon"]
-      assert_equal "#{format('%.7f', minlat)}", js["bounds"]["minlat"]
-      assert_equal "#{format('%.7f', maxlon)}", js["bounds"]["maxlon"]
-      assert_equal "#{format('%.7f', maxlat)}", js["bounds"]["maxlat"]
+      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"] == "#{format('%.7f', node.lat)}" }
-                                   .select { |a| a["lon"] == "#{format('%.7f', node.lon)}" }
+                                   .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 }