diff --git a/src/tests/kits/net/service/HttpTest.cpp b/src/tests/kits/net/service/HttpTest.cpp new file mode 100644 index 0000000000..58d1827e20 --- /dev/null +++ b/src/tests/kits/net/service/HttpTest.cpp @@ -0,0 +1,178 @@ +/* + * Copyright 2010, Christophe Huriaux + * Copyright 2014, Haiku, inc. + * Distributed under the terms of the MIT licence + */ + + +#include "HttpTest.h" + + +#include +#include +#include + +#include +#include + +#include +#include + + +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(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(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(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::GetTest", &HttpTest::GetTest)); + suite.addTest(new CppUnit::TestCaller( + "HttpTest::UploadTest", &HttpTest::UploadTest)); + suite.addTest(new CppUnit::TestCaller( + "HttpTest::AuthBasicTest", &HttpTest::AuthBasicTest)); + suite.addTest(new CppUnit::TestCaller( + "HttpTest::AuthDigestTest", &HttpTest::AuthDigestTest)); + + parent.addTest("HttpTest", &suite); + } + + { + CppUnit::TestSuite& suite = *new CppUnit::TestSuite("HttpsTest"); + + suite.addTest(new CppUnit::TestCaller( + "HttpsTest::GetTest", &HttpsTest::GetTest)); + suite.addTest(new CppUnit::TestCaller( + "HttpsTest::UploadTest", &HttpsTest::UploadTest)); + suite.addTest(new CppUnit::TestCaller( + "HttpsTest::AuthBasicTest", &HttpsTest::AuthBasicTest)); + suite.addTest(new CppUnit::TestCaller( + "HttpsTest::AuthDigestTest", &HttpsTest::AuthDigestTest)); + + parent.addTest("HttpsTest", &suite); + } +} + + +// # pragma mark - HTTPS + + +HttpsTest::HttpsTest() + : HttpTest() +{ + fBaseUrl.SetProtocol("https"); +} diff --git a/src/tests/kits/net/service/HttpTest.h b/src/tests/kits/net/service/HttpTest.h new file mode 100644 index 0000000000..6631a6b21f --- /dev/null +++ b/src/tests/kits/net/service/HttpTest.h @@ -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 + +#include +#include + + +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 diff --git a/src/tests/kits/net/service/Jamfile b/src/tests/kits/net/service/Jamfile index d42e79e969..a11c2cffcd 100644 --- a/src/tests/kits/net/service/Jamfile +++ b/src/tests/kits/net/service/Jamfile @@ -4,6 +4,7 @@ UnitTestLib servicekittest.so : ServiceKitTestAddon.cpp CookieTest.cpp + HttpTest.cpp UrlTest.cpp : be $(TARGET_NETWORK_LIBS) $(HAIKU_NETAPI_LIB) $(TARGET_LIBSTDC++) diff --git a/src/tests/kits/net/service/ServiceKitTestAddon.cpp b/src/tests/kits/net/service/ServiceKitTestAddon.cpp index ffc4ea6dc5..06a81baceb 100644 --- a/src/tests/kits/net/service/ServiceKitTestAddon.cpp +++ b/src/tests/kits/net/service/ServiceKitTestAddon.cpp @@ -8,6 +8,7 @@ #include #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; } diff --git a/src/tests/kits/net/urlRequest/Jamfile b/src/tests/kits/net/urlRequest/Jamfile index 93ac30039c..dd53bf9fbf 100644 --- a/src/tests/kits/net/urlRequest/Jamfile +++ b/src/tests/kits/net/urlRequest/Jamfile @@ -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++) diff --git a/src/tests/kits/net/urlRequest/myFile b/src/tests/kits/net/urlRequest/myFile deleted file mode 100644 index 16f5be74fe..0000000000 --- a/src/tests/kits/net/urlRequest/myFile +++ /dev/null @@ -1,3 +0,0 @@ -Hello, world ! - -this is a file ... diff --git a/src/tests/kits/net/urlRequest/urlAuthBasic_test.cpp b/src/tests/kits/net/urlRequest/urlAuthBasic_test.cpp deleted file mode 100644 index 515eaf71da..0000000000 --- a/src/tests/kits/net/urlRequest/urlAuthBasic_test.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -#include -#include - -#include -#include - -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; -} diff --git a/src/tests/kits/net/urlRequest/urlAuth_test.cpp b/src/tests/kits/net/urlRequest/urlAuth_test.cpp deleted file mode 100644 index 8f2d658da0..0000000000 --- a/src/tests/kits/net/urlRequest/urlAuth_test.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include -#include -#include -#include - -#include -#include - -#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; -} diff --git a/src/tests/kits/net/urlRequest/urlRequest_test.cpp b/src/tests/kits/net/urlRequest/urlRequest_test.cpp deleted file mode 100644 index 00d9ed290a..0000000000 --- a/src/tests/kits/net/urlRequest/urlRequest_test.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include -#include -#include -#include - -#include -#include - -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; -} diff --git a/src/tests/kits/net/urlRequest/urlUpload_test.cpp b/src/tests/kits/net/urlRequest/urlUpload_test.cpp deleted file mode 100644 index 80ce896019..0000000000 --- a/src/tests/kits/net/urlRequest/urlUpload_test.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include -#include -#include -#include - -#include -#include - -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; -}