From ff9be3a43a0f3ba43b94a4080b9e0cd8ed4cb7d0 Mon Sep 17 00:00:00 2001 From: Julian Mundhahs Date: Sun, 27 Oct 2024 16:53:34 +0100 Subject: [PATCH] Remove superfluous checks --- src/engine/Server.cpp | 9 +-------- src/util/http/MediaTypes.h | 2 ++ test/ServerTest.cpp | 4 +++- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/engine/Server.cpp b/src/engine/Server.cpp index e3511d46ee..8d1afd099e 100644 --- a/src/engine/Server.cpp +++ b/src/engine/Server.cpp @@ -747,15 +747,8 @@ MediaType Server::determineMediaType( if (!mediaType.has_value()) { mediaType = ad_utility::getMediaTypeFromAcceptHeader(acceptHeader); } + AD_CORRECTNESS_CHECK(mediaType.has_value()); - if (!mediaType.has_value()) { - throw NoSupportedMediatypeError( - absl::StrCat("Did not find any supported media type " - "in this \'Accept:\' header field: \"", - acceptHeader, "\". ", - ad_utility::getErrorMessageForSupportedMediaTypes())); - } - AD_CONTRACT_CHECK(mediaType.has_value()); return mediaType.value(); } diff --git a/src/util/http/MediaTypes.h b/src/util/http/MediaTypes.h index 43a809e33b..407473bd21 100644 --- a/src/util/http/MediaTypes.h +++ b/src/util/http/MediaTypes.h @@ -112,6 +112,8 @@ std::vector parseAcceptHeader( /// media types that appear earlier in the `SUPPORTED_MEDIA_TYPES`. If none of /// the `SUPPORTED_MEDIA_TYPES` is accepted by `acceptHeader`, then /// `std::nullopt` is returned. +// TODO: An exception is thrown if no supported media type is found. Update the +// docstring and signature to reflect this. std::optional getMediaTypeFromAcceptHeader( std::string_view acceptHeader); diff --git a/test/ServerTest.cpp b/test/ServerTest.cpp index 23c64f1882..3f32356935 100644 --- a/test/ServerTest.cpp +++ b/test/ServerTest.cpp @@ -208,7 +208,9 @@ TEST(ServerTest, determineMediaType) { EXPECT_THAT(Server::determineMediaType( {}, MakeRequest("application/sparql-results+json")), testing::Eq(ad_utility::MediaType::sparqlJson)); - // No supported media type. + // No supported media type in the `Accept` header. (Contrary to it's docstring + // and interface) `ad_utility::getMediaTypeFromAcceptHeader` throws an + // exception if no supported media type is found. EXPECT_THROW(Server::determineMediaType({}, MakeRequest("text/css")), std::runtime_error); // No `Accept` header means that any content type is allowed.