Make HttpRequest tests use the test framework

* Use httpbin.org as a server, rather than a local machine.
* Also add an HTTPS version
This commit is contained in:
Adrien Destugues 2014-06-09 14:11:15 +02:00
parent f38d4d4510
commit 3af9a2cac2
10 changed files with 223 additions and 224 deletions

View File

@ -0,0 +1,178 @@
/*
* Copyright 2010, Christophe Huriaux
* Copyright 2014, Haiku, inc.
* Distributed under the terms of the MIT licence
*/
#include "HttpTest.h"
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <NetworkKit.h>
#include <HttpRequest.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>
HttpTest::HttpTest()
: fBaseUrl("http://httpbin.org/")
{
}
HttpTest::~HttpTest()
{
}
void
HttpTest::GetTest()
{
BUrl testUrl(fBaseUrl, "/user-agent");
BUrlContext c;
BHttpRequest t(testUrl);
t.SetContext(&c);
CPPUNIT_ASSERT(t.Run());
while(t.IsRunning())
snooze(1000);
CPPUNIT_ASSERT(t.Status() == B_OK);
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(42, r.Length());
// Fixed size as we know the response format.
CPPUNIT_ASSERT(!c.GetCookieJar().GetIterator().HasNext());
// This page should not set cookies
}
void
HttpTest::UploadTest()
{
BUrl testUrl(fBaseUrl, "/post");
BUrlContext c;
BHttpRequest t(testUrl);
t.SetContext(&c);
BHttpForm f;
f.AddString("hello", "world");
CPPUNIT_ASSERT(f.AddFile("_uploadfile", BPath("/system/data/licenses/MIT"))
== B_OK);
t.SetPostFields(f);
CPPUNIT_ASSERT(t.Run());
while(t.IsRunning())
snooze(1000);
CPPUNIT_ASSERT(t.Status() == B_OK);
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());
// Fixed size as we know the response format.
}
void
HttpTest::AuthBasicTest()
{
BUrl testUrl(fBaseUrl, "/basic-auth/walter/secret");
_AuthTest(testUrl);
}
void
HttpTest::AuthDigestTest()
{
BUrl testUrl(fBaseUrl, "/digest-auth/auth/walter/secret");
_AuthTest(testUrl);
}
void
HttpTest::_AuthTest(BUrl& testUrl)
{
BUrlContext c;
BHttpRequest t(testUrl);
t.SetContext(&c);
t.SetUserName("walter");
t.SetPassword("secret");
CPPUNIT_ASSERT(t.Run());
while(t.IsRunning())
snooze(1000);
CPPUNIT_ASSERT(t.Status() == B_OK);
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());
// Fixed size as we know the response format.
}
/* static */ void
HttpTest::AddTests(BTestSuite& parent)
{
{
CppUnit::TestSuite& suite = *new CppUnit::TestSuite("HttpTest");
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));
parent.addTest("HttpTest", &suite);
}
{
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));
parent.addTest("HttpsTest", &suite);
}
}
// # pragma mark - HTTPS
HttpsTest::HttpsTest()
: HttpTest()
{
fBaseUrl.SetProtocol("https");
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2014 Haiku, inc.
* Distributed under the terms of the MIT License.
*/
#ifndef HTTP_TEST_H
#define HTTP_TEST_H
#include <Url.h>
#include <TestCase.h>
#include <TestSuite.h>
class HttpTest: public BTestCase {
public:
HttpTest();
virtual ~HttpTest();
void GetTest();
void UploadTest();
void AuthBasicTest();
void AuthDigestTest();
void ListenerTest();
static void AddTests(BTestSuite& suite);
private:
void _AuthTest(BUrl& url);
protected:
BUrl fBaseUrl;
};
class HttpsTest: public HttpTest {
public:
HttpsTest();
};
#endif

View File

@ -4,6 +4,7 @@ UnitTestLib servicekittest.so :
ServiceKitTestAddon.cpp
CookieTest.cpp
HttpTest.cpp
UrlTest.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)

View File

@ -8,6 +8,7 @@
#include <TestSuiteAddon.h>
#include "CookieTest.h"
#include "HttpTest.h"
#include "UrlTest.h"
@ -18,6 +19,7 @@ getTestSuite()
CookieTest::AddTests(*suite);
UrlTest::AddTests(*suite);
HttpTest::AddTests(*suite);
return suite;
}

View File

@ -1,25 +1,5 @@
SubDir HAIKU_TOP src tests kits net urlRequest ;
Application urlRequest_test
: urlRequest_test.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)
;
Application urlAuth_test
: urlAuth_test.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)
;
Application urlUpload_test
: urlUpload_test.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)
;
Application urlAuthBasic_test
: urlAuthBasic_test.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)
;
Application urlProtocolListener_test
: urlProtocolListener_test.cpp
: be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++)

View File

@ -1,3 +0,0 @@
Hello, world !
this is a file ...

View File

