diff --git a/src/tests/kits/net/url/Jamfile b/src/tests/kits/net/url/Jamfile index a5900b2398e..bb25e40a018 100644 --- a/src/tests/kits/net/url/Jamfile +++ b/src/tests/kits/net/url/Jamfile @@ -1,6 +1,9 @@ SubDir HAIKU_TOP src tests kits net url ; -Application url_test - : url_test.cpp +UnitTestLib servicekittest.so : + ServiceKitTestAddon.cpp + + UrlTest.cpp + : be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++) ; diff --git a/src/tests/kits/net/url/ServiceKitTestAddon.cpp b/src/tests/kits/net/url/ServiceKitTestAddon.cpp new file mode 100644 index 00000000000..5b665bc94f1 --- /dev/null +++ b/src/tests/kits/net/url/ServiceKitTestAddon.cpp @@ -0,0 +1,21 @@ +/* + * Copyright 2014 Haiku, Inc. + * Distributed under the terms of the MIT License. + */ + + +#include +#include + +#include "UrlTest.h" + + +BTestSuite* +getTestSuite() +{ + BTestSuite* suite = new BTestSuite("ServicesKit"); + + UrlTest::AddTests(*suite); + + return suite; +} diff --git a/src/tests/kits/net/url/UrlTest.cpp b/src/tests/kits/net/url/UrlTest.cpp new file mode 100644 index 00000000000..87c427ab87e --- /dev/null +++ b/src/tests/kits/net/url/UrlTest.cpp @@ -0,0 +1,144 @@ +/* + * Copyright 2010, Christophe Huriaux + * Copyright 2014, Haiku, inc. + * Distributed under the terms of the MIT licence + */ + + +#include "UrlTest.h" + + +#include +#include +#include + +#include + +#include +#include + + +UrlTest::UrlTest() +{ +} + + +UrlTest::~UrlTest() +{ +} + + +// Test if rebuild url are the same length of parsed one +void UrlTest::LengthTest() +{ + int8 testIndex; + BUrl testUrl; + + const char* kTestLength[] = + { + "http://user:pass@www.foo.com:80/path?query#fragment", + "http://user:pass@www.foo.com:80/path?query#", + "http://user:pass@www.foo.com:80/path?query", + "http://user:pass@www.foo.com:80/path?", + "http://user:pass@www.foo.com:80/path", + "http://user:pass@www.foo.com:80/", + "http://user:pass@www.foo.com", + "http://user:pass@", + "http://www.foo.com", + "http://", + "http:" + }; + + for (testIndex = 0; kTestLength[testIndex] != NULL; testIndex++) + { + NextSubTest(); + testUrl.SetUrlString(kTestLength[testIndex]); + + CPPUNIT_ASSERT(strlen(kTestLength[testIndex]) == strlen(testUrl.UrlString())); + } +} + +typedef struct +{ + const char* url; + + struct + { + const char* protocol; + const char* userName; + const char* password; + const char* host; + int16 port; + const char* path; + const char* request; + const char* fragment; + } expected; +} ExplodeTest; + + +const ExplodeTest kTestExplode[] = + // Url + // Protocol User Password Hostname Port Path Request Fragment + // -------- --------- --------- --------- ---- ---------- ---------- ------------ + { + { "http://user:pass@host:80/path?query#fragment", + { "http", "user", "pass", "host", 80, "/path", "query", "fragment" } }, + { "http://www.host.tld/path?query#fragment", + { "http", "", "", "www.host.tld",0, "/path", "query", "fragment" } } + }; + +void UrlTest::ExplodeImplodeTest() +{ + uint8 testIndex; + BUrl testUrl; + + for (testIndex = 0; testIndex < (sizeof(kTestExplode) / sizeof(ExplodeTest)); testIndex++) + { + NextSubTest(); + testUrl.SetUrlString(kTestExplode[testIndex].url); + + CPPUNIT_ASSERT(BString(kTestExplode[testIndex].url) + == BString(testUrl.UrlString())); + CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.protocol) + == BString(testUrl.Protocol())); + CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.userName) + == BString(testUrl.UserName())); + CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.password) + == BString(testUrl.Password())); + CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.host) + == BString(testUrl.Host())); + CPPUNIT_ASSERT(kTestExplode[testIndex].expected.port == testUrl.Port()); + CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.path) + == BString(testUrl.Path())); + CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.request) + == BString(testUrl.Request())); + CPPUNIT_ASSERT(BString(kTestExplode[testIndex].expected.fragment) + == BString(testUrl.Fragment())); + } +} + + +void +UrlTest::PathOnly() +{ + BUrl test = "lol"; + CPPUNIT_ASSERT(test.HasPath()); + CPPUNIT_ASSERT(test.Path() == "lol"); + CPPUNIT_ASSERT(test.UrlString() == "lol"); +} + + +/* static */ void +UrlTest::AddTests(BTestSuite& parent) +{ + CppUnit::TestSuite& suite = *new CppUnit::TestSuite("UrlTest"); + + suite.addTest(new CppUnit::TestCaller( + "UrlTest::LengthTest", &UrlTest::LengthTest)); + suite.addTest(new CppUnit::TestCaller( + "UrlTest::ExplodeImplodeTest", &UrlTest::ExplodeImplodeTest)); + suite.addTest(new CppUnit::TestCaller("UrlTest::PathOnly", + &UrlTest::PathOnly)); + + parent.addTest("UrlTest", &suite); +} diff --git a/src/tests/kits/net/url/UrlTest.h b/src/tests/kits/net/url/UrlTest.h new file mode 100644 index 00000000000..24c9722f9d3 --- /dev/null +++ b/src/tests/kits/net/url/UrlTest.h @@ -0,0 +1,26 @@ +/* + * Copyright 2014 Haiku, inc. + * Distributed under the terms of the MIT License. + */ +#ifndef URL_TEST_H +#define URL_TEST_H + + +#include +#include + + +class UrlTest: public BTestCase { +public: + UrlTest(); + virtual ~UrlTest(); + + void LengthTest(); + void ExplodeImplodeTest(); + void PathOnly(); + + static void AddTests(BTestSuite& suite); +}; + + +#endif diff --git a/src/tests/kits/net/url/url_test.cpp b/src/tests/kits/net/url/url_test.cpp deleted file mode 100644 index c23010a595b..00000000000 --- a/src/tests/kits/net/url/url_test.cpp +++ /dev/null @@ -1,116 +0,0 @@ -#include -#include -#include -#include - -#include - -#ifdef ASSERT -#undef ASSERT -#endif - -#define REPORT(assert, line) cout << "ASSERT() failed at line " << line << ": " << #assert << endl -#define ASSERT(assertion) { if (!(assertion)) { REPORT(assertion, __LINE__ ); } } - -using namespace std; - -typedef struct -{ - const char* url; - - struct - { - const char* protocol; - const char* userName; - const char* password; - const char* host; - int16 port; - const char* path; - const char* request; - const char* fragment; - } expected; -} ExplodeTest; - - -const char* kTestLength[] = - { - "http://user:pass@www.foo.com:80/path?query#fragment", - "http://user:pass@www.foo.com:80/path?query#", - "http://user:pass@www.foo.com:80/path?query", - "http://user:pass@www.foo.com:80/path?", - "http://user:pass@www.foo.com:80/path", - "http://user:pass@www.foo.com:80/", - "http://user:pass@www.foo.com", - "http://user:pass@", - "http://www.foo.com", - "http://", - "http:" - }; - -const ExplodeTest kTestExplode[] = - // Url - // Protocol User Password Hostname Port Path Request Fragment - // -------- --------- --------- --------- ---- ---------- ---------- ------------ - { - { "http://user:pass@host:80/path?query#fragment", - { "http", "user", "pass", "host", 80, "/path", "query", "fragment" } }, - { "http://www.host.tld/path?query#fragment", - { "http", "", "", "www.host.tld",0, "/path", "query", "fragment" } } - }; - -// Test if rebuild url are the same length of parsed one -void lengthTest() -{ - int8 testIndex; - BUrl testUrl; - - for (testIndex = 0; kTestLength[testIndex] != NULL; testIndex++) - { - testUrl.SetUrlString(kTestLength[testIndex]); - - cout << "`" << kTestLength[testIndex] << "' vs `" << testUrl.UrlString() << "'" << endl; - ASSERT(strlen(kTestLength[testIndex]) == strlen(testUrl.UrlString())); - } -} - -void explodeImplodeTest() -{ - uint8 testIndex; - BUrl testUrl; - - for (testIndex = 0; testIndex < (sizeof(kTestExplode) / sizeof(ExplodeTest)); testIndex++) - { - testUrl.SetUrlString(kTestExplode[testIndex].url); - - ASSERT(BString(kTestExplode[testIndex].url) == BString(testUrl.UrlString())); - ASSERT(BString(kTestExplode[testIndex].expected.protocol) == BString(testUrl.Protocol())); - ASSERT(BString(kTestExplode[testIndex].expected.userName) == BString(testUrl.UserName())); - ASSERT(BString(kTestExplode[testIndex].expected.password) == BString(testUrl.Password())); - ASSERT(BString(kTestExplode[testIndex].expected.host) == BString(testUrl.Host())); - ASSERT(kTestExplode[testIndex].expected.port == testUrl.Port()); - ASSERT(BString(kTestExplode[testIndex].expected.path) == BString(testUrl.Path())); - ASSERT(BString(kTestExplode[testIndex].expected.request) == BString(testUrl.Request())); - ASSERT(BString(kTestExplode[testIndex].expected.fragment) == BString(testUrl.Fragment())); - } -} - -using std::cout; -using std::endl; - -int -main(int, char**) -{ - cout << "Running lengthTest:" << endl; - lengthTest(); - cout << endl; - - cout << "Running explodeImplodeTest:" << endl; - explodeImplodeTest(); - cout << endl; - - BUrl test = "lol"; - cout << "Path: " << test.HasPath() << " -> " << test.Path() << endl; - cout << "Rebuild: " << test.UrlString() << endl; - - return EXIT_SUCCESS; -}