hrev47589 adds 1 changeset to branch 'master' old head: dc8665f7457238d301d20e49da1b8b20a72c2a6d new head: 1dc356fb06ea596f88abc075656dc225310e5de9 overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=1dc356f+%5Edc8665f ---------------------------------------------------------------------------- 1dc356f: Test for the "Host" header containing the port. * Also tests that receiving data using a listener works. [ Adrien Destugues <pulkomandy@xxxxxxxxxxxxx> ] ---------------------------------------------------------------------------- Revision: hrev47589 Commit: 1dc356fb06ea596f88abc075656dc225310e5de9 URL: http://cgit.haiku-os.org/haiku/commit/?id=1dc356f Author: Adrien Destugues <pulkomandy@xxxxxxxxxxxxx> Date: Mon Jul 28 14:05:52 2014 UTC ---------------------------------------------------------------------------- 2 files changed, 100 insertions(+), 36 deletions(-) src/tests/kits/net/service/HttpTest.cpp | 109 +++++++++++++++++++++------- src/tests/kits/net/service/HttpTest.h | 27 ++++--- ---------------------------------------------------------------------------- diff --git a/src/tests/kits/net/service/HttpTest.cpp b/src/tests/kits/net/service/HttpTest.cpp index 58d1827..3bde8ee 100644 --- a/src/tests/kits/net/service/HttpTest.cpp +++ b/src/tests/kits/net/service/HttpTest.cpp @@ -16,7 +16,10 @@ #include <HttpRequest.h> #include <cppunit/TestCaller.h> -#include <cppunit/TestSuite.h> + + +static const int kHeaderCountInTrivialRequest = 7; + // FIXME This is too strict and not very useful. HttpTest::HttpTest() @@ -44,13 +47,13 @@ HttpTest::GetTest() while(t.IsRunning()) snooze(1000); - CPPUNIT_ASSERT(t.Status() == B_OK); + CPPUNIT_ASSERT_EQUAL(B_OK, t.Status()); const BHttpResult& r = dynamic_cast<const BHttpResult&>(t.Result()); CPPUNIT_ASSERT_EQUAL(200, r.StatusCode()); CPPUNIT_ASSERT_EQUAL(BString("OK"), r.StatusText()); - CPPUNIT_ASSERT_EQUAL(6, r.Headers().CountHeaders()); - // FIXME This is too strict and not very useful. + CPPUNIT_ASSERT_EQUAL(kHeaderCountInTrivialRequest, + r.Headers().CountHeaders()); CPPUNIT_ASSERT_EQUAL(42, r.Length()); // Fixed size as we know the response format. CPPUNIT_ASSERT(!c.GetCookieJar().GetIterator().HasNext()); @@ -58,6 +61,47 @@ HttpTest::GetTest() } +class PortTestListener: public BUrlProtocolListener +{ +public: + virtual ~PortTestListener() {}; + + void DataReceived(BUrlRequest*, const char* data, off_t, + ssize_t size) + { + fResult.Append(data, size); + } + + BString fResult; +}; + + +void +HttpTest::PortTest() +{ + BUrl testUrl("http://portquiz.net:4242";); + BHttpRequest t(testUrl); + + // portquiz returns more easily parseable results when UA is Wget... + t.SetUserAgent("Wget/1.15 (haiku testsuite)"); + + PortTestListener listener; + t.SetListener(&listener); + + CPPUNIT_ASSERT(t.Run()); + + while(t.IsRunning()) + snooze(1000); + + CPPUNIT_ASSERT_EQUAL(B_OK, t.Status()); + + const BHttpResult& r = dynamic_cast<const BHttpResult&>(t.Result()); + CPPUNIT_ASSERT_EQUAL(200, r.StatusCode()); + + CPPUNIT_ASSERT(listener.fResult.StartsWith("Port 4242 test successful!")); +} + + void HttpTest::UploadTest() { @@ -79,12 +123,12 @@ HttpTest::UploadTest() while(t.IsRunning()) snooze(1000); - CPPUNIT_ASSERT(t.Status() == B_OK); + CPPUNIT_ASSERT_EQUAL(B_OK, t.Status()); const BHttpResult& r = dynamic_cast<const BHttpResult&>(t.Result()); CPPUNIT_ASSERT_EQUAL(200, r.StatusCode()); CPPUNIT_ASSERT_EQUAL(BString("OK"), r.StatusText()); - CPPUNIT_ASSERT_EQUAL(460, r.Length()); + CPPUNIT_ASSERT_EQUAL(474, r.Length()); // Fixed size as we know the response format. } @@ -121,32 +165,53 @@ HttpTest::_AuthTest(BUrl& testUrl) while(t.IsRunning()) snooze(1000); - CPPUNIT_ASSERT(t.Status() == B_OK); + CPPUNIT_ASSERT_EQUAL(B_OK, t.Status()); const BHttpResult& r = dynamic_cast<const BHttpResult&>(t.Result()); CPPUNIT_ASSERT_EQUAL(200, r.StatusCode()); CPPUNIT_ASSERT_EQUAL(BString("OK"), r.StatusText()); - CPPUNIT_ASSERT_EQUAL(6, r.Headers().CountHeaders()); - // FIXME This is too strict and not very useful. - CPPUNIT_ASSERT_EQUAL(47, r.Length()); + CPPUNIT_ASSERT_EQUAL(kHeaderCountInTrivialRequest, + r.Headers().CountHeaders()); + CPPUNIT_ASSERT_EQUAL(48, r.Length()); // Fixed size as we know the response format. } +/* static */ template<class T> void +HttpTest::_AddCommonTests(BString prefix, CppUnit::TestSuite& suite) +{ + BString name; + + name = prefix; + name << "GetTest"; + suite.addTest(new CppUnit::TestCaller<T>(name.String(), &T::GetTest)); + + name = prefix; + name << "UploadTest"; + suite.addTest(new CppUnit::TestCaller<T>(name.String(), &T::UploadTest)); + + name = prefix; + name << "AuthBasicTest"; + suite.addTest(new CppUnit::TestCaller<T>(name.String(), &T::AuthBasicTest)); + + name = prefix; + name << "AuthDigestTest"; + suite.addTest(new CppUnit::TestCaller<T>(name.String(), &T::AuthDigestTest)); +} + + /* static */ void HttpTest::AddTests(BTestSuite& parent) { { CppUnit::TestSuite& suite = *new CppUnit::TestSuite("HttpTest"); + // HTTP + HTTPs + _AddCommonTests<HttpTest>("HttpTest::", suite); + + // HTTP-only suite.addTest(new CppUnit::TestCaller<HttpTest>( - "HttpTest::GetTest", &HttpTest::GetTest)); - suite.addTest(new CppUnit::TestCaller<HttpTest>( - "HttpTest::UploadTest", &HttpTest::UploadTest)); - suite.addTest(new CppUnit::TestCaller<HttpTest>( - "HttpTest::AuthBasicTest", &HttpTest::AuthBasicTest)); - suite.addTest(new CppUnit::TestCaller<HttpTest>( - "HttpTest::AuthDigestTest", &HttpTest::AuthDigestTest)); + "HttpTest::PortTest", &HttpTest::PortTest)); parent.addTest("HttpTest", &suite); } @@ -154,14 +219,8 @@ HttpTest::AddTests(BTestSuite& parent) { CppUnit::TestSuite& suite = *new CppUnit::TestSuite("HttpsTest"); - suite.addTest(new CppUnit::TestCaller<HttpsTest>( - "HttpsTest::GetTest", &HttpsTest::GetTest)); - suite.addTest(new CppUnit::TestCaller<HttpsTest>( - "HttpsTest::UploadTest", &HttpsTest::UploadTest)); - suite.addTest(new CppUnit::TestCaller<HttpsTest>( - "HttpsTest::AuthBasicTest", &HttpsTest::AuthBasicTest)); - suite.addTest(new CppUnit::TestCaller<HttpsTest>( - "HttpsTest::AuthDigestTest", &HttpsTest::AuthDigestTest)); + // HTTP + HTTPs + _AddCommonTests<HttpsTest>("HttpsTest::", suite); parent.addTest("HttpsTest", &suite); } diff --git a/src/tests/kits/net/service/HttpTest.h b/src/tests/kits/net/service/HttpTest.h index 6631a6b..c8abf55 100644 --- a/src/tests/kits/net/service/HttpTest.h +++ b/src/tests/kits/net/service/HttpTest.h @@ -11,31 +11,36 @@ #include <TestCase.h> #include <TestSuite.h> +#include <cppunit/TestSuite.h> + class HttpTest: public BTestCase { public: - HttpTest(); - virtual ~HttpTest(); + HttpTest(); + virtual ~HttpTest(); - void GetTest(); - void UploadTest(); - void AuthBasicTest(); - void AuthDigestTest(); - void ListenerTest(); + void GetTest(); + void PortTest(); + void UploadTest(); + void AuthBasicTest(); + void AuthDigestTest(); - static void AddTests(BTestSuite& suite); + static void AddTests(BTestSuite& suite); private: - void _AuthTest(BUrl& url); + void _AuthTest(BUrl& url); + + template<class T> static void _AddCommonTests(BString prefix, + CppUnit::TestSuite& suite); protected: - BUrl fBaseUrl; + BUrl fBaseUrl; }; class HttpsTest: public HttpTest { public: - HttpsTest(); + HttpsTest(); };