1 /* 2 * Copyright 2010, Christophe Huriaux 3 * Copyright 2014, Haiku, inc. 4 * Distributed under the terms of the MIT licence 5 */ 6 7 8 #include "HttpTest.h" 9 10 11 #include <cstdlib> 12 #include <cstring> 13 #include <cstdio> 14 15 #include <NetworkKit.h> 16 #include <HttpRequest.h> 17 18 #include <cppunit/TestCaller.h> 19 20 21 static const int kHeaderCountInTrivialRequest = 7; 22 // FIXME This is too strict and not very useful. 23 24 25 HttpTest::HttpTest() 26 : fBaseUrl("http://httpbin.org/") 27 { 28 } 29 30 31 HttpTest::~HttpTest() 32 { 33 } 34 35 36 void 37 HttpTest::GetTest() 38 { 39 BUrl testUrl(fBaseUrl, "/user-agent"); 40 BUrlContext* c = new BUrlContext(); 41 c->AcquireReference(); 42 BHttpRequest t(testUrl); 43 44 t.SetContext(c); 45 46 CPPUNIT_ASSERT(t.Run()); 47 48 while (t.IsRunning()) 49 snooze(1000); 50 51 CPPUNIT_ASSERT_EQUAL(B_OK, t.Status()); 52 53 const BHttpResult& r = dynamic_cast<const BHttpResult&>(t.Result()); 54 CPPUNIT_ASSERT_EQUAL(200, r.StatusCode()); 55 CPPUNIT_ASSERT_EQUAL(BString("OK"), r.StatusText()); 56 CPPUNIT_ASSERT_EQUAL(kHeaderCountInTrivialRequest, 57 r.Headers().CountHeaders()); 58 CPPUNIT_ASSERT_EQUAL(42, r.Length()); 59 // Fixed size as we know the response format. 60 CPPUNIT_ASSERT(!c->GetCookieJar().GetIterator().HasNext()); 61 // This page should not set cookies 62 63 c->ReleaseReference(); 64 } 65 66 67 void 68 HttpTest::ProxyTest() 69 { 70 BUrl testUrl(fBaseUrl, "/user-agent"); 71 72 BUrlContext* c = new BUrlContext(); 73 c->AcquireReference(); 74 c->SetProxy("120.203.214.182", 83); 75 76 BHttpRequest t(testUrl); 77 t.SetContext(c); 78 79 BUrlProtocolListener l; 80 t.SetListener(&l); 81 82 CPPUNIT_ASSERT(t.Run()); 83 84 while (t.IsRunning()) 85 snooze(1000); 86 87 CPPUNIT_ASSERT_EQUAL(B_OK, t.Status()); 88 89 const BHttpResult& r = dynamic_cast<const BHttpResult&>(t.Result()); 90 91 printf("%s\n", r.StatusText().String()); 92 93 CPPUNIT_ASSERT_EQUAL(200, r.StatusCode()); 94 CPPUNIT_ASSERT_EQUAL(BString("OK"), r.StatusText()); 95 CPPUNIT_ASSERT_EQUAL(42, r.Length()); 96 // Fixed size as we know the response format. 97 CPPUNIT_ASSERT(!c->GetCookieJar().GetIterator().HasNext()); 98 // This page should not set cookies 99 100 c->ReleaseReference(); 101 } 102 103 104 class PortTestListener: public BUrlProtocolListener 105 { 106 public: 107 virtual ~PortTestListener() {}; 108 109 void DataReceived(BUrlRequest*, const char* data, off_t, 110 ssize_t size) 111 { 112 fResult.Append(data, size); 113 } 114 115 BString fResult; 116 }; 117 118 119 void 120 HttpTest::PortTest() 121 { 122 BUrl testUrl("http://portquiz.net:4242"); 123 BHttpRequest t(testUrl); 124 125 // portquiz returns more easily parseable results when UA is Wget... 126 t.SetUserAgent("Wget/1.15 (haiku testsuite)"); 127 128 PortTestListener listener; 129 t.SetListener(&listener); 130 131 CPPUNIT_ASSERT(t.Run()); 132 133 while (t.IsRunning()) 134 snooze(1000); 135 136 CPPUNIT_ASSERT_EQUAL(B_OK, t.Status()); 137 138 const BHttpResult& r = dynamic_cast<const BHttpResult&>(t.Result()); 139 CPPUNIT_ASSERT_EQUAL(200, r.StatusCode()); 140 141 CPPUNIT_ASSERT(listener.fResult.StartsWith("Port 4242 test successful!")); 142 } 143 144 145 void 146 HttpTest::UploadTest() 147 { 148 BUrl testUrl(fBaseUrl, "/post"); 149 BUrlContext c; 150 BHttpRequest t(testUrl); 151 152 t.SetContext(&c); 153 154 BHttpForm f; 155 f.AddString("hello", "world"); 156 CPPUNIT_ASSERT(f.AddFile("_uploadfile", BPath("/system/data/licenses/MIT")) 157 == B_OK); 158 159 t.SetPostFields(f); 160 161 CPPUNIT_ASSERT(t.Run()); 162 163 while (t.IsRunning()) 164 snooze(1000); 165 166 CPPUNIT_ASSERT_EQUAL(B_OK, t.Status()); 167 168 const BHttpResult& r = dynamic_cast<const BHttpResult&>(t.Result()); 169 CPPUNIT_ASSERT_EQUAL(200, r.StatusCode()); 170 CPPUNIT_ASSERT_EQUAL(BString("OK"), r.StatusText()); 171 CPPUNIT_ASSERT_EQUAL(466, r.Length()); 172 // Fixed size as we know the response format. 173 } 174 175 176 void 177 HttpTest::AuthBasicTest() 178 { 179 BUrl testUrl(fBaseUrl, "/basic-auth/walter/secret"); 180 _AuthTest(testUrl); 181 } 182 183 184 void 185 HttpTest::AuthDigestTest() 186 { 187 BUrl testUrl(fBaseUrl, "/digest-auth/auth/walter/secret"); 188 _AuthTest(testUrl); 189 } 190 191 192 void 193 HttpTest::_AuthTest(BUrl& testUrl) 194 { 195 BUrlContext c; 196 BHttpRequest t(testUrl); 197 198 t.SetContext(&c); 199 200 t.SetUserName("walter"); 201 t.SetPassword("secret"); 202 203 CPPUNIT_ASSERT(t.Run()); 204 205 while (t.IsRunning()) 206 snooze(1000); 207 208 CPPUNIT_ASSERT_EQUAL(B_OK, t.Status()); 209 210 const BHttpResult& r = dynamic_cast<const BHttpResult&>(t.Result()); 211 CPPUNIT_ASSERT_EQUAL(200, r.StatusCode()); 212 CPPUNIT_ASSERT_EQUAL(BString("OK"), r.StatusText()); 213 CPPUNIT_ASSERT_EQUAL(kHeaderCountInTrivialRequest, 214 r.Headers().CountHeaders()); 215 CPPUNIT_ASSERT_EQUAL(48, r.Length()); 216 // Fixed size as we know the response format. 217 } 218 219 220 /* static */ template<class T> void 221 HttpTest::_AddCommonTests(BString prefix, CppUnit::TestSuite& suite) 222 { 223 BString name; 224 225 name = prefix; 226 name << "GetTest"; 227 suite.addTest(new CppUnit::TestCaller<T>(name.String(), &T::GetTest)); 228 229 name = prefix; 230 name << "UploadTest"; 231 suite.addTest(new CppUnit::TestCaller<T>(name.String(), &T::UploadTest)); 232 233 name = prefix; 234 name << "AuthBasicTest"; 235 suite.addTest(new CppUnit::TestCaller<T>(name.String(), &T::AuthBasicTest)); 236 237 name = prefix; 238 name << "AuthDigestTest"; 239 suite.addTest(new CppUnit::TestCaller<T>(name.String(), &T::AuthDigestTest)); 240 } 241 242 243 /* static */ void 244 HttpTest::AddTests(BTestSuite& parent) 245 { 246 { 247 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("HttpTest"); 248 249 // HTTP + HTTPs 250 _AddCommonTests<HttpTest>("HttpTest::", suite); 251 252 // HTTP-only 253 suite.addTest(new CppUnit::TestCaller<HttpTest>( 254 "HttpTest::PortTest", &HttpTest::PortTest)); 255 256 suite.addTest(new CppUnit::TestCaller<HttpTest>("HttpTest::ProxyTest", 257 &HttpTest::ProxyTest)); 258 259 parent.addTest("HttpTest", &suite); 260 } 261 262 { 263 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("HttpsTest"); 264 265 // HTTP + HTTPs 266 _AddCommonTests<HttpsTest>("HttpsTest::", suite); 267 268 parent.addTest("HttpsTest", &suite); 269 } 270 } 271 272 273 // # pragma mark - HTTPS 274 275 276 HttpsTest::HttpsTest() 277 : HttpTest() 278 { 279 fBaseUrl.SetProtocol("https"); 280 } 281