summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--icespider/fcgi/cgiRequestBase.cpp4
-rw-r--r--icespider/fcgi/cgiRequestBase.h2
-rw-r--r--icespider/unittests/Jamfile.jam12
-rw-r--r--icespider/unittests/testFcgi.cpp152
4 files changed, 161 insertions, 9 deletions
diff --git a/icespider/fcgi/cgiRequestBase.cpp b/icespider/fcgi/cgiRequestBase.cpp
index b818285..922ba9a 100644
--- a/icespider/fcgi/cgiRequestBase.cpp
+++ b/icespider/fcgi/cgiRequestBase.cpp
@@ -43,14 +43,12 @@ namespace IceSpider {
while (start < end) {
auto amp = orelse(strchr(start, '&'), end);
auto eq = orelse(strchr(start, '='), end);
+ *amp = '\0';
if (eq < amp) {
*eq = '\0';
- *amp = '\0';
qsmap.insert({ start, Env( eq + 1, amp ) });
}
else {
- *eq = '\0';
- *amp = '\0';
qsmap.insert({ start, Env( eq + 1, eq + 1 ) });
}
start = amp + 1;
diff --git a/icespider/fcgi/cgiRequestBase.h b/icespider/fcgi/cgiRequestBase.h
index b8dcd1f..cc67407 100644
--- a/icespider/fcgi/cgiRequestBase.h
+++ b/icespider/fcgi/cgiRequestBase.h
@@ -20,11 +20,13 @@ namespace IceSpider {
void addenv(char *);
void initialize();
+ public:
const PathElements & getRequestPath() const override;
HttpMethod getRequestMethod() const override;
OptionalString getQueryStringParam(const std::string & key) const override;
OptionalString getHeaderParam(const std::string & key) const override;
+ private:
static OptionalString optionalLookup(const std::string & key, const VarMap &);
VarMap envmap;
diff --git a/icespider/unittests/Jamfile.jam b/icespider/unittests/Jamfile.jam
index 05fecb4..3ce12f0 100644
--- a/icespider/unittests/Jamfile.jam
+++ b/icespider/unittests/Jamfile.jam
@@ -22,6 +22,7 @@ lib slicer : : : : <include>/usr/include/slicer ;
lib slicer-json : : : : <include>/usr/include/slicer ;
lib slicer-xml : : : : <include>/usr/include/slicer ;
lib boost_utf : : <name>boost_unit_test_framework ;
+lib boost_system ;
lib dl ;
path-constant me : . ;
@@ -79,13 +80,14 @@ run
run
testFcgi.cpp
- :
- 'QUERY_STRING=noeq&noval=&someval=here&another=here'
- 'REDIRECT_URL=/'
- : :
+ ../fcgi/cgiRequestBase.cpp
+ : : :
+ <define>BOOST_TEST_DYN_LINK
+ <library>testCommon
<library>../common//icespider-common
<library>../core//icespider-core
- <library>../fcgi//icespider-fcgi
+ <library>boost_system
+ <include>../fcgi
: testFcgi ;
lib test-api :
diff --git a/icespider/unittests/testFcgi.cpp b/icespider/unittests/testFcgi.cpp
index 65e2cc3..a717c4d 100644
--- a/icespider/unittests/testFcgi.cpp
+++ b/icespider/unittests/testFcgi.cpp
@@ -1 +1,151 @@
-// intentionally blank
+#define BOOST_TEST_MODULE TestApp
+#include <boost/test/unit_test.hpp>
+
+#include <core.h>
+#include <cgiRequestBase.h>
+
+class TestRequest : public IceSpider::CgiRequestBase {
+ public:
+ TestRequest(IceSpider::Core * c, char ** env) : IceSpider::CgiRequestBase(c, env)
+ {
+ initialize();
+ }
+
+ std::ostream & getOutputStream() const override
+ {
+ return std::cout;
+ }
+
+ std::istream & getInputStream() const override
+ {
+ return std::cin;
+ }
+};
+
+class CharPtrPtrArray : public std::vector<char *> {
+ public:
+ CharPtrPtrArray()
+ {
+ push_back(NULL);
+ }
+
+ CharPtrPtrArray(const std::vector<std::string> & a)
+ {
+ for (const auto & e : a) {
+ push_back(strdup(e.c_str()));
+ }
+ push_back(NULL);
+ }
+
+ ~CharPtrPtrArray()
+ {
+ for (const auto & e : *this) {
+ free(e);
+ }
+ }
+
+ operator char **()
+ {
+ return &front();
+ }
+};
+
+namespace std {
+ static
+ std::ostream &
+ operator<<(std::ostream & s, const IceSpider::PathElements & pe)
+ {
+ for (const auto & e : pe) {
+ s << "/" << e;
+ }
+ return s;
+ }
+}
+
+BOOST_FIXTURE_TEST_SUITE( CgiRequestBase, IceSpider::Core );
+
+BOOST_AUTO_TEST_CASE( NoEnvironment )
+{
+ BOOST_REQUIRE_THROW({
+ CharPtrPtrArray env;
+ TestRequest r(this, env);
+ }, std::runtime_error);
+}
+
+BOOST_AUTO_TEST_CASE( script_name_root )
+{
+ CharPtrPtrArray env ({ "SCRIPT_NAME=/" });
+ TestRequest r(this, env);
+ BOOST_REQUIRE(r.getRequestPath().empty());
+}
+
+BOOST_AUTO_TEST_CASE( redirect_uri_root )
+{
+ CharPtrPtrArray env ({ "REDIRECT_URL=/" });
+ TestRequest r(this, env);
+ BOOST_REQUIRE(r.getRequestPath().empty());
+}
+
+BOOST_AUTO_TEST_CASE( script_name_foobar )
+{
+ CharPtrPtrArray env ({ "SCRIPT_NAME=/foo/bar" });
+ TestRequest r(this, env);
+ BOOST_REQUIRE_EQUAL(IceSpider::PathElements({"foo", "bar"}), r.getRequestPath());
+}
+
+BOOST_AUTO_TEST_CASE( query_string_empty )
+{
+ CharPtrPtrArray env ({ "SCRIPT_NAME=/foo/bar", "QUERY_STRING=" });
+ TestRequest r(this, env);
+ BOOST_REQUIRE(!r.getQueryStringParam(""));
+}
+
+BOOST_AUTO_TEST_CASE( query_string_one )
+{
+ CharPtrPtrArray env ({ "SCRIPT_NAME=/foo/bar", "QUERY_STRING=one=1" });
+ TestRequest r(this, env);
+ BOOST_REQUIRE(!r.getQueryStringParam(""));
+ BOOST_REQUIRE_EQUAL("1", *r.getQueryStringParam("one"));
+}
+
+BOOST_AUTO_TEST_CASE( query_string_two )
+{
+ CharPtrPtrArray env ({ "SCRIPT_NAME=/foo/bar", "QUERY_STRING=one=1&two=2" });
+ TestRequest r(this, env);
+ BOOST_REQUIRE(!r.getQueryStringParam(""));
+ BOOST_REQUIRE_EQUAL("1", *r.getQueryStringParam("one"));
+ BOOST_REQUIRE_EQUAL("2", *r.getQueryStringParam("two"));
+}
+
+BOOST_AUTO_TEST_CASE( query_string_three )
+{
+ CharPtrPtrArray env ({ "SCRIPT_NAME=/foo/bar", "QUERY_STRING=one=1&two=2&three=3" });
+ TestRequest r(this, env);
+ BOOST_REQUIRE(!r.getQueryStringParam(""));
+ BOOST_REQUIRE_EQUAL("1", *r.getQueryStringParam("one"));
+ BOOST_REQUIRE_EQUAL("2", *r.getQueryStringParam("two"));
+ BOOST_REQUIRE_EQUAL("3", *r.getQueryStringParam("three"));
+}
+
+BOOST_AUTO_TEST_CASE( query_string_three_emptyVal )
+{
+ CharPtrPtrArray env ({ "SCRIPT_NAME=/foo/bar", "QUERY_STRING=one=1&two=&three=3" });
+ TestRequest r(this, env);
+ BOOST_REQUIRE(!r.getQueryStringParam(""));
+ BOOST_REQUIRE_EQUAL("1", *r.getQueryStringParam("one"));
+ BOOST_REQUIRE_EQUAL("", *r.getQueryStringParam("two"));
+ BOOST_REQUIRE_EQUAL("3", *r.getQueryStringParam("three"));
+}
+
+BOOST_AUTO_TEST_CASE( query_string_three_noVal )
+{
+ CharPtrPtrArray env ({ "SCRIPT_NAME=/foo/bar", "QUERY_STRING=one=1&two&three=3" });
+ TestRequest r(this, env);
+ BOOST_REQUIRE(!r.getQueryStringParam(""));
+ BOOST_REQUIRE_EQUAL("1", *r.getQueryStringParam("one"));
+ BOOST_REQUIRE_EQUAL("", *r.getQueryStringParam("two"));
+ BOOST_REQUIRE_EQUAL("3", *r.getQueryStringParam("three"));
+}
+
+BOOST_AUTO_TEST_SUITE_END();
+