]> git.openstreetmap.org Git - rails.git/blobdiff - test/controllers/api/map_controller_test.rb
JSON: added clarification for non-supported Accept header formats
[rails.git] / test / controllers / api / map_controller_test.rb
index 54461868b7934eff2d22813dc93f20e7673c48fb..d5cbfb4efadd0e9b24ccd8ad87dc7f3b9a78f945 100644 (file)
@@ -29,6 +29,84 @@ module Api
       )
     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, ActionController::UnknownFormat error is expected
+      http_accept_format("text/json")
+      get :index, :params => { :bbox => bbox }
+      assert_response :internal_server_error, "text/json should fail"
+
+      # image/jpeg is a format which we don't support, ActionController::UnknownFormat error is expected
+      # HTTP 406 Not acceptable would be the correct response error code. That's outside of our control though.
+      http_accept_format("image/jpeg")
+      get :index, :params => { :bbox => bbox }
+      assert_response :internal_server_error, "text/json should fail"
+    end
+
     # -------------------------------------
     # Test reading a bounding box.
     # -------------------------------------