diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-08-17 02:08:03 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2016-08-17 02:08:03 +0100 |
commit | 929a8805fdfcf3bf9b4a4b68d1332702134c8009 (patch) | |
tree | 1eced43ee41d05a3cb35bc9fa90f67fdcf9472f7 | |
parent | Add basic content negotiation (diff) | |
download | icespider-929a8805fdfcf3bf9b4a4b68d1332702134c8009.tar.bz2 icespider-929a8805fdfcf3bf9b4a4b68d1332702134c8009.tar.xz icespider-929a8805fdfcf3bf9b4a4b68d1332702134c8009.zip |
Tests and fixes for content type selection with wildcards, choices and priorities
-rw-r--r-- | icespider/core/ihttpRequest.cpp | 19 | ||||
-rw-r--r-- | 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<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<SomeModel><value>index</value></SomeModel>\n"); + adp->deactivate(); } |