@ -1,44 +0,0 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <KernelKit.h>
#include <NetworkKit.h>
using std::cout;
using std::endl;
int
main(int, char**)
{
BUrl authTest("http://192.168.1.11/~chris/auth_basic/");
BUrlRequest t(authTest);
BUrlContext c;
t.SetContext(&c);
t.SetProtocolOption(B_HTTPOPT_AUTHUSERNAME, (void*)"haiku");
t.SetProtocolOption(B_HTTPOPT_AUTHPASSWORD, (void*)"haiku");
if (t.Perform() != B_OK) {
cout << "Error while performing request!" << endl;
return EXIT_FAILURE;
}
// Do nothing while the request is not finished
while (t.IsRunning()) {
cout << std::flush;
snooze(10000);
}
// Print the result
cout << "Request result : " << t.Result().StatusCode() << " (" << t.Result().StatusText() << ")" << endl;
//cout << " * " << c.GetCookieJar().CountCookies() << " cookies in context after request" << endl;
cout << " * " << t.Result().Headers().CountHeaders() << " headers received" << endl;
cout << " * " << t.Result().RawData().Position() << " bytes of raw data:" << endl;
cout << t.Result() << endl;
cout << "End of request" << endl << endl;
return EXIT_SUCCESS;
}

View File

@ -1,50 +0,0 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <KernelKit.h>
#include <NetworkKit.h>
#ifdef ASSERT
#undef ASSERT
#endif
#define REPORT(i, assert, line) cout << "ASSERT() failed at line " << line \
<< ": " << #assert << " (test #" << i << ")" << endl
#define ASSERT(index, assertion) { if (!(assertion)) { REPORT(index, assertion, __LINE__ ); } }
using std::cout;
using std::endl;
int
main(int, char**)
{
BUrl authTest("http://192.168.1.11/~chris/auth_digest/");
BUrlRequest t(authTest);
BUrlContext c;
t.SetContext(&c);
t.SetProtocolOption(B_HTTPOPT_AUTHUSERNAME, (void*)"haiku");
t.SetProtocolOption(B_HTTPOPT_AUTHPASSWORD, (void*)"haiku");
if (t.Perform() != B_OK) {
cout << "Error while performing request!" << endl;
return EXIT_FAILURE;
}
// Do nothing while the request is not finished
while (t.IsRunning()) {
cout << std::flush;
snooze(10000);
}
// Print the result
cout << "Request result : " << t.Result().StatusCode() << " (" << t.Result().StatusText() << ")" << endl;
//cout << " * " << c.GetCookieJar().CountCookies() << " cookies in context after request" << endl;
cout << " * " << t.Result().Headers().CountHeaders() << " headers received" << endl;
cout << " * " << t.Result().RawData().Position() << " bytes of raw data:" << endl;
cout << t.Result() << endl;
cout << "End of request" << endl << endl;
}

View File

@ -1,56 +0,0 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <KernelKit.h>
#include <NetworkKit.h>
using std::cout;
using std::endl;
int
main(int, char**)
{
BUrl yahoo("http://www.yahoo.fr");
BUrlContext c;
BUrlRequest t(yahoo);
t.SetContext(&c);
if (!t.InitCheck()) {
cout << "URL request failed to initialize" << endl;
return EXIT_FAILURE;
}
if (t.Perform() != B_OK) {
cout << "Error while performing request!" << endl;
return EXIT_FAILURE;
}
// Do nothing while the request is not finished
while (t.IsRunning()) {
cout << std::flush;
snooze(10000);
}
// Print the result
cout << "Request result : " << t.Result().StatusCode() << " (" << t.Result().StatusText() << ")" << endl;
//cout << " * " << c.GetCookieJar().CountCookies() << " cookies in context after request" << endl;
cout << " * " << t.Result().Headers().CountHeaders() << " headers received" << endl;
cout << " * " << t.Result().RawData().Position() << " bytes of raw data:" << endl;
cout << t.Result() << endl;
cout << "End of request" << endl << endl;
// Flat view of the cookie jar :
cout << "cookie.txt :" << endl;
ssize_t flatSize = c.GetCookieJar().FlattenedSize();
char *flatCookieJar = new char[flatSize];
c.GetCookieJar().Flatten(flatCookieJar, flatSize);
cout << flatCookieJar << endl << endl;
return EXIT_SUCCESS;
}

View File

@ -1,51 +0,0 @@
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <KernelKit.h>
#include <NetworkKit.h>
using std::cout;
using std::endl;
int
main(int, char**)
{
BUrl google("http://192.168.1.11/~chris/post.php");
BUrlSynchronousRequest t(google);
BHttpForm f;
f.AddString("hello", "world");
if (f.AddFile("_uploadfile", BPath("libservices.so")) != B_OK) {
cout << "File upload failed" << endl;
return EXIT_FAILURE;
}
// Print form
for (BHttpForm::Iterator it(f.GetIterator()); BHttpFormData* element = it.Next(); ) {
cout << element->Name() << ": " << element->Type() << endl;
}
if (!t.InitCheck()) {
cout << "URL request failed to initialize" << endl;
return EXIT_FAILURE;
}
// Inject form in request
bool discard = true;
t.SetProtocolOption(B_HTTPOPT_DISCARD_DATA, &discard);
t.SetProtocolOption(B_HTTPOPT_POSTFIELDS, &f);
if (t.Perform() != B_OK) {
cout << "Error while performing request!" << endl;
return EXIT_FAILURE;
}
t.WaitUntilCompletion();
cout << "Request finished!" << endl;
cout << t.Result() << endl;
return EXIT_SUCCESS;
}