OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/url_request_unittest.h" | 5 #include "net/url_request/url_request_unittest.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <shlobj.h> | 9 #include <shlobj.h> |
10 #endif | 10 #endif |
11 | 11 |
12 #include <algorithm> | 12 #include <algorithm> |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/platform_test.h" |
17 #include "base/process_util.h" | 18 #include "base/process_util.h" |
18 #include "base/string_util.h" | 19 #include "base/string_util.h" |
19 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
20 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
21 #include "net/base/net_module.h" | 22 #include "net/base/net_module.h" |
22 #include "net/base/net_util.h" | 23 #include "net/base/net_util.h" |
23 #include "net/disk_cache/disk_cache.h" | 24 #include "net/disk_cache/disk_cache.h" |
24 #include "net/http/http_cache.h" | 25 #include "net/http/http_cache.h" |
25 #include "net/http/http_network_layer.h" | 26 #include "net/http/http_network_layer.h" |
26 #include "net/url_request/url_request.h" | 27 #include "net/url_request/url_request.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
28 | 29 |
29 using base::Time; | 30 using base::Time; |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
33 class URLRequestTest : public testing::Test { | |
34 }; | |
35 | |
36 class URLRequestHttpCacheContext : public URLRequestContext { | 34 class URLRequestHttpCacheContext : public URLRequestContext { |
37 public: | 35 public: |
38 URLRequestHttpCacheContext() { | 36 URLRequestHttpCacheContext() { |
39 http_transaction_factory_ = | 37 http_transaction_factory_ = |
40 new net::HttpCache(net::HttpNetworkLayer::CreateFactory(NULL), | 38 new net::HttpCache(net::HttpNetworkLayer::CreateFactory(NULL), |
41 disk_cache::CreateInMemoryCacheBackend(0)); | 39 disk_cache::CreateInMemoryCacheBackend(0)); |
42 } | 40 } |
43 | 41 |
44 virtual ~URLRequestHttpCacheContext() { | 42 virtual ~URLRequestHttpCacheContext() { |
45 delete http_transaction_factory_; | 43 delete http_transaction_factory_; |
(...skipping 18 matching lines...) Expand all Loading... |
64 std::search(haystack.begin(), | 62 std::search(haystack.begin(), |
65 haystack.end(), | 63 haystack.end(), |
66 needle, | 64 needle, |
67 needle + strlen(needle), | 65 needle + strlen(needle), |
68 CaseInsensitiveCompare<char>()); | 66 CaseInsensitiveCompare<char>()); |
69 return it != haystack.end(); | 67 return it != haystack.end(); |
70 } | 68 } |
71 | 69 |
72 } // namespace | 70 } // namespace |
73 | 71 |
74 TEST(URLRequestTest, GetTest_NoCache) { | 72 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f |
| 73 class URLRequestTest : public PlatformTest { |
| 74 }; |
| 75 |
| 76 TEST_F(URLRequestTest, GetTest_NoCache) { |
75 TestServer server(L""); | 77 TestServer server(L""); |
76 TestDelegate d; | 78 TestDelegate d; |
77 { | 79 { |
78 TestURLRequest r(server.TestServerPage(""), &d); | 80 TestURLRequest r(server.TestServerPage(""), &d); |
79 | 81 |
80 r.Start(); | 82 r.Start(); |
81 EXPECT_TRUE(r.is_pending()); | 83 EXPECT_TRUE(r.is_pending()); |
82 | 84 |
83 MessageLoop::current()->Run(); | 85 MessageLoop::current()->Run(); |
84 | 86 |
85 EXPECT_EQ(1, d.response_started_count()); | 87 EXPECT_EQ(1, d.response_started_count()); |
86 EXPECT_FALSE(d.received_data_before_response()); | 88 EXPECT_FALSE(d.received_data_before_response()); |
87 EXPECT_NE(0, d.bytes_received()); | 89 EXPECT_NE(0, d.bytes_received()); |
88 } | 90 } |
89 #ifndef NDEBUG | 91 #ifndef NDEBUG |
90 DCHECK_EQ(url_request_metrics.object_count,0); | 92 DCHECK_EQ(url_request_metrics.object_count,0); |
91 #endif | 93 #endif |
92 } | 94 } |
93 | 95 |
94 TEST(URLRequestTest, GetTest) { | 96 TEST_F(URLRequestTest, GetTest) { |
95 TestServer server(L""); | 97 TestServer server(L""); |
96 TestDelegate d; | 98 TestDelegate d; |
97 { | 99 { |
98 TestURLRequest r(server.TestServerPage(""), &d); | 100 TestURLRequest r(server.TestServerPage(""), &d); |
99 | 101 |
100 r.Start(); | 102 r.Start(); |
101 EXPECT_TRUE(r.is_pending()); | 103 EXPECT_TRUE(r.is_pending()); |
102 | 104 |
103 MessageLoop::current()->Run(); | 105 MessageLoop::current()->Run(); |
104 | 106 |
105 EXPECT_EQ(1, d.response_started_count()); | 107 EXPECT_EQ(1, d.response_started_count()); |
106 EXPECT_FALSE(d.received_data_before_response()); | 108 EXPECT_FALSE(d.received_data_before_response()); |
107 EXPECT_NE(0, d.bytes_received()); | 109 EXPECT_NE(0, d.bytes_received()); |
108 } | 110 } |
109 #ifndef NDEBUG | 111 #ifndef NDEBUG |
110 DCHECK_EQ(url_request_metrics.object_count,0); | 112 DCHECK_EQ(url_request_metrics.object_count,0); |
111 #endif | 113 #endif |
112 } | 114 } |
113 | 115 |
114 TEST(URLRequestTest, CancelTest) { | 116 TEST_F(URLRequestTest, CancelTest) { |
115 TestDelegate d; | 117 TestDelegate d; |
116 { | 118 { |
117 TestURLRequest r(GURL("http://www.google.com/"), &d); | 119 TestURLRequest r(GURL("http://www.google.com/"), &d); |
118 | 120 |
119 r.Start(); | 121 r.Start(); |
120 EXPECT_TRUE(r.is_pending()); | 122 EXPECT_TRUE(r.is_pending()); |
121 | 123 |
122 r.Cancel(); | 124 r.Cancel(); |
123 | 125 |
124 MessageLoop::current()->Run(); | 126 MessageLoop::current()->Run(); |
125 | 127 |
126 // We expect to receive OnResponseStarted even though the request has been | 128 // We expect to receive OnResponseStarted even though the request has been |
127 // cancelled. | 129 // cancelled. |
128 EXPECT_EQ(1, d.response_started_count()); | 130 EXPECT_EQ(1, d.response_started_count()); |
129 EXPECT_EQ(0, d.bytes_received()); | 131 EXPECT_EQ(0, d.bytes_received()); |
130 EXPECT_FALSE(d.received_data_before_response()); | 132 EXPECT_FALSE(d.received_data_before_response()); |
131 } | 133 } |
132 #ifndef NDEBUG | 134 #ifndef NDEBUG |
133 DCHECK_EQ(url_request_metrics.object_count,0); | 135 DCHECK_EQ(url_request_metrics.object_count,0); |
134 #endif | 136 #endif |
135 } | 137 } |
136 | 138 |
137 TEST(URLRequestTest, CancelTest2) { | 139 TEST_F(URLRequestTest, CancelTest2) { |
138 TestServer server(L""); | 140 TestServer server(L""); |
139 TestDelegate d; | 141 TestDelegate d; |
140 { | 142 { |
141 TestURLRequest r(server.TestServerPage(""), &d); | 143 TestURLRequest r(server.TestServerPage(""), &d); |
142 | 144 |
143 d.set_cancel_in_response_started(true); | 145 d.set_cancel_in_response_started(true); |
144 | 146 |
145 r.Start(); | 147 r.Start(); |
146 EXPECT_TRUE(r.is_pending()); | 148 EXPECT_TRUE(r.is_pending()); |
147 | 149 |
148 MessageLoop::current()->Run(); | 150 MessageLoop::current()->Run(); |
149 | 151 |
150 EXPECT_EQ(1, d.response_started_count()); | 152 EXPECT_EQ(1, d.response_started_count()); |
151 EXPECT_EQ(0, d.bytes_received()); | 153 EXPECT_EQ(0, d.bytes_received()); |
152 EXPECT_FALSE(d.received_data_before_response()); | 154 EXPECT_FALSE(d.received_data_before_response()); |
153 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); | 155 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
154 } | 156 } |
155 #ifndef NDEBUG | 157 #ifndef NDEBUG |
156 DCHECK_EQ(url_request_metrics.object_count,0); | 158 DCHECK_EQ(url_request_metrics.object_count,0); |
157 #endif | 159 #endif |
158 } | 160 } |
159 | 161 |
160 TEST(URLRequestTest, CancelTest3) { | 162 TEST_F(URLRequestTest, CancelTest3) { |
161 TestServer server(L""); | 163 TestServer server(L""); |
162 TestDelegate d; | 164 TestDelegate d; |
163 { | 165 { |
164 TestURLRequest r(server.TestServerPage(""), &d); | 166 TestURLRequest r(server.TestServerPage(""), &d); |
165 | 167 |
166 d.set_cancel_in_received_data(true); | 168 d.set_cancel_in_received_data(true); |
167 | 169 |
168 r.Start(); | 170 r.Start(); |
169 EXPECT_TRUE(r.is_pending()); | 171 EXPECT_TRUE(r.is_pending()); |
170 | 172 |
171 MessageLoop::current()->Run(); | 173 MessageLoop::current()->Run(); |
172 | 174 |
173 EXPECT_EQ(1, d.response_started_count()); | 175 EXPECT_EQ(1, d.response_started_count()); |
174 // There is no guarantee about how much data was received | 176 // There is no guarantee about how much data was received |
175 // before the cancel was issued. It could have been 0 bytes, | 177 // before the cancel was issued. It could have been 0 bytes, |
176 // or it could have been all the bytes. | 178 // or it could have been all the bytes. |
177 // EXPECT_EQ(0, d.bytes_received()); | 179 // EXPECT_EQ(0, d.bytes_received()); |
178 EXPECT_FALSE(d.received_data_before_response()); | 180 EXPECT_FALSE(d.received_data_before_response()); |
179 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); | 181 EXPECT_EQ(URLRequestStatus::CANCELED, r.status().status()); |
180 } | 182 } |
181 #ifndef NDEBUG | 183 #ifndef NDEBUG |
182 DCHECK_EQ(url_request_metrics.object_count,0); | 184 DCHECK_EQ(url_request_metrics.object_count,0); |
183 #endif | 185 #endif |
184 } | 186 } |
185 | 187 |
186 TEST(URLRequestTest, CancelTest4) { | 188 TEST_F(URLRequestTest, CancelTest4) { |
187 TestServer server(L""); | 189 TestServer server(L""); |
188 TestDelegate d; | 190 TestDelegate d; |
189 { | 191 { |
190 TestURLRequest r(server.TestServerPage(""), &d); | 192 TestURLRequest r(server.TestServerPage(""), &d); |
191 | 193 |
192 r.Start(); | 194 r.Start(); |
193 EXPECT_TRUE(r.is_pending()); | 195 EXPECT_TRUE(r.is_pending()); |
194 | 196 |
195 // The request will be implicitly canceled when it is destroyed. The | 197 // The request will be implicitly canceled when it is destroyed. The |
196 // test delegate must not post a quit message when this happens because | 198 // test delegate must not post a quit message when this happens because |
197 // this test doesn't actually have a message loop. The quit message would | 199 // this test doesn't actually have a message loop. The quit message would |
198 // get put on this thread's message queue and the next test would exit | 200 // get put on this thread's message queue and the next test would exit |
199 // early, causing problems. | 201 // early, causing problems. |
200 d.set_quit_on_complete(false); | 202 d.set_quit_on_complete(false); |
201 } | 203 } |
202 // expect things to just cleanup properly. | 204 // expect things to just cleanup properly. |
203 | 205 |
204 // we won't actually get a received reponse here because we've never run the | 206 // we won't actually get a received reponse here because we've never run the |
205 // message loop | 207 // message loop |
206 EXPECT_FALSE(d.received_data_before_response()); | 208 EXPECT_FALSE(d.received_data_before_response()); |
207 EXPECT_EQ(0, d.bytes_received()); | 209 EXPECT_EQ(0, d.bytes_received()); |
208 } | 210 } |
209 | 211 |
210 TEST(URLRequestTest, CancelTest5) { | 212 TEST_F(URLRequestTest, CancelTest5) { |
211 TestServer server(L""); | 213 TestServer server(L""); |
212 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); | 214 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
213 | 215 |
214 // populate cache | 216 // populate cache |
215 { | 217 { |
216 TestDelegate d; | 218 TestDelegate d; |
217 URLRequest r(server.TestServerPage("cachetime"), &d); | 219 URLRequest r(server.TestServerPage("cachetime"), &d); |
218 r.set_context(context); | 220 r.set_context(context); |
219 r.Start(); | 221 r.Start(); |
220 MessageLoop::current()->Run(); | 222 MessageLoop::current()->Run(); |
(...skipping 13 matching lines...) Expand all Loading... |
234 EXPECT_EQ(1, d.response_started_count()); | 236 EXPECT_EQ(1, d.response_started_count()); |
235 EXPECT_EQ(0, d.bytes_received()); | 237 EXPECT_EQ(0, d.bytes_received()); |
236 EXPECT_FALSE(d.received_data_before_response()); | 238 EXPECT_FALSE(d.received_data_before_response()); |
237 } | 239 } |
238 | 240 |
239 #ifndef NDEBUG | 241 #ifndef NDEBUG |
240 DCHECK_EQ(url_request_metrics.object_count, 0); | 242 DCHECK_EQ(url_request_metrics.object_count, 0); |
241 #endif | 243 #endif |
242 } | 244 } |
243 | 245 |
244 TEST(URLRequestTest, PostTest) { | 246 TEST_F(URLRequestTest, PostTest) { |
245 TestServer server(L"net/data"); | 247 TestServer server(L"net/data"); |
246 | 248 |
247 const int kMsgSize = 20000; // multiple of 10 | 249 const int kMsgSize = 20000; // multiple of 10 |
248 const int kIterations = 50; | 250 const int kIterations = 50; |
249 char *uploadBytes = new char[kMsgSize+1]; | 251 char *uploadBytes = new char[kMsgSize+1]; |
250 char *ptr = uploadBytes; | 252 char *ptr = uploadBytes; |
251 char marker = 'a'; | 253 char marker = 'a'; |
252 for(int idx=0; idx<kMsgSize/10; idx++) { | 254 for(int idx=0; idx<kMsgSize/10; idx++) { |
253 memcpy(ptr, "----------", 10); | 255 memcpy(ptr, "----------", 10); |
254 ptr += 10; | 256 ptr += 10; |
(...skipping 30 matching lines...) Expand all Loading... |
285 EXPECT_EQ(uploadBytes, d.data_received()); | 287 EXPECT_EQ(uploadBytes, d.data_received()); |
286 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize),0); | 288 EXPECT_EQ(memcmp(uploadBytes, d.data_received().c_str(), kMsgSize),0); |
287 EXPECT_EQ(d.data_received().compare(uploadBytes), 0); | 289 EXPECT_EQ(d.data_received().compare(uploadBytes), 0); |
288 } | 290 } |
289 delete[] uploadBytes; | 291 delete[] uploadBytes; |
290 #ifndef NDEBUG | 292 #ifndef NDEBUG |
291 DCHECK_EQ(url_request_metrics.object_count,0); | 293 DCHECK_EQ(url_request_metrics.object_count,0); |
292 #endif | 294 #endif |
293 } | 295 } |
294 | 296 |
295 TEST(URLRequestTest, PostEmptyTest) { | 297 TEST_F(URLRequestTest, PostEmptyTest) { |
296 TestServer server(L"net/data"); | 298 TestServer server(L"net/data"); |
297 TestDelegate d; | 299 TestDelegate d; |
298 { | 300 { |
299 TestURLRequest r(server.TestServerPage("echo"), &d); | 301 TestURLRequest r(server.TestServerPage("echo"), &d); |
300 r.set_method("POST"); | 302 r.set_method("POST"); |
301 | 303 |
302 r.Start(); | 304 r.Start(); |
303 EXPECT_TRUE(r.is_pending()); | 305 EXPECT_TRUE(r.is_pending()); |
304 | 306 |
305 MessageLoop::current()->Run(); | 307 MessageLoop::current()->Run(); |
306 | 308 |
307 ASSERT_EQ(1, d.response_started_count()) << "request failed: " << | 309 ASSERT_EQ(1, d.response_started_count()) << "request failed: " << |
308 (int) r.status().status() << ", os error: " << r.status().os_error(); | 310 (int) r.status().status() << ", os error: " << r.status().os_error(); |
309 | 311 |
310 EXPECT_FALSE(d.received_data_before_response()); | 312 EXPECT_FALSE(d.received_data_before_response()); |
311 EXPECT_TRUE(d.data_received().empty()); | 313 EXPECT_TRUE(d.data_received().empty()); |
312 } | 314 } |
313 #ifndef NDEBUG | 315 #ifndef NDEBUG |
314 DCHECK_EQ(url_request_metrics.object_count,0); | 316 DCHECK_EQ(url_request_metrics.object_count,0); |
315 #endif | 317 #endif |
316 } | 318 } |
317 | 319 |
318 TEST(URLRequestTest, PostFileTest) { | 320 TEST_F(URLRequestTest, PostFileTest) { |
319 TestServer server(L"net/data"); | 321 TestServer server(L"net/data"); |
320 TestDelegate d; | 322 TestDelegate d; |
321 { | 323 { |
322 TestURLRequest r(server.TestServerPage("echo"), &d); | 324 TestURLRequest r(server.TestServerPage("echo"), &d); |
323 r.set_method("POST"); | 325 r.set_method("POST"); |
324 | 326 |
325 std::wstring dir; | 327 std::wstring dir; |
326 PathService::Get(base::DIR_EXE, &dir); | 328 PathService::Get(base::DIR_EXE, &dir); |
327 file_util::SetCurrentDirectory(dir); | 329 file_util::SetCurrentDirectory(dir); |
328 | 330 |
(...skipping 27 matching lines...) Expand all Loading... |
356 EXPECT_FALSE(d.received_data_before_response()); | 358 EXPECT_FALSE(d.received_data_before_response()); |
357 | 359 |
358 ASSERT_EQ(size, d.bytes_received()); | 360 ASSERT_EQ(size, d.bytes_received()); |
359 EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size)); | 361 EXPECT_EQ(0, memcmp(d.data_received().c_str(), buf.get(), size)); |
360 } | 362 } |
361 #ifndef NDEBUG | 363 #ifndef NDEBUG |
362 DCHECK_EQ(url_request_metrics.object_count,0); | 364 DCHECK_EQ(url_request_metrics.object_count,0); |
363 #endif | 365 #endif |
364 } | 366 } |
365 | 367 |
366 TEST(URLRequestTest, AboutBlankTest) { | 368 TEST_F(URLRequestTest, AboutBlankTest) { |
367 TestDelegate d; | 369 TestDelegate d; |
368 { | 370 { |
369 TestURLRequest r(GURL("about:blank"), &d); | 371 TestURLRequest r(GURL("about:blank"), &d); |
370 | 372 |
371 r.Start(); | 373 r.Start(); |
372 EXPECT_TRUE(r.is_pending()); | 374 EXPECT_TRUE(r.is_pending()); |
373 | 375 |
374 MessageLoop::current()->Run(); | 376 MessageLoop::current()->Run(); |
375 | 377 |
376 EXPECT_TRUE(!r.is_pending()); | 378 EXPECT_TRUE(!r.is_pending()); |
377 EXPECT_FALSE(d.received_data_before_response()); | 379 EXPECT_FALSE(d.received_data_before_response()); |
378 EXPECT_EQ(d.bytes_received(), 0); | 380 EXPECT_EQ(d.bytes_received(), 0); |
379 } | 381 } |
380 #ifndef NDEBUG | 382 #ifndef NDEBUG |
381 DCHECK_EQ(url_request_metrics.object_count,0); | 383 DCHECK_EQ(url_request_metrics.object_count,0); |
382 #endif | 384 #endif |
383 } | 385 } |
384 | 386 |
385 TEST(URLRequestTest, FileTest) { | 387 TEST_F(URLRequestTest, FileTest) { |
386 std::wstring app_path; | 388 std::wstring app_path; |
387 PathService::Get(base::FILE_EXE, &app_path); | 389 PathService::Get(base::FILE_EXE, &app_path); |
388 | 390 |
389 std::string app_url = WideToUTF8(app_path); | 391 std::string app_url = WideToUTF8(app_path); |
390 std::replace(app_url.begin(), app_url.end(), | 392 std::replace(app_url.begin(), app_url.end(), |
391 file_util::kPathSeparator, L'/'); | 393 file_util::kPathSeparator, L'/'); |
392 app_url.insert(0, "file:///"); | 394 app_url.insert(0, "file:///"); |
393 | 395 |
394 TestDelegate d; | 396 TestDelegate d; |
395 { | 397 { |
(...skipping 10 matching lines...) Expand all Loading... |
406 EXPECT_TRUE(!r.is_pending()); | 408 EXPECT_TRUE(!r.is_pending()); |
407 EXPECT_EQ(1, d.response_started_count()); | 409 EXPECT_EQ(1, d.response_started_count()); |
408 EXPECT_FALSE(d.received_data_before_response()); | 410 EXPECT_FALSE(d.received_data_before_response()); |
409 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); | 411 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
410 } | 412 } |
411 #ifndef NDEBUG | 413 #ifndef NDEBUG |
412 DCHECK_EQ(url_request_metrics.object_count,0); | 414 DCHECK_EQ(url_request_metrics.object_count,0); |
413 #endif | 415 #endif |
414 } | 416 } |
415 | 417 |
416 TEST(URLRequestTest, InvalidUrlTest) { | 418 TEST_F(URLRequestTest, InvalidUrlTest) { |
417 TestDelegate d; | 419 TestDelegate d; |
418 { | 420 { |
419 TestURLRequest r(GURL("invalid url"), &d); | 421 TestURLRequest r(GURL("invalid url"), &d); |
420 | 422 |
421 r.Start(); | 423 r.Start(); |
422 EXPECT_TRUE(r.is_pending()); | 424 EXPECT_TRUE(r.is_pending()); |
423 | 425 |
424 MessageLoop::current()->Run(); | 426 MessageLoop::current()->Run(); |
425 EXPECT_TRUE(d.request_failed()); | 427 EXPECT_TRUE(d.request_failed()); |
426 } | 428 } |
427 #ifndef NDEBUG | 429 #ifndef NDEBUG |
428 DCHECK_EQ(url_request_metrics.object_count,0); | 430 DCHECK_EQ(url_request_metrics.object_count,0); |
429 #endif | 431 #endif |
430 } | 432 } |
431 | 433 |
432 /* This test is disabled because it fails on some computers due to proxies | 434 /* This test is disabled because it fails on some computers due to proxies |
433 returning a page in response to this request rather than reporting failure. | 435 returning a page in response to this request rather than reporting failure. |
434 TEST(URLRequestTest, DnsFailureTest) { | 436 TEST_F(URLRequestTest, DnsFailureTest) { |
435 TestDelegate d; | 437 TestDelegate d; |
436 { | 438 { |
437 URLRequest r(GURL("http://thisisnotavalidurl0123456789foo.com/"), &d); | 439 URLRequest r(GURL("http://thisisnotavalidurl0123456789foo.com/"), &d); |
438 | 440 |
439 r.Start(); | 441 r.Start(); |
440 EXPECT_TRUE(r.is_pending()); | 442 EXPECT_TRUE(r.is_pending()); |
441 | 443 |
442 MessageLoop::current()->Run(); | 444 MessageLoop::current()->Run(); |
443 EXPECT_TRUE(d.request_failed()); | 445 EXPECT_TRUE(d.request_failed()); |
444 } | 446 } |
445 #ifndef NDEBUG | 447 #ifndef NDEBUG |
446 DCHECK_EQ(url_request_metrics.object_count,0); | 448 DCHECK_EQ(url_request_metrics.object_count,0); |
447 #endif | 449 #endif |
448 } | 450 } |
449 */ | 451 */ |
450 | 452 |
451 TEST(URLRequestTest, ResponseHeadersTest) { | 453 TEST_F(URLRequestTest, ResponseHeadersTest) { |
452 TestServer server(L"net/data/url_request_unittest"); | 454 TestServer server(L"net/data/url_request_unittest"); |
453 TestDelegate d; | 455 TestDelegate d; |
454 TestURLRequest req(server.TestServerPage("files/with-headers.html"), &d); | 456 TestURLRequest req(server.TestServerPage("files/with-headers.html"), &d); |
455 req.Start(); | 457 req.Start(); |
456 MessageLoop::current()->Run(); | 458 MessageLoop::current()->Run(); |
457 | 459 |
458 const net::HttpResponseHeaders* headers = req.response_headers(); | 460 const net::HttpResponseHeaders* headers = req.response_headers(); |
459 std::string header; | 461 std::string header; |
460 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); | 462 EXPECT_TRUE(headers->GetNormalizedHeader("cache-control", &header)); |
461 EXPECT_EQ("private", header); | 463 EXPECT_EQ("private", header); |
462 | 464 |
463 header.clear(); | 465 header.clear(); |
464 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); | 466 EXPECT_TRUE(headers->GetNormalizedHeader("content-type", &header)); |
465 EXPECT_EQ("text/html; charset=ISO-8859-1", header); | 467 EXPECT_EQ("text/html; charset=ISO-8859-1", header); |
466 | 468 |
467 // The response has two "X-Multiple-Entries" headers. | 469 // The response has two "X-Multiple-Entries" headers. |
468 // This verfies our output has them concatenated together. | 470 // This verfies our output has them concatenated together. |
469 header.clear(); | 471 header.clear(); |
470 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); | 472 EXPECT_TRUE(headers->GetNormalizedHeader("x-multiple-entries", &header)); |
471 EXPECT_EQ("a, b", header); | 473 EXPECT_EQ("a, b", header); |
472 } | 474 } |
473 | 475 |
474 TEST(URLRequestTest, BZip2ContentTest) { | 476 TEST_F(URLRequestTest, BZip2ContentTest) { |
475 TestServer server(L"net/data/filter_unittests"); | 477 TestServer server(L"net/data/filter_unittests"); |
476 | 478 |
477 // for localhost domain, we also should support bzip2 encoding | 479 // for localhost domain, we also should support bzip2 encoding |
478 // first, get the original file | 480 // first, get the original file |
479 TestDelegate d1; | 481 TestDelegate d1; |
480 TestURLRequest req1(server.TestServerPage("realfiles/google.txt"), &d1); | 482 TestURLRequest req1(server.TestServerPage("realfiles/google.txt"), &d1); |
481 req1.Start(); | 483 req1.Start(); |
482 MessageLoop::current()->Run(); | 484 MessageLoop::current()->Run(); |
483 | 485 |
484 const std::string& got_content = d1.data_received(); | 486 const std::string& got_content = d1.data_received(); |
485 | 487 |
486 // second, get bzip2 content | 488 // second, get bzip2 content |
487 TestDelegate d2; | 489 TestDelegate d2; |
488 TestURLRequest req2(server.TestServerPage("realbz2files/google.txt"), &d2); | 490 TestURLRequest req2(server.TestServerPage("realbz2files/google.txt"), &d2); |
489 req2.Start(); | 491 req2.Start(); |
490 MessageLoop::current()->Run(); | 492 MessageLoop::current()->Run(); |
491 | 493 |
492 const std::string& got_bz2_content = d2.data_received(); | 494 const std::string& got_bz2_content = d2.data_received(); |
493 | 495 |
494 // compare those two results | 496 // compare those two results |
495 EXPECT_EQ(got_content, got_bz2_content); | 497 EXPECT_EQ(got_content, got_bz2_content); |
496 } | 498 } |
497 | 499 |
498 TEST(URLRequestTest, BZip2ContentTest_IncrementalHeader) { | 500 TEST_F(URLRequestTest, BZip2ContentTest_IncrementalHeader) { |
499 TestServer server(L"net/data/filter_unittests"); | 501 TestServer server(L"net/data/filter_unittests"); |
500 | 502 |
501 // for localhost domain, we also should support bzip2 encoding | 503 // for localhost domain, we also should support bzip2 encoding |
502 // first, get the original file | 504 // first, get the original file |
503 TestDelegate d1; | 505 TestDelegate d1; |
504 TestURLRequest req1(server.TestServerPage("realfiles/google.txt"), &d1); | 506 TestURLRequest req1(server.TestServerPage("realfiles/google.txt"), &d1); |
505 req1.Start(); | 507 req1.Start(); |
506 MessageLoop::current()->Run(); | 508 MessageLoop::current()->Run(); |
507 | 509 |
508 const std::string& got_content = d1.data_received(); | 510 const std::string& got_content = d1.data_received(); |
509 | 511 |
510 // second, get bzip2 content. ask the testserver to send the BZ2 header in | 512 // second, get bzip2 content. ask the testserver to send the BZ2 header in |
511 // two chunks with a delay between them. this tests our fix for bug 867161. | 513 // two chunks with a delay between them. this tests our fix for bug 867161. |
512 TestDelegate d2; | 514 TestDelegate d2; |
513 TestURLRequest req2(server.TestServerPage("realbz2files/google.txt?incremental
-header"), &d2); | 515 TestURLRequest req2(server.TestServerPage("realbz2files/google.txt?incremental
-header"), &d2); |
514 req2.Start(); | 516 req2.Start(); |
515 MessageLoop::current()->Run(); | 517 MessageLoop::current()->Run(); |
516 | 518 |
517 const std::string& got_bz2_content = d2.data_received(); | 519 const std::string& got_bz2_content = d2.data_received(); |
518 | 520 |
519 // compare those two results | 521 // compare those two results |
520 EXPECT_EQ(got_content, got_bz2_content); | 522 EXPECT_EQ(got_content, got_bz2_content); |
521 } | 523 } |
522 | 524 |
523 #if defined(OS_WIN) | 525 #if defined(OS_WIN) |
524 TEST(URLRequestTest, ResolveShortcutTest) { | 526 TEST_F(URLRequestTest, ResolveShortcutTest) { |
525 std::wstring app_path; | 527 std::wstring app_path; |
526 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); | 528 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
527 file_util::AppendToPath(&app_path, L"net"); | 529 file_util::AppendToPath(&app_path, L"net"); |
528 file_util::AppendToPath(&app_path, L"data"); | 530 file_util::AppendToPath(&app_path, L"data"); |
529 file_util::AppendToPath(&app_path, L"url_request_unittest"); | 531 file_util::AppendToPath(&app_path, L"url_request_unittest"); |
530 file_util::AppendToPath(&app_path, L"with-headers.html"); | 532 file_util::AppendToPath(&app_path, L"with-headers.html"); |
531 | 533 |
532 std::wstring lnk_path = app_path + L".lnk"; | 534 std::wstring lnk_path = app_path + L".lnk"; |
533 | 535 |
534 HRESULT result; | 536 HRESULT result; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
586 // Clean the shortcut | 588 // Clean the shortcut |
587 DeleteFile(lnk_path.c_str()); | 589 DeleteFile(lnk_path.c_str()); |
588 CoUninitialize(); | 590 CoUninitialize(); |
589 | 591 |
590 #ifndef NDEBUG | 592 #ifndef NDEBUG |
591 DCHECK_EQ(url_request_metrics.object_count,0); | 593 DCHECK_EQ(url_request_metrics.object_count,0); |
592 #endif | 594 #endif |
593 } | 595 } |
594 #endif // defined(OS_WIN) | 596 #endif // defined(OS_WIN) |
595 | 597 |
596 TEST(URLRequestTest, ContentTypeNormalizationTest) { | 598 TEST_F(URLRequestTest, ContentTypeNormalizationTest) { |
597 TestServer server(L"net/data/url_request_unittest"); | 599 TestServer server(L"net/data/url_request_unittest"); |
598 TestDelegate d; | 600 TestDelegate d; |
599 TestURLRequest req(server.TestServerPage( | 601 TestURLRequest req(server.TestServerPage( |
600 "files/content-type-normalization.html"), &d); | 602 "files/content-type-normalization.html"), &d); |
601 req.Start(); | 603 req.Start(); |
602 MessageLoop::current()->Run(); | 604 MessageLoop::current()->Run(); |
603 | 605 |
604 std::string mime_type; | 606 std::string mime_type; |
605 req.GetMimeType(&mime_type); | 607 req.GetMimeType(&mime_type); |
606 EXPECT_EQ("text/html", mime_type); | 608 EXPECT_EQ("text/html", mime_type); |
607 | 609 |
608 std::string charset; | 610 std::string charset; |
609 req.GetCharset(&charset); | 611 req.GetCharset(&charset); |
610 EXPECT_EQ("utf-8", charset); | 612 EXPECT_EQ("utf-8", charset); |
611 req.Cancel(); | 613 req.Cancel(); |
612 } | 614 } |
613 | 615 |
614 TEST(URLRequestTest, FileDirCancelTest) { | 616 TEST_F(URLRequestTest, FileDirCancelTest) { |
615 // Put in mock resource provider. | 617 // Put in mock resource provider. |
616 net::NetModule::SetResourceProvider(TestNetResourceProvider); | 618 net::NetModule::SetResourceProvider(TestNetResourceProvider); |
617 | 619 |
618 TestDelegate d; | 620 TestDelegate d; |
619 { | 621 { |
620 std::wstring file_path; | 622 std::wstring file_path; |
621 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); | 623 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
622 file_util::AppendToPath(&file_path, L"net"); | 624 file_util::AppendToPath(&file_path, L"net"); |
623 file_util::AppendToPath(&file_path, L"data"); | 625 file_util::AppendToPath(&file_path, L"data"); |
624 file_util::AppendToPath(&file_path, L""); | 626 file_util::AppendToPath(&file_path, L""); |
625 | 627 |
626 TestURLRequest req(net::FilePathToFileURL(file_path), &d); | 628 TestURLRequest req(net::FilePathToFileURL(file_path), &d); |
627 req.Start(); | 629 req.Start(); |
628 EXPECT_TRUE(req.is_pending()); | 630 EXPECT_TRUE(req.is_pending()); |
629 | 631 |
630 d.set_cancel_in_received_data_pending(true); | 632 d.set_cancel_in_received_data_pending(true); |
631 | 633 |
632 MessageLoop::current()->Run(); | 634 MessageLoop::current()->Run(); |
633 } | 635 } |
634 #ifndef NDEBUG | 636 #ifndef NDEBUG |
635 DCHECK_EQ(url_request_metrics.object_count,0); | 637 DCHECK_EQ(url_request_metrics.object_count,0); |
636 #endif | 638 #endif |
637 | 639 |
638 // Take out mock resource provider. | 640 // Take out mock resource provider. |
639 net::NetModule::SetResourceProvider(NULL); | 641 net::NetModule::SetResourceProvider(NULL); |
640 } | 642 } |
641 | 643 |
642 TEST(URLRequestTest, RestrictRedirects) { | 644 TEST_F(URLRequestTest, RestrictRedirects) { |
643 TestServer server(L"net/data/url_request_unittest"); | 645 TestServer server(L"net/data/url_request_unittest"); |
644 TestDelegate d; | 646 TestDelegate d; |
645 TestURLRequest req(server.TestServerPage( | 647 TestURLRequest req(server.TestServerPage( |
646 "files/redirect-to-file.html"), &d); | 648 "files/redirect-to-file.html"), &d); |
647 req.Start(); | 649 req.Start(); |
648 MessageLoop::current()->Run(); | 650 MessageLoop::current()->Run(); |
649 | 651 |
650 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); | 652 EXPECT_EQ(URLRequestStatus::FAILED, req.status().status()); |
651 EXPECT_EQ(net::ERR_UNSAFE_REDIRECT, req.status().os_error()); | 653 EXPECT_EQ(net::ERR_UNSAFE_REDIRECT, req.status().os_error()); |
652 } | 654 } |
653 | 655 |
654 TEST(URLRequestTest, NoUserPassInReferrer) { | 656 TEST_F(URLRequestTest, NoUserPassInReferrer) { |
655 TestServer server(L"net/data/url_request_unittest"); | 657 TestServer server(L"net/data/url_request_unittest"); |
656 TestDelegate d; | 658 TestDelegate d; |
657 TestURLRequest req(server.TestServerPage( | 659 TestURLRequest req(server.TestServerPage( |
658 "echoheader?Referer"), &d); | 660 "echoheader?Referer"), &d); |
659 req.set_referrer("http://user:pass@foo.com/"); | 661 req.set_referrer("http://user:pass@foo.com/"); |
660 req.Start(); | 662 req.Start(); |
661 MessageLoop::current()->Run(); | 663 MessageLoop::current()->Run(); |
662 | 664 |
663 EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); | 665 EXPECT_EQ(std::string("http://foo.com/"), d.data_received()); |
664 } | 666 } |
665 | 667 |
666 TEST(URLRequestTest, CancelRedirect) { | 668 TEST_F(URLRequestTest, CancelRedirect) { |
667 TestServer server(L"net/data/url_request_unittest"); | 669 TestServer server(L"net/data/url_request_unittest"); |
668 TestDelegate d; | 670 TestDelegate d; |
669 { | 671 { |
670 d.set_cancel_in_received_redirect(true); | 672 d.set_cancel_in_received_redirect(true); |
671 TestURLRequest req(server.TestServerPage( | 673 TestURLRequest req(server.TestServerPage( |
672 "files/redirect-test.html"), &d); | 674 "files/redirect-test.html"), &d); |
673 req.Start(); | 675 req.Start(); |
674 MessageLoop::current()->Run(); | 676 MessageLoop::current()->Run(); |
675 | 677 |
676 EXPECT_EQ(1, d.response_started_count()); | 678 EXPECT_EQ(1, d.response_started_count()); |
677 EXPECT_EQ(0, d.bytes_received()); | 679 EXPECT_EQ(0, d.bytes_received()); |
678 EXPECT_FALSE(d.received_data_before_response()); | 680 EXPECT_FALSE(d.received_data_before_response()); |
679 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); | 681 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
680 } | 682 } |
681 } | 683 } |
682 | 684 |
683 TEST(URLRequestTest, VaryHeader) { | 685 TEST_F(URLRequestTest, VaryHeader) { |
684 TestServer server(L"net/data/url_request_unittest"); | 686 TestServer server(L"net/data/url_request_unittest"); |
685 | 687 |
686 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); | 688 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
687 | 689 |
688 Time response_time; | 690 Time response_time; |
689 | 691 |
690 // populate the cache | 692 // populate the cache |
691 { | 693 { |
692 TestDelegate d; | 694 TestDelegate d; |
693 URLRequest req(server.TestServerPage("echoheader?foo"), &d); | 695 URLRequest req(server.TestServerPage("echoheader?foo"), &d); |
(...skipping 27 matching lines...) Expand all Loading... |
721 URLRequest req(server.TestServerPage("echoheader?foo"), &d); | 723 URLRequest req(server.TestServerPage("echoheader?foo"), &d); |
722 req.set_context(context); | 724 req.set_context(context); |
723 req.SetExtraRequestHeaders("foo:2"); | 725 req.SetExtraRequestHeaders("foo:2"); |
724 req.Start(); | 726 req.Start(); |
725 MessageLoop::current()->Run(); | 727 MessageLoop::current()->Run(); |
726 | 728 |
727 EXPECT_FALSE(req.response_time() == response_time); | 729 EXPECT_FALSE(req.response_time() == response_time); |
728 } | 730 } |
729 } | 731 } |
730 | 732 |
731 TEST(URLRequestTest, BasicAuth) { | 733 TEST_F(URLRequestTest, BasicAuth) { |
732 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); | 734 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
733 TestServer server(L""); | 735 TestServer server(L""); |
734 | 736 |
735 Time response_time; | 737 Time response_time; |
736 | 738 |
737 // populate the cache | 739 // populate the cache |
738 { | 740 { |
739 TestDelegate d; | 741 TestDelegate d; |
740 d.set_username(L"user"); | 742 d.set_username(L"user"); |
741 d.set_password(L"secret"); | 743 d.set_password(L"secret"); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
775 // Should be the same cached document, which means that the response time | 777 // Should be the same cached document, which means that the response time |
776 // should not have changed. | 778 // should not have changed. |
777 EXPECT_TRUE(response_time == r.response_time()); | 779 EXPECT_TRUE(response_time == r.response_time()); |
778 } | 780 } |
779 } | 781 } |
780 | 782 |
781 // In this test, we do a POST which the server will 302 redirect. | 783 // In this test, we do a POST which the server will 302 redirect. |
782 // The subsequent transaction should use GET, and should not send the | 784 // The subsequent transaction should use GET, and should not send the |
783 // Content-Type header. | 785 // Content-Type header. |
784 // http://code.google.com/p/chromium/issues/detail?id=843 | 786 // http://code.google.com/p/chromium/issues/detail?id=843 |
785 TEST(URLRequestTest, Post302RedirectGet) { | 787 TEST_F(URLRequestTest, Post302RedirectGet) { |
786 TestServer server(L"net/data/url_request_unittest"); | 788 TestServer server(L"net/data/url_request_unittest"); |
787 TestDelegate d; | 789 TestDelegate d; |
788 TestURLRequest req(server.TestServerPage("files/redirect-to-echoall"), &d); | 790 TestURLRequest req(server.TestServerPage("files/redirect-to-echoall"), &d); |
789 req.set_method("POST"); | 791 req.set_method("POST"); |
790 | 792 |
791 // Set headers (some of which are specific to the POST). | 793 // Set headers (some of which are specific to the POST). |
792 // ("Content-Length: 10" is just a junk value to make sure it gets stripped). | 794 // ("Content-Length: 10" is just a junk value to make sure it gets stripped). |
793 req.SetExtraRequestHeaders( | 795 req.SetExtraRequestHeaders( |
794 "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryAADeAA+NA
AWMAAwZ\r\n" | 796 "Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryAADeAA+NA
AWMAAwZ\r\n" |
795 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text
/plain;q=0.8,image/png,*/*;q=0.5\r\n" | 797 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text
/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
(...skipping 15 matching lines...) Expand all Loading... |
811 EXPECT_FALSE(ContainsString(data, "Content-Length:")); | 813 EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
812 EXPECT_FALSE(ContainsString(data, "Content-Type:")); | 814 EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
813 EXPECT_FALSE(ContainsString(data, "Origin:")); | 815 EXPECT_FALSE(ContainsString(data, "Origin:")); |
814 | 816 |
815 // These extra request headers should not have been stripped. | 817 // These extra request headers should not have been stripped. |
816 EXPECT_TRUE(ContainsString(data, "Accept:")); | 818 EXPECT_TRUE(ContainsString(data, "Accept:")); |
817 EXPECT_TRUE(ContainsString(data, "Accept-Language:")); | 819 EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
818 EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); | 820 EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
819 } | 821 } |
820 | 822 |
OLD | NEW |