From 929a8805fdfcf3bf9b4a4b68d1332702134c8009 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 17 Aug 2016 02:08:03 +0100 Subject: Tests and fixes for content type selection with wildcards, choices and priorities --- icespider/core/ihttpRequest.cpp | 19 ++++++++++++++++--- icespider/unittests/testApp.cpp | 15 +++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/icespider/core/ihttpRequest.cpp b/icespider/core/ihttpRequest.cpp index 8e9b8f4..a4e25b2 100644 --- a/icespider/core/ihttpRequest.cpp +++ b/icespider/core/ihttpRequest.cpp @@ -35,15 +35,28 @@ namespace IceSpider { char * grp = NULL, * type = NULL; float pri = 0.0f; int chars, v; - while ((v = sscanf(accept, " %m[^/] / %m[^;,] %n ; q = %f , %n", &grp, &type, &chars, &pri, &chars)) >= 2) { - accepts.push_back( { grp, type, (v < 3 ? 1.0f : pri) } ); + while ((v = sscanf(accept, " %m[^ /] / %m[^ ;,] %n , %n", &grp, &type, &chars, &chars)) == 2) { + accept += chars; + chars = 0; + if ((v = sscanf(accept, " ; q = %f %n , %n", &pri, &chars, &chars)) != 1) { + pri = 1.0; + } + if (!strcmp(grp, "*")) { + free(grp); + grp = NULL; + } + if (!strcmp(type, "*")) { + free(type); + type = NULL; + } + accepts.push_back( { grp, type, pri } ); grp = NULL; type = NULL; accept += chars; } free(grp); free(type); - std::stable_sort(accepts.begin(), accepts.end(), [](const auto & a, const auto & b) { return a.pri < b.pri; }); + std::stable_sort(accepts.begin(), accepts.end(), [](const auto & a, const auto & b) { return a.pri > b.pri; }); Slicer::SerializerPtr serializer; auto & strm = getOutputStream(); for(auto & a : accepts) { diff --git a/icespider/unittests/testApp.cpp b/icespider/unittests/testApp.cpp index 1031eaf..7cb2b29 100644 --- a/icespider/unittests/testApp.cpp +++ b/icespider/unittests/testApp.cpp @@ -194,6 +194,16 @@ BOOST_AUTO_TEST_CASE( testCallMethods ) process(&requestJson); BOOST_REQUIRE_EQUAL(requestJson.output.str(), "Status: 200 OK\r\n\r\n{\"value\":\"index\"}"); + TestRequest requestAnyAny(this, HttpMethod::GET, "/"); + requestAnyAny.hdr["Accept"] = "*/*"; + process(&requestAnyAny); + BOOST_REQUIRE_EQUAL(requestAnyAny.output.str(), "Status: 200 OK\r\n\r\n{\"value\":\"index\"}"); + + TestRequest requestApplicationAny(this, HttpMethod::GET, "/"); + requestApplicationAny.hdr["Accept"] = "application/*"; + process(&requestApplicationAny); + BOOST_REQUIRE_EQUAL(requestApplicationAny.output.str(), "Status: 200 OK\r\n\r\n{\"value\":\"index\"}"); + TestRequest requestXml(this, HttpMethod::GET, "/"); requestXml.hdr["Accept"] = "application/xml"; process(&requestXml); @@ -204,6 +214,11 @@ BOOST_AUTO_TEST_CASE( testCallMethods ) process(&requestBadAccept); BOOST_REQUIRE_EQUAL(requestBadAccept.output.str(), "Status: 406 Unacceptable\r\n\r\n"); + TestRequest requestChoice(this, HttpMethod::GET, "/"); + requestChoice.hdr["Accept"] = "something/special ; q = 20, application/json, application/xml;q=1.1"; + process(&requestChoice); + BOOST_REQUIRE_EQUAL(requestChoice.output.str(), "Status: 200 OK\r\n\r\n\nindex\n"); + adp->deactivate(); } -- cgit v1.2.3