private
- # Set format to xml unless client requires a specific format
- def default_format_xml
+ ##
+ # Set default request format to xml unless a client requests a specific format,
+ # which can be done via (a) URL suffix and/or (b) HTTP Accept header, where
+ # the URL suffix always takes precedence over the Accept header.
+ def set_default_request_format
unless params[:format]
- request.format = "xml" unless request.format.symbol == :json
+ accept_header = request.headers["HTTP_ACCEPT"]
+ if accept_header.nil?
+ # e.g. unit tests don't set an Accept: header by default, force XML in this case
+ request.format = "xml"
+ return
+ end
+
+ req_mimetypes = []
+
+ # Some clients (JOSM) send Accept headers which cannot be parsed by Rails, example: *; q=.2
+ # To be fair, JOSM's Accept header doesn't adhere to RFC 7231, section 5.3.1, et al. either
+ # As a workaround for backwards compatibility, we're assuming XML format
+ begin
+ req_mimetypes = Mime::Type.parse(accept_header)
+ rescue Mime::Type::InvalidMimeType
+ request.format = "xml"
+ return
+ end
+
+ # req_mimetypes contains all Accept header MIME types with descending priority
+ req_mimetypes.each do |mime|
+ if mime.symbol == :xml
+ request.format = "xml"
+ break
+ end
+
+ if mime.symbol == :json
+ request.format = "json"
+ break
+ end
+
+ # Any format, not explicitly requesting XML or JSON -> assume XML as default
+ if mime == "*/*"
+ request.format = "xml"
+ break
+ end
+ end
end
end
)
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.
# -------------------------------------