Test for the "Host" header containing the port.
* Also tests that receiving data using a listener works.
This commit is contained in:
parent
dc8665f745
commit
1dc356fb06
@ -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);
|
||||
}
|
||||
|
@ -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();
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user