license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 4 | |
| 5 | #include <windows.h> |
| 6 | #include <shlobj.h> |
| 7 | #include <algorithm> |
| 8 | #include <string> |
| 9 | |
| 10 | #include "net/url_request/url_request_unittest.h" |
| 11 | |
| 12 | #include "base/message_loop.h" |
| 13 | #include "base/path_service.h" |
| 14 | #include "base/process_util.h" |
| 15 | #include "base/string_util.h" |
| 16 | #include "net/base/load_flags.h" |
| 17 | #include "net/base/net_errors.h" |
| 18 | #include "net/base/net_module.h" |
| 19 | #include "net/base/net_util.h" |
| 20 | #include "net/disk_cache/disk_cache.h" |
| 21 | #include "net/http/http_cache.h" |
| 22 | #include "net/http/http_network_layer.h" |
| 23 | #include "net/url_request/url_request.h" |
| 24 | #include "testing/gtest/include/gtest/gtest.h" |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | class URLRequestTest : public testing::Test { |
| 29 | }; |
| 30 | |
| 31 | class URLRequestHttpCacheContext : public URLRequestContext { |
| 32 | public: |
| 33 | URLRequestHttpCacheContext() { |
| 34 | http_transaction_factory_ = |
| 35 | new net::HttpCache(net::HttpNetworkLayer::CreateFactory(NULL), |
| 36 | disk_cache::CreateInMemoryCacheBackend(0)); |
| 37 | } |
| 38 | |
| 39 | virtual ~URLRequestHttpCacheContext() { |
| 40 | delete http_transaction_factory_; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | class TestURLRequest : public URLRequest { |
| 45 | public: |
| 46 | TestURLRequest(const GURL& url, Delegate* delegate) |
| 47 | : URLRequest(url, delegate) { |
| 48 | set_context(new URLRequestHttpCacheContext()); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | std::string TestNetResourceProvider(int key) { |
| 53 | return "header"; |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | |
| 58 | TEST(URLRequestTest, GetTest_NoCache) { |
| 59 | TestServer server(L""); |
| 60 | TestDelegate d; |
| 61 | { |
| 62 | TestURLRequest r(server.TestServerPage(""), &d); |
| 63 | |
| 64 | r.Start(); |
| 65 | EXPECT_TRUE(r.is_pending()); |
| 66 | |
| 67 | MessageLoop::current()->Run(); |
| 68 | |
| 69 | EXPECT_EQ(1, d.response_started_count()); |
| 70 | EXPECT_FALSE(d.received_data_before_response()); |
| 71 | EXPECT_NE(0, d.bytes_received()); |
| 72 | } |
| 73 | #ifndef NDEBUG |
| 74 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 75 | #endif |
| 76 | } |
| 77 | |
| 78 | TEST(URLRequestTest, GetTest) { |
| 79 | TestServer server(L""); |
| 80 | TestDelegate d; |
| 81 | { |
| 82 | TestURLRequest r(server.TestServerPage(""), &d); |
| 83 | |
| 84 | r.Start(); |
| 85 | EXPECT_TRUE(r.is_pending()); |
| 86 | |
| 87 | MessageLoop::current()->Run(); |
| 88 | |
| 89 | EXPECT_EQ(1, d.response_started_count()); |
| 90 | EXPECT_FALSE(d.received_data_before_response()); |
| 91 | EXPECT_NE(0, d.bytes_received()); |
| 92 | } |
| 93 | #ifndef NDEBUG |
| 94 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | TEST(URLRequestTest, CancelTest) { |
| 99 | TestDelegate d; |
| 100 | { |
| 101 | TestURLRequest r(GURL("http://www.google.com/"), &d); |
| 102 | |
| 103 | r.Start(); |
| 104 | EXPECT_TRUE(r.is_pending()); |
| 105 | |
| 106 | r.Cancel(); |
| 107 | |
| 108 | MessageLoop::current()->Run(); |
| 109 | |
| 110 | // We expect to receive OnResponseStarted even though the request has been |
| 111 | // cancelled. |
| 112 | EXPECT_EQ(1, d.response_started_count()); |
| 113 | EXPECT_EQ(0, d.bytes_received()); |
| 114 | EXPECT_FALSE(d.received_data_before_response()); |
| 115 | } |
| 116 | #ifndef NDEBUG |
| 117 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 118 | #endif |
| 119 | } |
| 120 | |
| 121 | TEST(URLRequestTest, CancelTest2) { |
| 122 | TestServer server(L""); |
| 123 | TestDelegate d; |
| 124 | { |
| 125 | TestURLRequest r(server.TestServerPage(""), &d); |
| 126 | |
| 127 | d.set_cancel_in_response_started(true); |
| 128 | |
| 129 | r.Start(); |
| 130 | EXPECT_TRUE(r.is_pending()); |
| 131 | |
| 132 | MessageLoop::current()->Run(); |
| 133 | |
| 134 | EXPECT_EQ(1, d.response_started_count()); |
| 135 | EXPECT_EQ(0, d.bytes_received()); |
| 136 | EXPECT_FALSE(d.received_data_before_response()); |
| 137 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 138 | } |
| 139 | #ifndef NDEBUG |
| 140 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 141 | #endif |
| 142 | } |
| 143 | |
| 144 | TEST(URLRequestTest, CancelTest3) { |
| 145 | TestServer server(L""); |
| 146 | TestDelegate d; |
| 147 | { |
| 148 | TestURLRequest r(server.TestServerPage(""), &d); |
| 149 | |
| 150 | d.set_cancel_in_received_data(true); |
| 151 | |
| 152 | r.Start(); |
| 153 | EXPECT_TRUE(r.is_pending()); |
| 154 | |
| 155 | MessageLoop::current()->Run(); |
| 156 | |
| 157 | EXPECT_EQ(1, d.response_started_count()); |
| 158 | // There is no guarantee about how much data was received |
| 159 | // before the cancel was issued. It could have been 0 bytes, |
| 160 | // or it could have been all the bytes. |
| 161 | // EXPECT_EQ(0, d.bytes_received()); |
| 162 | EXPECT_FALSE(d.received_data_before_response()); |
| 163 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 164 | } |
| 165 | #ifndef NDEBUG |
| 166 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 167 | #endif |
| 168 | } |
| 169 | |
| 170 | TEST(URLRequestTest, CancelTest4) { |
| 171 | TestServer server(L""); |
| 172 | TestDelegate d; |
| 173 | { |
| 174 | TestURLRequest r(server.TestServerPage(""), &d); |
| 175 | |
| 176 | r.Start(); |
| 177 | EXPECT_TRUE(r.is_pending()); |
| 178 | |
| 179 | // The request will be implicitly canceled when it is destroyed. The |
| 180 | // test delegate must not post a quit message when this happens because |
| 181 | // this test doesn't actually have a message loop. The quit message would |
| 182 | // get put on this thread's message queue and the next test would exit |
| 183 | // early, causing problems. |
| 184 | d.set_quit_on_complete(false); |
| 185 | } |
| 186 | // expect things to just cleanup properly. |
| 187 | |
| 188 | // we won't actually get a received reponse here because we've never run the |
| 189 | // message loop |
| 190 | EXPECT_FALSE(d.received_data_before_response()); |
| 191 | EXPECT_EQ(0, d.bytes_received()); |
| 192 | } |
| 193 | |
| 194 | TEST(URLRequestTest, CancelTest5) { |
| 195 | TestServer server(L""); |
| 196 | scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
| 197 | |
| 198 | // populate cache |
| 199 | { |
| 200 | TestDelegate d; |
| 201 | URLRequest r(server.TestServerPage("cachetime"), &d); |
| 202 | r.set_context(context); |
| 203 | r.Start(); |
| 204 | MessageLoop::current()->Run(); |
| 205 | EXPECT_EQ(URLRequestStatus::SUCCESS, r.status().status()); |
| 206 | } |
| 207 | |
| 208 | // cancel read from cache (see bug 990242) |
| 209 | { |
| 210 | TestDelegate d; |
| 211 | URLRequest r(server.TestServerPage("cachetime"), &d); |
| 212 | r.set_context(context); |
| 213 | r.Start(); |
| 214 | r.Cancel(); |
| 215 | MessageLoop::current()->Run(); |
| 216 | |
| 217 | EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
| 218 | EXPECT_EQ(1, d.response_started_count()); |
| 219 | EXPECT_EQ(0, d.bytes_received()); |
| 220 | EXPECT_FALSE(d.received_data_before_response()); |
| 221 | } |
| 222 | |
| 223 | #ifndef NDEBUG |
| 224 | DCHECK_EQ(url_request_metrics.object_count, 0); |
| 225 | #endif |
| 226 | } |
| 227 | |
| 228 | TEST(URLRequestTest, PostTest) { |
| 229 | TestServer server(L"net/data"); |
| 230 | |
| 231 | const int kMsgSize = 20000; // multiple of 10 |
| 232 | const int kIterations = 50; |
| 233 | char *uploadBytes = new char[kMsgSize+1]; |
| 234 | char *ptr = uploadBytes; |
| 235 | char marker = 'a'; |
| 236 | for(int idx=0; idx<kMsgSize/10; idx++) { |
| 237 | memcpy(ptr, "----------", 10); |
| 238 | ptr += 10; |
| 239 | if (idx % 100 == 0) { |
| 240 | ptr--; |
| 241 | *ptr++ = marker; |
| 242 | if (++marker > 'z') |
| 243 | marker = 'a'; |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | uploadBytes[kMsgSize] = '\0'; |
| 248 | |
| 249 | scoped_refptr<URLRequestContext> context = |
| 250 | new URLRequestHttpCacheContext(); |
| 251 | |
| 252 | for (int i = 0; i < kIterations; ++i) { |
| 253 | TestDelegate d; |
| 254 | URLRequest r(server.TestServerPage("echo"), &d); |
| 255 | r.set_context(context); |
| 256 | r.set_method("POST"); |
| 257 | |
| 258 | r.AppendBytesToUpload(uploadBytes, kMsgSize); |
| 259 | |
| 260 | r.Start(); |
| 261 | EXPECT_TRUE(r.is_pending()); |
| 262 | |
| 263 | MessageLoop::current()->Run(); |
| 264 | |
| 265 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 266 | (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 267 | |
| 268 | EXPECT_FALSE(d.received_data_before_response()); |
| 269 | EXPECT_EQ(uploadBytes, d.data_received()); |
| 270 | EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize),0); |
| 271 | EXPECT_EQ(d.data_received().compare(uploadBytes), 0); |
| 272 | } |
| 273 | delete[] uploadBytes; |
| 274 | #ifndef NDEBUG |
| 275 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 276 | #endif |
| 277 | } |
| 278 | |
| 279 | TEST(URLRequestTest, PostEmptyTest) { |
| 280 | TestServer server(L"net/data"); |
| 281 | TestDelegate d; |
| 282 | { |
| 283 | TestURLRequest r(server.TestServerPage("echo"), &d); |
| 284 | r.set_method("POST"); |
| 285 | |
| 286 | r.Start(); |
| 287 | EXPECT_TRUE(r.is_pending()); |
| 288 | |
| 289 | MessageLoop::current()->Run(); |
| 290 | |
| 291 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 292 | (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 293 | |
| 294 | EXPECT_FALSE(d.received_data_before_response()); |
| 295 | EXPECT_TRUE(d.data_received().empty()); |
| 296 | } |
| 297 | #ifndef NDEBUG |
| 298 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 299 | #endif |
| 300 | } |
| 301 | |
| 302 | TEST(URLRequestTest, PostFileTest) { |
| 303 | TestServer server(L"net/data"); |
| 304 | TestDelegate d; |
| 305 | { |
| 306 | TestURLRequest r(server.TestServerPage("echo"), &d); |
| 307 | r.set_method("POST"); |
| 308 | |
| 309 | std::wstring dir; |
| 310 | PathService::Get(base::DIR_EXE, &dir); |
| 311 | _wchdir(dir.c_str()); |
| 312 | |
| 313 | std::wstring path; |
| 314 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 315 | file_util::AppendToPath(&path, L"net"); |
| 316 | file_util::AppendToPath(&path, L"data"); |
| 317 | file_util::AppendToPath(&path, L"url_request_unittest"); |
| 318 | file_util::AppendToPath(&path, L"with-headers.html"); |
| 319 | r.AppendFileToUpload(path); |
| 320 | |
| 321 | // This file should just be ignored in the upload stream. |
| 322 | r.AppendFileToUpload(L"c:\\path\\to\\non\\existant\\file.randomness.12345"); |
| 323 | |
| 324 | r.Start(); |
| 325 | EXPECT_TRUE(r.is_pending()); |
| 326 | |
| 327 | MessageLoop::current()->Run(); |
| 328 | |
| 329 | HANDLE file = CreateFile(path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, |
| 330 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 331 | ASSERT_NE(INVALID_HANDLE_VALUE, file); |
| 332 | |
| 333 | DWORD size = GetFileSize(file, NULL); |
| 334 | scoped_array<char> buf(new char[size]); |
| 335 | |
| 336 | DWORD size_read; |
| 337 | EXPECT_TRUE(ReadFile(file, buf.get(), size, &size_read, NULL)); |
| 338 | |
| 339 | CloseHandle(file); |
| 340 | |
| 341 | EXPECT_EQ(size, size_read); |
| 342 | |
| 343 | ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
| 344 | (int) r.status().status() << ", os error: " << r.status().os_error(); |
| 345 | |
| 346 | EXPECT_FALSE(d.received_data_before_response()); |
| 347 | |
| 348 | ASSERT_EQ(size, d.bytes_received()); |
| 349 | EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size)); |
| 350 | } |
| 351 | #ifndef NDEBUG |
| 352 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 353 | #endif |
| 354 | } |
| 355 | |
| 356 | TEST(URLRequestTest, AboutBlankTest) { |
| 357 | TestDelegate d; |
| 358 | { |
| 359 | TestURLRequest r(GURL("about:blank"), &d); |
| 360 | |
| 361 | r.Start(); |
| 362 | EXPECT_TRUE(r.is_pending()); |
| 363 | |
| 364 | MessageLoop::current()->Run(); |
| 365 | |
| 366 | EXPECT_TRUE(!r.is_pending()); |
| 367 | EXPECT_FALSE(d.received_data_before_response()); |
| 368 | EXPECT_EQ(d.bytes_received(), 0); |
| 369 | } |
| 370 | #ifndef NDEBUG |
| 371 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 372 | #endif |
| 373 | } |
| 374 | |
| 375 | TEST(URLRequestTest, FileTest) { |
| 376 | std::wstring app_path; |
| 377 | PathService::Get(base::FILE_EXE, &app_path); |
| 378 | |
| 379 | std::string app_url = WideToUTF8(app_path); |
| 380 | std::replace(app_url.begin(), app_url.end(), |
| 381 | file_util::kPathSeparator, L'/'); |
| 382 | app_url.insert(0, "file:///"); |
| 383 | |
| 384 | TestDelegate d; |
| 385 | { |
| 386 | TestURLRequest r(GURL(app_url), &d); |
| 387 | |
| 388 | r.Start(); |
| 389 | EXPECT_TRUE(r.is_pending()); |
| 390 | |
| 391 | MessageLoop::current()->Run(); |
| 392 | |
| 393 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 394 | GetFileAttributesEx(app_path.c_str(), GetFileExInfoStandard, &data); |
| 395 | |
| 396 | EXPECT_TRUE(!r.is_pending()); |
| 397 | EXPECT_EQ(1, d.response_started_count()); |
| 398 | EXPECT_FALSE(d.received_data_before_response()); |
| 399 | EXPECT_EQ(d.bytes_received(), data.nFileSizeLow); |
| 400 | } |
| 401 | #ifndef NDEBUG |
| 402 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 403 | #endif |
| 404 | } |
| 405 | |
| 406 | TEST(URLRequestTest, InvalidUrlTest) { |
| 407 | TestDelegate d; |
| 408 | { |
| 409 | TestURLRequest r(GURL("invalid url"), &d); |
| 410 | |
| 411 | r.Start(); |
| 412 | EXPECT_TRUE(r.is_pending()); |
| 413 | |
| 414 | MessageLoop::current()->Run(); |
| 415 | EXPECT_TRUE(d.request_failed()); |
| 416 | } |
| 417 | #ifndef NDEBUG |
| 418 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 419 | #endif |
| 420 | } |
| 421 | |
| 422 | /* This test is disabled because it fails on some computers due to proxies |
| 423 | returning a page in response to this request rather than reporting failure. |
| 424 | TEST(URLRequestTest, DnsFailureTest) { |
| 425 | TestDelegate d; |
| 426 | { |
| 427 | URLRequest r(GURL("http://thisisnotavalidurl0123456789foo.com/"), &d); |
| 428 | |
| 429 | r.Start(); |
| 430 | EXPECT_TRUE(r.is_pending()); |
| 431 | |
| 432 | MessageLoop::current()->Run(); |
| 433 | EXPECT_TRUE(d.request_failed()); |
| 434 | } |
| 435 | #ifndef NDEBUG |
| 436 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 437 | #endif |
| 438 | } |
| 439 | */ |
| 440 | |
| 441 | TEST(URLRequestTest, ResponseHeadersTest) { |
| 442 | TestServer server(L"net/data/url_request_unittest"); |
| 443 | TestDelegate d; |
| 444 | TestURLRequest req(server.TestServerPage("files/with-headers.html"), &d); |
| 445 | req.Start(); |
| 446 | MessageLoop::current()->Run(); |
| 447 | |
| 448 | const net::HttpResponseHeaders* headers = req.response_headers(); |
| 449 | std::string header; |
| 450 | EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
| 451 | EXPECT_EQ("private", header); |
| 452 | |
| 453 | header.clear(); |
| 454 | EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
| 455 | EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
| 456 | |
| 457 | // The response has two "X-Multiple-Entries" headers. |
| 458 | // This verfies our output has them concatenated together. |
| 459 | header.clear(); |
| 460 | EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
| 461 | EXPECT_EQ("a, b", header); |
| 462 | } |
| 463 | |
| 464 | TEST(URLRequestTest, BZip2ContentTest) { |
| 465 | TestServer server(L"net/data/filter_unittests"); |
| 466 | |
| 467 | // for localhost domain, we also should support bzip2 encoding |
| 468 | // first, get the original file |
| 469 | TestDelegate d1; |
| 470 | TestURLRequest req1(server.TestServerPage("realfiles/google.txt"), &d1); |
| 471 | req1.Start(); |
| 472 | MessageLoop::current()->Run(); |
| 473 | |
| 474 | const std::string& got_content = d1.data_received(); |
| 475 | |
| 476 | // second, get bzip2 content |
| 477 | TestDelegate d2; |
| 478 | TestURLRequest req2(server.TestServerPage("realbz2files/google.txt"), &d2); |
| 479 | req2.Start(); |
| 480 | MessageLoop::current()->Run(); |
| 481 | |
| 482 | const std::string& got_bz2_content = d2.data_received(); |
| 483 | |
| 484 | // compare those two results |
[email protected] | 24cfabd | 2008-08-15 00:05:39 | [diff] [blame] | 485 | EXPECT_EQ(got_content, got_bz2_content); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | TEST(URLRequestTest, BZip2ContentTest_IncrementalHeader) { |
| 489 | TestServer server(L"net/data/filter_unittests"); |
| 490 | |
| 491 | // for localhost domain, we also should support bzip2 encoding |
| 492 | // first, get the original file |
| 493 | TestDelegate d1; |
| 494 | TestURLRequest req1(server.TestServerPage("realfiles/google.txt"), &d1); |
| 495 | req1.Start(); |
| 496 | MessageLoop::current()->Run(); |
| 497 | |
| 498 | const std::string& got_content = d1.data_received(); |
| 499 | |
| 500 | // second, get bzip2 content. ask the testserver to send the BZ2 header in |
| 501 | // two chunks with a delay between them. this tests our fix for bug 867161. |
| 502 | TestDelegate d2; |
| 503 | TestURLRequest req2(server.TestServerPage("realbz2files/google.txt?incremental-header"), &d2); |
| 504 | req2.Start(); |
| 505 | MessageLoop::current()->Run(); |
| 506 | |
| 507 | const std::string& got_bz2_content = d2.data_received(); |
| 508 | |
| 509 | // compare those two results |
[email protected] | 24cfabd | 2008-08-15 00:05:39 | [diff] [blame] | 510 | EXPECT_EQ(got_content, got_bz2_content); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | TEST(URLRequestTest, ResolveShortcutTest) { |
| 514 | std::wstring app_path; |
| 515 | PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 516 | file_util::AppendToPath(&app_path, L"net"); |
| 517 | file_util::AppendToPath(&app_path, L"data"); |
| 518 | file_util::AppendToPath(&app_path, L"url_request_unittest"); |
| 519 | file_util::AppendToPath(&app_path, L"with-headers.html"); |
| 520 | |
| 521 | std::wstring lnk_path = app_path + L".lnk"; |
| 522 | |
| 523 | HRESULT result; |
| 524 | IShellLink *shell = NULL; |
| 525 | IPersistFile *persist = NULL; |
| 526 | |
| 527 | CoInitialize(NULL); |
| 528 | // Temporarily create a shortcut for test |
| 529 | result = CoCreateInstance(CLSID_ShellLink, NULL, |
| 530 | CLSCTX_INPROC_SERVER, IID_IShellLink, |
| 531 | reinterpret_cast<LPVOID*>(&shell)); |
| 532 | EXPECT_TRUE(SUCCEEDED(result)); |
| 533 | result = shell->QueryInterface(IID_IPersistFile, |
| 534 | reinterpret_cast<LPVOID*>(&persist)); |
| 535 | EXPECT_TRUE(SUCCEEDED(result)); |
| 536 | result = shell->SetPath(app_path.c_str()); |
| 537 | EXPECT_TRUE(SUCCEEDED(result)); |
| 538 | result = shell->SetDescription(L"ResolveShortcutTest"); |
| 539 | EXPECT_TRUE(SUCCEEDED(result)); |
| 540 | result = persist->Save(lnk_path.c_str(), TRUE); |
| 541 | EXPECT_TRUE(SUCCEEDED(result)); |
| 542 | if (persist) |
| 543 | persist->Release(); |
| 544 | if (shell) |
| 545 | shell->Release(); |
| 546 | |
| 547 | TestDelegate d; |
| 548 | { |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 549 | TestURLRequest r(net::FilePathToFileURL(lnk_path), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 550 | |
| 551 | r.Start(); |
| 552 | EXPECT_TRUE(r.is_pending()); |
| 553 | |
| 554 | MessageLoop::current()->Run(); |
| 555 | |
| 556 | WIN32_FILE_ATTRIBUTE_DATA data; |
| 557 | GetFileAttributesEx(app_path.c_str(), GetFileExInfoStandard, &data); |
| 558 | HANDLE file = CreateFile(app_path.c_str(), GENERIC_READ, |
| 559 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 560 | FILE_ATTRIBUTE_NORMAL, NULL); |
| 561 | EXPECT_NE(INVALID_HANDLE_VALUE, file); |
| 562 | scoped_array<char> buffer(new char[data.nFileSizeLow]); |
| 563 | DWORD read_size; |
| 564 | BOOL result; |
| 565 | result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 566 | &read_size, NULL); |
| 567 | std::string content(buffer.get(), read_size); |
| 568 | CloseHandle(file); |
| 569 | |
| 570 | EXPECT_TRUE(!r.is_pending()); |
| 571 | EXPECT_EQ(1, d.received_redirect_count()); |
| 572 | EXPECT_EQ(content, d.data_received()); |
| 573 | } |
| 574 | |
| 575 | // Clean the shortcut |
| 576 | DeleteFile(lnk_path.c_str()); |
| 577 | CoUninitialize(); |
| 578 | |
| 579 | #ifndef NDEBUG |
| 580 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 581 | #endif |
| 582 | } |
| 583 | |
| 584 | TEST(URLRequestTest, ContentTypeNormalizationTest) { |
| 585 | TestServer server(L"net/data/url_request_unittest"); |
| 586 | TestDelegate d; |
| 587 | TestURLRequest req(server.TestServerPage( |
| 588 | "files/content-type-normalization.html"), &d); |
| 589 | req.Start(); |
| 590 | MessageLoop::current()->Run(); |
| 591 | |
| 592 | std::string mime_type; |
| 593 | req.GetMimeType(&mime_type); |
| 594 | EXPECT_EQ("text/html", mime_type); |
| 595 | |
| 596 | std::string charset; |
| 597 | req.GetCharset(&charset); |
| 598 | EXPECT_EQ("utf-8", charset); |
| 599 | req.Cancel(); |
| 600 | } |
| 601 | |
| 602 | TEST(URLRequestTest, FileDirCancelTest) { |
| 603 | // Put in mock resource provider. |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 604 | net::NetModule::SetResourceProvider(TestNetResourceProvider); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 605 | |
| 606 | TestDelegate d; |
| 607 | { |
| 608 | std::wstring file_path; |
| 609 | PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
| 610 | file_util::AppendToPath(&file_path, L"net"); |
| 611 | file_util::AppendToPath(&file_path, L"data"); |
| 612 | file_util::AppendToPath(&file_path, L""); |
| 613 | |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 614 | TestURLRequest req(net::FilePathToFileURL(file_path), &d); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 615 | req.Start(); |
| 616 | EXPECT_TRUE(req.is_pending()); |
| 617 | |
| 618 | d.set_cancel_in_received_data_pending(true); |
| 619 | |
| 620 | MessageLoop::current()->Run(); |
| 621 | } |
| 622 | #ifndef NDEBUG |
| 623 | DCHECK_EQ(url_request_metrics.object_count,0); |
| 624 | #endif |
| 625 | |
| 626 | // Take out mock resource provider. |
[email protected] | 8ac1a75 | 2008-07-31 19:40:37 | [diff] [blame] | 627 | net::NetModule::SetResourceProvider(NULL); |
initial.commit | 586acc5fe | 2008-07-26 22:42:52 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | TEST(URLRequestTest, RestrictRedirects) { |
| 631 | TestServer server(L"net/data/url_request_unittest"); |
| 632 | TestDelegate d; |
| 633 | TestURLRequest req(server.TestServerPage( |
| 634 | "files/redirect-to-file.html"), &d); |
| 635 | req.Start(); |
| 636 | MessageLoop::current()->Run(); |
| 637 | |
| 638 | EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
| 639 | EXPECT_EQ(net::ERR_UNSAFE_REDIRECT, req.status().os_error()); |
| 640 | } |
| 641 | |
| 642 | TEST(URLRequestTest, NoUserPassInReferrer) { |
| 643 | TestServer server(L"net/data/url_request_unittest"); |
| 644 | TestDelegate d; |
| 645 | TestURLRequest req(server.TestServerPage( |
| 646 | "echoheader?Referer"), &d); |
| 647 | req.set_referrer("http://user:[email protected]/"); |
| 648 | req.Start(); |
| 649 | MessageLoop::current()->Run(); |
| 650 | |
| 651 | EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
| 652 | } |
| 653 | |
| 654 | TEST(URLRequestTest, CancelRedirect) { |
| 655 | TestServer server(L"net/data/url_request_unittest"); |
| 656 | TestDelegate d; |
| 657 | { |
| 658 | d.set_cancel_in_received_redirect(true); |
| 659 | TestURLRequest req(server.TestServerPage( |
| 660 | "files/redirect-test.html"), &d); |
| 661 | req.Start(); |
| 662 | MessageLoop::current()->Run(); |
| 663 | |
| 664 | EXPECT_EQ(1, d.response_started_count()); |
| 665 | EXPECT_EQ(0, d.bytes_received()); |
| 666 | EXPECT_FALSE(d.received_data_before_response()); |
| 667 | EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | TEST(URLRequestTest, VaryHeader) { |
| 672 | TestServer server(L"net/data/url_request_unittest"); |
| 673 | |
| 674 | scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
| 675 | |
| 676 | Time response_time; |
| 677 | |
| 678 | // populate the cache |
| 679 | { |
| 680 | TestDelegate d; |
| 681 | URLRequest req(server.TestServerPage("echoheader?foo"), &d); |
| 682 | req.set_context(context); |
| 683 | req.SetExtraRequestHeaders("foo:1"); |
| 684 | req.Start(); |
| 685 | MessageLoop::current()->Run(); |
| 686 | |
| 687 | response_time = req.response_time(); |
| 688 | } |
| 689 | |
| 690 | // Make sure that the response time of a future response will be in the |
| 691 | // future! |
| 692 | Sleep(10); |
| 693 | |
| 694 | // expect a cache hit |
| 695 | { |
| 696 | TestDelegate d; |
| 697 | URLRequest req(server.TestServerPage("echoheader?foo"), &d); |
| 698 | req.set_context(context); |
| 699 | req.SetExtraRequestHeaders("foo:1"); |
| 700 | req.Start(); |
| 701 | MessageLoop::current()->Run(); |
| 702 | |
| 703 | EXPECT_TRUE(req.response_time() == response_time); |
| 704 | } |
| 705 | |
| 706 | // expect a cache miss |
| 707 | { |
| 708 | TestDelegate d; |
| 709 | URLRequest req(server.TestServerPage("echoheader?foo"), &d); |
| 710 | req.set_context(context); |
| 711 | req.SetExtraRequestHeaders("foo:2"); |
| 712 | req.Start(); |
| 713 | MessageLoop::current()->Run(); |
| 714 | |
| 715 | EXPECT_FALSE(req.response_time() == response_time); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | TEST(URLRequestTest, BasicAuth) { |
| 720 | scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
| 721 | TestServer server(L""); |
| 722 | |
| 723 | Time response_time; |
| 724 | |
| 725 | // populate the cache |
| 726 | { |
| 727 | TestDelegate d; |
| 728 | d.set_username(L"user"); |
| 729 | d.set_password(L"secret"); |
| 730 | |
| 731 | URLRequest r(server.TestServerPage("auth-basic"), &d); |
| 732 | r.set_context(context); |
| 733 | r.Start(); |
| 734 | |
| 735 | MessageLoop::current()->Run(); |
| 736 | |
| 737 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 738 | |
| 739 | response_time = r.response_time(); |
| 740 | } |
| 741 | |
| 742 | // Let some time pass so we can ensure that a future response will have a |
| 743 | // response time value in the future. |
| 744 | Sleep(10 /* milliseconds */); |
| 745 | |
| 746 | // repeat request with end-to-end validation. since auth-basic results in a |
| 747 | // cachable page, we expect this test to result in a 304. in which case, the |
| 748 | // response should be fetched from the cache. |
| 749 | { |
| 750 | TestDelegate d; |
| 751 | d.set_username(L"user"); |
| 752 | d.set_password(L"secret"); |
| 753 | |
| 754 | URLRequest r(server.TestServerPage("auth-basic"), &d); |
| 755 | r.set_context(context); |
| 756 | r.set_load_flags(net::LOAD_VALIDATE_CACHE); |
| 757 | r.Start(); |
| 758 | |
| 759 | MessageLoop::current()->Run(); |
| 760 | |
| 761 | EXPECT_TRUE(d.data_received().find("user/secret") != std::string::npos); |
| 762 | |
| 763 | // Should be the same cached document, which means that the response time |
| 764 | // should not have changed. |
| 765 | EXPECT_TRUE(response_time == r.response_time()); |
| 766 | } |
| 767 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame^] | 768 | |