From 1efda07933b5366e57abe1106242fc1b36fcac88 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 17 Sep 2016 02:22:55 +0100 Subject: Handle unsupported or missing content-type header with request payload --- icespider/core/core.cpp | 2 ++ icespider/core/core.h | 2 ++ icespider/core/ihttpRequest.cpp | 13 +++++++++---- icespider/unittests/testApp.cpp | 23 +++++++++++++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/icespider/core/core.cpp b/icespider/core/core.cpp index 58619aa..096b367 100644 --- a/icespider/core/core.cpp +++ b/icespider/core/core.cpp @@ -5,9 +5,11 @@ namespace IceSpider { const boost::filesystem::path Core::defaultConfig("config/ice.properties"); + DefineHttpEx(Http400_BadRequest, 400, "Bad Request"); DefineHttpEx(Http404_NotFound, 404, "Not found"); DefineHttpEx(Http405_MethodNotAllowed, 405, "Method Not Allowed"); DefineHttpEx(Http406_NotAcceptable, 406, "Not Acceptable"); + DefineHttpEx(Http415_UnsupportedMediaType, 415, "Unsupported Media Type"); static bool diff --git a/icespider/core/core.h b/icespider/core/core.h index 74c9108..c7c762f 100644 --- a/icespider/core/core.h +++ b/icespider/core/core.h @@ -20,9 +20,11 @@ const std::string Name::message(Message); namespace IceSpider { + DeclareHttpEx(Http400_BadRequest); DeclareHttpEx(Http404_NotFound); DeclareHttpEx(Http405_MethodNotAllowed); DeclareHttpEx(Http406_NotAcceptable); + DeclareHttpEx(Http415_UnsupportedMediaType); class DLL_PUBLIC Core { public: diff --git a/icespider/core/ihttpRequest.cpp b/icespider/core/ihttpRequest.cpp index 73da7ae..a49df42 100644 --- a/icespider/core/ihttpRequest.cpp +++ b/icespider/core/ihttpRequest.cpp @@ -19,10 +19,15 @@ namespace IceSpider { Slicer::DeserializerPtr IHttpRequest::getDeserializer() const { - return Slicer::StreamDeserializerFactory::createNew( - getHeaderParam("Content-Type") / []() -> std::string { - throw std::runtime_error("Content-Type must be specified to deserialize payload"); - }, getInputStream()); + try { + return Slicer::StreamDeserializerFactory::createNew( + getHeaderParam("Content-Type") / []() -> std::string { + throw Http400_BadRequest(); + }, getInputStream()); + } + catch (const AdHoc::NoSuchPluginException &) { + throw Http415_UnsupportedMediaType(); + } } ContentTypeSerializer diff --git a/icespider/unittests/testApp.cpp b/icespider/unittests/testApp.cpp index 4e32ddd..087d95d 100644 --- a/icespider/unittests/testApp.cpp +++ b/icespider/unittests/testApp.cpp @@ -292,6 +292,29 @@ BOOST_AUTO_TEST_CASE( testCallPost1234 ) BOOST_REQUIRE(requestUpdateItem.output.eof()); } +BOOST_AUTO_TEST_CASE( testCallPost1234NoContentType ) +{ + TestRequest requestUpdateItem(this, HttpMethod::POST, "/1234"); + requestUpdateItem.input << "{\"value\": \"some value\"}"; + process(&requestUpdateItem); + auto h = parseHeaders(requestUpdateItem.output); + BOOST_REQUIRE_EQUAL(h["Status"], "400 Bad Request"); + requestUpdateItem.output.get(); + BOOST_REQUIRE(requestUpdateItem.output.eof()); +} + +BOOST_AUTO_TEST_CASE( testCallPost1234UnsupportedMediaType ) +{ + TestRequest requestUpdateItem(this, HttpMethod::POST, "/1234"); + requestUpdateItem.hdr["Content-Type"] = "application/notathing"; + requestUpdateItem.input << "value=\"some value\""; + process(&requestUpdateItem); + auto h = parseHeaders(requestUpdateItem.output); + BOOST_REQUIRE_EQUAL(h["Status"], "415 Unsupported Media Type"); + requestUpdateItem.output.get(); + BOOST_REQUIRE(requestUpdateItem.output.eof()); +} + BOOST_AUTO_TEST_CASE( testCallIndexAcceptJson ) { TestRequest requestJson(this, HttpMethod::GET, "/"); -- cgit v1.2.3