OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "net/http/http_cache.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/memory/scoped_vector.h" | |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/run_loop.h" | |
14 #include "base/strings/string_util.h" | |
15 #include "base/strings/stringprintf.h" | |
16 #include "base/test/simple_test_clock.h" | |
17 #include "net/base/cache_type.h" | |
18 #include "net/base/elements_upload_data_stream.h" | |
19 #include "net/base/host_port_pair.h" | |
20 #include "net/base/load_flags.h" | |
21 #include "net/base/load_timing_info.h" | |
22 #include "net/base/load_timing_info_test_util.h" | |
23 #include "net/base/net_errors.h" | |
24 #include "net/base/net_log_unittest.h" | |
25 #include "net/base/upload_bytes_element_reader.h" | |
26 #include "net/cert/cert_status_flags.h" | |
27 #include "net/disk_cache/disk_cache.h" | |
28 #include "net/http/http_byte_range.h" | |
29 #include "net/http/http_cache_transaction.h" | |
30 #include "net/http/http_request_headers.h" | |
31 #include "net/http/http_request_info.h" | |
32 #include "net/http/http_response_headers.h" | |
33 #include "net/http/http_response_info.h" | |
34 #include "net/http/http_transaction.h" | |
35 #include "net/http/http_transaction_test_util.h" | |
36 #include "net/http/http_util.h" | |
37 #include "net/http/mock_http_cache.h" | |
38 #include "net/socket/client_socket_handle.h" | |
39 #include "net/ssl/ssl_cert_request_info.h" | |
40 #include "net/websockets/websocket_handshake_stream_base.h" | |
41 #include "testing/gtest/include/gtest/gtest.h" | |
42 | |
43 using base::Time; | |
44 | |
45 namespace { | |
46 | |
47 // Tests the load timing values of a request that goes through a | |
48 // MockNetworkTransaction. | |
49 void TestLoadTimingNetworkRequest(const net::LoadTimingInfo& load_timing_info) { | |
50 EXPECT_FALSE(load_timing_info.socket_reused); | |
51 EXPECT_NE(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); | |
52 | |
53 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); | |
54 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); | |
55 | |
56 net::ExpectConnectTimingHasTimes(load_timing_info.connect_timing, | |
57 net::CONNECT_TIMING_HAS_CONNECT_TIMES_ONLY); | |
58 EXPECT_LE(load_timing_info.connect_timing.connect_end, | |
59 load_timing_info.send_start); | |
60 | |
61 EXPECT_LE(load_timing_info.send_start, load_timing_info.send_end); | |
62 | |
63 // Set by URLRequest / URLRequestHttpJob, at a higher level. | |
64 EXPECT_TRUE(load_timing_info.request_start_time.is_null()); | |
65 EXPECT_TRUE(load_timing_info.request_start.is_null()); | |
66 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); | |
67 } | |
68 | |
69 // Tests the load timing values of a request that receives a cached response. | |
70 void TestLoadTimingCachedResponse(const net::LoadTimingInfo& load_timing_info) { | |
71 EXPECT_FALSE(load_timing_info.socket_reused); | |
72 EXPECT_EQ(net::NetLog::Source::kInvalidId, load_timing_info.socket_log_id); | |
73 | |
74 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); | |
75 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); | |
76 | |
77 net::ExpectConnectTimingHasNoTimes(load_timing_info.connect_timing); | |
78 | |
79 // Only the send start / end times should be sent, and they should have the | |
80 // same value. | |
81 EXPECT_FALSE(load_timing_info.send_start.is_null()); | |
82 EXPECT_EQ(load_timing_info.send_start, load_timing_info.send_end); | |
83 | |
84 // Set by URLRequest / URLRequestHttpJob, at a higher level. | |
85 EXPECT_TRUE(load_timing_info.request_start_time.is_null()); | |
86 EXPECT_TRUE(load_timing_info.request_start.is_null()); | |
87 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); | |
88 } | |
89 | |
90 class DeleteCacheCompletionCallback : public net::TestCompletionCallbackBase { | |
91 public: | |
92 explicit DeleteCacheCompletionCallback(MockHttpCache* cache) | |
93 : cache_(cache), | |
94 callback_(base::Bind(&DeleteCacheCompletionCallback::OnComplete, | |
95 base::Unretained(this))) { | |
96 } | |
97 | |
98 const net::CompletionCallback& callback() const { return callback_; } | |
99 | |
100 private: | |
101 void OnComplete(int result) { | |
102 delete cache_; | |
103 SetResult(result); | |
104 } | |
105 | |
106 MockHttpCache* cache_; | |
107 net::CompletionCallback callback_; | |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback); | |
110 }; | |
111 | |
112 //----------------------------------------------------------------------------- | |
113 // helpers | |
114 | |
115 void ReadAndVerifyTransaction(net::HttpTransaction* trans, | |
116 const MockTransaction& trans_info) { | |
117 std::string content; | |
118 int rv = ReadTransaction(trans, &content); | |
119 | |
120 EXPECT_EQ(net::OK, rv); | |
121 std::string expected(trans_info.data); | |
122 EXPECT_EQ(expected, content); | |
123 } | |
124 | |
125 void RunTransactionTestBase(net::HttpCache* cache, | |
126 const MockTransaction& trans_info, | |
127 const MockHttpRequest& request, | |
128 net::HttpResponseInfo* response_info, | |
129 const net::BoundNetLog& net_log, | |
130 net::LoadTimingInfo* load_timing_info, | |
131 int64* received_bytes) { | |
132 net::TestCompletionCallback callback; | |
133 | |
134 // write to the cache | |
135 | |
136 scoped_ptr<net::HttpTransaction> trans; | |
137 int rv = cache->CreateTransaction(net::DEFAULT_PRIORITY, &trans); | |
138 EXPECT_EQ(net::OK, rv); | |
139 ASSERT_TRUE(trans.get()); | |
140 | |
141 rv = trans->Start(&request, callback.callback(), net_log); | |
142 if (rv == net::ERR_IO_PENDING) | |
143 rv = callback.WaitForResult(); | |
144 ASSERT_EQ(trans_info.return_code, rv); | |
145 | |
146 if (net::OK != rv) | |
147 return; | |
148 | |
149 const net::HttpResponseInfo* response = trans->GetResponseInfo(); | |
150 ASSERT_TRUE(response); | |
151 | |
152 if (response_info) | |
153 *response_info = *response; | |
154 | |
155 if (load_timing_info) { | |
156 // If a fake network connection is used, need a NetLog to get a fake socket | |
157 // ID. | |
158 EXPECT_TRUE(net_log.net_log()); | |
159 *load_timing_info = net::LoadTimingInfo(); | |
160 trans->GetLoadTimingInfo(load_timing_info); | |
161 } | |
162 | |
163 ReadAndVerifyTransaction(trans.get(), trans_info); | |
164 | |
165 if (received_bytes) | |
166 *received_bytes = trans->GetTotalReceivedBytes(); | |
167 } | |
168 | |
169 void RunTransactionTestWithRequest(net::HttpCache* cache, | |
170 const MockTransaction& trans_info, | |
171 const MockHttpRequest& request, | |
172 net::HttpResponseInfo* response_info) { | |
173 RunTransactionTestBase(cache, trans_info, request, response_info, | |
174 net::BoundNetLog(), NULL, NULL); | |
175 } | |
176 | |
177 void RunTransactionTestAndGetTiming(net::HttpCache* cache, | |
178 const MockTransaction& trans_info, | |
179 const net::BoundNetLog& log, | |
180 net::LoadTimingInfo* load_timing_info) { | |
181 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), | |
182 NULL, log, load_timing_info, NULL); | |
183 } | |
184 | |
185 void RunTransactionTest(net::HttpCache* cache, | |
186 const MockTransaction& trans_info) { | |
187 RunTransactionTestAndGetTiming(cache, trans_info, net::BoundNetLog(), NULL); | |
188 } | |
189 | |
190 void RunTransactionTestWithLog(net::HttpCache* cache, | |
191 const MockTransaction& trans_info, | |
192 const net::BoundNetLog& log) { | |
193 RunTransactionTestAndGetTiming(cache, trans_info, log, NULL); | |
194 } | |
195 | |
196 void RunTransactionTestWithResponseInfo(net::HttpCache* cache, | |
197 const MockTransaction& trans_info, | |
198 net::HttpResponseInfo* response) { | |
199 RunTransactionTestWithRequest(cache, trans_info, MockHttpRequest(trans_info), | |
200 response); | |
201 } | |
202 | |
203 void RunTransactionTestWithResponseInfoAndGetTiming( | |
204 net::HttpCache* cache, | |
205 const MockTransaction& trans_info, | |
206 net::HttpResponseInfo* response, | |
207 const net::BoundNetLog& log, | |
208 net::LoadTimingInfo* load_timing_info) { | |
209 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), | |
210 response, log, load_timing_info, NULL); | |
211 } | |
212 | |
213 void RunTransactionTestWithResponse(net::HttpCache* cache, | |
214 const MockTransaction& trans_info, | |
215 std::string* response_headers) { | |
216 net::HttpResponseInfo response; | |
217 RunTransactionTestWithResponseInfo(cache, trans_info, &response); | |
218 response.headers->GetNormalizedHeaders(response_headers); | |
219 } | |
220 | |
221 void RunTransactionTestWithResponseAndGetTiming( | |
222 net::HttpCache* cache, | |
223 const MockTransaction& trans_info, | |
224 std::string* response_headers, | |
225 const net::BoundNetLog& log, | |
226 net::LoadTimingInfo* load_timing_info) { | |
227 net::HttpResponseInfo response; | |
228 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), | |
229 &response, log, load_timing_info, NULL); | |
230 response.headers->GetNormalizedHeaders(response_headers); | |
231 } | |
232 | |
233 // This class provides a handler for kFastNoStoreGET_Transaction so that the | |
234 // no-store header can be included on demand. | |
235 class FastTransactionServer { | |
236 public: | |
237 FastTransactionServer() { | |
238 no_store = false; | |
239 } | |
240 ~FastTransactionServer() {} | |
241 | |
242 void set_no_store(bool value) { no_store = value; } | |
243 | |
244 static void FastNoStoreHandler(const net::HttpRequestInfo* request, | |
245 std::string* response_status, | |
246 std::string* response_headers, | |
247 std::string* response_data) { | |
248 if (no_store) | |
249 *response_headers = "Cache-Control: no-store\n"; | |
250 } | |
251 | |
252 private: | |
253 static bool no_store; | |
254 DISALLOW_COPY_AND_ASSIGN(FastTransactionServer); | |
255 }; | |
256 bool FastTransactionServer::no_store; | |
257 | |
258 const MockTransaction kFastNoStoreGET_Transaction = { | |
259 "http://www.google.com/nostore", | |
260 "GET", | |
261 base::Time(), | |
262 "", | |
263 net::LOAD_VALIDATE_CACHE, | |
264 "HTTP/1.1 200 OK", | |
265 "Cache-Control: max-age=10000\n", | |
266 base::Time(), | |
267 "<html><body>Google Blah Blah</body></html>", | |
268 TEST_MODE_SYNC_NET_START, | |
269 &FastTransactionServer::FastNoStoreHandler, | |
270 0, | |
271 net::OK | |
272 }; | |
273 | |
274 // This class provides a handler for kRangeGET_TransactionOK so that the range | |
275 // request can be served on demand. | |
276 class RangeTransactionServer { | |
277 public: | |
278 RangeTransactionServer() { | |
279 not_modified_ = false; | |
280 modified_ = false; | |
281 bad_200_ = false; | |
282 } | |
283 ~RangeTransactionServer() { | |
284 not_modified_ = false; | |
285 modified_ = false; | |
286 bad_200_ = false; | |
287 } | |
288 | |
289 // Returns only 416 or 304 when set. | |
290 void set_not_modified(bool value) { not_modified_ = value; } | |
291 | |
292 // Returns 206 when revalidating a range (instead of 304). | |
293 void set_modified(bool value) { modified_ = value; } | |
294 | |
295 // Returns 200 instead of 206 (a malformed response overall). | |
296 void set_bad_200(bool value) { bad_200_ = value; } | |
297 | |
298 // Other than regular range related behavior (and the flags mentioned above), | |
299 // the server reacts to requests headers like so: | |
300 // X-Require-Mock-Auth -> return 401. | |
301 // X-Return-Default-Range -> assume 40-49 was requested. | |
302 static void RangeHandler(const net::HttpRequestInfo* request, | |
303 std::string* response_status, | |
304 std::string* response_headers, | |
305 std::string* response_data); | |
306 | |
307 private: | |
308 static bool not_modified_; | |
309 static bool modified_; | |
310 static bool bad_200_; | |
311 DISALLOW_COPY_AND_ASSIGN(RangeTransactionServer); | |
312 }; | |
313 bool RangeTransactionServer::not_modified_ = false; | |
314 bool RangeTransactionServer::modified_ = false; | |
315 bool RangeTransactionServer::bad_200_ = false; | |
316 | |
317 // A dummy extra header that must be preserved on a given request. | |
318 | |
319 // EXTRA_HEADER_LINE doesn't include a line terminator because it | |
320 // will be passed to AddHeaderFromString() which doesn't accept them. | |
321 #define EXTRA_HEADER_LINE "Extra: header" | |
322 | |
323 // EXTRA_HEADER contains a line terminator, as expected by | |
324 // AddHeadersFromString() (_not_ AddHeaderFromString()). | |
325 #define EXTRA_HEADER EXTRA_HEADER_LINE "\r\n" | |
326 | |
327 static const char kExtraHeaderKey[] = "Extra"; | |
328 | |
329 // Static. | |
330 void RangeTransactionServer::RangeHandler(const net::HttpRequestInfo* request, | |
331 std::string* response_status, | |
332 std::string* response_headers, | |
333 std::string* response_data) { | |
334 if (request->extra_headers.IsEmpty()) { | |
335 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); | |
336 response_data->clear(); | |
337 return; | |
338 } | |
339 | |
340 // We want to make sure we don't delete extra headers. | |
341 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); | |
342 | |
343 if (request->extra_headers.HasHeader("X-Require-Mock-Auth") && | |
344 !request->extra_headers.HasHeader("Authorization")) { | |
345 response_status->assign("HTTP/1.1 401 Unauthorized"); | |
346 response_data->assign("WWW-Authenticate: Foo\n"); | |
347 return; | |
348 } | |
349 | |
350 if (not_modified_) { | |
351 response_status->assign("HTTP/1.1 304 Not Modified"); | |
352 response_data->clear(); | |
353 return; | |
354 } | |
355 | |
356 std::vector<net::HttpByteRange> ranges; | |
357 std::string range_header; | |
358 if (!request->extra_headers.GetHeader( | |
359 net::HttpRequestHeaders::kRange, &range_header) || | |
360 !net::HttpUtil::ParseRangeHeader(range_header, &ranges) || bad_200_ || | |
361 ranges.size() != 1) { | |
362 // This is not a byte range request. We return 200. | |
363 response_status->assign("HTTP/1.1 200 OK"); | |
364 response_headers->assign("Date: Wed, 28 Nov 2007 09:40:09 GMT"); | |
365 response_data->assign("Not a range"); | |
366 return; | |
367 } | |
368 | |
369 // We can handle this range request. | |
370 net::HttpByteRange byte_range = ranges[0]; | |
371 | |
372 if (request->extra_headers.HasHeader("X-Return-Default-Range")) { | |
373 byte_range.set_first_byte_position(40); | |
374 byte_range.set_last_byte_position(49); | |
375 } | |
376 | |
377 if (byte_range.first_byte_position() > 79) { | |
378 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); | |
379 response_data->clear(); | |
380 return; | |
381 } | |
382 | |
383 EXPECT_TRUE(byte_range.ComputeBounds(80)); | |
384 int start = static_cast<int>(byte_range.first_byte_position()); | |
385 int end = static_cast<int>(byte_range.last_byte_position()); | |
386 | |
387 EXPECT_LT(end, 80); | |
388 | |
389 std::string content_range = base::StringPrintf( | |
390 "Content-Range: bytes %d-%d/80\n", start, end); | |
391 response_headers->append(content_range); | |
392 | |
393 if (!request->extra_headers.HasHeader("If-None-Match") || modified_) { | |
394 std::string data; | |
395 if (end == start) { | |
396 EXPECT_EQ(0, end % 10); | |
397 data = "r"; | |
398 } else { | |
399 EXPECT_EQ(9, (end - start) % 10); | |
400 for (int block_start = start; block_start < end; block_start += 10) { | |
401 base::StringAppendF(&data, "rg: %02d-%02d ", | |
402 block_start, block_start + 9); | |
403 } | |
404 } | |
405 *response_data = data; | |
406 | |
407 if (end - start != 9) { | |
408 // We also have to fix content-length. | |
409 int len = end - start + 1; | |
410 std::string content_length = base::StringPrintf("Content-Length: %d\n", | |
411 len); | |
412 response_headers->replace(response_headers->find("Content-Length:"), | |
413 content_length.size(), content_length); | |
414 } | |
415 } else { | |
416 response_status->assign("HTTP/1.1 304 Not Modified"); | |
417 response_data->clear(); | |
418 } | |
419 } | |
420 | |
421 const MockTransaction kRangeGET_TransactionOK = { | |
422 "http://www.google.com/range", | |
423 "GET", | |
424 base::Time(), | |
425 "Range: bytes = 40-49\r\n" | |
426 EXTRA_HEADER, | |
427 net::LOAD_NORMAL, | |
428 "HTTP/1.1 206 Partial Content", | |
429 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
430 "ETag: \"foo\"\n" | |
431 "Accept-Ranges: bytes\n" | |
432 "Content-Length: 10\n", | |
433 base::Time(), | |
434 "rg: 40-49 ", | |
435 TEST_MODE_NORMAL, | |
436 &RangeTransactionServer::RangeHandler, | |
437 0, | |
438 net::OK | |
439 }; | |
440 | |
441 const char kFullRangeData[] = | |
442 "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 " | |
443 "rg: 40-49 rg: 50-59 rg: 60-69 rg: 70-79 "; | |
444 | |
445 // Verifies the response headers (|response|) match a partial content | |
446 // response for the range starting at |start| and ending at |end|. | |
447 void Verify206Response(std::string response, int start, int end) { | |
448 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | |
449 response.size())); | |
450 scoped_refptr<net::HttpResponseHeaders> headers( | |
451 new net::HttpResponseHeaders(raw_headers)); | |
452 | |
453 ASSERT_EQ(206, headers->response_code()); | |
454 | |
455 int64 range_start, range_end, object_size; | |
456 ASSERT_TRUE( | |
457 headers->GetContentRange(&range_start, &range_end, &object_size)); | |
458 int64 content_length = headers->GetContentLength(); | |
459 | |
460 int length = end - start + 1; | |
461 ASSERT_EQ(length, content_length); | |
462 ASSERT_EQ(start, range_start); | |
463 ASSERT_EQ(end, range_end); | |
464 } | |
465 | |
466 // Creates a truncated entry that can be resumed using byte ranges. | |
467 void CreateTruncatedEntry(std::string raw_headers, MockHttpCache* cache) { | |
468 // Create a disk cache entry that stores an incomplete resource. | |
469 disk_cache::Entry* entry; | |
470 ASSERT_TRUE(cache->CreateBackendEntry(kRangeGET_TransactionOK.url, &entry, | |
471 NULL)); | |
472 | |
473 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), | |
474 raw_headers.size()); | |
475 | |
476 net::HttpResponseInfo response; | |
477 response.response_time = base::Time::Now(); | |
478 response.request_time = base::Time::Now(); | |
479 response.headers = new net::HttpResponseHeaders(raw_headers); | |
480 // Set the last argument for this to be an incomplete request. | |
481 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true)); | |
482 | |
483 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100)); | |
484 int len = static_cast<int>(base::strlcpy(buf->data(), | |
485 "rg: 00-09 rg: 10-19 ", 100)); | |
486 net::TestCompletionCallback cb; | |
487 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true); | |
488 EXPECT_EQ(len, cb.GetResult(rv)); | |
489 entry->Close(); | |
490 } | |
491 | |
492 // Helper to represent a network HTTP response. | |
493 struct Response { | |
494 // Set this response into |trans|. | |
495 void AssignTo(MockTransaction* trans) const { | |
496 trans->status = status; | |
497 trans->response_headers = headers; | |
498 trans->data = body; | |
499 } | |
500 | |
501 std::string status_and_headers() const { | |
502 return std::string(status) + "\n" + std::string(headers); | |
503 } | |
504 | |
505 const char* status; | |
506 const char* headers; | |
507 const char* body; | |
508 }; | |
509 | |
510 struct Context { | |
511 Context() : result(net::ERR_IO_PENDING) {} | |
512 | |
513 int result; | |
514 net::TestCompletionCallback callback; | |
515 scoped_ptr<net::HttpTransaction> trans; | |
516 }; | |
517 | |
518 class FakeWebSocketHandshakeStreamCreateHelper | |
519 : public net::WebSocketHandshakeStreamBase::CreateHelper { | |
520 public: | |
521 ~FakeWebSocketHandshakeStreamCreateHelper() override {} | |
522 net::WebSocketHandshakeStreamBase* CreateBasicStream( | |
523 scoped_ptr<net::ClientSocketHandle> connect, | |
524 bool using_proxy) override { | |
525 return NULL; | |
526 } | |
527 net::WebSocketHandshakeStreamBase* CreateSpdyStream( | |
528 const base::WeakPtr<net::SpdySession>& session, | |
529 bool use_relative_url) override { | |
530 return NULL; | |
531 } | |
532 }; | |
533 | |
534 // Returns true if |entry| is not one of the log types paid attention to in this | |
535 // test. Note that TYPE_HTTP_CACHE_WRITE_INFO and TYPE_HTTP_CACHE_*_DATA are | |
536 // ignored. | |
537 bool ShouldIgnoreLogEntry(const net::CapturingNetLog::CapturedEntry& entry) { | |
538 switch (entry.type) { | |
539 case net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND: | |
540 case net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY: | |
541 case net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY: | |
542 case net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY: | |
543 case net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY: | |
544 case net::NetLog::TYPE_HTTP_CACHE_READ_INFO: | |
545 return false; | |
546 default: | |
547 return true; | |
548 } | |
549 } | |
550 | |
551 // Modifies |entries| to only include log entries created by the cache layer and | |
552 // asserted on in these tests. | |
553 void FilterLogEntries(net::CapturingNetLog::CapturedEntryList* entries) { | |
554 entries->erase(std::remove_if(entries->begin(), entries->end(), | |
555 &ShouldIgnoreLogEntry), | |
556 entries->end()); | |
557 } | |
558 | |
559 bool LogContainsEventType(const net::CapturingBoundNetLog& log, | |
560 net::NetLog::EventType expected) { | |
561 net::CapturingNetLog::CapturedEntryList entries; | |
562 log.GetEntries(&entries); | |
563 for (size_t i = 0; i < entries.size(); i++) { | |
564 if (entries[i].type == expected) | |
565 return true; | |
566 } | |
567 return false; | |
568 } | |
569 | |
570 } // namespace | |
571 | |
572 | |
573 //----------------------------------------------------------------------------- | |
574 // Tests. | |
575 | |
576 TEST(HttpCache, CreateThenDestroy) { | |
577 MockHttpCache cache; | |
578 | |
579 scoped_ptr<net::HttpTransaction> trans; | |
580 EXPECT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
581 ASSERT_TRUE(trans.get()); | |
582 } | |
583 | |
584 TEST(HttpCache, GetBackend) { | |
585 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0)); | |
586 | |
587 disk_cache::Backend* backend; | |
588 net::TestCompletionCallback cb; | |
589 // This will lazily initialize the backend. | |
590 int rv = cache.http_cache()->GetBackend(&backend, cb.callback()); | |
591 EXPECT_EQ(net::OK, cb.GetResult(rv)); | |
592 } | |
593 | |
594 TEST(HttpCache, SimpleGET) { | |
595 MockHttpCache cache; | |
596 net::CapturingBoundNetLog log; | |
597 net::LoadTimingInfo load_timing_info; | |
598 | |
599 // Write to the cache. | |
600 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction, | |
601 log.bound(), &load_timing_info); | |
602 | |
603 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
604 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
605 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
606 TestLoadTimingNetworkRequest(load_timing_info); | |
607 } | |
608 | |
609 TEST(HttpCache, SimpleGETNoDiskCache) { | |
610 MockHttpCache cache; | |
611 | |
612 cache.disk_cache()->set_fail_requests(); | |
613 | |
614 net::CapturingBoundNetLog log; | |
615 net::LoadTimingInfo load_timing_info; | |
616 | |
617 // Read from the network, and don't use the cache. | |
618 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction, | |
619 log.bound(), &load_timing_info); | |
620 | |
621 // Check that the NetLog was filled as expected. | |
622 // (We attempted to both Open and Create entries, but both failed). | |
623 net::CapturingNetLog::CapturedEntryList entries; | |
624 log.GetEntries(&entries); | |
625 FilterLogEntries(&entries); | |
626 | |
627 EXPECT_EQ(6u, entries.size()); | |
628 EXPECT_TRUE(net::LogContainsBeginEvent( | |
629 entries, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); | |
630 EXPECT_TRUE(net::LogContainsEndEvent( | |
631 entries, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); | |
632 EXPECT_TRUE(net::LogContainsBeginEvent( | |
633 entries, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | |
634 EXPECT_TRUE(net::LogContainsEndEvent( | |
635 entries, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | |
636 EXPECT_TRUE(net::LogContainsBeginEvent( | |
637 entries, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | |
638 EXPECT_TRUE(net::LogContainsEndEvent( | |
639 entries, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | |
640 | |
641 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
642 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
643 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
644 TestLoadTimingNetworkRequest(load_timing_info); | |
645 } | |
646 | |
647 TEST(HttpCache, SimpleGETNoDiskCache2) { | |
648 // This will initialize a cache object with NULL backend. | |
649 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | |
650 factory->set_fail(true); | |
651 factory->FinishCreation(); // We'll complete synchronously. | |
652 MockHttpCache cache(factory); | |
653 | |
654 // Read from the network, and don't use the cache. | |
655 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
656 | |
657 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
658 EXPECT_FALSE(cache.http_cache()->GetCurrentBackend()); | |
659 } | |
660 | |
661 // Tests that IOBuffers are not referenced after IO completes. | |
662 TEST(HttpCache, ReleaseBuffer) { | |
663 MockHttpCache cache; | |
664 | |
665 // Write to the cache. | |
666 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
667 | |
668 MockHttpRequest request(kSimpleGET_Transaction); | |
669 scoped_ptr<net::HttpTransaction> trans; | |
670 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
671 | |
672 const int kBufferSize = 10; | |
673 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | |
674 net::ReleaseBufferCompletionCallback cb(buffer.get()); | |
675 | |
676 int rv = trans->Start(&request, cb.callback(), net::BoundNetLog()); | |
677 EXPECT_EQ(net::OK, cb.GetResult(rv)); | |
678 | |
679 rv = trans->Read(buffer.get(), kBufferSize, cb.callback()); | |
680 EXPECT_EQ(kBufferSize, cb.GetResult(rv)); | |
681 } | |
682 | |
683 TEST(HttpCache, SimpleGETWithDiskFailures) { | |
684 MockHttpCache cache; | |
685 | |
686 cache.disk_cache()->set_soft_failures(true); | |
687 | |
688 // Read from the network, and fail to write to the cache. | |
689 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
690 | |
691 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
692 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
693 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
694 | |
695 // This one should see an empty cache again. | |
696 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
697 | |
698 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
699 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
700 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
701 } | |
702 | |
703 // Tests that disk failures after the transaction has started don't cause the | |
704 // request to fail. | |
705 TEST(HttpCache, SimpleGETWithDiskFailures2) { | |
706 MockHttpCache cache; | |
707 | |
708 MockHttpRequest request(kSimpleGET_Transaction); | |
709 | |
710 scoped_ptr<Context> c(new Context()); | |
711 int rv = cache.CreateTransaction(&c->trans); | |
712 ASSERT_EQ(net::OK, rv); | |
713 | |
714 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
715 EXPECT_EQ(net::ERR_IO_PENDING, rv); | |
716 rv = c->callback.WaitForResult(); | |
717 | |
718 // Start failing request now. | |
719 cache.disk_cache()->set_soft_failures(true); | |
720 | |
721 // We have to open the entry again to propagate the failure flag. | |
722 disk_cache::Entry* en; | |
723 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &en)); | |
724 en->Close(); | |
725 | |
726 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | |
727 c.reset(); | |
728 | |
729 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
730 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
731 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
732 | |
733 // This one should see an empty cache again. | |
734 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
735 | |
736 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
737 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
738 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
739 } | |
740 | |
741 // Tests that we handle failures to read from the cache. | |
742 TEST(HttpCache, SimpleGETWithDiskFailures3) { | |
743 MockHttpCache cache; | |
744 | |
745 // Read from the network, and write to the cache. | |
746 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
747 | |
748 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
749 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
750 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
751 | |
752 cache.disk_cache()->set_soft_failures(true); | |
753 | |
754 // Now fail to read from the cache. | |
755 scoped_ptr<Context> c(new Context()); | |
756 int rv = cache.CreateTransaction(&c->trans); | |
757 ASSERT_EQ(net::OK, rv); | |
758 | |
759 MockHttpRequest request(kSimpleGET_Transaction); | |
760 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
761 EXPECT_EQ(net::OK, c->callback.GetResult(rv)); | |
762 | |
763 // Now verify that the entry was removed from the cache. | |
764 cache.disk_cache()->set_soft_failures(false); | |
765 | |
766 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
767 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
768 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
769 | |
770 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
771 | |
772 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
773 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
774 EXPECT_EQ(3, cache.disk_cache()->create_count()); | |
775 } | |
776 | |
777 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit) { | |
778 MockHttpCache cache; | |
779 | |
780 net::CapturingBoundNetLog log; | |
781 net::LoadTimingInfo load_timing_info; | |
782 | |
783 // Write to the cache. | |
784 RunTransactionTestAndGetTiming(cache.http_cache(), kSimpleGET_Transaction, | |
785 log.bound(), &load_timing_info); | |
786 | |
787 // Check that the NetLog was filled as expected. | |
788 net::CapturingNetLog::CapturedEntryList entries; | |
789 log.GetEntries(&entries); | |
790 FilterLogEntries(&entries); | |
791 | |
792 EXPECT_EQ(8u, entries.size()); | |
793 EXPECT_TRUE(net::LogContainsBeginEvent( | |
794 entries, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); | |
795 EXPECT_TRUE(net::LogContainsEndEvent( | |
796 entries, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); | |
797 EXPECT_TRUE(net::LogContainsBeginEvent( | |
798 entries, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | |
799 EXPECT_TRUE(net::LogContainsEndEvent( | |
800 entries, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | |
801 EXPECT_TRUE(net::LogContainsBeginEvent( | |
802 entries, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | |
803 EXPECT_TRUE(net::LogContainsEndEvent( | |
804 entries, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | |
805 EXPECT_TRUE(net::LogContainsBeginEvent( | |
806 entries, 6, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); | |
807 EXPECT_TRUE(net::LogContainsEndEvent( | |
808 entries, 7, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); | |
809 | |
810 TestLoadTimingNetworkRequest(load_timing_info); | |
811 | |
812 // Force this transaction to read from the cache. | |
813 MockTransaction transaction(kSimpleGET_Transaction); | |
814 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
815 | |
816 log.Clear(); | |
817 | |
818 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), | |
819 &load_timing_info); | |
820 | |
821 // Check that the NetLog was filled as expected. | |
822 log.GetEntries(&entries); | |
823 FilterLogEntries(&entries); | |
824 | |
825 EXPECT_EQ(8u, entries.size()); | |
826 EXPECT_TRUE(net::LogContainsBeginEvent( | |
827 entries, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); | |
828 EXPECT_TRUE(net::LogContainsEndEvent( | |
829 entries, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); | |
830 EXPECT_TRUE(net::LogContainsBeginEvent( | |
831 entries, 2, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | |
832 EXPECT_TRUE(net::LogContainsEndEvent( | |
833 entries, 3, net::NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY)); | |
834 EXPECT_TRUE(net::LogContainsBeginEvent( | |
835 entries, 4, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); | |
836 EXPECT_TRUE(net::LogContainsEndEvent( | |
837 entries, 5, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); | |
838 EXPECT_TRUE(net::LogContainsBeginEvent( | |
839 entries, 6, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); | |
840 EXPECT_TRUE(net::LogContainsEndEvent( | |
841 entries, 7, net::NetLog::TYPE_HTTP_CACHE_READ_INFO)); | |
842 | |
843 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
844 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
845 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
846 TestLoadTimingCachedResponse(load_timing_info); | |
847 } | |
848 | |
849 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Miss) { | |
850 MockHttpCache cache; | |
851 | |
852 // force this transaction to read from the cache | |
853 MockTransaction transaction(kSimpleGET_Transaction); | |
854 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
855 | |
856 MockHttpRequest request(transaction); | |
857 net::TestCompletionCallback callback; | |
858 | |
859 scoped_ptr<net::HttpTransaction> trans; | |
860 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
861 | |
862 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
863 if (rv == net::ERR_IO_PENDING) | |
864 rv = callback.WaitForResult(); | |
865 ASSERT_EQ(net::ERR_CACHE_MISS, rv); | |
866 | |
867 trans.reset(); | |
868 | |
869 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | |
870 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
871 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
872 } | |
873 | |
874 TEST(HttpCache, SimpleGET_LoadPreferringCache_Hit) { | |
875 MockHttpCache cache; | |
876 | |
877 // write to the cache | |
878 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
879 | |
880 // force this transaction to read from the cache if valid | |
881 MockTransaction transaction(kSimpleGET_Transaction); | |
882 transaction.load_flags |= net::LOAD_PREFERRING_CACHE; | |
883 | |
884 RunTransactionTest(cache.http_cache(), transaction); | |
885 | |
886 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
887 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
888 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
889 } | |
890 | |
891 TEST(HttpCache, SimpleGET_LoadPreferringCache_Miss) { | |
892 MockHttpCache cache; | |
893 | |
894 // force this transaction to read from the cache if valid | |
895 MockTransaction transaction(kSimpleGET_Transaction); | |
896 transaction.load_flags |= net::LOAD_PREFERRING_CACHE; | |
897 | |
898 RunTransactionTest(cache.http_cache(), transaction); | |
899 | |
900 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
901 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
902 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
903 } | |
904 | |
905 // Tests LOAD_PREFERRING_CACHE in the presence of vary headers. | |
906 TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMatch) { | |
907 MockHttpCache cache; | |
908 | |
909 // Write to the cache. | |
910 MockTransaction transaction(kSimpleGET_Transaction); | |
911 transaction.request_headers = "Foo: bar\r\n"; | |
912 transaction.response_headers = "Cache-Control: max-age=10000\n" | |
913 "Vary: Foo\n"; | |
914 AddMockTransaction(&transaction); | |
915 RunTransactionTest(cache.http_cache(), transaction); | |
916 | |
917 // Read from the cache. | |
918 transaction.load_flags |= net::LOAD_PREFERRING_CACHE; | |
919 RunTransactionTest(cache.http_cache(), transaction); | |
920 | |
921 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
922 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
923 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
924 RemoveMockTransaction(&transaction); | |
925 } | |
926 | |
927 // Tests LOAD_PREFERRING_CACHE in the presence of vary headers. | |
928 TEST(HttpCache, SimpleGET_LoadPreferringCache_VaryMismatch) { | |
929 MockHttpCache cache; | |
930 | |
931 // Write to the cache. | |
932 MockTransaction transaction(kSimpleGET_Transaction); | |
933 transaction.request_headers = "Foo: bar\r\n"; | |
934 transaction.response_headers = "Cache-Control: max-age=10000\n" | |
935 "Vary: Foo\n"; | |
936 AddMockTransaction(&transaction); | |
937 RunTransactionTest(cache.http_cache(), transaction); | |
938 | |
939 // Attempt to read from the cache... this is a vary mismatch that must reach | |
940 // the network again. | |
941 transaction.load_flags |= net::LOAD_PREFERRING_CACHE; | |
942 transaction.request_headers = "Foo: none\r\n"; | |
943 net::CapturingBoundNetLog log; | |
944 net::LoadTimingInfo load_timing_info; | |
945 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), | |
946 &load_timing_info); | |
947 | |
948 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
949 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
950 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
951 TestLoadTimingNetworkRequest(load_timing_info); | |
952 RemoveMockTransaction(&transaction); | |
953 } | |
954 | |
955 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on | |
956 // network success | |
957 TEST(HttpCache, SimpleGET_CacheOverride_Network) { | |
958 MockHttpCache cache; | |
959 | |
960 // Prime cache. | |
961 MockTransaction transaction(kSimpleGET_Transaction); | |
962 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; | |
963 transaction.response_headers = "Cache-Control: no-cache\n"; | |
964 | |
965 AddMockTransaction(&transaction); | |
966 RunTransactionTest(cache.http_cache(), transaction); | |
967 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
968 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
969 RemoveMockTransaction(&transaction); | |
970 | |
971 // Re-run transaction; make sure the result came from the network, | |
972 // not the cache. | |
973 transaction.data = "Changed data."; | |
974 AddMockTransaction(&transaction); | |
975 net::HttpResponseInfo response_info; | |
976 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, | |
977 &response_info); | |
978 | |
979 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
980 EXPECT_FALSE(response_info.server_data_unavailable); | |
981 EXPECT_TRUE(response_info.network_accessed); | |
982 | |
983 RemoveMockTransaction(&transaction); | |
984 } | |
985 | |
986 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on | |
987 // offline failure | |
988 TEST(HttpCache, SimpleGET_CacheOverride_Offline) { | |
989 MockHttpCache cache; | |
990 | |
991 // Prime cache. | |
992 MockTransaction transaction(kSimpleGET_Transaction); | |
993 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; | |
994 transaction.response_headers = "Cache-Control: no-cache\n"; | |
995 | |
996 AddMockTransaction(&transaction); | |
997 RunTransactionTest(cache.http_cache(), transaction); | |
998 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
999 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1000 RemoveMockTransaction(&transaction); | |
1001 | |
1002 // Network failure with offline error; should return cache entry above + | |
1003 // flag signalling stale data. | |
1004 transaction.return_code = net::ERR_NAME_NOT_RESOLVED; | |
1005 AddMockTransaction(&transaction); | |
1006 | |
1007 MockHttpRequest request(transaction); | |
1008 net::TestCompletionCallback callback; | |
1009 scoped_ptr<net::HttpTransaction> trans; | |
1010 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
1011 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
1012 EXPECT_EQ(net::OK, callback.GetResult(rv)); | |
1013 | |
1014 const net::HttpResponseInfo* response_info = trans->GetResponseInfo(); | |
1015 ASSERT_TRUE(response_info); | |
1016 EXPECT_TRUE(response_info->server_data_unavailable); | |
1017 EXPECT_TRUE(response_info->was_cached); | |
1018 EXPECT_FALSE(response_info->network_accessed); | |
1019 ReadAndVerifyTransaction(trans.get(), transaction); | |
1020 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1021 | |
1022 RemoveMockTransaction(&transaction); | |
1023 } | |
1024 | |
1025 // Tests that LOAD_FROM_CACHE_IF_OFFLINE returns proper response on | |
1026 // non-offline failure. | |
1027 TEST(HttpCache, SimpleGET_CacheOverride_NonOffline) { | |
1028 MockHttpCache cache; | |
1029 | |
1030 // Prime cache. | |
1031 MockTransaction transaction(kSimpleGET_Transaction); | |
1032 transaction.load_flags |= net::LOAD_FROM_CACHE_IF_OFFLINE; | |
1033 transaction.response_headers = "Cache-Control: no-cache\n"; | |
1034 | |
1035 AddMockTransaction(&transaction); | |
1036 RunTransactionTest(cache.http_cache(), transaction); | |
1037 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1038 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1039 RemoveMockTransaction(&transaction); | |
1040 | |
1041 // Network failure with non-offline error; should fail with that error. | |
1042 transaction.return_code = net::ERR_PROXY_CONNECTION_FAILED; | |
1043 AddMockTransaction(&transaction); | |
1044 | |
1045 net::HttpResponseInfo response_info2; | |
1046 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, | |
1047 &response_info2); | |
1048 | |
1049 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1050 EXPECT_FALSE(response_info2.server_data_unavailable); | |
1051 | |
1052 RemoveMockTransaction(&transaction); | |
1053 } | |
1054 | |
1055 // Tests that was_cached was set properly on a failure, even if the cached | |
1056 // response wasn't returned. | |
1057 TEST(HttpCache, SimpleGET_CacheSignal_Failure) { | |
1058 MockHttpCache cache; | |
1059 | |
1060 // Prime cache. | |
1061 MockTransaction transaction(kSimpleGET_Transaction); | |
1062 transaction.response_headers = "Cache-Control: no-cache\n"; | |
1063 | |
1064 AddMockTransaction(&transaction); | |
1065 RunTransactionTest(cache.http_cache(), transaction); | |
1066 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1067 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1068 RemoveMockTransaction(&transaction); | |
1069 | |
1070 // Network failure with error; should fail but have was_cached set. | |
1071 transaction.return_code = net::ERR_FAILED; | |
1072 AddMockTransaction(&transaction); | |
1073 | |
1074 MockHttpRequest request(transaction); | |
1075 net::TestCompletionCallback callback; | |
1076 scoped_ptr<net::HttpTransaction> trans; | |
1077 int rv = cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, &trans); | |
1078 EXPECT_EQ(net::OK, rv); | |
1079 ASSERT_TRUE(trans.get()); | |
1080 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
1081 EXPECT_EQ(net::ERR_FAILED, callback.GetResult(rv)); | |
1082 | |
1083 const net::HttpResponseInfo* response_info = trans->GetResponseInfo(); | |
1084 ASSERT_TRUE(response_info); | |
1085 EXPECT_TRUE(response_info->was_cached); | |
1086 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1087 | |
1088 RemoveMockTransaction(&transaction); | |
1089 } | |
1090 | |
1091 // Confirm if we have an empty cache, a read is marked as network verified. | |
1092 TEST(HttpCache, SimpleGET_NetworkAccessed_Network) { | |
1093 MockHttpCache cache; | |
1094 | |
1095 // write to the cache | |
1096 net::HttpResponseInfo response_info; | |
1097 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | |
1098 &response_info); | |
1099 | |
1100 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1101 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1102 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1103 EXPECT_TRUE(response_info.network_accessed); | |
1104 } | |
1105 | |
1106 // Confirm if we have a fresh entry in cache, it isn't marked as | |
1107 // network verified. | |
1108 TEST(HttpCache, SimpleGET_NetworkAccessed_Cache) { | |
1109 MockHttpCache cache; | |
1110 | |
1111 // Prime cache. | |
1112 MockTransaction transaction(kSimpleGET_Transaction); | |
1113 | |
1114 RunTransactionTest(cache.http_cache(), transaction); | |
1115 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1116 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1117 | |
1118 // Re-run transaction; make sure we don't mark the network as accessed. | |
1119 net::HttpResponseInfo response_info; | |
1120 RunTransactionTestWithResponseInfo(cache.http_cache(), transaction, | |
1121 &response_info); | |
1122 | |
1123 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1124 EXPECT_FALSE(response_info.server_data_unavailable); | |
1125 EXPECT_FALSE(response_info.network_accessed); | |
1126 } | |
1127 | |
1128 TEST(HttpCache, SimpleGET_LoadBypassCache) { | |
1129 MockHttpCache cache; | |
1130 | |
1131 // Write to the cache. | |
1132 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
1133 | |
1134 // Force this transaction to write to the cache again. | |
1135 MockTransaction transaction(kSimpleGET_Transaction); | |
1136 transaction.load_flags |= net::LOAD_BYPASS_CACHE; | |
1137 | |
1138 net::CapturingBoundNetLog log; | |
1139 net::LoadTimingInfo load_timing_info; | |
1140 | |
1141 // Write to the cache. | |
1142 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), | |
1143 &load_timing_info); | |
1144 | |
1145 // Check that the NetLog was filled as expected. | |
1146 net::CapturingNetLog::CapturedEntryList entries; | |
1147 log.GetEntries(&entries); | |
1148 FilterLogEntries(&entries); | |
1149 | |
1150 EXPECT_EQ(8u, entries.size()); | |
1151 EXPECT_TRUE(net::LogContainsBeginEvent( | |
1152 entries, 0, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); | |
1153 EXPECT_TRUE(net::LogContainsEndEvent( | |
1154 entries, 1, net::NetLog::TYPE_HTTP_CACHE_GET_BACKEND)); | |
1155 EXPECT_TRUE(net::LogContainsBeginEvent( | |
1156 entries, 2, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); | |
1157 EXPECT_TRUE(net::LogContainsEndEvent( | |
1158 entries, 3, net::NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY)); | |
1159 EXPECT_TRUE(net::LogContainsBeginEvent( | |
1160 entries, 4, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | |
1161 EXPECT_TRUE(net::LogContainsEndEvent( | |
1162 entries, 5, net::NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY)); | |
1163 EXPECT_TRUE(net::LogContainsBeginEvent( | |
1164 entries, 6, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); | |
1165 EXPECT_TRUE(net::LogContainsEndEvent( | |
1166 entries, 7, net::NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY)); | |
1167 | |
1168 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1169 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1170 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
1171 TestLoadTimingNetworkRequest(load_timing_info); | |
1172 } | |
1173 | |
1174 TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit) { | |
1175 MockHttpCache cache; | |
1176 | |
1177 // write to the cache | |
1178 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
1179 | |
1180 // force this transaction to write to the cache again | |
1181 MockTransaction transaction(kSimpleGET_Transaction); | |
1182 transaction.request_headers = "pragma: no-cache\r\n"; | |
1183 | |
1184 RunTransactionTest(cache.http_cache(), transaction); | |
1185 | |
1186 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1187 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1188 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
1189 } | |
1190 | |
1191 TEST(HttpCache, SimpleGET_LoadBypassCache_Implicit2) { | |
1192 MockHttpCache cache; | |
1193 | |
1194 // write to the cache | |
1195 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
1196 | |
1197 // force this transaction to write to the cache again | |
1198 MockTransaction transaction(kSimpleGET_Transaction); | |
1199 transaction.request_headers = "cache-control: no-cache\r\n"; | |
1200 | |
1201 RunTransactionTest(cache.http_cache(), transaction); | |
1202 | |
1203 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1204 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1205 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
1206 } | |
1207 | |
1208 TEST(HttpCache, SimpleGET_LoadValidateCache) { | |
1209 MockHttpCache cache; | |
1210 | |
1211 // Write to the cache. | |
1212 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
1213 | |
1214 // Read from the cache. | |
1215 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
1216 | |
1217 // Force this transaction to validate the cache. | |
1218 MockTransaction transaction(kSimpleGET_Transaction); | |
1219 transaction.load_flags |= net::LOAD_VALIDATE_CACHE; | |
1220 | |
1221 net::HttpResponseInfo response_info; | |
1222 net::CapturingBoundNetLog log; | |
1223 net::LoadTimingInfo load_timing_info; | |
1224 RunTransactionTestWithResponseInfoAndGetTiming( | |
1225 cache.http_cache(), transaction, &response_info, log.bound(), | |
1226 &load_timing_info); | |
1227 | |
1228 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1229 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
1230 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1231 EXPECT_TRUE(response_info.network_accessed); | |
1232 TestLoadTimingNetworkRequest(load_timing_info); | |
1233 } | |
1234 | |
1235 TEST(HttpCache, SimpleGET_LoadValidateCache_Implicit) { | |
1236 MockHttpCache cache; | |
1237 | |
1238 // write to the cache | |
1239 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
1240 | |
1241 // read from the cache | |
1242 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
1243 | |
1244 // force this transaction to validate the cache | |
1245 MockTransaction transaction(kSimpleGET_Transaction); | |
1246 transaction.request_headers = "cache-control: max-age=0\r\n"; | |
1247 | |
1248 RunTransactionTest(cache.http_cache(), transaction); | |
1249 | |
1250 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1251 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
1252 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1253 } | |
1254 | |
1255 static void PreserveRequestHeaders_Handler( | |
1256 const net::HttpRequestInfo* request, | |
1257 std::string* response_status, | |
1258 std::string* response_headers, | |
1259 std::string* response_data) { | |
1260 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); | |
1261 } | |
1262 | |
1263 // Tests that we don't remove extra headers for simple requests. | |
1264 TEST(HttpCache, SimpleGET_PreserveRequestHeaders) { | |
1265 MockHttpCache cache; | |
1266 | |
1267 MockTransaction transaction(kSimpleGET_Transaction); | |
1268 transaction.handler = PreserveRequestHeaders_Handler; | |
1269 transaction.request_headers = EXTRA_HEADER; | |
1270 transaction.response_headers = "Cache-Control: max-age=0\n"; | |
1271 AddMockTransaction(&transaction); | |
1272 | |
1273 // Write, then revalidate the entry. | |
1274 RunTransactionTest(cache.http_cache(), transaction); | |
1275 RunTransactionTest(cache.http_cache(), transaction); | |
1276 | |
1277 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1278 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
1279 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1280 RemoveMockTransaction(&transaction); | |
1281 } | |
1282 | |
1283 // Tests that we don't remove extra headers for conditionalized requests. | |
1284 TEST(HttpCache, ConditionalizedGET_PreserveRequestHeaders) { | |
1285 MockHttpCache cache; | |
1286 | |
1287 // Write to the cache. | |
1288 RunTransactionTest(cache.http_cache(), kETagGET_Transaction); | |
1289 | |
1290 MockTransaction transaction(kETagGET_Transaction); | |
1291 transaction.handler = PreserveRequestHeaders_Handler; | |
1292 transaction.request_headers = "If-None-Match: \"foopy\"\r\n" | |
1293 EXTRA_HEADER; | |
1294 AddMockTransaction(&transaction); | |
1295 | |
1296 RunTransactionTest(cache.http_cache(), transaction); | |
1297 | |
1298 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1299 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
1300 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1301 RemoveMockTransaction(&transaction); | |
1302 } | |
1303 | |
1304 TEST(HttpCache, SimpleGET_ManyReaders) { | |
1305 MockHttpCache cache; | |
1306 | |
1307 MockHttpRequest request(kSimpleGET_Transaction); | |
1308 | |
1309 std::vector<Context*> context_list; | |
1310 const int kNumTransactions = 5; | |
1311 | |
1312 for (int i = 0; i < kNumTransactions; ++i) { | |
1313 context_list.push_back(new Context()); | |
1314 Context* c = context_list[i]; | |
1315 | |
1316 c->result = cache.CreateTransaction(&c->trans); | |
1317 ASSERT_EQ(net::OK, c->result); | |
1318 EXPECT_EQ(net::LOAD_STATE_IDLE, c->trans->GetLoadState()); | |
1319 | |
1320 c->result = c->trans->Start( | |
1321 &request, c->callback.callback(), net::BoundNetLog()); | |
1322 } | |
1323 | |
1324 // All requests are waiting for the active entry. | |
1325 for (int i = 0; i < kNumTransactions; ++i) { | |
1326 Context* c = context_list[i]; | |
1327 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, c->trans->GetLoadState()); | |
1328 } | |
1329 | |
1330 // Allow all requests to move from the Create queue to the active entry. | |
1331 base::MessageLoop::current()->RunUntilIdle(); | |
1332 | |
1333 // The first request should be a writer at this point, and the subsequent | |
1334 // requests should be pending. | |
1335 | |
1336 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1337 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1338 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1339 | |
1340 // All requests depend on the writer, and the writer is between Start and | |
1341 // Read, i.e. idle. | |
1342 for (int i = 0; i < kNumTransactions; ++i) { | |
1343 Context* c = context_list[i]; | |
1344 EXPECT_EQ(net::LOAD_STATE_IDLE, c->trans->GetLoadState()); | |
1345 } | |
1346 | |
1347 for (int i = 0; i < kNumTransactions; ++i) { | |
1348 Context* c = context_list[i]; | |
1349 if (c->result == net::ERR_IO_PENDING) | |
1350 c->result = c->callback.WaitForResult(); | |
1351 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | |
1352 } | |
1353 | |
1354 // We should not have had to re-open the disk entry | |
1355 | |
1356 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1357 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1358 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1359 | |
1360 for (int i = 0; i < kNumTransactions; ++i) { | |
1361 Context* c = context_list[i]; | |
1362 delete c; | |
1363 } | |
1364 } | |
1365 | |
1366 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4769. | |
1367 // If cancelling a request is racing with another request for the same resource | |
1368 // finishing, we have to make sure that we remove both transactions from the | |
1369 // entry. | |
1370 TEST(HttpCache, SimpleGET_RacingReaders) { | |
1371 MockHttpCache cache; | |
1372 | |
1373 MockHttpRequest request(kSimpleGET_Transaction); | |
1374 MockHttpRequest reader_request(kSimpleGET_Transaction); | |
1375 reader_request.load_flags = net::LOAD_ONLY_FROM_CACHE; | |
1376 | |
1377 std::vector<Context*> context_list; | |
1378 const int kNumTransactions = 5; | |
1379 | |
1380 for (int i = 0; i < kNumTransactions; ++i) { | |
1381 context_list.push_back(new Context()); | |
1382 Context* c = context_list[i]; | |
1383 | |
1384 c->result = cache.CreateTransaction(&c->trans); | |
1385 ASSERT_EQ(net::OK, c->result); | |
1386 | |
1387 MockHttpRequest* this_request = &request; | |
1388 if (i == 1 || i == 2) | |
1389 this_request = &reader_request; | |
1390 | |
1391 c->result = c->trans->Start( | |
1392 this_request, c->callback.callback(), net::BoundNetLog()); | |
1393 } | |
1394 | |
1395 // Allow all requests to move from the Create queue to the active entry. | |
1396 base::MessageLoop::current()->RunUntilIdle(); | |
1397 | |
1398 // The first request should be a writer at this point, and the subsequent | |
1399 // requests should be pending. | |
1400 | |
1401 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1402 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1403 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1404 | |
1405 Context* c = context_list[0]; | |
1406 ASSERT_EQ(net::ERR_IO_PENDING, c->result); | |
1407 c->result = c->callback.WaitForResult(); | |
1408 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | |
1409 | |
1410 // Now we have 2 active readers and two queued transactions. | |
1411 | |
1412 EXPECT_EQ(net::LOAD_STATE_IDLE, | |
1413 context_list[2]->trans->GetLoadState()); | |
1414 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, | |
1415 context_list[3]->trans->GetLoadState()); | |
1416 | |
1417 c = context_list[1]; | |
1418 ASSERT_EQ(net::ERR_IO_PENDING, c->result); | |
1419 c->result = c->callback.WaitForResult(); | |
1420 if (c->result == net::OK) | |
1421 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | |
1422 | |
1423 // At this point we have one reader, two pending transactions and a task on | |
1424 // the queue to move to the next transaction. Now we cancel the request that | |
1425 // is the current reader, and expect the queued task to be able to start the | |
1426 // next request. | |
1427 | |
1428 c = context_list[2]; | |
1429 c->trans.reset(); | |
1430 | |
1431 for (int i = 3; i < kNumTransactions; ++i) { | |
1432 Context* c = context_list[i]; | |
1433 if (c->result == net::ERR_IO_PENDING) | |
1434 c->result = c->callback.WaitForResult(); | |
1435 if (c->result == net::OK) | |
1436 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | |
1437 } | |
1438 | |
1439 // We should not have had to re-open the disk entry. | |
1440 | |
1441 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1442 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1443 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1444 | |
1445 for (int i = 0; i < kNumTransactions; ++i) { | |
1446 Context* c = context_list[i]; | |
1447 delete c; | |
1448 } | |
1449 } | |
1450 | |
1451 // Tests that we can doom an entry with pending transactions and delete one of | |
1452 // the pending transactions before the first one completes. | |
1453 // See http://code.google.com/p/chromium/issues/detail?id=25588 | |
1454 TEST(HttpCache, SimpleGET_DoomWithPending) { | |
1455 // We need simultaneous doomed / not_doomed entries so let's use a real cache. | |
1456 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(1024 * 1024)); | |
1457 | |
1458 MockHttpRequest request(kSimpleGET_Transaction); | |
1459 MockHttpRequest writer_request(kSimpleGET_Transaction); | |
1460 writer_request.load_flags = net::LOAD_BYPASS_CACHE; | |
1461 | |
1462 ScopedVector<Context> context_list; | |
1463 const int kNumTransactions = 4; | |
1464 | |
1465 for (int i = 0; i < kNumTransactions; ++i) { | |
1466 context_list.push_back(new Context()); | |
1467 Context* c = context_list[i]; | |
1468 | |
1469 c->result = cache.CreateTransaction(&c->trans); | |
1470 ASSERT_EQ(net::OK, c->result); | |
1471 | |
1472 MockHttpRequest* this_request = &request; | |
1473 if (i == 3) | |
1474 this_request = &writer_request; | |
1475 | |
1476 c->result = c->trans->Start( | |
1477 this_request, c->callback.callback(), net::BoundNetLog()); | |
1478 } | |
1479 | |
1480 // The first request should be a writer at this point, and the two subsequent | |
1481 // requests should be pending. The last request doomed the first entry. | |
1482 | |
1483 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1484 | |
1485 // Cancel the first queued transaction. | |
1486 delete context_list[1]; | |
1487 context_list.get()[1] = NULL; | |
1488 | |
1489 for (int i = 0; i < kNumTransactions; ++i) { | |
1490 if (i == 1) | |
1491 continue; | |
1492 Context* c = context_list[i]; | |
1493 ASSERT_EQ(net::ERR_IO_PENDING, c->result); | |
1494 c->result = c->callback.WaitForResult(); | |
1495 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | |
1496 } | |
1497 } | |
1498 | |
1499 // This is a test for http://code.google.com/p/chromium/issues/detail?id=4731. | |
1500 // We may attempt to delete an entry synchronously with the act of adding a new | |
1501 // transaction to said entry. | |
1502 TEST(HttpCache, FastNoStoreGET_DoneWithPending) { | |
1503 MockHttpCache cache; | |
1504 | |
1505 // The headers will be served right from the call to Start() the request. | |
1506 MockHttpRequest request(kFastNoStoreGET_Transaction); | |
1507 FastTransactionServer request_handler; | |
1508 AddMockTransaction(&kFastNoStoreGET_Transaction); | |
1509 | |
1510 std::vector<Context*> context_list; | |
1511 const int kNumTransactions = 3; | |
1512 | |
1513 for (int i = 0; i < kNumTransactions; ++i) { | |
1514 context_list.push_back(new Context()); | |
1515 Context* c = context_list[i]; | |
1516 | |
1517 c->result = cache.CreateTransaction(&c->trans); | |
1518 ASSERT_EQ(net::OK, c->result); | |
1519 | |
1520 c->result = c->trans->Start( | |
1521 &request, c->callback.callback(), net::BoundNetLog()); | |
1522 } | |
1523 | |
1524 // Allow all requests to move from the Create queue to the active entry. | |
1525 base::MessageLoop::current()->RunUntilIdle(); | |
1526 | |
1527 // The first request should be a writer at this point, and the subsequent | |
1528 // requests should be pending. | |
1529 | |
1530 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1531 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1532 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1533 | |
1534 // Now, make sure that the second request asks for the entry not to be stored. | |
1535 request_handler.set_no_store(true); | |
1536 | |
1537 for (int i = 0; i < kNumTransactions; ++i) { | |
1538 Context* c = context_list[i]; | |
1539 if (c->result == net::ERR_IO_PENDING) | |
1540 c->result = c->callback.WaitForResult(); | |
1541 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); | |
1542 delete c; | |
1543 } | |
1544 | |
1545 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
1546 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1547 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
1548 | |
1549 RemoveMockTransaction(&kFastNoStoreGET_Transaction); | |
1550 } | |
1551 | |
1552 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { | |
1553 MockHttpCache cache; | |
1554 | |
1555 MockHttpRequest request(kSimpleGET_Transaction); | |
1556 | |
1557 std::vector<Context*> context_list; | |
1558 const int kNumTransactions = 2; | |
1559 | |
1560 for (int i = 0; i < kNumTransactions; ++i) { | |
1561 context_list.push_back(new Context()); | |
1562 Context* c = context_list[i]; | |
1563 | |
1564 c->result = cache.CreateTransaction(&c->trans); | |
1565 ASSERT_EQ(net::OK, c->result); | |
1566 | |
1567 c->result = c->trans->Start( | |
1568 &request, c->callback.callback(), net::BoundNetLog()); | |
1569 } | |
1570 | |
1571 // Allow all requests to move from the Create queue to the active entry. | |
1572 base::MessageLoop::current()->RunUntilIdle(); | |
1573 | |
1574 // The first request should be a writer at this point, and the subsequent | |
1575 // requests should be pending. | |
1576 | |
1577 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1578 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1579 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1580 | |
1581 for (int i = 0; i < kNumTransactions; ++i) { | |
1582 Context* c = context_list[i]; | |
1583 if (c->result == net::ERR_IO_PENDING) | |
1584 c->result = c->callback.WaitForResult(); | |
1585 // Destroy only the first transaction. | |
1586 if (i == 0) { | |
1587 delete c; | |
1588 context_list[i] = NULL; | |
1589 } | |
1590 } | |
1591 | |
1592 // Complete the rest of the transactions. | |
1593 for (int i = 1; i < kNumTransactions; ++i) { | |
1594 Context* c = context_list[i]; | |
1595 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | |
1596 } | |
1597 | |
1598 // We should have had to re-open the disk entry. | |
1599 | |
1600 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
1601 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1602 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
1603 | |
1604 for (int i = 1; i < kNumTransactions; ++i) { | |
1605 Context* c = context_list[i]; | |
1606 delete c; | |
1607 } | |
1608 } | |
1609 | |
1610 // Tests that we can cancel requests that are queued waiting to open the disk | |
1611 // cache entry. | |
1612 TEST(HttpCache, SimpleGET_ManyWriters_CancelCreate) { | |
1613 MockHttpCache cache; | |
1614 | |
1615 MockHttpRequest request(kSimpleGET_Transaction); | |
1616 | |
1617 std::vector<Context*> context_list; | |
1618 const int kNumTransactions = 5; | |
1619 | |
1620 for (int i = 0; i < kNumTransactions; i++) { | |
1621 context_list.push_back(new Context()); | |
1622 Context* c = context_list[i]; | |
1623 | |
1624 c->result = cache.CreateTransaction(&c->trans); | |
1625 ASSERT_EQ(net::OK, c->result); | |
1626 | |
1627 c->result = c->trans->Start( | |
1628 &request, c->callback.callback(), net::BoundNetLog()); | |
1629 } | |
1630 | |
1631 // The first request should be creating the disk cache entry and the others | |
1632 // should be pending. | |
1633 | |
1634 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | |
1635 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1636 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1637 | |
1638 // Cancel a request from the pending queue. | |
1639 delete context_list[3]; | |
1640 context_list[3] = NULL; | |
1641 | |
1642 // Cancel the request that is creating the entry. This will force the pending | |
1643 // operations to restart. | |
1644 delete context_list[0]; | |
1645 context_list[0] = NULL; | |
1646 | |
1647 // Complete the rest of the transactions. | |
1648 for (int i = 1; i < kNumTransactions; i++) { | |
1649 Context* c = context_list[i]; | |
1650 if (c) { | |
1651 c->result = c->callback.GetResult(c->result); | |
1652 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | |
1653 } | |
1654 } | |
1655 | |
1656 // We should have had to re-create the disk entry. | |
1657 | |
1658 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1659 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1660 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
1661 | |
1662 for (int i = 1; i < kNumTransactions; ++i) { | |
1663 delete context_list[i]; | |
1664 } | |
1665 } | |
1666 | |
1667 // Tests that we can cancel a single request to open a disk cache entry. | |
1668 TEST(HttpCache, SimpleGET_CancelCreate) { | |
1669 MockHttpCache cache; | |
1670 | |
1671 MockHttpRequest request(kSimpleGET_Transaction); | |
1672 | |
1673 Context* c = new Context(); | |
1674 | |
1675 c->result = cache.CreateTransaction(&c->trans); | |
1676 ASSERT_EQ(net::OK, c->result); | |
1677 | |
1678 c->result = c->trans->Start( | |
1679 &request, c->callback.callback(), net::BoundNetLog()); | |
1680 EXPECT_EQ(net::ERR_IO_PENDING, c->result); | |
1681 | |
1682 // Release the reference that the mock disk cache keeps for this entry, so | |
1683 // that we test that the http cache handles the cancellation correctly. | |
1684 cache.disk_cache()->ReleaseAll(); | |
1685 delete c; | |
1686 | |
1687 base::MessageLoop::current()->RunUntilIdle(); | |
1688 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1689 } | |
1690 | |
1691 // Tests that we delete/create entries even if multiple requests are queued. | |
1692 TEST(HttpCache, SimpleGET_ManyWriters_BypassCache) { | |
1693 MockHttpCache cache; | |
1694 | |
1695 MockHttpRequest request(kSimpleGET_Transaction); | |
1696 request.load_flags = net::LOAD_BYPASS_CACHE; | |
1697 | |
1698 std::vector<Context*> context_list; | |
1699 const int kNumTransactions = 5; | |
1700 | |
1701 for (int i = 0; i < kNumTransactions; i++) { | |
1702 context_list.push_back(new Context()); | |
1703 Context* c = context_list[i]; | |
1704 | |
1705 c->result = cache.CreateTransaction(&c->trans); | |
1706 ASSERT_EQ(net::OK, c->result); | |
1707 | |
1708 c->result = c->trans->Start( | |
1709 &request, c->callback.callback(), net::BoundNetLog()); | |
1710 } | |
1711 | |
1712 // The first request should be deleting the disk cache entry and the others | |
1713 // should be pending. | |
1714 | |
1715 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | |
1716 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1717 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
1718 | |
1719 // Complete the transactions. | |
1720 for (int i = 0; i < kNumTransactions; i++) { | |
1721 Context* c = context_list[i]; | |
1722 c->result = c->callback.GetResult(c->result); | |
1723 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | |
1724 } | |
1725 | |
1726 // We should have had to re-create the disk entry multiple times. | |
1727 | |
1728 EXPECT_EQ(5, cache.network_layer()->transaction_count()); | |
1729 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
1730 EXPECT_EQ(5, cache.disk_cache()->create_count()); | |
1731 | |
1732 for (int i = 0; i < kNumTransactions; ++i) { | |
1733 delete context_list[i]; | |
1734 } | |
1735 } | |
1736 | |
1737 // Tests that a (simulated) timeout allows transactions waiting on the cache | |
1738 // lock to continue. | |
1739 TEST(HttpCache, SimpleGET_WriterTimeout) { | |
1740 MockHttpCache cache; | |
1741 cache.BypassCacheLock(); | |
1742 | |
1743 MockHttpRequest request(kSimpleGET_Transaction); | |
1744 Context c1, c2; | |
1745 ASSERT_EQ(net::OK, cache.CreateTransaction(&c1.trans)); | |
1746 ASSERT_EQ(net::ERR_IO_PENDING, | |
1747 c1.trans->Start(&request, c1.callback.callback(), | |
1748 net::BoundNetLog())); | |
1749 ASSERT_EQ(net::OK, cache.CreateTransaction(&c2.trans)); | |
1750 ASSERT_EQ(net::ERR_IO_PENDING, | |
1751 c2.trans->Start(&request, c2.callback.callback(), | |
1752 net::BoundNetLog())); | |
1753 | |
1754 // The second request is queued after the first one. | |
1755 | |
1756 c2.callback.WaitForResult(); | |
1757 ReadAndVerifyTransaction(c2.trans.get(), kSimpleGET_Transaction); | |
1758 | |
1759 // Complete the first transaction. | |
1760 c1.callback.WaitForResult(); | |
1761 ReadAndVerifyTransaction(c1.trans.get(), kSimpleGET_Transaction); | |
1762 } | |
1763 | |
1764 TEST(HttpCache, SimpleGET_AbandonedCacheRead) { | |
1765 MockHttpCache cache; | |
1766 | |
1767 // write to the cache | |
1768 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
1769 | |
1770 MockHttpRequest request(kSimpleGET_Transaction); | |
1771 net::TestCompletionCallback callback; | |
1772 | |
1773 scoped_ptr<net::HttpTransaction> trans; | |
1774 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
1775 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
1776 if (rv == net::ERR_IO_PENDING) | |
1777 rv = callback.WaitForResult(); | |
1778 ASSERT_EQ(net::OK, rv); | |
1779 | |
1780 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); | |
1781 rv = trans->Read(buf.get(), 256, callback.callback()); | |
1782 EXPECT_EQ(net::ERR_IO_PENDING, rv); | |
1783 | |
1784 // Test that destroying the transaction while it is reading from the cache | |
1785 // works properly. | |
1786 trans.reset(); | |
1787 | |
1788 // Make sure we pump any pending events, which should include a call to | |
1789 // HttpCache::Transaction::OnCacheReadCompleted. | |
1790 base::MessageLoop::current()->RunUntilIdle(); | |
1791 } | |
1792 | |
1793 // Tests that we can delete the HttpCache and deal with queued transactions | |
1794 // ("waiting for the backend" as opposed to Active or Doomed entries). | |
1795 TEST(HttpCache, SimpleGET_ManyWriters_DeleteCache) { | |
1796 scoped_ptr<MockHttpCache> cache(new MockHttpCache( | |
1797 new MockBackendNoCbFactory())); | |
1798 | |
1799 MockHttpRequest request(kSimpleGET_Transaction); | |
1800 | |
1801 std::vector<Context*> context_list; | |
1802 const int kNumTransactions = 5; | |
1803 | |
1804 for (int i = 0; i < kNumTransactions; i++) { | |
1805 context_list.push_back(new Context()); | |
1806 Context* c = context_list[i]; | |
1807 | |
1808 c->result = cache->CreateTransaction(&c->trans); | |
1809 ASSERT_EQ(net::OK, c->result); | |
1810 | |
1811 c->result = c->trans->Start( | |
1812 &request, c->callback.callback(), net::BoundNetLog()); | |
1813 } | |
1814 | |
1815 // The first request should be creating the disk cache entry and the others | |
1816 // should be pending. | |
1817 | |
1818 EXPECT_EQ(0, cache->network_layer()->transaction_count()); | |
1819 EXPECT_EQ(0, cache->disk_cache()->open_count()); | |
1820 EXPECT_EQ(0, cache->disk_cache()->create_count()); | |
1821 | |
1822 cache.reset(); | |
1823 | |
1824 // There is not much to do with the transactions at this point... they are | |
1825 // waiting for a callback that will not fire. | |
1826 for (int i = 0; i < kNumTransactions; ++i) { | |
1827 delete context_list[i]; | |
1828 } | |
1829 } | |
1830 | |
1831 // Tests that we queue requests when initializing the backend. | |
1832 TEST(HttpCache, SimpleGET_WaitForBackend) { | |
1833 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | |
1834 MockHttpCache cache(factory); | |
1835 | |
1836 MockHttpRequest request0(kSimpleGET_Transaction); | |
1837 MockHttpRequest request1(kTypicalGET_Transaction); | |
1838 MockHttpRequest request2(kETagGET_Transaction); | |
1839 | |
1840 std::vector<Context*> context_list; | |
1841 const int kNumTransactions = 3; | |
1842 | |
1843 for (int i = 0; i < kNumTransactions; i++) { | |
1844 context_list.push_back(new Context()); | |
1845 Context* c = context_list[i]; | |
1846 | |
1847 c->result = cache.CreateTransaction(&c->trans); | |
1848 ASSERT_EQ(net::OK, c->result); | |
1849 } | |
1850 | |
1851 context_list[0]->result = context_list[0]->trans->Start( | |
1852 &request0, context_list[0]->callback.callback(), net::BoundNetLog()); | |
1853 context_list[1]->result = context_list[1]->trans->Start( | |
1854 &request1, context_list[1]->callback.callback(), net::BoundNetLog()); | |
1855 context_list[2]->result = context_list[2]->trans->Start( | |
1856 &request2, context_list[2]->callback.callback(), net::BoundNetLog()); | |
1857 | |
1858 // Just to make sure that everything is still pending. | |
1859 base::MessageLoop::current()->RunUntilIdle(); | |
1860 | |
1861 // The first request should be creating the disk cache. | |
1862 EXPECT_FALSE(context_list[0]->callback.have_result()); | |
1863 | |
1864 factory->FinishCreation(); | |
1865 | |
1866 base::MessageLoop::current()->RunUntilIdle(); | |
1867 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
1868 EXPECT_EQ(3, cache.disk_cache()->create_count()); | |
1869 | |
1870 for (int i = 0; i < kNumTransactions; ++i) { | |
1871 EXPECT_TRUE(context_list[i]->callback.have_result()); | |
1872 delete context_list[i]; | |
1873 } | |
1874 } | |
1875 | |
1876 // Tests that we can cancel requests that are queued waiting for the backend | |
1877 // to be initialized. | |
1878 TEST(HttpCache, SimpleGET_WaitForBackend_CancelCreate) { | |
1879 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | |
1880 MockHttpCache cache(factory); | |
1881 | |
1882 MockHttpRequest request0(kSimpleGET_Transaction); | |
1883 MockHttpRequest request1(kTypicalGET_Transaction); | |
1884 MockHttpRequest request2(kETagGET_Transaction); | |
1885 | |
1886 std::vector<Context*> context_list; | |
1887 const int kNumTransactions = 3; | |
1888 | |
1889 for (int i = 0; i < kNumTransactions; i++) { | |
1890 context_list.push_back(new Context()); | |
1891 Context* c = context_list[i]; | |
1892 | |
1893 c->result = cache.CreateTransaction(&c->trans); | |
1894 ASSERT_EQ(net::OK, c->result); | |
1895 } | |
1896 | |
1897 context_list[0]->result = context_list[0]->trans->Start( | |
1898 &request0, context_list[0]->callback.callback(), net::BoundNetLog()); | |
1899 context_list[1]->result = context_list[1]->trans->Start( | |
1900 &request1, context_list[1]->callback.callback(), net::BoundNetLog()); | |
1901 context_list[2]->result = context_list[2]->trans->Start( | |
1902 &request2, context_list[2]->callback.callback(), net::BoundNetLog()); | |
1903 | |
1904 // Just to make sure that everything is still pending. | |
1905 base::MessageLoop::current()->RunUntilIdle(); | |
1906 | |
1907 // The first request should be creating the disk cache. | |
1908 EXPECT_FALSE(context_list[0]->callback.have_result()); | |
1909 | |
1910 // Cancel a request from the pending queue. | |
1911 delete context_list[1]; | |
1912 context_list[1] = NULL; | |
1913 | |
1914 // Cancel the request that is creating the entry. | |
1915 delete context_list[0]; | |
1916 context_list[0] = NULL; | |
1917 | |
1918 // Complete the last transaction. | |
1919 factory->FinishCreation(); | |
1920 | |
1921 context_list[2]->result = | |
1922 context_list[2]->callback.GetResult(context_list[2]->result); | |
1923 ReadAndVerifyTransaction(context_list[2]->trans.get(), kETagGET_Transaction); | |
1924 | |
1925 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
1926 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
1927 | |
1928 delete context_list[2]; | |
1929 } | |
1930 | |
1931 // Tests that we can delete the cache while creating the backend. | |
1932 TEST(HttpCache, DeleteCacheWaitingForBackend) { | |
1933 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | |
1934 scoped_ptr<MockHttpCache> cache(new MockHttpCache(factory)); | |
1935 | |
1936 MockHttpRequest request(kSimpleGET_Transaction); | |
1937 | |
1938 scoped_ptr<Context> c(new Context()); | |
1939 c->result = cache->CreateTransaction(&c->trans); | |
1940 ASSERT_EQ(net::OK, c->result); | |
1941 | |
1942 c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
1943 | |
1944 // Just to make sure that everything is still pending. | |
1945 base::MessageLoop::current()->RunUntilIdle(); | |
1946 | |
1947 // The request should be creating the disk cache. | |
1948 EXPECT_FALSE(c->callback.have_result()); | |
1949 | |
1950 // We cannot call FinishCreation because the factory itself will go away with | |
1951 // the cache, so grab the callback and attempt to use it. | |
1952 net::CompletionCallback callback = factory->callback(); | |
1953 scoped_ptr<disk_cache::Backend>* backend = factory->backend(); | |
1954 | |
1955 cache.reset(); | |
1956 base::MessageLoop::current()->RunUntilIdle(); | |
1957 | |
1958 backend->reset(); | |
1959 callback.Run(net::ERR_ABORTED); | |
1960 } | |
1961 | |
1962 // Tests that we can delete the cache while creating the backend, from within | |
1963 // one of the callbacks. | |
1964 TEST(HttpCache, DeleteCacheWaitingForBackend2) { | |
1965 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | |
1966 MockHttpCache* cache = new MockHttpCache(factory); | |
1967 | |
1968 DeleteCacheCompletionCallback cb(cache); | |
1969 disk_cache::Backend* backend; | |
1970 int rv = cache->http_cache()->GetBackend(&backend, cb.callback()); | |
1971 EXPECT_EQ(net::ERR_IO_PENDING, rv); | |
1972 | |
1973 // Now let's queue a regular transaction | |
1974 MockHttpRequest request(kSimpleGET_Transaction); | |
1975 | |
1976 scoped_ptr<Context> c(new Context()); | |
1977 c->result = cache->CreateTransaction(&c->trans); | |
1978 ASSERT_EQ(net::OK, c->result); | |
1979 | |
1980 c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
1981 | |
1982 // And another direct backend request. | |
1983 net::TestCompletionCallback cb2; | |
1984 rv = cache->http_cache()->GetBackend(&backend, cb2.callback()); | |
1985 EXPECT_EQ(net::ERR_IO_PENDING, rv); | |
1986 | |
1987 // Just to make sure that everything is still pending. | |
1988 base::MessageLoop::current()->RunUntilIdle(); | |
1989 | |
1990 // The request should be queued. | |
1991 EXPECT_FALSE(c->callback.have_result()); | |
1992 | |
1993 // Generate the callback. | |
1994 factory->FinishCreation(); | |
1995 rv = cb.WaitForResult(); | |
1996 | |
1997 // The cache should be gone by now. | |
1998 base::MessageLoop::current()->RunUntilIdle(); | |
1999 EXPECT_EQ(net::OK, c->callback.GetResult(c->result)); | |
2000 EXPECT_FALSE(cb2.have_result()); | |
2001 } | |
2002 | |
2003 TEST(HttpCache, TypicalGET_ConditionalRequest) { | |
2004 MockHttpCache cache; | |
2005 | |
2006 // write to the cache | |
2007 RunTransactionTest(cache.http_cache(), kTypicalGET_Transaction); | |
2008 | |
2009 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2010 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2011 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2012 | |
2013 // Get the same URL again, but this time we expect it to result | |
2014 // in a conditional request. | |
2015 net::CapturingBoundNetLog log; | |
2016 net::LoadTimingInfo load_timing_info; | |
2017 RunTransactionTestAndGetTiming(cache.http_cache(), kTypicalGET_Transaction, | |
2018 log.bound(), &load_timing_info); | |
2019 | |
2020 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2021 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2022 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2023 TestLoadTimingNetworkRequest(load_timing_info); | |
2024 } | |
2025 | |
2026 static void ETagGet_ConditionalRequest_Handler( | |
2027 const net::HttpRequestInfo* request, | |
2028 std::string* response_status, | |
2029 std::string* response_headers, | |
2030 std::string* response_data) { | |
2031 EXPECT_TRUE( | |
2032 request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch)); | |
2033 response_status->assign("HTTP/1.1 304 Not Modified"); | |
2034 response_headers->assign(kETagGET_Transaction.response_headers); | |
2035 response_data->clear(); | |
2036 } | |
2037 | |
2038 TEST(HttpCache, ETagGET_ConditionalRequest_304) { | |
2039 MockHttpCache cache; | |
2040 | |
2041 ScopedMockTransaction transaction(kETagGET_Transaction); | |
2042 | |
2043 // write to the cache | |
2044 RunTransactionTest(cache.http_cache(), transaction); | |
2045 | |
2046 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2047 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2048 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2049 | |
2050 // Get the same URL again, but this time we expect it to result | |
2051 // in a conditional request. | |
2052 transaction.load_flags = net::LOAD_VALIDATE_CACHE; | |
2053 transaction.handler = ETagGet_ConditionalRequest_Handler; | |
2054 net::CapturingBoundNetLog log; | |
2055 net::LoadTimingInfo load_timing_info; | |
2056 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), | |
2057 &load_timing_info); | |
2058 | |
2059 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2060 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2061 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2062 TestLoadTimingNetworkRequest(load_timing_info); | |
2063 } | |
2064 | |
2065 class RevalidationServer { | |
2066 public: | |
2067 RevalidationServer() { | |
2068 s_etag_used_ = false; | |
2069 s_last_modified_used_ = false; | |
2070 } | |
2071 | |
2072 bool EtagUsed() { return s_etag_used_; } | |
2073 bool LastModifiedUsed() { return s_last_modified_used_; } | |
2074 | |
2075 static void Handler(const net::HttpRequestInfo* request, | |
2076 std::string* response_status, | |
2077 std::string* response_headers, | |
2078 std::string* response_data); | |
2079 | |
2080 private: | |
2081 static bool s_etag_used_; | |
2082 static bool s_last_modified_used_; | |
2083 }; | |
2084 bool RevalidationServer::s_etag_used_ = false; | |
2085 bool RevalidationServer::s_last_modified_used_ = false; | |
2086 | |
2087 void RevalidationServer::Handler(const net::HttpRequestInfo* request, | |
2088 std::string* response_status, | |
2089 std::string* response_headers, | |
2090 std::string* response_data) { | |
2091 if (request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch)) | |
2092 s_etag_used_ = true; | |
2093 | |
2094 if (request->extra_headers.HasHeader( | |
2095 net::HttpRequestHeaders::kIfModifiedSince)) { | |
2096 s_last_modified_used_ = true; | |
2097 } | |
2098 | |
2099 if (s_etag_used_ || s_last_modified_used_) { | |
2100 response_status->assign("HTTP/1.1 304 Not Modified"); | |
2101 response_headers->assign(kTypicalGET_Transaction.response_headers); | |
2102 response_data->clear(); | |
2103 } else { | |
2104 response_status->assign(kTypicalGET_Transaction.status); | |
2105 response_headers->assign(kTypicalGET_Transaction.response_headers); | |
2106 response_data->assign(kTypicalGET_Transaction.data); | |
2107 } | |
2108 } | |
2109 | |
2110 // Tests revalidation after a vary match. | |
2111 TEST(HttpCache, SimpleGET_LoadValidateCache_VaryMatch) { | |
2112 MockHttpCache cache; | |
2113 | |
2114 // Write to the cache. | |
2115 MockTransaction transaction(kTypicalGET_Transaction); | |
2116 transaction.request_headers = "Foo: bar\r\n"; | |
2117 transaction.response_headers = | |
2118 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | |
2119 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | |
2120 "Etag: \"foopy\"\n" | |
2121 "Cache-Control: max-age=0\n" | |
2122 "Vary: Foo\n"; | |
2123 AddMockTransaction(&transaction); | |
2124 RunTransactionTest(cache.http_cache(), transaction); | |
2125 | |
2126 // Read from the cache. | |
2127 RevalidationServer server; | |
2128 transaction.handler = server.Handler; | |
2129 net::CapturingBoundNetLog log; | |
2130 net::LoadTimingInfo load_timing_info; | |
2131 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), | |
2132 &load_timing_info); | |
2133 | |
2134 EXPECT_TRUE(server.EtagUsed()); | |
2135 EXPECT_TRUE(server.LastModifiedUsed()); | |
2136 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2137 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2138 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2139 TestLoadTimingNetworkRequest(load_timing_info); | |
2140 RemoveMockTransaction(&transaction); | |
2141 } | |
2142 | |
2143 // Tests revalidation after a vary mismatch if etag is present. | |
2144 TEST(HttpCache, SimpleGET_LoadValidateCache_VaryMismatch) { | |
2145 MockHttpCache cache; | |
2146 | |
2147 // Write to the cache. | |
2148 MockTransaction transaction(kTypicalGET_Transaction); | |
2149 transaction.request_headers = "Foo: bar\r\n"; | |
2150 transaction.response_headers = | |
2151 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | |
2152 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | |
2153 "Etag: \"foopy\"\n" | |
2154 "Cache-Control: max-age=0\n" | |
2155 "Vary: Foo\n"; | |
2156 AddMockTransaction(&transaction); | |
2157 RunTransactionTest(cache.http_cache(), transaction); | |
2158 | |
2159 // Read from the cache and revalidate the entry. | |
2160 RevalidationServer server; | |
2161 transaction.handler = server.Handler; | |
2162 transaction.request_headers = "Foo: none\r\n"; | |
2163 net::CapturingBoundNetLog log; | |
2164 net::LoadTimingInfo load_timing_info; | |
2165 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), | |
2166 &load_timing_info); | |
2167 | |
2168 EXPECT_TRUE(server.EtagUsed()); | |
2169 EXPECT_FALSE(server.LastModifiedUsed()); | |
2170 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2171 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2172 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2173 TestLoadTimingNetworkRequest(load_timing_info); | |
2174 RemoveMockTransaction(&transaction); | |
2175 } | |
2176 | |
2177 // Tests lack of revalidation after a vary mismatch and no etag. | |
2178 TEST(HttpCache, SimpleGET_LoadDontValidateCache_VaryMismatch) { | |
2179 MockHttpCache cache; | |
2180 | |
2181 // Write to the cache. | |
2182 MockTransaction transaction(kTypicalGET_Transaction); | |
2183 transaction.request_headers = "Foo: bar\r\n"; | |
2184 transaction.response_headers = | |
2185 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | |
2186 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | |
2187 "Cache-Control: max-age=0\n" | |
2188 "Vary: Foo\n"; | |
2189 AddMockTransaction(&transaction); | |
2190 RunTransactionTest(cache.http_cache(), transaction); | |
2191 | |
2192 // Read from the cache and don't revalidate the entry. | |
2193 RevalidationServer server; | |
2194 transaction.handler = server.Handler; | |
2195 transaction.request_headers = "Foo: none\r\n"; | |
2196 net::CapturingBoundNetLog log; | |
2197 net::LoadTimingInfo load_timing_info; | |
2198 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), | |
2199 &load_timing_info); | |
2200 | |
2201 EXPECT_FALSE(server.EtagUsed()); | |
2202 EXPECT_FALSE(server.LastModifiedUsed()); | |
2203 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2204 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2205 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2206 TestLoadTimingNetworkRequest(load_timing_info); | |
2207 RemoveMockTransaction(&transaction); | |
2208 } | |
2209 | |
2210 static void ETagGet_UnconditionalRequest_Handler( | |
2211 const net::HttpRequestInfo* request, | |
2212 std::string* response_status, | |
2213 std::string* response_headers, | |
2214 std::string* response_data) { | |
2215 EXPECT_FALSE( | |
2216 request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch)); | |
2217 } | |
2218 | |
2219 TEST(HttpCache, ETagGET_Http10) { | |
2220 MockHttpCache cache; | |
2221 | |
2222 ScopedMockTransaction transaction(kETagGET_Transaction); | |
2223 transaction.status = "HTTP/1.0 200 OK"; | |
2224 | |
2225 // Write to the cache. | |
2226 RunTransactionTest(cache.http_cache(), transaction); | |
2227 | |
2228 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2229 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2230 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2231 | |
2232 // Get the same URL again, without generating a conditional request. | |
2233 transaction.load_flags = net::LOAD_VALIDATE_CACHE; | |
2234 transaction.handler = ETagGet_UnconditionalRequest_Handler; | |
2235 RunTransactionTest(cache.http_cache(), transaction); | |
2236 | |
2237 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2238 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2239 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2240 } | |
2241 | |
2242 TEST(HttpCache, ETagGET_Http10_Range) { | |
2243 MockHttpCache cache; | |
2244 | |
2245 ScopedMockTransaction transaction(kETagGET_Transaction); | |
2246 transaction.status = "HTTP/1.0 200 OK"; | |
2247 | |
2248 // Write to the cache. | |
2249 RunTransactionTest(cache.http_cache(), transaction); | |
2250 | |
2251 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2252 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2253 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2254 | |
2255 // Get the same URL again, but use a byte range request. | |
2256 transaction.load_flags = net::LOAD_VALIDATE_CACHE; | |
2257 transaction.handler = ETagGet_UnconditionalRequest_Handler; | |
2258 transaction.request_headers = "Range: bytes = 5-\r\n"; | |
2259 RunTransactionTest(cache.http_cache(), transaction); | |
2260 | |
2261 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2262 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2263 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
2264 } | |
2265 | |
2266 static void ETagGet_ConditionalRequest_NoStore_Handler( | |
2267 const net::HttpRequestInfo* request, | |
2268 std::string* response_status, | |
2269 std::string* response_headers, | |
2270 std::string* response_data) { | |
2271 EXPECT_TRUE( | |
2272 request->extra_headers.HasHeader(net::HttpRequestHeaders::kIfNoneMatch)); | |
2273 response_status->assign("HTTP/1.1 304 Not Modified"); | |
2274 response_headers->assign("Cache-Control: no-store\n"); | |
2275 response_data->clear(); | |
2276 } | |
2277 | |
2278 TEST(HttpCache, ETagGET_ConditionalRequest_304_NoStore) { | |
2279 MockHttpCache cache; | |
2280 | |
2281 ScopedMockTransaction transaction(kETagGET_Transaction); | |
2282 | |
2283 // Write to the cache. | |
2284 RunTransactionTest(cache.http_cache(), transaction); | |
2285 | |
2286 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2287 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2288 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2289 | |
2290 // Get the same URL again, but this time we expect it to result | |
2291 // in a conditional request. | |
2292 transaction.load_flags = net::LOAD_VALIDATE_CACHE; | |
2293 transaction.handler = ETagGet_ConditionalRequest_NoStore_Handler; | |
2294 RunTransactionTest(cache.http_cache(), transaction); | |
2295 | |
2296 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2297 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2298 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2299 | |
2300 ScopedMockTransaction transaction2(kETagGET_Transaction); | |
2301 | |
2302 // Write to the cache again. This should create a new entry. | |
2303 RunTransactionTest(cache.http_cache(), transaction2); | |
2304 | |
2305 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
2306 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2307 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
2308 } | |
2309 | |
2310 // Helper that does 4 requests using HttpCache: | |
2311 // | |
2312 // (1) loads |kUrl| -- expects |net_response_1| to be returned. | |
2313 // (2) loads |kUrl| from cache only -- expects |net_response_1| to be returned. | |
2314 // (3) loads |kUrl| using |extra_request_headers| -- expects |net_response_2| to | |
2315 // be returned. | |
2316 // (4) loads |kUrl| from cache only -- expects |cached_response_2| to be | |
2317 // returned. | |
2318 static void ConditionalizedRequestUpdatesCacheHelper( | |
2319 const Response& net_response_1, | |
2320 const Response& net_response_2, | |
2321 const Response& cached_response_2, | |
2322 const char* extra_request_headers) { | |
2323 MockHttpCache cache; | |
2324 | |
2325 // The URL we will be requesting. | |
2326 const char kUrl[] = "http://foobar.com/main.css"; | |
2327 | |
2328 // Junk network response. | |
2329 static const Response kUnexpectedResponse = { | |
2330 "HTTP/1.1 500 Unexpected", | |
2331 "Server: unexpected_header", | |
2332 "unexpected body" | |
2333 }; | |
2334 | |
2335 // We will control the network layer's responses for |kUrl| using | |
2336 // |mock_network_response|. | |
2337 MockTransaction mock_network_response = { 0 }; | |
2338 mock_network_response.url = kUrl; | |
2339 AddMockTransaction(&mock_network_response); | |
2340 | |
2341 // Request |kUrl| for the first time. It should hit the network and | |
2342 // receive |kNetResponse1|, which it saves into the HTTP cache. | |
2343 | |
2344 MockTransaction request = { 0 }; | |
2345 request.url = kUrl; | |
2346 request.method = "GET"; | |
2347 request.request_headers = ""; | |
2348 | |
2349 net_response_1.AssignTo(&mock_network_response); // Network mock. | |
2350 net_response_1.AssignTo(&request); // Expected result. | |
2351 | |
2352 std::string response_headers; | |
2353 RunTransactionTestWithResponse( | |
2354 cache.http_cache(), request, &response_headers); | |
2355 | |
2356 EXPECT_EQ(net_response_1.status_and_headers(), response_headers); | |
2357 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2358 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2359 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2360 | |
2361 // Request |kUrl| a second time. Now |kNetResponse1| it is in the HTTP | |
2362 // cache, so we don't hit the network. | |
2363 | |
2364 request.load_flags = net::LOAD_ONLY_FROM_CACHE; | |
2365 | |
2366 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. | |
2367 net_response_1.AssignTo(&request); // Expected result. | |
2368 | |
2369 RunTransactionTestWithResponse( | |
2370 cache.http_cache(), request, &response_headers); | |
2371 | |
2372 EXPECT_EQ(net_response_1.status_and_headers(), response_headers); | |
2373 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2374 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2375 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2376 | |
2377 // Request |kUrl| yet again, but this time give the request an | |
2378 // "If-Modified-Since" header. This will cause the request to re-hit the | |
2379 // network. However now the network response is going to be | |
2380 // different -- this simulates a change made to the CSS file. | |
2381 | |
2382 request.request_headers = extra_request_headers; | |
2383 request.load_flags = net::LOAD_NORMAL; | |
2384 | |
2385 net_response_2.AssignTo(&mock_network_response); // Network mock. | |
2386 net_response_2.AssignTo(&request); // Expected result. | |
2387 | |
2388 RunTransactionTestWithResponse( | |
2389 cache.http_cache(), request, &response_headers); | |
2390 | |
2391 EXPECT_EQ(net_response_2.status_and_headers(), response_headers); | |
2392 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2393 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2394 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2395 | |
2396 // Finally, request |kUrl| again. This request should be serviced from | |
2397 // the cache. Moreover, the value in the cache should be |kNetResponse2| | |
2398 // and NOT |kNetResponse1|. The previous step should have replaced the | |
2399 // value in the cache with the modified response. | |
2400 | |
2401 request.request_headers = ""; | |
2402 request.load_flags = net::LOAD_ONLY_FROM_CACHE; | |
2403 | |
2404 kUnexpectedResponse.AssignTo(&mock_network_response); // Network mock. | |
2405 cached_response_2.AssignTo(&request); // Expected result. | |
2406 | |
2407 RunTransactionTestWithResponse( | |
2408 cache.http_cache(), request, &response_headers); | |
2409 | |
2410 EXPECT_EQ(cached_response_2.status_and_headers(), response_headers); | |
2411 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2412 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
2413 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2414 | |
2415 RemoveMockTransaction(&mock_network_response); | |
2416 } | |
2417 | |
2418 // Check that when an "if-modified-since" header is attached | |
2419 // to the request, the result still updates the cached entry. | |
2420 TEST(HttpCache, ConditionalizedRequestUpdatesCache1) { | |
2421 // First network response for |kUrl|. | |
2422 static const Response kNetResponse1 = { | |
2423 "HTTP/1.1 200 OK", | |
2424 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" | |
2425 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2426 "body1" | |
2427 }; | |
2428 | |
2429 // Second network response for |kUrl|. | |
2430 static const Response kNetResponse2 = { | |
2431 "HTTP/1.1 200 OK", | |
2432 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | |
2433 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", | |
2434 "body2" | |
2435 }; | |
2436 | |
2437 const char extra_headers[] = | |
2438 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; | |
2439 | |
2440 ConditionalizedRequestUpdatesCacheHelper( | |
2441 kNetResponse1, kNetResponse2, kNetResponse2, extra_headers); | |
2442 } | |
2443 | |
2444 // Check that when an "if-none-match" header is attached | |
2445 // to the request, the result updates the cached entry. | |
2446 TEST(HttpCache, ConditionalizedRequestUpdatesCache2) { | |
2447 // First network response for |kUrl|. | |
2448 static const Response kNetResponse1 = { | |
2449 "HTTP/1.1 200 OK", | |
2450 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" | |
2451 "Etag: \"ETAG1\"\n" | |
2452 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire. | |
2453 "body1" | |
2454 }; | |
2455 | |
2456 // Second network response for |kUrl|. | |
2457 static const Response kNetResponse2 = { | |
2458 "HTTP/1.1 200 OK", | |
2459 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | |
2460 "Etag: \"ETAG2\"\n" | |
2461 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n", // Should never expire. | |
2462 "body2" | |
2463 }; | |
2464 | |
2465 const char extra_headers[] = "If-None-Match: \"ETAG1\"\r\n"; | |
2466 | |
2467 ConditionalizedRequestUpdatesCacheHelper( | |
2468 kNetResponse1, kNetResponse2, kNetResponse2, extra_headers); | |
2469 } | |
2470 | |
2471 // Check that when an "if-modified-since" header is attached | |
2472 // to a request, the 304 (not modified result) result updates the cached | |
2473 // headers, and the 304 response is returned rather than the cached response. | |
2474 TEST(HttpCache, ConditionalizedRequestUpdatesCache3) { | |
2475 // First network response for |kUrl|. | |
2476 static const Response kNetResponse1 = { | |
2477 "HTTP/1.1 200 OK", | |
2478 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" | |
2479 "Server: server1\n" | |
2480 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2481 "body1" | |
2482 }; | |
2483 | |
2484 // Second network response for |kUrl|. | |
2485 static const Response kNetResponse2 = { | |
2486 "HTTP/1.1 304 Not Modified", | |
2487 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | |
2488 "Server: server2\n" | |
2489 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2490 "" | |
2491 }; | |
2492 | |
2493 static const Response kCachedResponse2 = { | |
2494 "HTTP/1.1 200 OK", | |
2495 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | |
2496 "Server: server2\n" | |
2497 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2498 "body1" | |
2499 }; | |
2500 | |
2501 const char extra_headers[] = | |
2502 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; | |
2503 | |
2504 ConditionalizedRequestUpdatesCacheHelper( | |
2505 kNetResponse1, kNetResponse2, kCachedResponse2, extra_headers); | |
2506 } | |
2507 | |
2508 // Test that when doing an externally conditionalized if-modified-since | |
2509 // and there is no corresponding cache entry, a new cache entry is NOT | |
2510 // created (304 response). | |
2511 TEST(HttpCache, ConditionalizedRequestUpdatesCache4) { | |
2512 MockHttpCache cache; | |
2513 | |
2514 const char kUrl[] = "http://foobar.com/main.css"; | |
2515 | |
2516 static const Response kNetResponse = { | |
2517 "HTTP/1.1 304 Not Modified", | |
2518 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | |
2519 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2520 "" | |
2521 }; | |
2522 | |
2523 const char kExtraRequestHeaders[] = | |
2524 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; | |
2525 | |
2526 // We will control the network layer's responses for |kUrl| using | |
2527 // |mock_network_response|. | |
2528 MockTransaction mock_network_response = { 0 }; | |
2529 mock_network_response.url = kUrl; | |
2530 AddMockTransaction(&mock_network_response); | |
2531 | |
2532 MockTransaction request = { 0 }; | |
2533 request.url = kUrl; | |
2534 request.method = "GET"; | |
2535 request.request_headers = kExtraRequestHeaders; | |
2536 | |
2537 kNetResponse.AssignTo(&mock_network_response); // Network mock. | |
2538 kNetResponse.AssignTo(&request); // Expected result. | |
2539 | |
2540 std::string response_headers; | |
2541 RunTransactionTestWithResponse( | |
2542 cache.http_cache(), request, &response_headers); | |
2543 | |
2544 EXPECT_EQ(kNetResponse.status_and_headers(), response_headers); | |
2545 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2546 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2547 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
2548 | |
2549 RemoveMockTransaction(&mock_network_response); | |
2550 } | |
2551 | |
2552 // Test that when doing an externally conditionalized if-modified-since | |
2553 // and there is no corresponding cache entry, a new cache entry is NOT | |
2554 // created (200 response). | |
2555 TEST(HttpCache, ConditionalizedRequestUpdatesCache5) { | |
2556 MockHttpCache cache; | |
2557 | |
2558 const char kUrl[] = "http://foobar.com/main.css"; | |
2559 | |
2560 static const Response kNetResponse = { | |
2561 "HTTP/1.1 200 OK", | |
2562 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | |
2563 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2564 "foobar!!!" | |
2565 }; | |
2566 | |
2567 const char kExtraRequestHeaders[] = | |
2568 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n"; | |
2569 | |
2570 // We will control the network layer's responses for |kUrl| using | |
2571 // |mock_network_response|. | |
2572 MockTransaction mock_network_response = { 0 }; | |
2573 mock_network_response.url = kUrl; | |
2574 AddMockTransaction(&mock_network_response); | |
2575 | |
2576 MockTransaction request = { 0 }; | |
2577 request.url = kUrl; | |
2578 request.method = "GET"; | |
2579 request.request_headers = kExtraRequestHeaders; | |
2580 | |
2581 kNetResponse.AssignTo(&mock_network_response); // Network mock. | |
2582 kNetResponse.AssignTo(&request); // Expected result. | |
2583 | |
2584 std::string response_headers; | |
2585 RunTransactionTestWithResponse( | |
2586 cache.http_cache(), request, &response_headers); | |
2587 | |
2588 EXPECT_EQ(kNetResponse.status_and_headers(), response_headers); | |
2589 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2590 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2591 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
2592 | |
2593 RemoveMockTransaction(&mock_network_response); | |
2594 } | |
2595 | |
2596 // Test that when doing an externally conditionalized if-modified-since | |
2597 // if the date does not match the cache entry's last-modified date, | |
2598 // then we do NOT use the response (304) to update the cache. | |
2599 // (the if-modified-since date is 2 days AFTER the cache's modification date). | |
2600 TEST(HttpCache, ConditionalizedRequestUpdatesCache6) { | |
2601 static const Response kNetResponse1 = { | |
2602 "HTTP/1.1 200 OK", | |
2603 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" | |
2604 "Server: server1\n" | |
2605 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2606 "body1" | |
2607 }; | |
2608 | |
2609 // Second network response for |kUrl|. | |
2610 static const Response kNetResponse2 = { | |
2611 "HTTP/1.1 304 Not Modified", | |
2612 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | |
2613 "Server: server2\n" | |
2614 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2615 "" | |
2616 }; | |
2617 | |
2618 // This is two days in the future from the original response's last-modified | |
2619 // date! | |
2620 const char kExtraRequestHeaders[] = | |
2621 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n"; | |
2622 | |
2623 ConditionalizedRequestUpdatesCacheHelper( | |
2624 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); | |
2625 } | |
2626 | |
2627 // Test that when doing an externally conditionalized if-none-match | |
2628 // if the etag does not match the cache entry's etag, then we do not use the | |
2629 // response (304) to update the cache. | |
2630 TEST(HttpCache, ConditionalizedRequestUpdatesCache7) { | |
2631 static const Response kNetResponse1 = { | |
2632 "HTTP/1.1 200 OK", | |
2633 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" | |
2634 "Etag: \"Foo1\"\n" | |
2635 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2636 "body1" | |
2637 }; | |
2638 | |
2639 // Second network response for |kUrl|. | |
2640 static const Response kNetResponse2 = { | |
2641 "HTTP/1.1 304 Not Modified", | |
2642 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | |
2643 "Etag: \"Foo2\"\n" | |
2644 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2645 "" | |
2646 }; | |
2647 | |
2648 // Different etag from original response. | |
2649 const char kExtraRequestHeaders[] = "If-None-Match: \"Foo2\"\r\n"; | |
2650 | |
2651 ConditionalizedRequestUpdatesCacheHelper( | |
2652 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); | |
2653 } | |
2654 | |
2655 // Test that doing an externally conditionalized request with both if-none-match | |
2656 // and if-modified-since updates the cache. | |
2657 TEST(HttpCache, ConditionalizedRequestUpdatesCache8) { | |
2658 static const Response kNetResponse1 = { | |
2659 "HTTP/1.1 200 OK", | |
2660 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" | |
2661 "Etag: \"Foo1\"\n" | |
2662 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2663 "body1" | |
2664 }; | |
2665 | |
2666 // Second network response for |kUrl|. | |
2667 static const Response kNetResponse2 = { | |
2668 "HTTP/1.1 200 OK", | |
2669 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | |
2670 "Etag: \"Foo2\"\n" | |
2671 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", | |
2672 "body2" | |
2673 }; | |
2674 | |
2675 const char kExtraRequestHeaders[] = | |
2676 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n" | |
2677 "If-None-Match: \"Foo1\"\r\n"; | |
2678 | |
2679 ConditionalizedRequestUpdatesCacheHelper( | |
2680 kNetResponse1, kNetResponse2, kNetResponse2, kExtraRequestHeaders); | |
2681 } | |
2682 | |
2683 // Test that doing an externally conditionalized request with both if-none-match | |
2684 // and if-modified-since does not update the cache with only one match. | |
2685 TEST(HttpCache, ConditionalizedRequestUpdatesCache9) { | |
2686 static const Response kNetResponse1 = { | |
2687 "HTTP/1.1 200 OK", | |
2688 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" | |
2689 "Etag: \"Foo1\"\n" | |
2690 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2691 "body1" | |
2692 }; | |
2693 | |
2694 // Second network response for |kUrl|. | |
2695 static const Response kNetResponse2 = { | |
2696 "HTTP/1.1 200 OK", | |
2697 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | |
2698 "Etag: \"Foo2\"\n" | |
2699 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", | |
2700 "body2" | |
2701 }; | |
2702 | |
2703 // The etag doesn't match what we have stored. | |
2704 const char kExtraRequestHeaders[] = | |
2705 "If-Modified-Since: Wed, 06 Feb 2008 22:38:21 GMT\r\n" | |
2706 "If-None-Match: \"Foo2\"\r\n"; | |
2707 | |
2708 ConditionalizedRequestUpdatesCacheHelper( | |
2709 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); | |
2710 } | |
2711 | |
2712 // Test that doing an externally conditionalized request with both if-none-match | |
2713 // and if-modified-since does not update the cache with only one match. | |
2714 TEST(HttpCache, ConditionalizedRequestUpdatesCache10) { | |
2715 static const Response kNetResponse1 = { | |
2716 "HTTP/1.1 200 OK", | |
2717 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" | |
2718 "Etag: \"Foo1\"\n" | |
2719 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
2720 "body1" | |
2721 }; | |
2722 | |
2723 // Second network response for |kUrl|. | |
2724 static const Response kNetResponse2 = { | |
2725 "HTTP/1.1 200 OK", | |
2726 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | |
2727 "Etag: \"Foo2\"\n" | |
2728 "Last-Modified: Fri, 03 Jul 2009 02:14:27 GMT\n", | |
2729 "body2" | |
2730 }; | |
2731 | |
2732 // The modification date doesn't match what we have stored. | |
2733 const char kExtraRequestHeaders[] = | |
2734 "If-Modified-Since: Fri, 08 Feb 2008 22:38:21 GMT\r\n" | |
2735 "If-None-Match: \"Foo1\"\r\n"; | |
2736 | |
2737 ConditionalizedRequestUpdatesCacheHelper( | |
2738 kNetResponse1, kNetResponse2, kNetResponse1, kExtraRequestHeaders); | |
2739 } | |
2740 | |
2741 TEST(HttpCache, UrlContainingHash) { | |
2742 MockHttpCache cache; | |
2743 | |
2744 // Do a typical GET request -- should write an entry into our cache. | |
2745 MockTransaction trans(kTypicalGET_Transaction); | |
2746 RunTransactionTest(cache.http_cache(), trans); | |
2747 | |
2748 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2749 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2750 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2751 | |
2752 // Request the same URL, but this time with a reference section (hash). | |
2753 // Since the cache key strips the hash sections, this should be a cache hit. | |
2754 std::string url_with_hash = std::string(trans.url) + "#multiple#hashes"; | |
2755 trans.url = url_with_hash.c_str(); | |
2756 trans.load_flags = net::LOAD_ONLY_FROM_CACHE; | |
2757 | |
2758 RunTransactionTest(cache.http_cache(), trans); | |
2759 | |
2760 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2761 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2762 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2763 } | |
2764 | |
2765 // Tests that we skip the cache for POST requests that do not have an upload | |
2766 // identifier. | |
2767 TEST(HttpCache, SimplePOST_SkipsCache) { | |
2768 MockHttpCache cache; | |
2769 | |
2770 RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction); | |
2771 | |
2772 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2773 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2774 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
2775 } | |
2776 | |
2777 // Tests POST handling with a disabled cache (no DCHECK). | |
2778 TEST(HttpCache, SimplePOST_DisabledCache) { | |
2779 MockHttpCache cache; | |
2780 cache.http_cache()->set_mode(net::HttpCache::Mode::DISABLE); | |
2781 | |
2782 RunTransactionTest(cache.http_cache(), kSimplePOST_Transaction); | |
2783 | |
2784 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2785 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2786 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
2787 } | |
2788 | |
2789 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Miss) { | |
2790 MockHttpCache cache; | |
2791 | |
2792 MockTransaction transaction(kSimplePOST_Transaction); | |
2793 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
2794 | |
2795 MockHttpRequest request(transaction); | |
2796 net::TestCompletionCallback callback; | |
2797 | |
2798 scoped_ptr<net::HttpTransaction> trans; | |
2799 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
2800 ASSERT_TRUE(trans.get()); | |
2801 | |
2802 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
2803 ASSERT_EQ(net::ERR_CACHE_MISS, callback.GetResult(rv)); | |
2804 | |
2805 trans.reset(); | |
2806 | |
2807 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | |
2808 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2809 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
2810 } | |
2811 | |
2812 TEST(HttpCache, SimplePOST_LoadOnlyFromCache_Hit) { | |
2813 MockHttpCache cache; | |
2814 | |
2815 // Test that we hit the cache for POST requests. | |
2816 | |
2817 MockTransaction transaction(kSimplePOST_Transaction); | |
2818 | |
2819 const int64 kUploadId = 1; // Just a dummy value. | |
2820 | |
2821 ScopedVector<net::UploadElementReader> element_readers; | |
2822 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
2823 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), | |
2824 kUploadId); | |
2825 MockHttpRequest request(transaction); | |
2826 request.upload_data_stream = &upload_data_stream; | |
2827 | |
2828 // Populate the cache. | |
2829 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); | |
2830 | |
2831 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2832 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2833 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2834 | |
2835 // Load from cache. | |
2836 request.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
2837 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); | |
2838 | |
2839 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2840 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
2841 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2842 } | |
2843 | |
2844 // Test that we don't hit the cache for POST requests if there is a byte range. | |
2845 TEST(HttpCache, SimplePOST_WithRanges) { | |
2846 MockHttpCache cache; | |
2847 | |
2848 MockTransaction transaction(kSimplePOST_Transaction); | |
2849 transaction.request_headers = "Range: bytes = 0-4\r\n"; | |
2850 | |
2851 const int64 kUploadId = 1; // Just a dummy value. | |
2852 | |
2853 ScopedVector<net::UploadElementReader> element_readers; | |
2854 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
2855 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), | |
2856 kUploadId); | |
2857 | |
2858 MockHttpRequest request(transaction); | |
2859 request.upload_data_stream = &upload_data_stream; | |
2860 | |
2861 // Attempt to populate the cache. | |
2862 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); | |
2863 | |
2864 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2865 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2866 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
2867 } | |
2868 | |
2869 // Tests that a POST is cached separately from a previously cached GET. | |
2870 TEST(HttpCache, SimplePOST_SeparateCache) { | |
2871 MockHttpCache cache; | |
2872 | |
2873 ScopedVector<net::UploadElementReader> element_readers; | |
2874 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
2875 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 1); | |
2876 | |
2877 MockTransaction transaction(kSimplePOST_Transaction); | |
2878 MockHttpRequest req1(transaction); | |
2879 req1.upload_data_stream = &upload_data_stream; | |
2880 | |
2881 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
2882 | |
2883 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2884 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2885 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2886 | |
2887 transaction.method = "GET"; | |
2888 MockHttpRequest req2(transaction); | |
2889 | |
2890 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | |
2891 | |
2892 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2893 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2894 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
2895 } | |
2896 | |
2897 // Tests that a successful POST invalidates a previously cached GET. | |
2898 TEST(HttpCache, SimplePOST_Invalidate_205) { | |
2899 MockHttpCache cache; | |
2900 | |
2901 MockTransaction transaction(kSimpleGET_Transaction); | |
2902 AddMockTransaction(&transaction); | |
2903 MockHttpRequest req1(transaction); | |
2904 | |
2905 // Attempt to populate the cache. | |
2906 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
2907 | |
2908 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2909 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2910 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2911 | |
2912 ScopedVector<net::UploadElementReader> element_readers; | |
2913 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
2914 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 1); | |
2915 | |
2916 transaction.method = "POST"; | |
2917 transaction.status = "HTTP/1.1 205 No Content"; | |
2918 MockHttpRequest req2(transaction); | |
2919 req2.upload_data_stream = &upload_data_stream; | |
2920 | |
2921 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | |
2922 | |
2923 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2924 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2925 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
2926 | |
2927 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
2928 | |
2929 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
2930 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2931 EXPECT_EQ(3, cache.disk_cache()->create_count()); | |
2932 RemoveMockTransaction(&transaction); | |
2933 } | |
2934 | |
2935 // Tests that a successful POST invalidates a previously cached GET, even when | |
2936 // there is no upload identifier. | |
2937 TEST(HttpCache, SimplePOST_NoUploadId_Invalidate_205) { | |
2938 MockHttpCache cache; | |
2939 | |
2940 MockTransaction transaction(kSimpleGET_Transaction); | |
2941 AddMockTransaction(&transaction); | |
2942 MockHttpRequest req1(transaction); | |
2943 | |
2944 // Attempt to populate the cache. | |
2945 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
2946 | |
2947 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
2948 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2949 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2950 | |
2951 ScopedVector<net::UploadElementReader> element_readers; | |
2952 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
2953 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0); | |
2954 | |
2955 transaction.method = "POST"; | |
2956 transaction.status = "HTTP/1.1 205 No Content"; | |
2957 MockHttpRequest req2(transaction); | |
2958 req2.upload_data_stream = &upload_data_stream; | |
2959 | |
2960 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | |
2961 | |
2962 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
2963 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2964 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
2965 | |
2966 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
2967 | |
2968 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
2969 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
2970 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
2971 RemoveMockTransaction(&transaction); | |
2972 } | |
2973 | |
2974 // Tests that processing a POST before creating the backend doesn't crash. | |
2975 TEST(HttpCache, SimplePOST_NoUploadId_NoBackend) { | |
2976 // This will initialize a cache object with NULL backend. | |
2977 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | |
2978 factory->set_fail(true); | |
2979 factory->FinishCreation(); | |
2980 MockHttpCache cache(factory); | |
2981 | |
2982 ScopedVector<net::UploadElementReader> element_readers; | |
2983 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
2984 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0); | |
2985 | |
2986 MockTransaction transaction(kSimplePOST_Transaction); | |
2987 AddMockTransaction(&transaction); | |
2988 MockHttpRequest req(transaction); | |
2989 req.upload_data_stream = &upload_data_stream; | |
2990 | |
2991 RunTransactionTestWithRequest(cache.http_cache(), transaction, req, NULL); | |
2992 | |
2993 RemoveMockTransaction(&transaction); | |
2994 } | |
2995 | |
2996 // Tests that we don't invalidate entries as a result of a failed POST. | |
2997 TEST(HttpCache, SimplePOST_DontInvalidate_100) { | |
2998 MockHttpCache cache; | |
2999 | |
3000 MockTransaction transaction(kSimpleGET_Transaction); | |
3001 AddMockTransaction(&transaction); | |
3002 MockHttpRequest req1(transaction); | |
3003 | |
3004 // Attempt to populate the cache. | |
3005 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
3006 | |
3007 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3008 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3009 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3010 | |
3011 ScopedVector<net::UploadElementReader> element_readers; | |
3012 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
3013 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 1); | |
3014 | |
3015 transaction.method = "POST"; | |
3016 transaction.status = "HTTP/1.1 100 Continue"; | |
3017 MockHttpRequest req2(transaction); | |
3018 req2.upload_data_stream = &upload_data_stream; | |
3019 | |
3020 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | |
3021 | |
3022 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3023 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3024 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3025 | |
3026 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
3027 | |
3028 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3029 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3030 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3031 RemoveMockTransaction(&transaction); | |
3032 } | |
3033 | |
3034 // Tests that a HEAD request is not cached by itself. | |
3035 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Miss) { | |
3036 MockHttpCache cache; | |
3037 MockTransaction transaction(kSimplePOST_Transaction); | |
3038 AddMockTransaction(&transaction); | |
3039 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
3040 transaction.method = "HEAD"; | |
3041 | |
3042 MockHttpRequest request(transaction); | |
3043 net::TestCompletionCallback callback; | |
3044 | |
3045 scoped_ptr<net::HttpTransaction> trans; | |
3046 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
3047 ASSERT_TRUE(trans.get()); | |
3048 | |
3049 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
3050 ASSERT_EQ(net::ERR_CACHE_MISS, callback.GetResult(rv)); | |
3051 | |
3052 trans.reset(); | |
3053 | |
3054 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | |
3055 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3056 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
3057 RemoveMockTransaction(&transaction); | |
3058 } | |
3059 | |
3060 // Tests that a HEAD request is served from a cached GET. | |
3061 TEST(HttpCache, SimpleHEAD_LoadOnlyFromCache_Hit) { | |
3062 MockHttpCache cache; | |
3063 MockTransaction transaction(kSimpleGET_Transaction); | |
3064 AddMockTransaction(&transaction); | |
3065 | |
3066 // Populate the cache. | |
3067 RunTransactionTest(cache.http_cache(), transaction); | |
3068 | |
3069 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3070 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3071 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3072 | |
3073 // Load from cache. | |
3074 transaction.method = "HEAD"; | |
3075 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
3076 transaction.data = ""; | |
3077 RunTransactionTest(cache.http_cache(), transaction); | |
3078 | |
3079 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3080 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3081 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3082 RemoveMockTransaction(&transaction); | |
3083 } | |
3084 | |
3085 // Tests that a read-only request served from the cache preserves CL. | |
3086 TEST(HttpCache, SimpleHEAD_ContentLengthOnHit_Read) { | |
3087 MockHttpCache cache; | |
3088 MockTransaction transaction(kSimpleGET_Transaction); | |
3089 AddMockTransaction(&transaction); | |
3090 transaction.response_headers = "Content-Length: 42\n"; | |
3091 | |
3092 // Populate the cache. | |
3093 RunTransactionTest(cache.http_cache(), transaction); | |
3094 | |
3095 // Load from cache. | |
3096 transaction.method = "HEAD"; | |
3097 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
3098 transaction.data = ""; | |
3099 std::string headers; | |
3100 | |
3101 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3102 | |
3103 EXPECT_EQ("HTTP/1.1 200 OK\nContent-Length: 42\n", headers); | |
3104 RemoveMockTransaction(&transaction); | |
3105 } | |
3106 | |
3107 // Tests that a read-write request served from the cache preserves CL. | |
3108 TEST(HttpCache, ETagHEAD_ContentLengthOnHit_ReadWrite) { | |
3109 MockHttpCache cache; | |
3110 MockTransaction transaction(kETagGET_Transaction); | |
3111 AddMockTransaction(&transaction); | |
3112 std::string server_headers(kETagGET_Transaction.response_headers); | |
3113 server_headers.append("Content-Length: 42\n"); | |
3114 transaction.response_headers = server_headers.data(); | |
3115 | |
3116 // Populate the cache. | |
3117 RunTransactionTest(cache.http_cache(), transaction); | |
3118 | |
3119 // Load from cache. | |
3120 transaction.method = "HEAD"; | |
3121 transaction.data = ""; | |
3122 std::string headers; | |
3123 | |
3124 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3125 | |
3126 EXPECT_NE(std::string::npos, headers.find("Content-Length: 42\n")); | |
3127 RemoveMockTransaction(&transaction); | |
3128 } | |
3129 | |
3130 // Tests that a HEAD request that includes byte ranges bypasses the cache. | |
3131 TEST(HttpCache, SimpleHEAD_WithRanges) { | |
3132 MockHttpCache cache; | |
3133 MockTransaction transaction(kSimpleGET_Transaction); | |
3134 AddMockTransaction(&transaction); | |
3135 | |
3136 // Populate the cache. | |
3137 RunTransactionTest(cache.http_cache(), transaction); | |
3138 | |
3139 // Load from cache. | |
3140 transaction.method = "HEAD"; | |
3141 transaction.request_headers = "Range: bytes = 0-4\r\n"; | |
3142 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
3143 transaction.return_code = net::ERR_CACHE_MISS; | |
3144 RunTransactionTest(cache.http_cache(), transaction); | |
3145 | |
3146 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3147 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3148 RemoveMockTransaction(&transaction); | |
3149 } | |
3150 | |
3151 // Tests that a HEAD request can be served from a partialy cached resource. | |
3152 TEST(HttpCache, SimpleHEAD_WithCachedRanges) { | |
3153 MockHttpCache cache; | |
3154 AddMockTransaction(&kRangeGET_TransactionOK); | |
3155 | |
3156 // Write to the cache (40-49). | |
3157 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
3158 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
3159 | |
3160 MockTransaction transaction(kSimpleGET_Transaction); | |
3161 | |
3162 transaction.url = kRangeGET_TransactionOK.url; | |
3163 transaction.method = "HEAD"; | |
3164 transaction.data = ""; | |
3165 AddMockTransaction(&transaction); | |
3166 std::string headers; | |
3167 | |
3168 // Load from cache. | |
3169 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3170 | |
3171 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n")); | |
3172 EXPECT_NE(std::string::npos, headers.find("Content-Length: 80\n")); | |
3173 EXPECT_EQ(std::string::npos, headers.find("Content-Range")); | |
3174 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3175 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3176 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3177 RemoveMockTransaction(&transaction); | |
3178 } | |
3179 | |
3180 // Tests that a HEAD request can be served from a truncated resource. | |
3181 TEST(HttpCache, SimpleHEAD_WithTruncatedEntry) { | |
3182 MockHttpCache cache; | |
3183 AddMockTransaction(&kRangeGET_TransactionOK); | |
3184 | |
3185 std::string raw_headers("HTTP/1.1 200 OK\n" | |
3186 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
3187 "ETag: \"foo\"\n" | |
3188 "Accept-Ranges: bytes\n" | |
3189 "Content-Length: 80\n"); | |
3190 CreateTruncatedEntry(raw_headers, &cache); | |
3191 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
3192 | |
3193 MockTransaction transaction(kSimpleGET_Transaction); | |
3194 | |
3195 transaction.url = kRangeGET_TransactionOK.url; | |
3196 transaction.method = "HEAD"; | |
3197 transaction.data = ""; | |
3198 AddMockTransaction(&transaction); | |
3199 std::string headers; | |
3200 | |
3201 // Load from cache. | |
3202 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3203 | |
3204 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n")); | |
3205 EXPECT_NE(std::string::npos, headers.find("Content-Length: 80\n")); | |
3206 EXPECT_EQ(std::string::npos, headers.find("Content-Range")); | |
3207 EXPECT_EQ(0, cache.network_layer()->transaction_count()); | |
3208 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3209 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3210 RemoveMockTransaction(&transaction); | |
3211 } | |
3212 | |
3213 // Tests that a HEAD request updates the cached response. | |
3214 TEST(HttpCache, TypicalHEAD_UpdatesResponse) { | |
3215 MockHttpCache cache; | |
3216 MockTransaction transaction(kTypicalGET_Transaction); | |
3217 AddMockTransaction(&transaction); | |
3218 | |
3219 // Populate the cache. | |
3220 RunTransactionTest(cache.http_cache(), transaction); | |
3221 | |
3222 // Update the cache. | |
3223 transaction.method = "HEAD"; | |
3224 transaction.response_headers = "Foo: bar\n"; | |
3225 transaction.data = ""; | |
3226 transaction.status = "HTTP/1.1 304 Not Modified\n"; | |
3227 std::string headers; | |
3228 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3229 RemoveMockTransaction(&transaction); | |
3230 | |
3231 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 200 OK\n")); | |
3232 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3233 | |
3234 MockTransaction transaction2(kTypicalGET_Transaction); | |
3235 AddMockTransaction(&transaction2); | |
3236 | |
3237 // Make sure we are done with the previous transaction. | |
3238 base::MessageLoop::current()->RunUntilIdle(); | |
3239 | |
3240 // Load from the cache. | |
3241 transaction2.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
3242 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); | |
3243 | |
3244 EXPECT_NE(std::string::npos, headers.find("Foo: bar\n")); | |
3245 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3246 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
3247 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3248 RemoveMockTransaction(&transaction2); | |
3249 } | |
3250 | |
3251 // Tests that an externally conditionalized HEAD request updates the cache. | |
3252 TEST(HttpCache, TypicalHEAD_ConditionalizedRequestUpdatesResponse) { | |
3253 MockHttpCache cache; | |
3254 MockTransaction transaction(kTypicalGET_Transaction); | |
3255 AddMockTransaction(&transaction); | |
3256 | |
3257 // Populate the cache. | |
3258 RunTransactionTest(cache.http_cache(), transaction); | |
3259 | |
3260 // Update the cache. | |
3261 transaction.method = "HEAD"; | |
3262 transaction.request_headers = | |
3263 "If-Modified-Since: Wed, 28 Nov 2007 00:40:09 GMT\r\n"; | |
3264 transaction.response_headers = "Foo: bar\n"; | |
3265 transaction.data = ""; | |
3266 transaction.status = "HTTP/1.1 304 Not Modified\n"; | |
3267 std::string headers; | |
3268 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3269 RemoveMockTransaction(&transaction); | |
3270 | |
3271 EXPECT_NE(std::string::npos, headers.find("HTTP/1.1 304 Not Modified\n")); | |
3272 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3273 | |
3274 MockTransaction transaction2(kTypicalGET_Transaction); | |
3275 AddMockTransaction(&transaction2); | |
3276 | |
3277 // Make sure we are done with the previous transaction. | |
3278 base::MessageLoop::current()->RunUntilIdle(); | |
3279 | |
3280 // Load from the cache. | |
3281 transaction2.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
3282 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); | |
3283 | |
3284 EXPECT_NE(std::string::npos, headers.find("Foo: bar\n")); | |
3285 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3286 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
3287 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3288 RemoveMockTransaction(&transaction2); | |
3289 } | |
3290 | |
3291 // Tests that a HEAD request invalidates an old cached entry. | |
3292 TEST(HttpCache, SimpleHEAD_InvalidatesEntry) { | |
3293 MockHttpCache cache; | |
3294 MockTransaction transaction(kTypicalGET_Transaction); | |
3295 AddMockTransaction(&transaction); | |
3296 | |
3297 // Populate the cache. | |
3298 RunTransactionTest(cache.http_cache(), transaction); | |
3299 | |
3300 // Update the cache. | |
3301 transaction.method = "HEAD"; | |
3302 transaction.data = ""; | |
3303 RunTransactionTest(cache.http_cache(), transaction); | |
3304 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3305 | |
3306 // Load from the cache. | |
3307 transaction.method = "GET"; | |
3308 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
3309 transaction.return_code = net::ERR_CACHE_MISS; | |
3310 RunTransactionTest(cache.http_cache(), transaction); | |
3311 | |
3312 RemoveMockTransaction(&transaction); | |
3313 } | |
3314 | |
3315 // Tests that we do not cache the response of a PUT. | |
3316 TEST(HttpCache, SimplePUT_Miss) { | |
3317 MockHttpCache cache; | |
3318 | |
3319 MockTransaction transaction(kSimplePOST_Transaction); | |
3320 transaction.method = "PUT"; | |
3321 | |
3322 ScopedVector<net::UploadElementReader> element_readers; | |
3323 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
3324 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0); | |
3325 | |
3326 MockHttpRequest request(transaction); | |
3327 request.upload_data_stream = &upload_data_stream; | |
3328 | |
3329 // Attempt to populate the cache. | |
3330 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); | |
3331 | |
3332 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3333 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3334 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
3335 } | |
3336 | |
3337 // Tests that we invalidate entries as a result of a PUT. | |
3338 TEST(HttpCache, SimplePUT_Invalidate) { | |
3339 MockHttpCache cache; | |
3340 | |
3341 MockTransaction transaction(kSimpleGET_Transaction); | |
3342 MockHttpRequest req1(transaction); | |
3343 | |
3344 // Attempt to populate the cache. | |
3345 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
3346 | |
3347 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3348 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3349 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3350 | |
3351 ScopedVector<net::UploadElementReader> element_readers; | |
3352 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
3353 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0); | |
3354 | |
3355 transaction.method = "PUT"; | |
3356 MockHttpRequest req2(transaction); | |
3357 req2.upload_data_stream = &upload_data_stream; | |
3358 | |
3359 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | |
3360 | |
3361 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3362 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3363 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3364 | |
3365 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
3366 | |
3367 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
3368 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3369 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3370 } | |
3371 | |
3372 // Tests that we invalidate entries as a result of a PUT. | |
3373 TEST(HttpCache, SimplePUT_Invalidate_305) { | |
3374 MockHttpCache cache; | |
3375 | |
3376 MockTransaction transaction(kSimpleGET_Transaction); | |
3377 AddMockTransaction(&transaction); | |
3378 MockHttpRequest req1(transaction); | |
3379 | |
3380 // Attempt to populate the cache. | |
3381 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
3382 | |
3383 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3384 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3385 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3386 | |
3387 ScopedVector<net::UploadElementReader> element_readers; | |
3388 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
3389 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0); | |
3390 | |
3391 transaction.method = "PUT"; | |
3392 transaction.status = "HTTP/1.1 305 Use Proxy"; | |
3393 MockHttpRequest req2(transaction); | |
3394 req2.upload_data_stream = &upload_data_stream; | |
3395 | |
3396 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | |
3397 | |
3398 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3399 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3400 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3401 | |
3402 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
3403 | |
3404 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
3405 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3406 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3407 RemoveMockTransaction(&transaction); | |
3408 } | |
3409 | |
3410 // Tests that we don't invalidate entries as a result of a failed PUT. | |
3411 TEST(HttpCache, SimplePUT_DontInvalidate_404) { | |
3412 MockHttpCache cache; | |
3413 | |
3414 MockTransaction transaction(kSimpleGET_Transaction); | |
3415 AddMockTransaction(&transaction); | |
3416 MockHttpRequest req1(transaction); | |
3417 | |
3418 // Attempt to populate the cache. | |
3419 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
3420 | |
3421 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3422 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3423 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3424 | |
3425 ScopedVector<net::UploadElementReader> element_readers; | |
3426 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
3427 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0); | |
3428 | |
3429 transaction.method = "PUT"; | |
3430 transaction.status = "HTTP/1.1 404 Not Found"; | |
3431 MockHttpRequest req2(transaction); | |
3432 req2.upload_data_stream = &upload_data_stream; | |
3433 | |
3434 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | |
3435 | |
3436 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3437 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3438 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3439 | |
3440 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
3441 | |
3442 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3443 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
3444 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3445 RemoveMockTransaction(&transaction); | |
3446 } | |
3447 | |
3448 // Tests that we do not cache the response of a DELETE. | |
3449 TEST(HttpCache, SimpleDELETE_Miss) { | |
3450 MockHttpCache cache; | |
3451 | |
3452 MockTransaction transaction(kSimplePOST_Transaction); | |
3453 transaction.method = "DELETE"; | |
3454 | |
3455 ScopedVector<net::UploadElementReader> element_readers; | |
3456 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
3457 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0); | |
3458 | |
3459 MockHttpRequest request(transaction); | |
3460 request.upload_data_stream = &upload_data_stream; | |
3461 | |
3462 // Attempt to populate the cache. | |
3463 RunTransactionTestWithRequest(cache.http_cache(), transaction, request, NULL); | |
3464 | |
3465 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3466 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3467 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
3468 } | |
3469 | |
3470 // Tests that we invalidate entries as a result of a DELETE. | |
3471 TEST(HttpCache, SimpleDELETE_Invalidate) { | |
3472 MockHttpCache cache; | |
3473 | |
3474 MockTransaction transaction(kSimpleGET_Transaction); | |
3475 MockHttpRequest req1(transaction); | |
3476 | |
3477 // Attempt to populate the cache. | |
3478 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
3479 | |
3480 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3481 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3482 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3483 | |
3484 ScopedVector<net::UploadElementReader> element_readers; | |
3485 element_readers.push_back(new net::UploadBytesElementReader("hello", 5)); | |
3486 net::ElementsUploadDataStream upload_data_stream(element_readers.Pass(), 0); | |
3487 | |
3488 transaction.method = "DELETE"; | |
3489 MockHttpRequest req2(transaction); | |
3490 req2.upload_data_stream = &upload_data_stream; | |
3491 | |
3492 RunTransactionTestWithRequest(cache.http_cache(), transaction, req2, NULL); | |
3493 | |
3494 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3495 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3496 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3497 | |
3498 RunTransactionTestWithRequest(cache.http_cache(), transaction, req1, NULL); | |
3499 | |
3500 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
3501 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3502 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3503 } | |
3504 | |
3505 // Tests that we invalidate entries as a result of a DELETE. | |
3506 TEST(HttpCache, SimpleDELETE_Invalidate_301) { | |
3507 MockHttpCache cache; | |
3508 | |
3509 MockTransaction transaction(kSimpleGET_Transaction); | |
3510 AddMockTransaction(&transaction); | |
3511 | |
3512 // Attempt to populate the cache. | |
3513 RunTransactionTest(cache.http_cache(), transaction); | |
3514 | |
3515 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3516 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3517 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3518 | |
3519 transaction.method = "DELETE"; | |
3520 transaction.status = "HTTP/1.1 301 Moved Permanently "; | |
3521 | |
3522 RunTransactionTest(cache.http_cache(), transaction); | |
3523 | |
3524 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3525 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3526 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3527 | |
3528 transaction.method = "GET"; | |
3529 RunTransactionTest(cache.http_cache(), transaction); | |
3530 | |
3531 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
3532 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3533 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3534 RemoveMockTransaction(&transaction); | |
3535 } | |
3536 | |
3537 // Tests that we don't invalidate entries as a result of a failed DELETE. | |
3538 TEST(HttpCache, SimpleDELETE_DontInvalidate_416) { | |
3539 MockHttpCache cache; | |
3540 | |
3541 MockTransaction transaction(kSimpleGET_Transaction); | |
3542 AddMockTransaction(&transaction); | |
3543 | |
3544 // Attempt to populate the cache. | |
3545 RunTransactionTest(cache.http_cache(), transaction); | |
3546 | |
3547 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3548 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3549 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3550 | |
3551 transaction.method = "DELETE"; | |
3552 transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable"; | |
3553 | |
3554 RunTransactionTest(cache.http_cache(), transaction); | |
3555 | |
3556 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3557 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3558 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3559 | |
3560 transaction.method = "GET"; | |
3561 transaction.status = "HTTP/1.1 200 OK"; | |
3562 RunTransactionTest(cache.http_cache(), transaction); | |
3563 | |
3564 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3565 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
3566 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3567 RemoveMockTransaction(&transaction); | |
3568 } | |
3569 | |
3570 // Tests that we don't invalidate entries after a failed network transaction. | |
3571 TEST(HttpCache, SimpleGET_DontInvalidateOnFailure) { | |
3572 MockHttpCache cache; | |
3573 | |
3574 // Populate the cache. | |
3575 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
3576 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3577 | |
3578 // Fail the network request. | |
3579 MockTransaction transaction(kSimpleGET_Transaction); | |
3580 transaction.return_code = net::ERR_FAILED; | |
3581 transaction.load_flags |= net::LOAD_VALIDATE_CACHE; | |
3582 | |
3583 AddMockTransaction(&transaction); | |
3584 RunTransactionTest(cache.http_cache(), transaction); | |
3585 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3586 RemoveMockTransaction(&transaction); | |
3587 | |
3588 transaction.load_flags = net::LOAD_ONLY_FROM_CACHE; | |
3589 transaction.return_code = net::OK; | |
3590 AddMockTransaction(&transaction); | |
3591 RunTransactionTest(cache.http_cache(), transaction); | |
3592 | |
3593 // Make sure the transaction didn't reach the network. | |
3594 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3595 RemoveMockTransaction(&transaction); | |
3596 } | |
3597 | |
3598 TEST(HttpCache, RangeGET_SkipsCache) { | |
3599 MockHttpCache cache; | |
3600 | |
3601 // Test that we skip the cache for range GET requests. Eventually, we will | |
3602 // want to cache these, but we'll still have cases where skipping the cache | |
3603 // makes sense, so we want to make sure that it works properly. | |
3604 | |
3605 RunTransactionTest(cache.http_cache(), kRangeGET_Transaction); | |
3606 | |
3607 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3608 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3609 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
3610 | |
3611 MockTransaction transaction(kSimpleGET_Transaction); | |
3612 transaction.request_headers = "If-None-Match: foo\r\n"; | |
3613 RunTransactionTest(cache.http_cache(), transaction); | |
3614 | |
3615 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3616 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3617 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
3618 | |
3619 transaction.request_headers = | |
3620 "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n"; | |
3621 RunTransactionTest(cache.http_cache(), transaction); | |
3622 | |
3623 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
3624 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3625 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
3626 } | |
3627 | |
3628 // Test that we skip the cache for range requests that include a validation | |
3629 // header. | |
3630 TEST(HttpCache, RangeGET_SkipsCache2) { | |
3631 MockHttpCache cache; | |
3632 | |
3633 MockTransaction transaction(kRangeGET_Transaction); | |
3634 transaction.request_headers = "If-None-Match: foo\r\n" | |
3635 EXTRA_HEADER | |
3636 "Range: bytes = 40-49\r\n"; | |
3637 RunTransactionTest(cache.http_cache(), transaction); | |
3638 | |
3639 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3640 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3641 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
3642 | |
3643 transaction.request_headers = | |
3644 "If-Modified-Since: Wed, 28 Nov 2007 00:45:20 GMT\r\n" | |
3645 EXTRA_HEADER | |
3646 "Range: bytes = 40-49\r\n"; | |
3647 RunTransactionTest(cache.http_cache(), transaction); | |
3648 | |
3649 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3650 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3651 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
3652 | |
3653 transaction.request_headers = "If-Range: bla\r\n" | |
3654 EXTRA_HEADER | |
3655 "Range: bytes = 40-49\r\n"; | |
3656 RunTransactionTest(cache.http_cache(), transaction); | |
3657 | |
3658 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
3659 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3660 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
3661 } | |
3662 | |
3663 TEST(HttpCache, SimpleGET_DoesntLogHeaders) { | |
3664 MockHttpCache cache; | |
3665 | |
3666 net::CapturingBoundNetLog log; | |
3667 RunTransactionTestWithLog(cache.http_cache(), kSimpleGET_Transaction, | |
3668 log.bound()); | |
3669 | |
3670 EXPECT_FALSE(LogContainsEventType( | |
3671 log, net::NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS)); | |
3672 } | |
3673 | |
3674 TEST(HttpCache, RangeGET_LogsHeaders) { | |
3675 MockHttpCache cache; | |
3676 | |
3677 net::CapturingBoundNetLog log; | |
3678 RunTransactionTestWithLog(cache.http_cache(), kRangeGET_Transaction, | |
3679 log.bound()); | |
3680 | |
3681 EXPECT_TRUE(LogContainsEventType( | |
3682 log, net::NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS)); | |
3683 } | |
3684 | |
3685 TEST(HttpCache, ExternalValidation_LogsHeaders) { | |
3686 MockHttpCache cache; | |
3687 | |
3688 net::CapturingBoundNetLog log; | |
3689 MockTransaction transaction(kSimpleGET_Transaction); | |
3690 transaction.request_headers = "If-None-Match: foo\r\n" EXTRA_HEADER; | |
3691 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); | |
3692 | |
3693 EXPECT_TRUE(LogContainsEventType( | |
3694 log, net::NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS)); | |
3695 } | |
3696 | |
3697 TEST(HttpCache, SpecialHeaders_LogsHeaders) { | |
3698 MockHttpCache cache; | |
3699 | |
3700 net::CapturingBoundNetLog log; | |
3701 MockTransaction transaction(kSimpleGET_Transaction); | |
3702 transaction.request_headers = "cache-control: no-cache\r\n" EXTRA_HEADER; | |
3703 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); | |
3704 | |
3705 EXPECT_TRUE(LogContainsEventType( | |
3706 log, net::NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS)); | |
3707 } | |
3708 | |
3709 // Tests that receiving 206 for a regular request is handled correctly. | |
3710 TEST(HttpCache, GET_Crazy206) { | |
3711 MockHttpCache cache; | |
3712 | |
3713 // Write to the cache. | |
3714 MockTransaction transaction(kRangeGET_TransactionOK); | |
3715 AddMockTransaction(&transaction); | |
3716 transaction.request_headers = EXTRA_HEADER; | |
3717 transaction.handler = NULL; | |
3718 RunTransactionTest(cache.http_cache(), transaction); | |
3719 | |
3720 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3721 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3722 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3723 | |
3724 // This should read again from the net. | |
3725 RunTransactionTest(cache.http_cache(), transaction); | |
3726 | |
3727 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3728 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3729 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3730 RemoveMockTransaction(&transaction); | |
3731 } | |
3732 | |
3733 // Tests that receiving 416 for a regular request is handled correctly. | |
3734 TEST(HttpCache, GET_Crazy416) { | |
3735 MockHttpCache cache; | |
3736 | |
3737 // Write to the cache. | |
3738 MockTransaction transaction(kSimpleGET_Transaction); | |
3739 AddMockTransaction(&transaction); | |
3740 transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable"; | |
3741 RunTransactionTest(cache.http_cache(), transaction); | |
3742 | |
3743 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3744 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3745 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3746 | |
3747 RemoveMockTransaction(&transaction); | |
3748 } | |
3749 | |
3750 // Tests that we don't store partial responses that can't be validated. | |
3751 TEST(HttpCache, RangeGET_NoStrongValidators) { | |
3752 MockHttpCache cache; | |
3753 std::string headers; | |
3754 | |
3755 // Attempt to write to the cache (40-49). | |
3756 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
3757 transaction.response_headers = "Content-Length: 10\n" | |
3758 "Cache-Control: max-age=3600\n" | |
3759 "ETag: w/\"foo\"\n"; | |
3760 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3761 | |
3762 Verify206Response(headers, 40, 49); | |
3763 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3764 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3765 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3766 | |
3767 // Now verify that there's no cached data. | |
3768 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, | |
3769 &headers); | |
3770 | |
3771 Verify206Response(headers, 40, 49); | |
3772 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3773 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3774 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3775 } | |
3776 | |
3777 // Tests failures to conditionalize byte range requests. | |
3778 TEST(HttpCache, RangeGET_NoConditionalization) { | |
3779 MockHttpCache cache; | |
3780 cache.FailConditionalizations(); | |
3781 std::string headers; | |
3782 | |
3783 // Write to the cache (40-49). | |
3784 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
3785 transaction.response_headers = "Content-Length: 10\n" | |
3786 "ETag: \"foo\"\n"; | |
3787 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3788 | |
3789 Verify206Response(headers, 40, 49); | |
3790 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3791 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3792 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3793 | |
3794 // Now verify that the cached data is not used. | |
3795 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, | |
3796 &headers); | |
3797 | |
3798 Verify206Response(headers, 40, 49); | |
3799 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3800 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3801 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3802 } | |
3803 | |
3804 // Tests that restarting a partial request when the cached data cannot be | |
3805 // revalidated logs an event. | |
3806 TEST(HttpCache, RangeGET_NoValidation_LogsRestart) { | |
3807 MockHttpCache cache; | |
3808 cache.FailConditionalizations(); | |
3809 | |
3810 // Write to the cache (40-49). | |
3811 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
3812 transaction.response_headers = "Content-Length: 10\n" | |
3813 "ETag: \"foo\"\n"; | |
3814 RunTransactionTest(cache.http_cache(), transaction); | |
3815 | |
3816 // Now verify that the cached data is not used. | |
3817 net::CapturingBoundNetLog log; | |
3818 RunTransactionTestWithLog(cache.http_cache(), kRangeGET_TransactionOK, | |
3819 log.bound()); | |
3820 | |
3821 EXPECT_TRUE(LogContainsEventType( | |
3822 log, net::NetLog::TYPE_HTTP_CACHE_RESTART_PARTIAL_REQUEST)); | |
3823 } | |
3824 | |
3825 // Tests that a failure to conditionalize a regular request (no range) with a | |
3826 // sparse entry results in a full response. | |
3827 TEST(HttpCache, GET_NoConditionalization) { | |
3828 MockHttpCache cache; | |
3829 cache.FailConditionalizations(); | |
3830 std::string headers; | |
3831 | |
3832 // Write to the cache (40-49). | |
3833 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
3834 transaction.response_headers = "Content-Length: 10\n" | |
3835 "ETag: \"foo\"\n"; | |
3836 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3837 | |
3838 Verify206Response(headers, 40, 49); | |
3839 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3840 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3841 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3842 | |
3843 // Now verify that the cached data is not used. | |
3844 // Don't ask for a range. The cache will attempt to use the cached data but | |
3845 // should discard it as it cannot be validated. A regular request should go | |
3846 // to the server and a new entry should be created. | |
3847 transaction.request_headers = EXTRA_HEADER; | |
3848 transaction.data = "Not a range"; | |
3849 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3850 | |
3851 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); | |
3852 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3853 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3854 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3855 | |
3856 // The last response was saved. | |
3857 RunTransactionTest(cache.http_cache(), transaction); | |
3858 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
3859 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
3860 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3861 } | |
3862 | |
3863 // Verifies that conditionalization failures when asking for a range that would | |
3864 // require the cache to modify the range to ask, result in a network request | |
3865 // that matches the user's one. | |
3866 TEST(HttpCache, RangeGET_NoConditionalization2) { | |
3867 MockHttpCache cache; | |
3868 cache.FailConditionalizations(); | |
3869 std::string headers; | |
3870 | |
3871 // Write to the cache (40-49). | |
3872 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
3873 transaction.response_headers = "Content-Length: 10\n" | |
3874 "ETag: \"foo\"\n"; | |
3875 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3876 | |
3877 Verify206Response(headers, 40, 49); | |
3878 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3879 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3880 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3881 | |
3882 // Now verify that the cached data is not used. | |
3883 // Ask for a range that extends before and after the cached data so that the | |
3884 // cache would normally mix data from three sources. After deleting the entry, | |
3885 // the response will come from a single network request. | |
3886 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; | |
3887 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; | |
3888 transaction.response_headers = kRangeGET_TransactionOK.response_headers; | |
3889 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3890 | |
3891 Verify206Response(headers, 20, 59); | |
3892 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3893 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3894 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3895 | |
3896 // The last response was saved. | |
3897 RunTransactionTest(cache.http_cache(), transaction); | |
3898 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3899 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
3900 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
3901 } | |
3902 | |
3903 // Tests that we cache partial responses that lack content-length. | |
3904 TEST(HttpCache, RangeGET_NoContentLength) { | |
3905 MockHttpCache cache; | |
3906 std::string headers; | |
3907 | |
3908 // Attempt to write to the cache (40-49). | |
3909 MockTransaction transaction(kRangeGET_TransactionOK); | |
3910 AddMockTransaction(&transaction); | |
3911 transaction.response_headers = "ETag: \"foo\"\n" | |
3912 "Accept-Ranges: bytes\n" | |
3913 "Content-Range: bytes 40-49/80\n"; | |
3914 transaction.handler = NULL; | |
3915 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3916 | |
3917 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3918 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3919 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3920 | |
3921 // Now verify that there's no cached data. | |
3922 transaction.handler = &RangeTransactionServer::RangeHandler; | |
3923 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, | |
3924 &headers); | |
3925 | |
3926 Verify206Response(headers, 40, 49); | |
3927 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3928 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3929 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3930 | |
3931 RemoveMockTransaction(&transaction); | |
3932 } | |
3933 | |
3934 // Tests that we can cache range requests and fetch random blocks from the | |
3935 // cache and the network. | |
3936 TEST(HttpCache, RangeGET_OK) { | |
3937 MockHttpCache cache; | |
3938 AddMockTransaction(&kRangeGET_TransactionOK); | |
3939 std::string headers; | |
3940 | |
3941 // Write to the cache (40-49). | |
3942 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, | |
3943 &headers); | |
3944 | |
3945 Verify206Response(headers, 40, 49); | |
3946 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3947 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
3948 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3949 | |
3950 // Read from the cache (40-49). | |
3951 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, | |
3952 &headers); | |
3953 | |
3954 Verify206Response(headers, 40, 49); | |
3955 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
3956 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
3957 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3958 | |
3959 // Make sure we are done with the previous transaction. | |
3960 base::MessageLoop::current()->RunUntilIdle(); | |
3961 | |
3962 // Write to the cache (30-39). | |
3963 MockTransaction transaction(kRangeGET_TransactionOK); | |
3964 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; | |
3965 transaction.data = "rg: 30-39 "; | |
3966 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
3967 | |
3968 Verify206Response(headers, 30, 39); | |
3969 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
3970 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
3971 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3972 | |
3973 // Make sure we are done with the previous transaction. | |
3974 base::MessageLoop::current()->RunUntilIdle(); | |
3975 | |
3976 // Write and read from the cache (20-59). | |
3977 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; | |
3978 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; | |
3979 net::CapturingBoundNetLog log; | |
3980 net::LoadTimingInfo load_timing_info; | |
3981 RunTransactionTestWithResponseAndGetTiming( | |
3982 cache.http_cache(), transaction, &headers, log.bound(), | |
3983 &load_timing_info); | |
3984 | |
3985 Verify206Response(headers, 20, 59); | |
3986 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | |
3987 EXPECT_EQ(3, cache.disk_cache()->open_count()); | |
3988 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
3989 TestLoadTimingNetworkRequest(load_timing_info); | |
3990 | |
3991 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
3992 } | |
3993 | |
3994 // Tests that we can cache range requests and fetch random blocks from the | |
3995 // cache and the network, with synchronous responses. | |
3996 TEST(HttpCache, RangeGET_SyncOK) { | |
3997 MockHttpCache cache; | |
3998 | |
3999 MockTransaction transaction(kRangeGET_TransactionOK); | |
4000 transaction.test_mode = TEST_MODE_SYNC_ALL; | |
4001 AddMockTransaction(&transaction); | |
4002 | |
4003 // Write to the cache (40-49). | |
4004 std::string headers; | |
4005 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4006 | |
4007 Verify206Response(headers, 40, 49); | |
4008 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4009 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4010 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4011 | |
4012 // Read from the cache (40-49). | |
4013 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4014 | |
4015 Verify206Response(headers, 40, 49); | |
4016 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4017 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4018 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4019 | |
4020 // Make sure we are done with the previous transaction. | |
4021 base::MessageLoop::current()->RunUntilIdle(); | |
4022 | |
4023 // Write to the cache (30-39). | |
4024 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; | |
4025 transaction.data = "rg: 30-39 "; | |
4026 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4027 | |
4028 Verify206Response(headers, 30, 39); | |
4029 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4030 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4031 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4032 | |
4033 // Make sure we are done with the previous transaction. | |
4034 base::MessageLoop::current()->RunUntilIdle(); | |
4035 | |
4036 // Write and read from the cache (20-59). | |
4037 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; | |
4038 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; | |
4039 net::CapturingBoundNetLog log; | |
4040 net::LoadTimingInfo load_timing_info; | |
4041 RunTransactionTestWithResponseAndGetTiming( | |
4042 cache.http_cache(), transaction, &headers, log.bound(), | |
4043 &load_timing_info); | |
4044 | |
4045 Verify206Response(headers, 20, 59); | |
4046 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | |
4047 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
4048 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4049 TestLoadTimingNetworkRequest(load_timing_info); | |
4050 | |
4051 RemoveMockTransaction(&transaction); | |
4052 } | |
4053 | |
4054 // Tests that we don't revalidate an entry unless we are required to do so. | |
4055 TEST(HttpCache, RangeGET_Revalidate1) { | |
4056 MockHttpCache cache; | |
4057 std::string headers; | |
4058 | |
4059 // Write to the cache (40-49). | |
4060 MockTransaction transaction(kRangeGET_TransactionOK); | |
4061 transaction.response_headers = | |
4062 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" | |
4063 "Expires: Wed, 7 Sep 2033 21:46:42 GMT\n" // Should never expire. | |
4064 "ETag: \"foo\"\n" | |
4065 "Accept-Ranges: bytes\n" | |
4066 "Content-Length: 10\n"; | |
4067 AddMockTransaction(&transaction); | |
4068 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4069 | |
4070 Verify206Response(headers, 40, 49); | |
4071 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4072 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4073 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4074 | |
4075 // Read from the cache (40-49). | |
4076 net::CapturingBoundNetLog log; | |
4077 net::LoadTimingInfo load_timing_info; | |
4078 RunTransactionTestWithResponseAndGetTiming( | |
4079 cache.http_cache(), transaction, &headers, log.bound(), | |
4080 &load_timing_info); | |
4081 | |
4082 Verify206Response(headers, 40, 49); | |
4083 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4084 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4085 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4086 TestLoadTimingCachedResponse(load_timing_info); | |
4087 | |
4088 // Read again forcing the revalidation. | |
4089 transaction.load_flags |= net::LOAD_VALIDATE_CACHE; | |
4090 RunTransactionTestWithResponseAndGetTiming( | |
4091 cache.http_cache(), transaction, &headers, log.bound(), | |
4092 &load_timing_info); | |
4093 | |
4094 Verify206Response(headers, 40, 49); | |
4095 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4096 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4097 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4098 TestLoadTimingNetworkRequest(load_timing_info); | |
4099 | |
4100 RemoveMockTransaction(&transaction); | |
4101 } | |
4102 | |
4103 // Checks that we revalidate an entry when the headers say so. | |
4104 TEST(HttpCache, RangeGET_Revalidate2) { | |
4105 MockHttpCache cache; | |
4106 std::string headers; | |
4107 | |
4108 // Write to the cache (40-49). | |
4109 MockTransaction transaction(kRangeGET_TransactionOK); | |
4110 transaction.response_headers = | |
4111 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" | |
4112 "Expires: Sat, 18 Apr 2009 01:10:43 GMT\n" // Expired. | |
4113 "ETag: \"foo\"\n" | |
4114 "Accept-Ranges: bytes\n" | |
4115 "Content-Length: 10\n"; | |
4116 AddMockTransaction(&transaction); | |
4117 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4118 | |
4119 Verify206Response(headers, 40, 49); | |
4120 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4121 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4122 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4123 | |
4124 // Read from the cache (40-49). | |
4125 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4126 Verify206Response(headers, 40, 49); | |
4127 | |
4128 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4129 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4130 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4131 | |
4132 RemoveMockTransaction(&transaction); | |
4133 } | |
4134 | |
4135 // Tests that we deal with 304s for range requests. | |
4136 TEST(HttpCache, RangeGET_304) { | |
4137 MockHttpCache cache; | |
4138 AddMockTransaction(&kRangeGET_TransactionOK); | |
4139 std::string headers; | |
4140 | |
4141 // Write to the cache (40-49). | |
4142 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, | |
4143 &headers); | |
4144 | |
4145 Verify206Response(headers, 40, 49); | |
4146 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4147 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4148 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4149 | |
4150 // Read from the cache (40-49). | |
4151 RangeTransactionServer handler; | |
4152 handler.set_not_modified(true); | |
4153 MockTransaction transaction(kRangeGET_TransactionOK); | |
4154 transaction.load_flags |= net::LOAD_VALIDATE_CACHE; | |
4155 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4156 | |
4157 Verify206Response(headers, 40, 49); | |
4158 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4159 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4160 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4161 | |
4162 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
4163 } | |
4164 | |
4165 // Tests that we deal with 206s when revalidating range requests. | |
4166 TEST(HttpCache, RangeGET_ModifiedResult) { | |
4167 MockHttpCache cache; | |
4168 AddMockTransaction(&kRangeGET_TransactionOK); | |
4169 std::string headers; | |
4170 | |
4171 // Write to the cache (40-49). | |
4172 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, | |
4173 &headers); | |
4174 | |
4175 Verify206Response(headers, 40, 49); | |
4176 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4177 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4178 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4179 | |
4180 // Attempt to read from the cache (40-49). | |
4181 RangeTransactionServer handler; | |
4182 handler.set_modified(true); | |
4183 MockTransaction transaction(kRangeGET_TransactionOK); | |
4184 transaction.load_flags |= net::LOAD_VALIDATE_CACHE; | |
4185 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4186 | |
4187 Verify206Response(headers, 40, 49); | |
4188 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4189 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4190 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4191 | |
4192 // And the entry should be gone. | |
4193 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
4194 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
4195 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4196 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
4197 | |
4198 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
4199 } | |
4200 | |
4201 // Tests that when a server returns 206 with a sub-range of the requested range, | |
4202 // and there is nothing stored in the cache, the returned response is passed to | |
4203 // the caller as is. In this context, a subrange means a response that starts | |
4204 // with the same byte that was requested, but that is not the whole range that | |
4205 // was requested. | |
4206 TEST(HttpCache, RangeGET_206ReturnsSubrangeRange_NoCachedContent) { | |
4207 MockHttpCache cache; | |
4208 std::string headers; | |
4209 | |
4210 // Request a large range (40-59). The server sends 40-49. | |
4211 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
4212 transaction.request_headers = "Range: bytes = 40-59\r\n" EXTRA_HEADER; | |
4213 transaction.response_headers = | |
4214 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
4215 "ETag: \"foo\"\n" | |
4216 "Accept-Ranges: bytes\n" | |
4217 "Content-Length: 10\n" | |
4218 "Content-Range: bytes 40-49/80\n"; | |
4219 transaction.handler = nullptr; | |
4220 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4221 | |
4222 Verify206Response(headers, 40, 49); | |
4223 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4224 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4225 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4226 } | |
4227 | |
4228 // Tests that when a server returns 206 with a sub-range of the requested range, | |
4229 // and there was an entry stored in the cache, the cache gets out of the way. | |
4230 TEST(HttpCache, RangeGET_206ReturnsSubrangeRange_CachedContent) { | |
4231 MockHttpCache cache; | |
4232 std::string headers; | |
4233 | |
4234 // Write to the cache (70-79). | |
4235 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
4236 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; | |
4237 transaction.data = "rg: 70-79 "; | |
4238 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4239 Verify206Response(headers, 70, 79); | |
4240 | |
4241 // Request a large range (40-79). The cache will ask the server for 40-59. | |
4242 // The server returns 40-49. The cache should consider the server confused and | |
4243 // abort caching, restarting the request without caching. | |
4244 transaction.request_headers = "Range: bytes = 40-79\r\n" EXTRA_HEADER; | |
4245 transaction.response_headers = | |
4246 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
4247 "ETag: \"foo\"\n" | |
4248 "Accept-Ranges: bytes\n" | |
4249 "Content-Length: 10\n" | |
4250 "Content-Range: bytes 40-49/80\n"; | |
4251 transaction.handler = nullptr; | |
4252 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4253 | |
4254 // Two new network requests were issued, one from the cache and another after | |
4255 // deleting the entry. | |
4256 Verify206Response(headers, 40, 49); | |
4257 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
4258 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4259 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4260 | |
4261 // The entry was deleted. | |
4262 RunTransactionTest(cache.http_cache(), transaction); | |
4263 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | |
4264 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4265 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
4266 } | |
4267 | |
4268 // Tests that when a server returns 206 with a sub-range of the requested range, | |
4269 // and there was an entry stored in the cache, the cache gets out of the way, | |
4270 // when the caller is not using ranges. | |
4271 TEST(HttpCache, GET_206ReturnsSubrangeRange_CachedContent) { | |
4272 MockHttpCache cache; | |
4273 std::string headers; | |
4274 | |
4275 // Write to the cache (70-79). | |
4276 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
4277 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; | |
4278 transaction.data = "rg: 70-79 "; | |
4279 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4280 Verify206Response(headers, 70, 79); | |
4281 | |
4282 // Don't ask for a range. The cache will ask the server for 0-69. | |
4283 // The server returns 40-49. The cache should consider the server confused and | |
4284 // abort caching, restarting the request. | |
4285 // The second network request should not be a byte range request so the server | |
4286 // should return 200 + "Not a range" | |
4287 transaction.request_headers = "X-Return-Default-Range:\r\n" EXTRA_HEADER; | |
4288 transaction.data = "Not a range"; | |
4289 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4290 | |
4291 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); | |
4292 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
4293 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4294 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4295 | |
4296 // The entry was deleted. | |
4297 RunTransactionTest(cache.http_cache(), transaction); | |
4298 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | |
4299 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4300 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
4301 } | |
4302 | |
4303 // Tests that when a server returns 206 with a random range and there is | |
4304 // nothing stored in the cache, the returned response is passed to the caller | |
4305 // as is. In this context, a WrongRange means that the returned range may or may | |
4306 // not have any relationship with the requested range (may or may not be | |
4307 // contained). The important part is that the first byte doesn't match the first | |
4308 // requested byte. | |
4309 TEST(HttpCache, RangeGET_206ReturnsWrongRange_NoCachedContent) { | |
4310 MockHttpCache cache; | |
4311 std::string headers; | |
4312 | |
4313 // Request a large range (30-59). The server sends (40-49). | |
4314 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
4315 transaction.request_headers = "Range: bytes = 30-59\r\n" EXTRA_HEADER; | |
4316 transaction.response_headers = | |
4317 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
4318 "ETag: \"foo\"\n" | |
4319 "Accept-Ranges: bytes\n" | |
4320 "Content-Length: 10\n" | |
4321 "Content-Range: bytes 40-49/80\n"; | |
4322 transaction.handler = nullptr; | |
4323 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4324 | |
4325 Verify206Response(headers, 40, 49); | |
4326 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4327 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4328 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4329 | |
4330 // The entry was deleted. | |
4331 RunTransactionTest(cache.http_cache(), transaction); | |
4332 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4333 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4334 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
4335 } | |
4336 | |
4337 // Tests that when a server returns 206 with a random range and there is | |
4338 // an entry stored in the cache, the cache gets out of the way. | |
4339 TEST(HttpCache, RangeGET_206ReturnsWrongRange_CachedContent) { | |
4340 MockHttpCache cache; | |
4341 std::string headers; | |
4342 | |
4343 // Write to the cache (70-79). | |
4344 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
4345 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; | |
4346 transaction.data = "rg: 70-79 "; | |
4347 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4348 Verify206Response(headers, 70, 79); | |
4349 | |
4350 // Request a large range (30-79). The cache will ask the server for 30-69. | |
4351 // The server returns 40-49. The cache should consider the server confused and | |
4352 // abort caching, returning the weird range to the caller. | |
4353 transaction.request_headers = "Range: bytes = 30-79\r\n" EXTRA_HEADER; | |
4354 transaction.response_headers = | |
4355 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
4356 "ETag: \"foo\"\n" | |
4357 "Accept-Ranges: bytes\n" | |
4358 "Content-Length: 10\n" | |
4359 "Content-Range: bytes 40-49/80\n"; | |
4360 transaction.handler = nullptr; | |
4361 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4362 | |
4363 Verify206Response(headers, 40, 49); | |
4364 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
4365 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4366 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4367 | |
4368 // The entry was deleted. | |
4369 RunTransactionTest(cache.http_cache(), transaction); | |
4370 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | |
4371 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4372 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
4373 } | |
4374 | |
4375 // Tests that when a caller asks for a range beyond EOF, with an empty cache, | |
4376 // the response matches the one provided by the server. | |
4377 TEST(HttpCache, RangeGET_206ReturnsSmallerFile_NoCachedContent) { | |
4378 MockHttpCache cache; | |
4379 std::string headers; | |
4380 | |
4381 // Request a large range (70-99). The server sends 70-79. | |
4382 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
4383 transaction.request_headers = "Range: bytes = 70-99\r\n" EXTRA_HEADER; | |
4384 transaction.data = "rg: 70-79 "; | |
4385 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4386 | |
4387 Verify206Response(headers, 70, 79); | |
4388 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4389 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4390 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4391 | |
4392 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
4393 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4394 } | |
4395 | |
4396 // Tests that when a caller asks for a range beyond EOF, with a cached entry, | |
4397 // the cache automatically fixes the request. | |
4398 TEST(HttpCache, RangeGET_206ReturnsSmallerFile_CachedContent) { | |
4399 MockHttpCache cache; | |
4400 std::string headers; | |
4401 | |
4402 // Write to the cache (40-49). | |
4403 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
4404 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4405 | |
4406 // Request a large range (70-99). The server sends 70-79. | |
4407 transaction.request_headers = "Range: bytes = 70-99\r\n" EXTRA_HEADER; | |
4408 transaction.data = "rg: 70-79 "; | |
4409 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4410 | |
4411 Verify206Response(headers, 70, 79); | |
4412 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4413 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4414 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4415 | |
4416 // The entry was not deleted (the range was automatically fixed). | |
4417 RunTransactionTest(cache.http_cache(), transaction); | |
4418 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4419 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
4420 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4421 } | |
4422 | |
4423 // Tests that when a caller asks for a not-satisfiable range, the server's | |
4424 // response is forwarded to the caller. | |
4425 TEST(HttpCache, RangeGET_416_NoCachedContent) { | |
4426 MockHttpCache cache; | |
4427 std::string headers; | |
4428 | |
4429 // Request a range beyond EOF (80-99). | |
4430 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
4431 transaction.request_headers = "Range: bytes = 80-99\r\n" EXTRA_HEADER; | |
4432 transaction.data = ""; | |
4433 transaction.status = "HTTP/1.1 416 Requested Range Not Satisfiable"; | |
4434 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4435 | |
4436 EXPECT_EQ(0U, headers.find(transaction.status)); | |
4437 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4438 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4439 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4440 | |
4441 // The entry was deleted. | |
4442 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
4443 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
4444 } | |
4445 | |
4446 // Tests that we cache 301s for range requests. | |
4447 TEST(HttpCache, RangeGET_301) { | |
4448 MockHttpCache cache; | |
4449 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
4450 transaction.status = "HTTP/1.1 301 Moved Permanently"; | |
4451 transaction.response_headers = "Location: http://www.bar.com/\n"; | |
4452 transaction.data = ""; | |
4453 transaction.handler = NULL; | |
4454 | |
4455 // Write to the cache. | |
4456 RunTransactionTest(cache.http_cache(), transaction); | |
4457 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4458 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4459 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4460 | |
4461 // Read from the cache. | |
4462 RunTransactionTest(cache.http_cache(), transaction); | |
4463 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4464 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4465 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4466 } | |
4467 | |
4468 // Tests that we can cache range requests when the start or end is unknown. | |
4469 // We start with one suffix request, followed by a request from a given point. | |
4470 TEST(HttpCache, UnknownRangeGET_1) { | |
4471 MockHttpCache cache; | |
4472 AddMockTransaction(&kRangeGET_TransactionOK); | |
4473 std::string headers; | |
4474 | |
4475 // Write to the cache (70-79). | |
4476 MockTransaction transaction(kRangeGET_TransactionOK); | |
4477 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; | |
4478 transaction.data = "rg: 70-79 "; | |
4479 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4480 | |
4481 Verify206Response(headers, 70, 79); | |
4482 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4483 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4484 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4485 | |
4486 // Make sure we are done with the previous transaction. | |
4487 base::MessageLoop::current()->RunUntilIdle(); | |
4488 | |
4489 // Write and read from the cache (60-79). | |
4490 transaction.request_headers = "Range: bytes = 60-\r\n" EXTRA_HEADER; | |
4491 transaction.data = "rg: 60-69 rg: 70-79 "; | |
4492 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4493 | |
4494 Verify206Response(headers, 60, 79); | |
4495 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4496 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4497 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4498 | |
4499 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
4500 } | |
4501 | |
4502 // Tests that we can cache range requests when the start or end is unknown. | |
4503 // We start with one request from a given point, followed by a suffix request. | |
4504 // We'll also verify that synchronous cache responses work as intended. | |
4505 TEST(HttpCache, UnknownRangeGET_2) { | |
4506 MockHttpCache cache; | |
4507 std::string headers; | |
4508 | |
4509 MockTransaction transaction(kRangeGET_TransactionOK); | |
4510 transaction.test_mode = TEST_MODE_SYNC_CACHE_START | | |
4511 TEST_MODE_SYNC_CACHE_READ | | |
4512 TEST_MODE_SYNC_CACHE_WRITE; | |
4513 AddMockTransaction(&transaction); | |
4514 | |
4515 // Write to the cache (70-79). | |
4516 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER; | |
4517 transaction.data = "rg: 70-79 "; | |
4518 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4519 | |
4520 Verify206Response(headers, 70, 79); | |
4521 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4522 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4523 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4524 | |
4525 // Make sure we are done with the previous transaction. | |
4526 base::MessageLoop::current()->RunUntilIdle(); | |
4527 | |
4528 // Write and read from the cache (60-79). | |
4529 transaction.request_headers = "Range: bytes = -20\r\n" EXTRA_HEADER; | |
4530 transaction.data = "rg: 60-69 rg: 70-79 "; | |
4531 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4532 | |
4533 Verify206Response(headers, 60, 79); | |
4534 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4535 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4536 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4537 | |
4538 RemoveMockTransaction(&transaction); | |
4539 } | |
4540 | |
4541 // Tests that receiving Not Modified when asking for an open range doesn't mess | |
4542 // up things. | |
4543 TEST(HttpCache, UnknownRangeGET_304) { | |
4544 MockHttpCache cache; | |
4545 std::string headers; | |
4546 | |
4547 MockTransaction transaction(kRangeGET_TransactionOK); | |
4548 AddMockTransaction(&transaction); | |
4549 | |
4550 RangeTransactionServer handler; | |
4551 handler.set_not_modified(true); | |
4552 | |
4553 // Ask for the end of the file, without knowing the length. | |
4554 transaction.request_headers = "Range: bytes = 70-\r\n" EXTRA_HEADER; | |
4555 transaction.data = ""; | |
4556 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4557 | |
4558 // We just bypass the cache. | |
4559 EXPECT_EQ(0U, headers.find("HTTP/1.1 304 Not Modified\n")); | |
4560 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4561 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4562 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4563 | |
4564 RunTransactionTest(cache.http_cache(), transaction); | |
4565 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
4566 | |
4567 RemoveMockTransaction(&transaction); | |
4568 } | |
4569 | |
4570 // Tests that we can handle non-range requests when we have cached a range. | |
4571 TEST(HttpCache, GET_Previous206) { | |
4572 MockHttpCache cache; | |
4573 AddMockTransaction(&kRangeGET_TransactionOK); | |
4574 std::string headers; | |
4575 net::CapturingBoundNetLog log; | |
4576 net::LoadTimingInfo load_timing_info; | |
4577 | |
4578 // Write to the cache (40-49). | |
4579 RunTransactionTestWithResponseAndGetTiming( | |
4580 cache.http_cache(), kRangeGET_TransactionOK, &headers, log.bound(), | |
4581 &load_timing_info); | |
4582 | |
4583 Verify206Response(headers, 40, 49); | |
4584 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4585 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4586 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4587 TestLoadTimingNetworkRequest(load_timing_info); | |
4588 | |
4589 // Write and read from the cache (0-79), when not asked for a range. | |
4590 MockTransaction transaction(kRangeGET_TransactionOK); | |
4591 transaction.request_headers = EXTRA_HEADER; | |
4592 transaction.data = kFullRangeData; | |
4593 RunTransactionTestWithResponseAndGetTiming( | |
4594 cache.http_cache(), transaction, &headers, log.bound(), | |
4595 &load_timing_info); | |
4596 | |
4597 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); | |
4598 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
4599 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4600 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4601 TestLoadTimingNetworkRequest(load_timing_info); | |
4602 | |
4603 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
4604 } | |
4605 | |
4606 // Tests that we can handle non-range requests when we have cached the first | |
4607 // part of the object and the server replies with 304 (Not Modified). | |
4608 TEST(HttpCache, GET_Previous206_NotModified) { | |
4609 MockHttpCache cache; | |
4610 | |
4611 MockTransaction transaction(kRangeGET_TransactionOK); | |
4612 AddMockTransaction(&transaction); | |
4613 std::string headers; | |
4614 net::CapturingBoundNetLog log; | |
4615 net::LoadTimingInfo load_timing_info; | |
4616 | |
4617 // Write to the cache (0-9). | |
4618 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; | |
4619 transaction.data = "rg: 00-09 "; | |
4620 RunTransactionTestWithResponseAndGetTiming( | |
4621 cache.http_cache(), transaction, &headers, log.bound(), | |
4622 &load_timing_info); | |
4623 Verify206Response(headers, 0, 9); | |
4624 TestLoadTimingNetworkRequest(load_timing_info); | |
4625 | |
4626 // Write to the cache (70-79). | |
4627 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; | |
4628 transaction.data = "rg: 70-79 "; | |
4629 RunTransactionTestWithResponseAndGetTiming( | |
4630 cache.http_cache(), transaction, &headers, log.bound(), | |
4631 &load_timing_info); | |
4632 Verify206Response(headers, 70, 79); | |
4633 | |
4634 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4635 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4636 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4637 TestLoadTimingNetworkRequest(load_timing_info); | |
4638 | |
4639 // Read from the cache (0-9), write and read from cache (10 - 79). | |
4640 transaction.load_flags |= net::LOAD_VALIDATE_CACHE; | |
4641 transaction.request_headers = "Foo: bar\r\n" EXTRA_HEADER; | |
4642 transaction.data = kFullRangeData; | |
4643 RunTransactionTestWithResponseAndGetTiming( | |
4644 cache.http_cache(), transaction, &headers, log.bound(), | |
4645 &load_timing_info); | |
4646 | |
4647 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); | |
4648 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | |
4649 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
4650 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4651 TestLoadTimingNetworkRequest(load_timing_info); | |
4652 | |
4653 RemoveMockTransaction(&transaction); | |
4654 } | |
4655 | |
4656 // Tests that we can handle a regular request to a sparse entry, that results in | |
4657 // new content provided by the server (206). | |
4658 TEST(HttpCache, GET_Previous206_NewContent) { | |
4659 MockHttpCache cache; | |
4660 AddMockTransaction(&kRangeGET_TransactionOK); | |
4661 std::string headers; | |
4662 | |
4663 // Write to the cache (0-9). | |
4664 MockTransaction transaction(kRangeGET_TransactionOK); | |
4665 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; | |
4666 transaction.data = "rg: 00-09 "; | |
4667 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4668 | |
4669 Verify206Response(headers, 0, 9); | |
4670 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4671 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4672 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4673 | |
4674 // Now we'll issue a request without any range that should result first in a | |
4675 // 206 (when revalidating), and then in a weird standard answer: the test | |
4676 // server will not modify the response so we'll get the default range... a | |
4677 // real server will answer with 200. | |
4678 MockTransaction transaction2(kRangeGET_TransactionOK); | |
4679 transaction2.request_headers = EXTRA_HEADER; | |
4680 transaction2.load_flags |= net::LOAD_VALIDATE_CACHE; | |
4681 transaction2.data = "Not a range"; | |
4682 RangeTransactionServer handler; | |
4683 handler.set_modified(true); | |
4684 net::CapturingBoundNetLog log; | |
4685 net::LoadTimingInfo load_timing_info; | |
4686 RunTransactionTestWithResponseAndGetTiming( | |
4687 cache.http_cache(), transaction2, &headers, log.bound(), | |
4688 &load_timing_info); | |
4689 | |
4690 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); | |
4691 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
4692 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4693 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4694 TestLoadTimingNetworkRequest(load_timing_info); | |
4695 | |
4696 // Verify that the previous request deleted the entry. | |
4697 RunTransactionTest(cache.http_cache(), transaction); | |
4698 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
4699 | |
4700 RemoveMockTransaction(&transaction); | |
4701 } | |
4702 | |
4703 // Tests that we can handle cached 206 responses that are not sparse. | |
4704 TEST(HttpCache, GET_Previous206_NotSparse) { | |
4705 MockHttpCache cache; | |
4706 | |
4707 // Create a disk cache entry that stores 206 headers while not being sparse. | |
4708 disk_cache::Entry* entry; | |
4709 ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry, | |
4710 NULL)); | |
4711 | |
4712 std::string raw_headers(kRangeGET_TransactionOK.status); | |
4713 raw_headers.append("\n"); | |
4714 raw_headers.append(kRangeGET_TransactionOK.response_headers); | |
4715 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), | |
4716 raw_headers.size()); | |
4717 | |
4718 net::HttpResponseInfo response; | |
4719 response.headers = new net::HttpResponseHeaders(raw_headers); | |
4720 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); | |
4721 | |
4722 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500)); | |
4723 int len = static_cast<int>(base::strlcpy(buf->data(), | |
4724 kRangeGET_TransactionOK.data, 500)); | |
4725 net::TestCompletionCallback cb; | |
4726 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true); | |
4727 EXPECT_EQ(len, cb.GetResult(rv)); | |
4728 entry->Close(); | |
4729 | |
4730 // Now see that we don't use the stored entry. | |
4731 std::string headers; | |
4732 net::CapturingBoundNetLog log; | |
4733 net::LoadTimingInfo load_timing_info; | |
4734 RunTransactionTestWithResponseAndGetTiming( | |
4735 cache.http_cache(), kSimpleGET_Transaction, &headers, log.bound(), | |
4736 &load_timing_info); | |
4737 | |
4738 // We are expecting a 200. | |
4739 std::string expected_headers(kSimpleGET_Transaction.status); | |
4740 expected_headers.append("\n"); | |
4741 expected_headers.append(kSimpleGET_Transaction.response_headers); | |
4742 EXPECT_EQ(expected_headers, headers); | |
4743 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4744 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4745 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
4746 TestLoadTimingNetworkRequest(load_timing_info); | |
4747 } | |
4748 | |
4749 // Tests that we can handle cached 206 responses that are not sparse. This time | |
4750 // we issue a range request and expect to receive a range. | |
4751 TEST(HttpCache, RangeGET_Previous206_NotSparse_2) { | |
4752 MockHttpCache cache; | |
4753 AddMockTransaction(&kRangeGET_TransactionOK); | |
4754 | |
4755 // Create a disk cache entry that stores 206 headers while not being sparse. | |
4756 disk_cache::Entry* entry; | |
4757 ASSERT_TRUE(cache.CreateBackendEntry(kRangeGET_TransactionOK.url, &entry, | |
4758 NULL)); | |
4759 | |
4760 std::string raw_headers(kRangeGET_TransactionOK.status); | |
4761 raw_headers.append("\n"); | |
4762 raw_headers.append(kRangeGET_TransactionOK.response_headers); | |
4763 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), | |
4764 raw_headers.size()); | |
4765 | |
4766 net::HttpResponseInfo response; | |
4767 response.headers = new net::HttpResponseHeaders(raw_headers); | |
4768 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); | |
4769 | |
4770 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500)); | |
4771 int len = static_cast<int>(base::strlcpy(buf->data(), | |
4772 kRangeGET_TransactionOK.data, 500)); | |
4773 net::TestCompletionCallback cb; | |
4774 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true); | |
4775 EXPECT_EQ(len, cb.GetResult(rv)); | |
4776 entry->Close(); | |
4777 | |
4778 // Now see that we don't use the stored entry. | |
4779 std::string headers; | |
4780 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, | |
4781 &headers); | |
4782 | |
4783 // We are expecting a 206. | |
4784 Verify206Response(headers, 40, 49); | |
4785 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4786 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4787 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
4788 | |
4789 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
4790 } | |
4791 | |
4792 // Tests that we can handle cached 206 responses that can't be validated. | |
4793 TEST(HttpCache, GET_Previous206_NotValidation) { | |
4794 MockHttpCache cache; | |
4795 | |
4796 // Create a disk cache entry that stores 206 headers. | |
4797 disk_cache::Entry* entry; | |
4798 ASSERT_TRUE(cache.CreateBackendEntry(kSimpleGET_Transaction.url, &entry, | |
4799 NULL)); | |
4800 | |
4801 // Make sure that the headers cannot be validated with the server. | |
4802 std::string raw_headers(kRangeGET_TransactionOK.status); | |
4803 raw_headers.append("\n"); | |
4804 raw_headers.append("Content-Length: 80\n"); | |
4805 raw_headers = net::HttpUtil::AssembleRawHeaders(raw_headers.data(), | |
4806 raw_headers.size()); | |
4807 | |
4808 net::HttpResponseInfo response; | |
4809 response.headers = new net::HttpResponseHeaders(raw_headers); | |
4810 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); | |
4811 | |
4812 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(500)); | |
4813 int len = static_cast<int>(base::strlcpy(buf->data(), | |
4814 kRangeGET_TransactionOK.data, 500)); | |
4815 net::TestCompletionCallback cb; | |
4816 int rv = entry->WriteData(1, 0, buf.get(), len, cb.callback(), true); | |
4817 EXPECT_EQ(len, cb.GetResult(rv)); | |
4818 entry->Close(); | |
4819 | |
4820 // Now see that we don't use the stored entry. | |
4821 std::string headers; | |
4822 RunTransactionTestWithResponse(cache.http_cache(), kSimpleGET_Transaction, | |
4823 &headers); | |
4824 | |
4825 // We are expecting a 200. | |
4826 std::string expected_headers(kSimpleGET_Transaction.status); | |
4827 expected_headers.append("\n"); | |
4828 expected_headers.append(kSimpleGET_Transaction.response_headers); | |
4829 EXPECT_EQ(expected_headers, headers); | |
4830 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4831 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4832 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
4833 } | |
4834 | |
4835 // Tests that we can handle range requests with cached 200 responses. | |
4836 TEST(HttpCache, RangeGET_Previous200) { | |
4837 MockHttpCache cache; | |
4838 | |
4839 // Store the whole thing with status 200. | |
4840 MockTransaction transaction(kTypicalGET_Transaction); | |
4841 transaction.url = kRangeGET_TransactionOK.url; | |
4842 transaction.data = kFullRangeData; | |
4843 AddMockTransaction(&transaction); | |
4844 RunTransactionTest(cache.http_cache(), transaction); | |
4845 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4846 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4847 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4848 | |
4849 RemoveMockTransaction(&transaction); | |
4850 AddMockTransaction(&kRangeGET_TransactionOK); | |
4851 | |
4852 // Now see that we use the stored entry. | |
4853 std::string headers; | |
4854 MockTransaction transaction2(kRangeGET_TransactionOK); | |
4855 RangeTransactionServer handler; | |
4856 handler.set_not_modified(true); | |
4857 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); | |
4858 | |
4859 // We are expecting a 206. | |
4860 Verify206Response(headers, 40, 49); | |
4861 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4862 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4863 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4864 | |
4865 // The last transaction has finished so make sure the entry is deactivated. | |
4866 base::MessageLoop::current()->RunUntilIdle(); | |
4867 | |
4868 // Make a request for an invalid range. | |
4869 MockTransaction transaction3(kRangeGET_TransactionOK); | |
4870 transaction3.request_headers = "Range: bytes = 80-90\r\n" EXTRA_HEADER; | |
4871 transaction3.data = transaction.data; | |
4872 transaction3.load_flags = net::LOAD_PREFERRING_CACHE; | |
4873 RunTransactionTestWithResponse(cache.http_cache(), transaction3, &headers); | |
4874 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
4875 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 ")); | |
4876 EXPECT_EQ(std::string::npos, headers.find("Content-Range:")); | |
4877 EXPECT_EQ(std::string::npos, headers.find("Content-Length: 80")); | |
4878 | |
4879 // Make sure the entry is deactivated. | |
4880 base::MessageLoop::current()->RunUntilIdle(); | |
4881 | |
4882 // Even though the request was invalid, we should have the entry. | |
4883 RunTransactionTest(cache.http_cache(), transaction2); | |
4884 EXPECT_EQ(3, cache.disk_cache()->open_count()); | |
4885 | |
4886 // Make sure the entry is deactivated. | |
4887 base::MessageLoop::current()->RunUntilIdle(); | |
4888 | |
4889 // Now we should receive a range from the server and drop the stored entry. | |
4890 handler.set_not_modified(false); | |
4891 transaction2.request_headers = kRangeGET_TransactionOK.request_headers; | |
4892 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); | |
4893 Verify206Response(headers, 40, 49); | |
4894 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | |
4895 EXPECT_EQ(4, cache.disk_cache()->open_count()); | |
4896 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4897 | |
4898 RunTransactionTest(cache.http_cache(), transaction2); | |
4899 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
4900 | |
4901 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
4902 } | |
4903 | |
4904 // Tests that we can handle a 200 response when dealing with sparse entries. | |
4905 TEST(HttpCache, RangeRequestResultsIn200) { | |
4906 MockHttpCache cache; | |
4907 AddMockTransaction(&kRangeGET_TransactionOK); | |
4908 std::string headers; | |
4909 | |
4910 // Write to the cache (70-79). | |
4911 MockTransaction transaction(kRangeGET_TransactionOK); | |
4912 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; | |
4913 transaction.data = "rg: 70-79 "; | |
4914 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4915 | |
4916 Verify206Response(headers, 70, 79); | |
4917 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4918 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4919 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4920 | |
4921 // Now we'll issue a request that results in a plain 200 response, but to | |
4922 // the to the same URL that we used to store sparse data, and making sure | |
4923 // that we ask for a range. | |
4924 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
4925 MockTransaction transaction2(kSimpleGET_Transaction); | |
4926 transaction2.url = kRangeGET_TransactionOK.url; | |
4927 transaction2.request_headers = kRangeGET_TransactionOK.request_headers; | |
4928 AddMockTransaction(&transaction2); | |
4929 | |
4930 RunTransactionTestWithResponse(cache.http_cache(), transaction2, &headers); | |
4931 | |
4932 std::string expected_headers(kSimpleGET_Transaction.status); | |
4933 expected_headers.append("\n"); | |
4934 expected_headers.append(kSimpleGET_Transaction.response_headers); | |
4935 EXPECT_EQ(expected_headers, headers); | |
4936 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4937 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4938 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4939 | |
4940 RemoveMockTransaction(&transaction2); | |
4941 } | |
4942 | |
4943 // Tests that a range request that falls outside of the size that we know about | |
4944 // only deletes the entry if the resource has indeed changed. | |
4945 TEST(HttpCache, RangeGET_MoreThanCurrentSize) { | |
4946 MockHttpCache cache; | |
4947 AddMockTransaction(&kRangeGET_TransactionOK); | |
4948 std::string headers; | |
4949 | |
4950 // Write to the cache (40-49). | |
4951 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, | |
4952 &headers); | |
4953 | |
4954 Verify206Response(headers, 40, 49); | |
4955 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4956 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4957 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4958 | |
4959 // A weird request should not delete this entry. Ask for bytes 120-. | |
4960 MockTransaction transaction(kRangeGET_TransactionOK); | |
4961 transaction.request_headers = "Range: bytes = 120-\r\n" EXTRA_HEADER; | |
4962 transaction.data = ""; | |
4963 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
4964 | |
4965 EXPECT_EQ(0U, headers.find("HTTP/1.1 416 ")); | |
4966 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
4967 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
4968 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4969 | |
4970 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
4971 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
4972 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4973 | |
4974 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
4975 } | |
4976 | |
4977 // Tests that we don't delete a sparse entry when we cancel a request. | |
4978 TEST(HttpCache, RangeGET_Cancel) { | |
4979 MockHttpCache cache; | |
4980 AddMockTransaction(&kRangeGET_TransactionOK); | |
4981 | |
4982 MockHttpRequest request(kRangeGET_TransactionOK); | |
4983 | |
4984 Context* c = new Context(); | |
4985 int rv = cache.CreateTransaction(&c->trans); | |
4986 ASSERT_EQ(net::OK, rv); | |
4987 | |
4988 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
4989 if (rv == net::ERR_IO_PENDING) | |
4990 rv = c->callback.WaitForResult(); | |
4991 | |
4992 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
4993 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
4994 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
4995 | |
4996 // Make sure that the entry has some data stored. | |
4997 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); | |
4998 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | |
4999 if (rv == net::ERR_IO_PENDING) | |
5000 rv = c->callback.WaitForResult(); | |
5001 EXPECT_EQ(buf->size(), rv); | |
5002 | |
5003 // Destroy the transaction. | |
5004 delete c; | |
5005 | |
5006 // Verify that the entry has not been deleted. | |
5007 disk_cache::Entry* entry; | |
5008 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
5009 entry->Close(); | |
5010 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5011 } | |
5012 | |
5013 // Tests that we don't delete a sparse entry when we start a new request after | |
5014 // cancelling the previous one. | |
5015 TEST(HttpCache, RangeGET_Cancel2) { | |
5016 MockHttpCache cache; | |
5017 AddMockTransaction(&kRangeGET_TransactionOK); | |
5018 | |
5019 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
5020 MockHttpRequest request(kRangeGET_TransactionOK); | |
5021 request.load_flags |= net::LOAD_VALIDATE_CACHE; | |
5022 | |
5023 Context* c = new Context(); | |
5024 int rv = cache.CreateTransaction(&c->trans); | |
5025 ASSERT_EQ(net::OK, rv); | |
5026 | |
5027 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
5028 if (rv == net::ERR_IO_PENDING) | |
5029 rv = c->callback.WaitForResult(); | |
5030 | |
5031 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5032 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5033 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5034 | |
5035 // Make sure that we revalidate the entry and read from the cache (a single | |
5036 // read will return while waiting for the network). | |
5037 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(5)); | |
5038 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | |
5039 EXPECT_EQ(5, c->callback.GetResult(rv)); | |
5040 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | |
5041 EXPECT_EQ(net::ERR_IO_PENDING, rv); | |
5042 | |
5043 // Destroy the transaction before completing the read. | |
5044 delete c; | |
5045 | |
5046 // We have the read and the delete (OnProcessPendingQueue) waiting on the | |
5047 // message loop. This means that a new transaction will just reuse the same | |
5048 // active entry (no open or create). | |
5049 | |
5050 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
5051 | |
5052 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5053 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5054 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5055 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5056 } | |
5057 | |
5058 // A slight variation of the previous test, this time we cancel two requests in | |
5059 // a row, making sure that the second is waiting for the entry to be ready. | |
5060 TEST(HttpCache, RangeGET_Cancel3) { | |
5061 MockHttpCache cache; | |
5062 AddMockTransaction(&kRangeGET_TransactionOK); | |
5063 | |
5064 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
5065 MockHttpRequest request(kRangeGET_TransactionOK); | |
5066 request.load_flags |= net::LOAD_VALIDATE_CACHE; | |
5067 | |
5068 Context* c = new Context(); | |
5069 int rv = cache.CreateTransaction(&c->trans); | |
5070 ASSERT_EQ(net::OK, rv); | |
5071 | |
5072 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
5073 EXPECT_EQ(net::ERR_IO_PENDING, rv); | |
5074 rv = c->callback.WaitForResult(); | |
5075 | |
5076 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5077 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5078 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5079 | |
5080 // Make sure that we revalidate the entry and read from the cache (a single | |
5081 // read will return while waiting for the network). | |
5082 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(5)); | |
5083 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | |
5084 EXPECT_EQ(5, c->callback.GetResult(rv)); | |
5085 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | |
5086 EXPECT_EQ(net::ERR_IO_PENDING, rv); | |
5087 | |
5088 // Destroy the transaction before completing the read. | |
5089 delete c; | |
5090 | |
5091 // We have the read and the delete (OnProcessPendingQueue) waiting on the | |
5092 // message loop. This means that a new transaction will just reuse the same | |
5093 // active entry (no open or create). | |
5094 | |
5095 c = new Context(); | |
5096 rv = cache.CreateTransaction(&c->trans); | |
5097 ASSERT_EQ(net::OK, rv); | |
5098 | |
5099 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
5100 EXPECT_EQ(net::ERR_IO_PENDING, rv); | |
5101 | |
5102 MockDiskEntry::IgnoreCallbacks(true); | |
5103 base::MessageLoop::current()->RunUntilIdle(); | |
5104 MockDiskEntry::IgnoreCallbacks(false); | |
5105 | |
5106 // The new transaction is waiting for the query range callback. | |
5107 delete c; | |
5108 | |
5109 // And we should not crash when the callback is delivered. | |
5110 base::MessageLoop::current()->RunUntilIdle(); | |
5111 | |
5112 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5113 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5114 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5115 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5116 } | |
5117 | |
5118 // Tests that an invalid range response results in no cached entry. | |
5119 TEST(HttpCache, RangeGET_InvalidResponse1) { | |
5120 MockHttpCache cache; | |
5121 std::string headers; | |
5122 | |
5123 MockTransaction transaction(kRangeGET_TransactionOK); | |
5124 transaction.handler = NULL; | |
5125 transaction.response_headers = "Content-Range: bytes 40-49/45\n" | |
5126 "Content-Length: 10\n"; | |
5127 AddMockTransaction(&transaction); | |
5128 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
5129 | |
5130 std::string expected(transaction.status); | |
5131 expected.append("\n"); | |
5132 expected.append(transaction.response_headers); | |
5133 EXPECT_EQ(expected, headers); | |
5134 | |
5135 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5136 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5137 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5138 | |
5139 // Verify that we don't have a cached entry. | |
5140 disk_cache::Entry* entry; | |
5141 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
5142 | |
5143 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5144 } | |
5145 | |
5146 // Tests that we reject a range that doesn't match the content-length. | |
5147 TEST(HttpCache, RangeGET_InvalidResponse2) { | |
5148 MockHttpCache cache; | |
5149 std::string headers; | |
5150 | |
5151 MockTransaction transaction(kRangeGET_TransactionOK); | |
5152 transaction.handler = NULL; | |
5153 transaction.response_headers = "Content-Range: bytes 40-49/80\n" | |
5154 "Content-Length: 20\n"; | |
5155 AddMockTransaction(&transaction); | |
5156 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
5157 | |
5158 std::string expected(transaction.status); | |
5159 expected.append("\n"); | |
5160 expected.append(transaction.response_headers); | |
5161 EXPECT_EQ(expected, headers); | |
5162 | |
5163 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5164 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5165 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5166 | |
5167 // Verify that we don't have a cached entry. | |
5168 disk_cache::Entry* entry; | |
5169 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
5170 | |
5171 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5172 } | |
5173 | |
5174 // Tests that if a server tells us conflicting information about a resource we | |
5175 // drop the entry. | |
5176 TEST(HttpCache, RangeGET_InvalidResponse3) { | |
5177 MockHttpCache cache; | |
5178 std::string headers; | |
5179 | |
5180 MockTransaction transaction(kRangeGET_TransactionOK); | |
5181 transaction.handler = NULL; | |
5182 transaction.request_headers = "Range: bytes = 50-59\r\n" EXTRA_HEADER; | |
5183 std::string response_headers(transaction.response_headers); | |
5184 response_headers.append("Content-Range: bytes 50-59/160\n"); | |
5185 transaction.response_headers = response_headers.c_str(); | |
5186 AddMockTransaction(&transaction); | |
5187 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
5188 | |
5189 Verify206Response(headers, 50, 59); | |
5190 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5191 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5192 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5193 | |
5194 RemoveMockTransaction(&transaction); | |
5195 AddMockTransaction(&kRangeGET_TransactionOK); | |
5196 | |
5197 // This transaction will report a resource size of 80 bytes, and we think it's | |
5198 // 160 so we should ignore the response. | |
5199 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, | |
5200 &headers); | |
5201 | |
5202 Verify206Response(headers, 40, 49); | |
5203 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5204 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5205 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5206 | |
5207 // Verify that the entry is gone. | |
5208 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
5209 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5210 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
5211 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5212 } | |
5213 | |
5214 // Tests that we handle large range values properly. | |
5215 TEST(HttpCache, RangeGET_LargeValues) { | |
5216 // We need a real sparse cache for this test. | |
5217 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(1024 * 1024)); | |
5218 std::string headers; | |
5219 | |
5220 MockTransaction transaction(kRangeGET_TransactionOK); | |
5221 transaction.handler = NULL; | |
5222 transaction.request_headers = "Range: bytes = 4294967288-4294967297\r\n" | |
5223 EXTRA_HEADER; | |
5224 transaction.response_headers = | |
5225 "ETag: \"foo\"\n" | |
5226 "Content-Range: bytes 4294967288-4294967297/4294967299\n" | |
5227 "Content-Length: 10\n"; | |
5228 AddMockTransaction(&transaction); | |
5229 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
5230 | |
5231 std::string expected(transaction.status); | |
5232 expected.append("\n"); | |
5233 expected.append(transaction.response_headers); | |
5234 EXPECT_EQ(expected, headers); | |
5235 | |
5236 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5237 | |
5238 // Verify that we have a cached entry. | |
5239 disk_cache::Entry* en; | |
5240 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &en)); | |
5241 en->Close(); | |
5242 | |
5243 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5244 } | |
5245 | |
5246 // Tests that we don't crash with a range request if the disk cache was not | |
5247 // initialized properly. | |
5248 TEST(HttpCache, RangeGET_NoDiskCache) { | |
5249 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | |
5250 factory->set_fail(true); | |
5251 factory->FinishCreation(); // We'll complete synchronously. | |
5252 MockHttpCache cache(factory); | |
5253 | |
5254 AddMockTransaction(&kRangeGET_TransactionOK); | |
5255 | |
5256 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
5257 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5258 | |
5259 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5260 } | |
5261 | |
5262 // Tests that we handle byte range requests that skip the cache. | |
5263 TEST(HttpCache, RangeHEAD) { | |
5264 MockHttpCache cache; | |
5265 AddMockTransaction(&kRangeGET_TransactionOK); | |
5266 | |
5267 MockTransaction transaction(kRangeGET_TransactionOK); | |
5268 transaction.request_headers = "Range: bytes = -10\r\n" EXTRA_HEADER; | |
5269 transaction.method = "HEAD"; | |
5270 transaction.data = "rg: 70-79 "; | |
5271 | |
5272 std::string headers; | |
5273 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
5274 | |
5275 Verify206Response(headers, 70, 79); | |
5276 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5277 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5278 EXPECT_EQ(0, cache.disk_cache()->create_count()); | |
5279 | |
5280 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5281 } | |
5282 | |
5283 // Tests that we don't crash when after reading from the cache we issue a | |
5284 // request for the next range and the server gives us a 200 synchronously. | |
5285 TEST(HttpCache, RangeGET_FastFlakyServer) { | |
5286 MockHttpCache cache; | |
5287 | |
5288 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | |
5289 transaction.request_headers = "Range: bytes = 40-\r\n" EXTRA_HEADER; | |
5290 transaction.test_mode = TEST_MODE_SYNC_NET_START; | |
5291 transaction.load_flags |= net::LOAD_VALIDATE_CACHE; | |
5292 | |
5293 // Write to the cache. | |
5294 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
5295 | |
5296 // And now read from the cache and the network. | |
5297 RangeTransactionServer handler; | |
5298 handler.set_bad_200(true); | |
5299 transaction.data = "Not a range"; | |
5300 net::CapturingBoundNetLog log; | |
5301 RunTransactionTestWithLog(cache.http_cache(), transaction, log.bound()); | |
5302 | |
5303 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
5304 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5305 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5306 EXPECT_TRUE(LogContainsEventType( | |
5307 log, net::NetLog::TYPE_HTTP_CACHE_RE_SEND_PARTIAL_REQUEST)); | |
5308 } | |
5309 | |
5310 // Tests that when the server gives us less data than expected, we don't keep | |
5311 // asking for more data. | |
5312 TEST(HttpCache, RangeGET_FastFlakyServer2) { | |
5313 MockHttpCache cache; | |
5314 | |
5315 // First, check with an empty cache (WRITE mode). | |
5316 MockTransaction transaction(kRangeGET_TransactionOK); | |
5317 transaction.request_headers = "Range: bytes = 40-49\r\n" EXTRA_HEADER; | |
5318 transaction.data = "rg: 40-"; // Less than expected. | |
5319 transaction.handler = NULL; | |
5320 std::string headers(transaction.response_headers); | |
5321 headers.append("Content-Range: bytes 40-49/80\n"); | |
5322 transaction.response_headers = headers.c_str(); | |
5323 | |
5324 AddMockTransaction(&transaction); | |
5325 | |
5326 // Write to the cache. | |
5327 RunTransactionTest(cache.http_cache(), transaction); | |
5328 | |
5329 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5330 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5331 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5332 | |
5333 // Now verify that even in READ_WRITE mode, we forward the bad response to | |
5334 // the caller. | |
5335 transaction.request_headers = "Range: bytes = 60-69\r\n" EXTRA_HEADER; | |
5336 transaction.data = "rg: 60-"; // Less than expected. | |
5337 headers = kRangeGET_TransactionOK.response_headers; | |
5338 headers.append("Content-Range: bytes 60-69/80\n"); | |
5339 transaction.response_headers = headers.c_str(); | |
5340 | |
5341 RunTransactionTest(cache.http_cache(), transaction); | |
5342 | |
5343 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5344 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5345 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5346 | |
5347 RemoveMockTransaction(&transaction); | |
5348 } | |
5349 | |
5350 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) | |
5351 // This test hits a NOTREACHED so it is a release mode only test. | |
5352 TEST(HttpCache, RangeGET_OK_LoadOnlyFromCache) { | |
5353 MockHttpCache cache; | |
5354 AddMockTransaction(&kRangeGET_TransactionOK); | |
5355 | |
5356 // Write to the cache (40-49). | |
5357 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
5358 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5359 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5360 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5361 | |
5362 // Force this transaction to read from the cache. | |
5363 MockTransaction transaction(kRangeGET_TransactionOK); | |
5364 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
5365 | |
5366 MockHttpRequest request(transaction); | |
5367 net::TestCompletionCallback callback; | |
5368 | |
5369 scoped_ptr<net::HttpTransaction> trans; | |
5370 int rv = cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, &trans); | |
5371 EXPECT_EQ(net::OK, rv); | |
5372 ASSERT_TRUE(trans.get()); | |
5373 | |
5374 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
5375 if (rv == net::ERR_IO_PENDING) | |
5376 rv = callback.WaitForResult(); | |
5377 ASSERT_EQ(net::ERR_CACHE_MISS, rv); | |
5378 | |
5379 trans.reset(); | |
5380 | |
5381 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5382 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5383 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5384 | |
5385 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5386 } | |
5387 #endif | |
5388 | |
5389 // Tests the handling of the "truncation" flag. | |
5390 TEST(HttpCache, WriteResponseInfo_Truncated) { | |
5391 MockHttpCache cache; | |
5392 disk_cache::Entry* entry; | |
5393 ASSERT_TRUE(cache.CreateBackendEntry("http://www.google.com", &entry, | |
5394 NULL)); | |
5395 | |
5396 std::string headers("HTTP/1.1 200 OK"); | |
5397 headers = net::HttpUtil::AssembleRawHeaders(headers.data(), headers.size()); | |
5398 net::HttpResponseInfo response; | |
5399 response.headers = new net::HttpResponseHeaders(headers); | |
5400 | |
5401 // Set the last argument for this to be an incomplete request. | |
5402 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, true)); | |
5403 bool truncated = false; | |
5404 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
5405 EXPECT_TRUE(truncated); | |
5406 | |
5407 // And now test the opposite case. | |
5408 EXPECT_TRUE(MockHttpCache::WriteResponseInfo(entry, &response, true, false)); | |
5409 truncated = true; | |
5410 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
5411 EXPECT_FALSE(truncated); | |
5412 entry->Close(); | |
5413 } | |
5414 | |
5415 // Tests basic pickling/unpickling of HttpResponseInfo. | |
5416 TEST(HttpCache, PersistHttpResponseInfo) { | |
5417 // Set some fields (add more if needed.) | |
5418 net::HttpResponseInfo response1; | |
5419 response1.was_cached = false; | |
5420 response1.socket_address = net::HostPortPair("1.2.3.4", 80); | |
5421 response1.headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK"); | |
5422 | |
5423 // Pickle. | |
5424 Pickle pickle; | |
5425 response1.Persist(&pickle, false, false); | |
5426 | |
5427 // Unpickle. | |
5428 net::HttpResponseInfo response2; | |
5429 bool response_truncated; | |
5430 EXPECT_TRUE(response2.InitFromPickle(pickle, &response_truncated)); | |
5431 EXPECT_FALSE(response_truncated); | |
5432 | |
5433 // Verify fields. | |
5434 EXPECT_TRUE(response2.was_cached); // InitFromPickle sets this flag. | |
5435 EXPECT_EQ("1.2.3.4", response2.socket_address.host()); | |
5436 EXPECT_EQ(80, response2.socket_address.port()); | |
5437 EXPECT_EQ("HTTP/1.1 200 OK", response2.headers->GetStatusLine()); | |
5438 } | |
5439 | |
5440 // Tests that we delete an entry when the request is cancelled before starting | |
5441 // to read from the network. | |
5442 TEST(HttpCache, DoomOnDestruction) { | |
5443 MockHttpCache cache; | |
5444 | |
5445 MockHttpRequest request(kSimpleGET_Transaction); | |
5446 | |
5447 Context* c = new Context(); | |
5448 int rv = cache.CreateTransaction(&c->trans); | |
5449 ASSERT_EQ(net::OK, rv); | |
5450 | |
5451 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
5452 if (rv == net::ERR_IO_PENDING) | |
5453 c->result = c->callback.WaitForResult(); | |
5454 | |
5455 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5456 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5457 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5458 | |
5459 // Destroy the transaction. We only have the headers so we should delete this | |
5460 // entry. | |
5461 delete c; | |
5462 | |
5463 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
5464 | |
5465 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5466 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5467 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
5468 } | |
5469 | |
5470 // Tests that we delete an entry when the request is cancelled if the response | |
5471 // does not have content-length and strong validators. | |
5472 TEST(HttpCache, DoomOnDestruction2) { | |
5473 MockHttpCache cache; | |
5474 | |
5475 MockHttpRequest request(kSimpleGET_Transaction); | |
5476 | |
5477 Context* c = new Context(); | |
5478 int rv = cache.CreateTransaction(&c->trans); | |
5479 ASSERT_EQ(net::OK, rv); | |
5480 | |
5481 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
5482 if (rv == net::ERR_IO_PENDING) | |
5483 rv = c->callback.WaitForResult(); | |
5484 | |
5485 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5486 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5487 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5488 | |
5489 // Make sure that the entry has some data stored. | |
5490 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); | |
5491 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | |
5492 if (rv == net::ERR_IO_PENDING) | |
5493 rv = c->callback.WaitForResult(); | |
5494 EXPECT_EQ(buf->size(), rv); | |
5495 | |
5496 // Destroy the transaction. | |
5497 delete c; | |
5498 | |
5499 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
5500 | |
5501 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5502 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5503 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
5504 } | |
5505 | |
5506 // Tests that we delete an entry when the request is cancelled if the response | |
5507 // has an "Accept-Ranges: none" header. | |
5508 TEST(HttpCache, DoomOnDestruction3) { | |
5509 MockHttpCache cache; | |
5510 | |
5511 MockTransaction transaction(kSimpleGET_Transaction); | |
5512 transaction.response_headers = | |
5513 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | |
5514 "Content-Length: 22\n" | |
5515 "Accept-Ranges: none\n" | |
5516 "Etag: \"foopy\"\n"; | |
5517 AddMockTransaction(&transaction); | |
5518 MockHttpRequest request(transaction); | |
5519 | |
5520 Context* c = new Context(); | |
5521 int rv = cache.CreateTransaction(&c->trans); | |
5522 ASSERT_EQ(net::OK, rv); | |
5523 | |
5524 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
5525 if (rv == net::ERR_IO_PENDING) | |
5526 rv = c->callback.WaitForResult(); | |
5527 | |
5528 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5529 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5530 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5531 | |
5532 // Make sure that the entry has some data stored. | |
5533 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); | |
5534 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | |
5535 if (rv == net::ERR_IO_PENDING) | |
5536 rv = c->callback.WaitForResult(); | |
5537 EXPECT_EQ(buf->size(), rv); | |
5538 | |
5539 // Destroy the transaction. | |
5540 delete c; | |
5541 | |
5542 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
5543 | |
5544 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5545 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5546 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
5547 | |
5548 RemoveMockTransaction(&transaction); | |
5549 } | |
5550 | |
5551 // Tests that we mark an entry as incomplete when the request is cancelled. | |
5552 TEST(HttpCache, SetTruncatedFlag) { | |
5553 MockHttpCache cache; | |
5554 | |
5555 MockTransaction transaction(kSimpleGET_Transaction); | |
5556 transaction.response_headers = | |
5557 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | |
5558 "Content-Length: 22\n" | |
5559 "Etag: \"foopy\"\n"; | |
5560 AddMockTransaction(&transaction); | |
5561 MockHttpRequest request(transaction); | |
5562 | |
5563 scoped_ptr<Context> c(new Context()); | |
5564 | |
5565 int rv = cache.CreateTransaction(&c->trans); | |
5566 ASSERT_EQ(net::OK, rv); | |
5567 | |
5568 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
5569 if (rv == net::ERR_IO_PENDING) | |
5570 rv = c->callback.WaitForResult(); | |
5571 | |
5572 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5573 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
5574 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5575 | |
5576 // Make sure that the entry has some data stored. | |
5577 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); | |
5578 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | |
5579 if (rv == net::ERR_IO_PENDING) | |
5580 rv = c->callback.WaitForResult(); | |
5581 EXPECT_EQ(buf->size(), rv); | |
5582 | |
5583 // We want to cancel the request when the transaction is busy. | |
5584 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | |
5585 EXPECT_EQ(net::ERR_IO_PENDING, rv); | |
5586 EXPECT_FALSE(c->callback.have_result()); | |
5587 | |
5588 MockHttpCache::SetTestMode(TEST_MODE_SYNC_ALL); | |
5589 | |
5590 // Destroy the transaction. | |
5591 c->trans.reset(); | |
5592 MockHttpCache::SetTestMode(0); | |
5593 | |
5594 | |
5595 // Make sure that we don't invoke the callback. We may have an issue if the | |
5596 // UrlRequestJob is killed directly (without cancelling the UrlRequest) so we | |
5597 // could end up with the transaction being deleted twice if we send any | |
5598 // notification from the transaction destructor (see http://crbug.com/31723). | |
5599 EXPECT_FALSE(c->callback.have_result()); | |
5600 | |
5601 // Verify that the entry is marked as incomplete. | |
5602 disk_cache::Entry* entry; | |
5603 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
5604 net::HttpResponseInfo response; | |
5605 bool truncated = false; | |
5606 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
5607 EXPECT_TRUE(truncated); | |
5608 entry->Close(); | |
5609 | |
5610 RemoveMockTransaction(&transaction); | |
5611 } | |
5612 | |
5613 // Tests that we don't mark an entry as truncated when we read everything. | |
5614 TEST(HttpCache, DontSetTruncatedFlag) { | |
5615 MockHttpCache cache; | |
5616 | |
5617 MockTransaction transaction(kSimpleGET_Transaction); | |
5618 transaction.response_headers = | |
5619 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | |
5620 "Content-Length: 22\n" | |
5621 "Etag: \"foopy\"\n"; | |
5622 AddMockTransaction(&transaction); | |
5623 MockHttpRequest request(transaction); | |
5624 | |
5625 scoped_ptr<Context> c(new Context()); | |
5626 int rv = cache.CreateTransaction(&c->trans); | |
5627 ASSERT_EQ(net::OK, rv); | |
5628 | |
5629 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
5630 EXPECT_EQ(net::OK, c->callback.GetResult(rv)); | |
5631 | |
5632 // Read everything. | |
5633 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(22)); | |
5634 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | |
5635 EXPECT_EQ(buf->size(), c->callback.GetResult(rv)); | |
5636 | |
5637 // Destroy the transaction. | |
5638 c->trans.reset(); | |
5639 | |
5640 // Verify that the entry is not marked as truncated. | |
5641 disk_cache::Entry* entry; | |
5642 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
5643 net::HttpResponseInfo response; | |
5644 bool truncated = true; | |
5645 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
5646 EXPECT_FALSE(truncated); | |
5647 entry->Close(); | |
5648 | |
5649 RemoveMockTransaction(&transaction); | |
5650 } | |
5651 | |
5652 // Tests that we can continue with a request that was interrupted. | |
5653 TEST(HttpCache, GET_IncompleteResource) { | |
5654 MockHttpCache cache; | |
5655 AddMockTransaction(&kRangeGET_TransactionOK); | |
5656 | |
5657 std::string raw_headers("HTTP/1.1 200 OK\n" | |
5658 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
5659 "ETag: \"foo\"\n" | |
5660 "Accept-Ranges: bytes\n" | |
5661 "Content-Length: 80\n"); | |
5662 CreateTruncatedEntry(raw_headers, &cache); | |
5663 | |
5664 // Now make a regular request. | |
5665 std::string headers; | |
5666 MockTransaction transaction(kRangeGET_TransactionOK); | |
5667 transaction.request_headers = EXTRA_HEADER; | |
5668 transaction.data = kFullRangeData; | |
5669 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
5670 | |
5671 // We update the headers with the ones received while revalidating. | |
5672 std::string expected_headers( | |
5673 "HTTP/1.1 200 OK\n" | |
5674 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
5675 "Accept-Ranges: bytes\n" | |
5676 "ETag: \"foo\"\n" | |
5677 "Content-Length: 80\n"); | |
5678 | |
5679 EXPECT_EQ(expected_headers, headers); | |
5680 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5681 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5682 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5683 | |
5684 // Verify that the disk entry was updated. | |
5685 disk_cache::Entry* entry; | |
5686 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
5687 EXPECT_EQ(80, entry->GetDataSize(1)); | |
5688 bool truncated = true; | |
5689 net::HttpResponseInfo response; | |
5690 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
5691 EXPECT_FALSE(truncated); | |
5692 entry->Close(); | |
5693 | |
5694 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5695 } | |
5696 | |
5697 // Tests the handling of no-store when revalidating a truncated entry. | |
5698 TEST(HttpCache, GET_IncompleteResource_NoStore) { | |
5699 MockHttpCache cache; | |
5700 AddMockTransaction(&kRangeGET_TransactionOK); | |
5701 | |
5702 std::string raw_headers("HTTP/1.1 200 OK\n" | |
5703 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
5704 "ETag: \"foo\"\n" | |
5705 "Accept-Ranges: bytes\n" | |
5706 "Content-Length: 80\n"); | |
5707 CreateTruncatedEntry(raw_headers, &cache); | |
5708 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5709 | |
5710 // Now make a regular request. | |
5711 MockTransaction transaction(kRangeGET_TransactionOK); | |
5712 transaction.request_headers = EXTRA_HEADER; | |
5713 std::string response_headers(transaction.response_headers); | |
5714 response_headers += ("Cache-Control: no-store\n"); | |
5715 transaction.response_headers = response_headers.c_str(); | |
5716 transaction.data = kFullRangeData; | |
5717 AddMockTransaction(&transaction); | |
5718 | |
5719 std::string headers; | |
5720 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
5721 | |
5722 // We update the headers with the ones received while revalidating. | |
5723 std::string expected_headers( | |
5724 "HTTP/1.1 200 OK\n" | |
5725 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
5726 "Accept-Ranges: bytes\n" | |
5727 "Cache-Control: no-store\n" | |
5728 "ETag: \"foo\"\n" | |
5729 "Content-Length: 80\n"); | |
5730 | |
5731 EXPECT_EQ(expected_headers, headers); | |
5732 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5733 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5734 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5735 | |
5736 // Verify that the disk entry was deleted. | |
5737 disk_cache::Entry* entry; | |
5738 EXPECT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
5739 RemoveMockTransaction(&transaction); | |
5740 } | |
5741 | |
5742 // Tests cancelling a request after the server sent no-store. | |
5743 TEST(HttpCache, GET_IncompleteResource_Cancel) { | |
5744 MockHttpCache cache; | |
5745 AddMockTransaction(&kRangeGET_TransactionOK); | |
5746 | |
5747 std::string raw_headers("HTTP/1.1 200 OK\n" | |
5748 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
5749 "ETag: \"foo\"\n" | |
5750 "Accept-Ranges: bytes\n" | |
5751 "Content-Length: 80\n"); | |
5752 CreateTruncatedEntry(raw_headers, &cache); | |
5753 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5754 | |
5755 // Now make a regular request. | |
5756 MockTransaction transaction(kRangeGET_TransactionOK); | |
5757 transaction.request_headers = EXTRA_HEADER; | |
5758 std::string response_headers(transaction.response_headers); | |
5759 response_headers += ("Cache-Control: no-store\n"); | |
5760 transaction.response_headers = response_headers.c_str(); | |
5761 transaction.data = kFullRangeData; | |
5762 AddMockTransaction(&transaction); | |
5763 | |
5764 MockHttpRequest request(transaction); | |
5765 Context* c = new Context(); | |
5766 | |
5767 int rv = cache.CreateTransaction(&c->trans); | |
5768 ASSERT_EQ(net::OK, rv); | |
5769 | |
5770 // Queue another request to this transaction. We have to start this request | |
5771 // before the first one gets the response from the server and dooms the entry, | |
5772 // otherwise it will just create a new entry without being queued to the first | |
5773 // request. | |
5774 Context* pending = new Context(); | |
5775 ASSERT_EQ(net::OK, cache.CreateTransaction(&pending->trans)); | |
5776 | |
5777 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
5778 EXPECT_EQ(net::ERR_IO_PENDING, | |
5779 pending->trans->Start(&request, pending->callback.callback(), | |
5780 net::BoundNetLog())); | |
5781 EXPECT_EQ(net::OK, c->callback.GetResult(rv)); | |
5782 | |
5783 // Make sure that the entry has some data stored. | |
5784 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(5)); | |
5785 rv = c->trans->Read(buf.get(), buf->size(), c->callback.callback()); | |
5786 EXPECT_EQ(5, c->callback.GetResult(rv)); | |
5787 | |
5788 // Cancel the requests. | |
5789 delete c; | |
5790 delete pending; | |
5791 | |
5792 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5793 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5794 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
5795 | |
5796 base::MessageLoop::current()->RunUntilIdle(); | |
5797 RemoveMockTransaction(&transaction); | |
5798 } | |
5799 | |
5800 // Tests that we delete truncated entries if the server changes its mind midway. | |
5801 TEST(HttpCache, GET_IncompleteResource2) { | |
5802 MockHttpCache cache; | |
5803 AddMockTransaction(&kRangeGET_TransactionOK); | |
5804 | |
5805 // Content-length will be intentionally bad. | |
5806 std::string raw_headers("HTTP/1.1 200 OK\n" | |
5807 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
5808 "ETag: \"foo\"\n" | |
5809 "Accept-Ranges: bytes\n" | |
5810 "Content-Length: 50\n"); | |
5811 CreateTruncatedEntry(raw_headers, &cache); | |
5812 | |
5813 // Now make a regular request. We expect the code to fail the validation and | |
5814 // retry the request without using byte ranges. | |
5815 std::string headers; | |
5816 MockTransaction transaction(kRangeGET_TransactionOK); | |
5817 transaction.request_headers = EXTRA_HEADER; | |
5818 transaction.data = "Not a range"; | |
5819 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
5820 | |
5821 // The server will return 200 instead of a byte range. | |
5822 std::string expected_headers( | |
5823 "HTTP/1.1 200 OK\n" | |
5824 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"); | |
5825 | |
5826 EXPECT_EQ(expected_headers, headers); | |
5827 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5828 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5829 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5830 | |
5831 // Verify that the disk entry was deleted. | |
5832 disk_cache::Entry* entry; | |
5833 ASSERT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
5834 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5835 } | |
5836 | |
5837 // Tests that we always validate a truncated request. | |
5838 TEST(HttpCache, GET_IncompleteResource3) { | |
5839 MockHttpCache cache; | |
5840 AddMockTransaction(&kRangeGET_TransactionOK); | |
5841 | |
5842 // This should not require validation for 10 hours. | |
5843 std::string raw_headers("HTTP/1.1 200 OK\n" | |
5844 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" | |
5845 "ETag: \"foo\"\n" | |
5846 "Cache-Control: max-age= 36000\n" | |
5847 "Accept-Ranges: bytes\n" | |
5848 "Content-Length: 80\n"); | |
5849 CreateTruncatedEntry(raw_headers, &cache); | |
5850 | |
5851 // Now make a regular request. | |
5852 std::string headers; | |
5853 MockTransaction transaction(kRangeGET_TransactionOK); | |
5854 transaction.request_headers = EXTRA_HEADER; | |
5855 transaction.data = kFullRangeData; | |
5856 | |
5857 scoped_ptr<Context> c(new Context); | |
5858 int rv = cache.CreateTransaction(&c->trans); | |
5859 ASSERT_EQ(net::OK, rv); | |
5860 | |
5861 MockHttpRequest request(transaction); | |
5862 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
5863 EXPECT_EQ(net::OK, c->callback.GetResult(rv)); | |
5864 | |
5865 // We should have checked with the server before finishing Start(). | |
5866 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5867 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5868 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5869 | |
5870 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5871 } | |
5872 | |
5873 // Tests that we handle 401s for truncated resources. | |
5874 TEST(HttpCache, GET_IncompleteResourceWithAuth) { | |
5875 MockHttpCache cache; | |
5876 AddMockTransaction(&kRangeGET_TransactionOK); | |
5877 | |
5878 std::string raw_headers("HTTP/1.1 200 OK\n" | |
5879 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
5880 "ETag: \"foo\"\n" | |
5881 "Accept-Ranges: bytes\n" | |
5882 "Content-Length: 80\n"); | |
5883 CreateTruncatedEntry(raw_headers, &cache); | |
5884 | |
5885 // Now make a regular request. | |
5886 MockTransaction transaction(kRangeGET_TransactionOK); | |
5887 transaction.request_headers = "X-Require-Mock-Auth: dummy\r\n" | |
5888 EXTRA_HEADER; | |
5889 transaction.data = kFullRangeData; | |
5890 RangeTransactionServer handler; | |
5891 | |
5892 scoped_ptr<Context> c(new Context); | |
5893 int rv = cache.CreateTransaction(&c->trans); | |
5894 ASSERT_EQ(net::OK, rv); | |
5895 | |
5896 MockHttpRequest request(transaction); | |
5897 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
5898 EXPECT_EQ(net::OK, c->callback.GetResult(rv)); | |
5899 | |
5900 const net::HttpResponseInfo* response = c->trans->GetResponseInfo(); | |
5901 ASSERT_TRUE(response); | |
5902 ASSERT_EQ(401, response->headers->response_code()); | |
5903 rv = c->trans->RestartWithAuth(net::AuthCredentials(), | |
5904 c->callback.callback()); | |
5905 EXPECT_EQ(net::OK, c->callback.GetResult(rv)); | |
5906 response = c->trans->GetResponseInfo(); | |
5907 ASSERT_TRUE(response); | |
5908 ASSERT_EQ(200, response->headers->response_code()); | |
5909 | |
5910 ReadAndVerifyTransaction(c->trans.get(), transaction); | |
5911 c.reset(); // The destructor could delete the entry. | |
5912 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5913 | |
5914 // Verify that the entry was not deleted. | |
5915 disk_cache::Entry* entry; | |
5916 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
5917 entry->Close(); | |
5918 | |
5919 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5920 } | |
5921 | |
5922 // Tests that we cache a 200 response to the validation request. | |
5923 TEST(HttpCache, GET_IncompleteResource4) { | |
5924 MockHttpCache cache; | |
5925 AddMockTransaction(&kRangeGET_TransactionOK); | |
5926 | |
5927 std::string raw_headers("HTTP/1.1 200 OK\n" | |
5928 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" | |
5929 "ETag: \"foo\"\n" | |
5930 "Accept-Ranges: bytes\n" | |
5931 "Content-Length: 80\n"); | |
5932 CreateTruncatedEntry(raw_headers, &cache); | |
5933 | |
5934 // Now make a regular request. | |
5935 std::string headers; | |
5936 MockTransaction transaction(kRangeGET_TransactionOK); | |
5937 transaction.request_headers = EXTRA_HEADER; | |
5938 transaction.data = "Not a range"; | |
5939 RangeTransactionServer handler; | |
5940 handler.set_bad_200(true); | |
5941 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | |
5942 | |
5943 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
5944 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5945 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5946 | |
5947 // Verify that the disk entry was updated. | |
5948 disk_cache::Entry* entry; | |
5949 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
5950 EXPECT_EQ(11, entry->GetDataSize(1)); | |
5951 bool truncated = true; | |
5952 net::HttpResponseInfo response; | |
5953 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
5954 EXPECT_FALSE(truncated); | |
5955 entry->Close(); | |
5956 | |
5957 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
5958 } | |
5959 | |
5960 // Tests that when we cancel a request that was interrupted, we mark it again | |
5961 // as truncated. | |
5962 TEST(HttpCache, GET_CancelIncompleteResource) { | |
5963 MockHttpCache cache; | |
5964 AddMockTransaction(&kRangeGET_TransactionOK); | |
5965 | |
5966 std::string raw_headers("HTTP/1.1 200 OK\n" | |
5967 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" | |
5968 "ETag: \"foo\"\n" | |
5969 "Accept-Ranges: bytes\n" | |
5970 "Content-Length: 80\n"); | |
5971 CreateTruncatedEntry(raw_headers, &cache); | |
5972 | |
5973 // Now make a regular request. | |
5974 MockTransaction transaction(kRangeGET_TransactionOK); | |
5975 transaction.request_headers = EXTRA_HEADER; | |
5976 | |
5977 MockHttpRequest request(transaction); | |
5978 Context* c = new Context(); | |
5979 int rv = cache.CreateTransaction(&c->trans); | |
5980 ASSERT_EQ(net::OK, rv); | |
5981 | |
5982 rv = c->trans->Start(&request, c->callback.callback(), net::BoundNetLog()); | |
5983 EXPECT_EQ(net::OK, c->callback.GetResult(rv)); | |
5984 | |
5985 // Read 20 bytes from the cache, and 10 from the net. | |
5986 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(100)); | |
5987 rv = c->trans->Read(buf.get(), 20, c->callback.callback()); | |
5988 EXPECT_EQ(20, c->callback.GetResult(rv)); | |
5989 rv = c->trans->Read(buf.get(), 10, c->callback.callback()); | |
5990 EXPECT_EQ(10, c->callback.GetResult(rv)); | |
5991 | |
5992 // At this point, we are already reading so canceling the request should leave | |
5993 // a truncated one. | |
5994 delete c; | |
5995 | |
5996 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
5997 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
5998 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
5999 | |
6000 // Verify that the disk entry was updated: now we have 30 bytes. | |
6001 disk_cache::Entry* entry; | |
6002 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
6003 EXPECT_EQ(30, entry->GetDataSize(1)); | |
6004 bool truncated = false; | |
6005 net::HttpResponseInfo response; | |
6006 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
6007 EXPECT_TRUE(truncated); | |
6008 entry->Close(); | |
6009 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
6010 } | |
6011 | |
6012 // Tests that we can handle range requests when we have a truncated entry. | |
6013 TEST(HttpCache, RangeGET_IncompleteResource) { | |
6014 MockHttpCache cache; | |
6015 AddMockTransaction(&kRangeGET_TransactionOK); | |
6016 | |
6017 // Content-length will be intentionally bogus. | |
6018 std::string raw_headers("HTTP/1.1 200 OK\n" | |
6019 "Last-Modified: something\n" | |
6020 "ETag: \"foo\"\n" | |
6021 "Accept-Ranges: bytes\n" | |
6022 "Content-Length: 10\n"); | |
6023 CreateTruncatedEntry(raw_headers, &cache); | |
6024 | |
6025 // Now make a range request. | |
6026 std::string headers; | |
6027 RunTransactionTestWithResponse(cache.http_cache(), kRangeGET_TransactionOK, | |
6028 &headers); | |
6029 | |
6030 Verify206Response(headers, 40, 49); | |
6031 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6032 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
6033 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
6034 | |
6035 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
6036 } | |
6037 | |
6038 TEST(HttpCache, SyncRead) { | |
6039 MockHttpCache cache; | |
6040 | |
6041 // This test ensures that a read that completes synchronously does not cause | |
6042 // any problems. | |
6043 | |
6044 ScopedMockTransaction transaction(kSimpleGET_Transaction); | |
6045 transaction.test_mode |= (TEST_MODE_SYNC_CACHE_START | | |
6046 TEST_MODE_SYNC_CACHE_READ | | |
6047 TEST_MODE_SYNC_CACHE_WRITE); | |
6048 | |
6049 MockHttpRequest r1(transaction), | |
6050 r2(transaction), | |
6051 r3(transaction); | |
6052 | |
6053 TestTransactionConsumer c1(net::DEFAULT_PRIORITY, cache.http_cache()), | |
6054 c2(net::DEFAULT_PRIORITY, cache.http_cache()), | |
6055 c3(net::DEFAULT_PRIORITY, cache.http_cache()); | |
6056 | |
6057 c1.Start(&r1, net::BoundNetLog()); | |
6058 | |
6059 r2.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
6060 c2.Start(&r2, net::BoundNetLog()); | |
6061 | |
6062 r3.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
6063 c3.Start(&r3, net::BoundNetLog()); | |
6064 | |
6065 base::MessageLoop::current()->Run(); | |
6066 | |
6067 EXPECT_TRUE(c1.is_done()); | |
6068 EXPECT_TRUE(c2.is_done()); | |
6069 EXPECT_TRUE(c3.is_done()); | |
6070 | |
6071 EXPECT_EQ(net::OK, c1.error()); | |
6072 EXPECT_EQ(net::OK, c2.error()); | |
6073 EXPECT_EQ(net::OK, c3.error()); | |
6074 } | |
6075 | |
6076 TEST(HttpCache, ValidationResultsIn200) { | |
6077 MockHttpCache cache; | |
6078 | |
6079 // This test ensures that a conditional request, which results in a 200 | |
6080 // instead of a 304, properly truncates the existing response data. | |
6081 | |
6082 // write to the cache | |
6083 RunTransactionTest(cache.http_cache(), kETagGET_Transaction); | |
6084 | |
6085 // force this transaction to validate the cache | |
6086 MockTransaction transaction(kETagGET_Transaction); | |
6087 transaction.load_flags |= net::LOAD_VALIDATE_CACHE; | |
6088 RunTransactionTest(cache.http_cache(), transaction); | |
6089 | |
6090 // read from the cache | |
6091 RunTransactionTest(cache.http_cache(), kETagGET_Transaction); | |
6092 } | |
6093 | |
6094 TEST(HttpCache, CachedRedirect) { | |
6095 MockHttpCache cache; | |
6096 | |
6097 ScopedMockTransaction kTestTransaction(kSimpleGET_Transaction); | |
6098 kTestTransaction.status = "HTTP/1.1 301 Moved Permanently"; | |
6099 kTestTransaction.response_headers = "Location: http://www.bar.com/\n"; | |
6100 | |
6101 MockHttpRequest request(kTestTransaction); | |
6102 net::TestCompletionCallback callback; | |
6103 | |
6104 // Write to the cache. | |
6105 { | |
6106 scoped_ptr<net::HttpTransaction> trans; | |
6107 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
6108 | |
6109 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
6110 if (rv == net::ERR_IO_PENDING) | |
6111 rv = callback.WaitForResult(); | |
6112 ASSERT_EQ(net::OK, rv); | |
6113 | |
6114 const net::HttpResponseInfo* info = trans->GetResponseInfo(); | |
6115 ASSERT_TRUE(info); | |
6116 | |
6117 EXPECT_EQ(info->headers->response_code(), 301); | |
6118 | |
6119 std::string location; | |
6120 info->headers->EnumerateHeader(NULL, "Location", &location); | |
6121 EXPECT_EQ(location, "http://www.bar.com/"); | |
6122 | |
6123 // Mark the transaction as completed so it is cached. | |
6124 trans->DoneReading(); | |
6125 | |
6126 // Destroy transaction when going out of scope. We have not actually | |
6127 // read the response body -- want to test that it is still getting cached. | |
6128 } | |
6129 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6130 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6131 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6132 | |
6133 // Active entries in the cache are not retired synchronously. Make | |
6134 // sure the next run hits the MockHttpCache and open_count is | |
6135 // correct. | |
6136 base::MessageLoop::current()->RunUntilIdle(); | |
6137 | |
6138 // Read from the cache. | |
6139 { | |
6140 scoped_ptr<net::HttpTransaction> trans; | |
6141 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
6142 | |
6143 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
6144 if (rv == net::ERR_IO_PENDING) | |
6145 rv = callback.WaitForResult(); | |
6146 ASSERT_EQ(net::OK, rv); | |
6147 | |
6148 const net::HttpResponseInfo* info = trans->GetResponseInfo(); | |
6149 ASSERT_TRUE(info); | |
6150 | |
6151 EXPECT_EQ(info->headers->response_code(), 301); | |
6152 | |
6153 std::string location; | |
6154 info->headers->EnumerateHeader(NULL, "Location", &location); | |
6155 EXPECT_EQ(location, "http://www.bar.com/"); | |
6156 | |
6157 // Mark the transaction as completed so it is cached. | |
6158 trans->DoneReading(); | |
6159 | |
6160 // Destroy transaction when going out of scope. We have not actually | |
6161 // read the response body -- want to test that it is still getting cached. | |
6162 } | |
6163 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6164 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
6165 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6166 } | |
6167 | |
6168 // Verify that no-cache resources are stored in cache, but are not fetched from | |
6169 // cache during normal loads. | |
6170 TEST(HttpCache, CacheControlNoCacheNormalLoad) { | |
6171 MockHttpCache cache; | |
6172 | |
6173 ScopedMockTransaction transaction(kSimpleGET_Transaction); | |
6174 transaction.response_headers = "cache-control: no-cache\n"; | |
6175 | |
6176 // Initial load. | |
6177 RunTransactionTest(cache.http_cache(), transaction); | |
6178 | |
6179 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6180 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6181 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6182 | |
6183 // Try loading again; it should result in a network fetch. | |
6184 RunTransactionTest(cache.http_cache(), transaction); | |
6185 | |
6186 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6187 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
6188 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6189 | |
6190 disk_cache::Entry* entry; | |
6191 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry)); | |
6192 entry->Close(); | |
6193 } | |
6194 | |
6195 // Verify that no-cache resources are stored in cache and fetched from cache | |
6196 // when the LOAD_PREFERRING_CACHE flag is set. | |
6197 TEST(HttpCache, CacheControlNoCacheHistoryLoad) { | |
6198 MockHttpCache cache; | |
6199 | |
6200 ScopedMockTransaction transaction(kSimpleGET_Transaction); | |
6201 transaction.response_headers = "cache-control: no-cache\n"; | |
6202 | |
6203 // Initial load. | |
6204 RunTransactionTest(cache.http_cache(), transaction); | |
6205 | |
6206 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6207 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6208 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6209 | |
6210 // Try loading again with LOAD_PREFERRING_CACHE. | |
6211 transaction.load_flags = net::LOAD_PREFERRING_CACHE; | |
6212 RunTransactionTest(cache.http_cache(), transaction); | |
6213 | |
6214 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6215 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
6216 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6217 | |
6218 disk_cache::Entry* entry; | |
6219 EXPECT_TRUE(cache.OpenBackendEntry(transaction.url, &entry)); | |
6220 entry->Close(); | |
6221 } | |
6222 | |
6223 TEST(HttpCache, CacheControlNoStore) { | |
6224 MockHttpCache cache; | |
6225 | |
6226 ScopedMockTransaction transaction(kSimpleGET_Transaction); | |
6227 transaction.response_headers = "cache-control: no-store\n"; | |
6228 | |
6229 // initial load | |
6230 RunTransactionTest(cache.http_cache(), transaction); | |
6231 | |
6232 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6233 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6234 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6235 | |
6236 // try loading again; it should result in a network fetch | |
6237 RunTransactionTest(cache.http_cache(), transaction); | |
6238 | |
6239 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6240 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6241 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
6242 | |
6243 disk_cache::Entry* entry; | |
6244 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); | |
6245 } | |
6246 | |
6247 TEST(HttpCache, CacheControlNoStore2) { | |
6248 // this test is similar to the above test, except that the initial response | |
6249 // is cachable, but when it is validated, no-store is received causing the | |
6250 // cached document to be deleted. | |
6251 MockHttpCache cache; | |
6252 | |
6253 ScopedMockTransaction transaction(kETagGET_Transaction); | |
6254 | |
6255 // initial load | |
6256 RunTransactionTest(cache.http_cache(), transaction); | |
6257 | |
6258 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6259 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6260 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6261 | |
6262 // try loading again; it should result in a network fetch | |
6263 transaction.load_flags = net::LOAD_VALIDATE_CACHE; | |
6264 transaction.response_headers = "cache-control: no-store\n"; | |
6265 RunTransactionTest(cache.http_cache(), transaction); | |
6266 | |
6267 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6268 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
6269 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6270 | |
6271 disk_cache::Entry* entry; | |
6272 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); | |
6273 } | |
6274 | |
6275 TEST(HttpCache, CacheControlNoStore3) { | |
6276 // this test is similar to the above test, except that the response is a 304 | |
6277 // instead of a 200. this should never happen in practice, but it seems like | |
6278 // a good thing to verify that we still destroy the cache entry. | |
6279 MockHttpCache cache; | |
6280 | |
6281 ScopedMockTransaction transaction(kETagGET_Transaction); | |
6282 | |
6283 // initial load | |
6284 RunTransactionTest(cache.http_cache(), transaction); | |
6285 | |
6286 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6287 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6288 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6289 | |
6290 // try loading again; it should result in a network fetch | |
6291 transaction.load_flags = net::LOAD_VALIDATE_CACHE; | |
6292 transaction.response_headers = "cache-control: no-store\n"; | |
6293 transaction.status = "HTTP/1.1 304 Not Modified"; | |
6294 RunTransactionTest(cache.http_cache(), transaction); | |
6295 | |
6296 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6297 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
6298 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6299 | |
6300 disk_cache::Entry* entry; | |
6301 EXPECT_FALSE(cache.OpenBackendEntry(transaction.url, &entry)); | |
6302 } | |
6303 | |
6304 // Ensure that we don't cache requests served over bad HTTPS. | |
6305 TEST(HttpCache, SimpleGET_SSLError) { | |
6306 MockHttpCache cache; | |
6307 | |
6308 MockTransaction transaction = kSimpleGET_Transaction; | |
6309 transaction.cert_status = net::CERT_STATUS_REVOKED; | |
6310 ScopedMockTransaction scoped_transaction(transaction); | |
6311 | |
6312 // write to the cache | |
6313 RunTransactionTest(cache.http_cache(), transaction); | |
6314 | |
6315 // Test that it was not cached. | |
6316 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
6317 | |
6318 MockHttpRequest request(transaction); | |
6319 net::TestCompletionCallback callback; | |
6320 | |
6321 scoped_ptr<net::HttpTransaction> trans; | |
6322 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
6323 | |
6324 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
6325 if (rv == net::ERR_IO_PENDING) | |
6326 rv = callback.WaitForResult(); | |
6327 ASSERT_EQ(net::ERR_CACHE_MISS, rv); | |
6328 } | |
6329 | |
6330 // Ensure that we don't crash by if left-behind transactions. | |
6331 TEST(HttpCache, OutlivedTransactions) { | |
6332 MockHttpCache* cache = new MockHttpCache; | |
6333 | |
6334 scoped_ptr<net::HttpTransaction> trans; | |
6335 EXPECT_EQ(net::OK, cache->CreateTransaction(&trans)); | |
6336 | |
6337 delete cache; | |
6338 trans.reset(); | |
6339 } | |
6340 | |
6341 // Test that the disabled mode works. | |
6342 TEST(HttpCache, CacheDisabledMode) { | |
6343 MockHttpCache cache; | |
6344 | |
6345 // write to the cache | |
6346 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
6347 | |
6348 // go into disabled mode | |
6349 cache.http_cache()->set_mode(net::HttpCache::DISABLE); | |
6350 | |
6351 // force this transaction to write to the cache again | |
6352 MockTransaction transaction(kSimpleGET_Transaction); | |
6353 | |
6354 RunTransactionTest(cache.http_cache(), transaction); | |
6355 | |
6356 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6357 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6358 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6359 } | |
6360 | |
6361 // Other tests check that the response headers of the cached response | |
6362 // get updated on 304. Here we specifically check that the | |
6363 // HttpResponseHeaders::request_time and HttpResponseHeaders::response_time | |
6364 // fields also gets updated. | |
6365 // http://crbug.com/20594. | |
6366 TEST(HttpCache, UpdatesRequestResponseTimeOn304) { | |
6367 MockHttpCache cache; | |
6368 | |
6369 const char kUrl[] = "http://foobar"; | |
6370 const char kData[] = "body"; | |
6371 | |
6372 MockTransaction mock_network_response = { 0 }; | |
6373 mock_network_response.url = kUrl; | |
6374 | |
6375 AddMockTransaction(&mock_network_response); | |
6376 | |
6377 // Request |kUrl|, causing |kNetResponse1| to be written to the cache. | |
6378 | |
6379 MockTransaction request = { 0 }; | |
6380 request.url = kUrl; | |
6381 request.method = "GET"; | |
6382 request.request_headers = "\r\n"; | |
6383 request.data = kData; | |
6384 | |
6385 static const Response kNetResponse1 = { | |
6386 "HTTP/1.1 200 OK", | |
6387 "Date: Fri, 12 Jun 2009 21:46:42 GMT\n" | |
6388 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
6389 kData | |
6390 }; | |
6391 | |
6392 kNetResponse1.AssignTo(&mock_network_response); | |
6393 | |
6394 RunTransactionTest(cache.http_cache(), request); | |
6395 | |
6396 // Request |kUrl| again, this time validating the cache and getting | |
6397 // a 304 back. | |
6398 | |
6399 request.load_flags = net::LOAD_VALIDATE_CACHE; | |
6400 | |
6401 static const Response kNetResponse2 = { | |
6402 "HTTP/1.1 304 Not Modified", | |
6403 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n", | |
6404 "" | |
6405 }; | |
6406 | |
6407 kNetResponse2.AssignTo(&mock_network_response); | |
6408 | |
6409 base::Time request_time = base::Time() + base::TimeDelta::FromHours(1234); | |
6410 base::Time response_time = base::Time() + base::TimeDelta::FromHours(1235); | |
6411 | |
6412 mock_network_response.request_time = request_time; | |
6413 mock_network_response.response_time = response_time; | |
6414 | |
6415 net::HttpResponseInfo response; | |
6416 RunTransactionTestWithResponseInfo(cache.http_cache(), request, &response); | |
6417 | |
6418 // The request and response times should have been updated. | |
6419 EXPECT_EQ(request_time.ToInternalValue(), | |
6420 response.request_time.ToInternalValue()); | |
6421 EXPECT_EQ(response_time.ToInternalValue(), | |
6422 response.response_time.ToInternalValue()); | |
6423 | |
6424 std::string headers; | |
6425 response.headers->GetNormalizedHeaders(&headers); | |
6426 | |
6427 EXPECT_EQ("HTTP/1.1 200 OK\n" | |
6428 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | |
6429 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | |
6430 headers); | |
6431 | |
6432 RemoveMockTransaction(&mock_network_response); | |
6433 } | |
6434 | |
6435 // Tests that we can write metadata to an entry. | |
6436 TEST(HttpCache, WriteMetadata_OK) { | |
6437 MockHttpCache cache; | |
6438 | |
6439 // Write to the cache | |
6440 net::HttpResponseInfo response; | |
6441 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | |
6442 &response); | |
6443 EXPECT_TRUE(response.metadata.get() == NULL); | |
6444 | |
6445 // Trivial call. | |
6446 cache.http_cache()->WriteMetadata(GURL("foo"), net::DEFAULT_PRIORITY, | |
6447 Time::Now().ToDoubleT(), NULL, 0); | |
6448 | |
6449 // Write meta data to the same entry. | |
6450 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); | |
6451 memset(buf->data(), 0, buf->size()); | |
6452 base::strlcpy(buf->data(), "Hi there", buf->size()); | |
6453 cache.http_cache()->WriteMetadata( | |
6454 GURL(kSimpleGET_Transaction.url), net::DEFAULT_PRIORITY, | |
6455 response.response_time.ToDoubleT(), buf.get(), buf->size()); | |
6456 | |
6457 // Release the buffer before the operation takes place. | |
6458 buf = NULL; | |
6459 | |
6460 // Makes sure we finish pending operations. | |
6461 base::MessageLoop::current()->RunUntilIdle(); | |
6462 | |
6463 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | |
6464 &response); | |
6465 ASSERT_TRUE(response.metadata.get() != NULL); | |
6466 EXPECT_EQ(50, response.metadata->size()); | |
6467 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); | |
6468 | |
6469 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6470 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
6471 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6472 } | |
6473 | |
6474 // Tests that we only write metadata to an entry if the time stamp matches. | |
6475 TEST(HttpCache, WriteMetadata_Fail) { | |
6476 MockHttpCache cache; | |
6477 | |
6478 // Write to the cache | |
6479 net::HttpResponseInfo response; | |
6480 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | |
6481 &response); | |
6482 EXPECT_TRUE(response.metadata.get() == NULL); | |
6483 | |
6484 // Attempt to write meta data to the same entry. | |
6485 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); | |
6486 memset(buf->data(), 0, buf->size()); | |
6487 base::strlcpy(buf->data(), "Hi there", buf->size()); | |
6488 base::Time expected_time = response.response_time - | |
6489 base::TimeDelta::FromMilliseconds(20); | |
6490 cache.http_cache()->WriteMetadata( | |
6491 GURL(kSimpleGET_Transaction.url), net::DEFAULT_PRIORITY, | |
6492 expected_time.ToDoubleT(), buf.get(), buf->size()); | |
6493 | |
6494 // Makes sure we finish pending operations. | |
6495 base::MessageLoop::current()->RunUntilIdle(); | |
6496 | |
6497 RunTransactionTestWithResponseInfo(cache.http_cache(), kSimpleGET_Transaction, | |
6498 &response); | |
6499 EXPECT_TRUE(response.metadata.get() == NULL); | |
6500 | |
6501 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6502 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
6503 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6504 } | |
6505 | |
6506 // Tests that we can read metadata after validating the entry and with READ mode | |
6507 // transactions. | |
6508 TEST(HttpCache, ReadMetadata) { | |
6509 MockHttpCache cache; | |
6510 | |
6511 // Write to the cache | |
6512 net::HttpResponseInfo response; | |
6513 RunTransactionTestWithResponseInfo(cache.http_cache(), | |
6514 kTypicalGET_Transaction, &response); | |
6515 EXPECT_TRUE(response.metadata.get() == NULL); | |
6516 | |
6517 // Write meta data to the same entry. | |
6518 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(50)); | |
6519 memset(buf->data(), 0, buf->size()); | |
6520 base::strlcpy(buf->data(), "Hi there", buf->size()); | |
6521 cache.http_cache()->WriteMetadata( | |
6522 GURL(kTypicalGET_Transaction.url), net::DEFAULT_PRIORITY, | |
6523 response.response_time.ToDoubleT(), buf.get(), buf->size()); | |
6524 | |
6525 // Makes sure we finish pending operations. | |
6526 base::MessageLoop::current()->RunUntilIdle(); | |
6527 | |
6528 // Start with a READ mode transaction. | |
6529 MockTransaction trans1(kTypicalGET_Transaction); | |
6530 trans1.load_flags = net::LOAD_ONLY_FROM_CACHE; | |
6531 | |
6532 RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response); | |
6533 ASSERT_TRUE(response.metadata.get() != NULL); | |
6534 EXPECT_EQ(50, response.metadata->size()); | |
6535 EXPECT_EQ(0, strcmp(response.metadata->data(), "Hi there")); | |
6536 | |
6537 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6538 EXPECT_EQ(2, cache.disk_cache()->open_count()); | |
6539 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6540 base::MessageLoop::current()->RunUntilIdle(); | |
6541 | |
6542 // Now make sure that the entry is re-validated with the server. | |
6543 trans1.load_flags = net::LOAD_VALIDATE_CACHE; | |
6544 trans1.status = "HTTP/1.1 304 Not Modified"; | |
6545 AddMockTransaction(&trans1); | |
6546 | |
6547 response.metadata = NULL; | |
6548 RunTransactionTestWithResponseInfo(cache.http_cache(), trans1, &response); | |
6549 EXPECT_TRUE(response.metadata.get() != NULL); | |
6550 | |
6551 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6552 EXPECT_EQ(3, cache.disk_cache()->open_count()); | |
6553 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6554 base::MessageLoop::current()->RunUntilIdle(); | |
6555 RemoveMockTransaction(&trans1); | |
6556 | |
6557 // Now return 200 when validating the entry so the metadata will be lost. | |
6558 MockTransaction trans2(kTypicalGET_Transaction); | |
6559 trans2.load_flags = net::LOAD_VALIDATE_CACHE; | |
6560 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); | |
6561 EXPECT_TRUE(response.metadata.get() == NULL); | |
6562 | |
6563 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | |
6564 EXPECT_EQ(4, cache.disk_cache()->open_count()); | |
6565 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6566 } | |
6567 | |
6568 // Tests that we don't mark entries as truncated when a filter detects the end | |
6569 // of the stream. | |
6570 TEST(HttpCache, FilterCompletion) { | |
6571 MockHttpCache cache; | |
6572 net::TestCompletionCallback callback; | |
6573 | |
6574 { | |
6575 scoped_ptr<net::HttpTransaction> trans; | |
6576 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
6577 | |
6578 MockHttpRequest request(kSimpleGET_Transaction); | |
6579 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
6580 EXPECT_EQ(net::OK, callback.GetResult(rv)); | |
6581 | |
6582 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); | |
6583 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6584 EXPECT_GT(callback.GetResult(rv), 0); | |
6585 | |
6586 // Now make sure that the entry is preserved. | |
6587 trans->DoneReading(); | |
6588 } | |
6589 | |
6590 // Make sure that the ActiveEntry is gone. | |
6591 base::MessageLoop::current()->RunUntilIdle(); | |
6592 | |
6593 // Read from the cache. | |
6594 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
6595 | |
6596 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6597 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
6598 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6599 } | |
6600 | |
6601 // Tests that we don't mark entries as truncated and release the cache | |
6602 // entry when DoneReading() is called before any Read() calls, such as | |
6603 // for a redirect. | |
6604 TEST(HttpCache, DoneReading) { | |
6605 MockHttpCache cache; | |
6606 net::TestCompletionCallback callback; | |
6607 | |
6608 ScopedMockTransaction transaction(kSimpleGET_Transaction); | |
6609 transaction.data = ""; | |
6610 | |
6611 scoped_ptr<net::HttpTransaction> trans; | |
6612 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
6613 | |
6614 MockHttpRequest request(transaction); | |
6615 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
6616 EXPECT_EQ(net::OK, callback.GetResult(rv)); | |
6617 | |
6618 trans->DoneReading(); | |
6619 // Leave the transaction around. | |
6620 | |
6621 // Make sure that the ActiveEntry is gone. | |
6622 base::MessageLoop::current()->RunUntilIdle(); | |
6623 | |
6624 // Read from the cache. This should not deadlock. | |
6625 RunTransactionTest(cache.http_cache(), transaction); | |
6626 | |
6627 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | |
6628 EXPECT_EQ(1, cache.disk_cache()->open_count()); | |
6629 EXPECT_EQ(1, cache.disk_cache()->create_count()); | |
6630 } | |
6631 | |
6632 // Tests that we stop caching when told. | |
6633 TEST(HttpCache, StopCachingDeletesEntry) { | |
6634 MockHttpCache cache; | |
6635 net::TestCompletionCallback callback; | |
6636 MockHttpRequest request(kSimpleGET_Transaction); | |
6637 | |
6638 { | |
6639 scoped_ptr<net::HttpTransaction> trans; | |
6640 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
6641 | |
6642 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
6643 EXPECT_EQ(net::OK, callback.GetResult(rv)); | |
6644 | |
6645 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); | |
6646 rv = trans->Read(buf.get(), 10, callback.callback()); | |
6647 EXPECT_EQ(10, callback.GetResult(rv)); | |
6648 | |
6649 trans->StopCaching(); | |
6650 | |
6651 // We should be able to keep reading. | |
6652 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6653 EXPECT_GT(callback.GetResult(rv), 0); | |
6654 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6655 EXPECT_EQ(0, callback.GetResult(rv)); | |
6656 } | |
6657 | |
6658 // Make sure that the ActiveEntry is gone. | |
6659 base::MessageLoop::current()->RunUntilIdle(); | |
6660 | |
6661 // Verify that the entry is gone. | |
6662 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
6663 | |
6664 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6665 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6666 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
6667 } | |
6668 | |
6669 // Tests that we stop caching when told, even if DoneReading is called | |
6670 // after StopCaching. | |
6671 TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) { | |
6672 MockHttpCache cache; | |
6673 net::TestCompletionCallback callback; | |
6674 MockHttpRequest request(kSimpleGET_Transaction); | |
6675 | |
6676 { | |
6677 scoped_ptr<net::HttpTransaction> trans; | |
6678 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
6679 | |
6680 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
6681 EXPECT_EQ(net::OK, callback.GetResult(rv)); | |
6682 | |
6683 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); | |
6684 rv = trans->Read(buf.get(), 10, callback.callback()); | |
6685 EXPECT_EQ(10, callback.GetResult(rv)); | |
6686 | |
6687 trans->StopCaching(); | |
6688 | |
6689 // We should be able to keep reading. | |
6690 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6691 EXPECT_GT(callback.GetResult(rv), 0); | |
6692 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6693 EXPECT_EQ(0, callback.GetResult(rv)); | |
6694 | |
6695 // We should be able to call DoneReading. | |
6696 trans->DoneReading(); | |
6697 } | |
6698 | |
6699 // Make sure that the ActiveEntry is gone. | |
6700 base::MessageLoop::current()->RunUntilIdle(); | |
6701 | |
6702 // Verify that the entry is gone. | |
6703 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
6704 | |
6705 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6706 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6707 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
6708 } | |
6709 | |
6710 // Tests that we stop caching when told, when using auth. | |
6711 TEST(HttpCache, StopCachingWithAuthDeletesEntry) { | |
6712 MockHttpCache cache; | |
6713 net::TestCompletionCallback callback; | |
6714 MockTransaction mock_transaction(kSimpleGET_Transaction); | |
6715 mock_transaction.status = "HTTP/1.1 401 Unauthorized"; | |
6716 AddMockTransaction(&mock_transaction); | |
6717 MockHttpRequest request(mock_transaction); | |
6718 | |
6719 { | |
6720 scoped_ptr<net::HttpTransaction> trans; | |
6721 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
6722 | |
6723 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
6724 EXPECT_EQ(net::OK, callback.GetResult(rv)); | |
6725 | |
6726 trans->StopCaching(); | |
6727 | |
6728 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); | |
6729 rv = trans->Read(buf.get(), 10, callback.callback()); | |
6730 EXPECT_EQ(callback.GetResult(rv), 10); | |
6731 } | |
6732 RemoveMockTransaction(&mock_transaction); | |
6733 | |
6734 // Make sure that the ActiveEntry is gone. | |
6735 base::MessageLoop::current()->RunUntilIdle(); | |
6736 | |
6737 // Verify that the entry is gone. | |
6738 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
6739 | |
6740 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6741 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6742 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
6743 } | |
6744 | |
6745 // Tests that when we are told to stop caching we don't throw away valid data. | |
6746 TEST(HttpCache, StopCachingSavesEntry) { | |
6747 MockHttpCache cache; | |
6748 net::TestCompletionCallback callback; | |
6749 MockHttpRequest request(kSimpleGET_Transaction); | |
6750 | |
6751 { | |
6752 scoped_ptr<net::HttpTransaction> trans; | |
6753 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
6754 | |
6755 // Force a response that can be resumed. | |
6756 MockTransaction mock_transaction(kSimpleGET_Transaction); | |
6757 AddMockTransaction(&mock_transaction); | |
6758 mock_transaction.response_headers = "Cache-Control: max-age=10000\n" | |
6759 "Content-Length: 42\n" | |
6760 "Etag: \"foo\"\n"; | |
6761 | |
6762 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
6763 EXPECT_EQ(net::OK, callback.GetResult(rv)); | |
6764 | |
6765 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); | |
6766 rv = trans->Read(buf.get(), 10, callback.callback()); | |
6767 EXPECT_EQ(callback.GetResult(rv), 10); | |
6768 | |
6769 trans->StopCaching(); | |
6770 | |
6771 // We should be able to keep reading. | |
6772 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6773 EXPECT_GT(callback.GetResult(rv), 0); | |
6774 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6775 EXPECT_EQ(callback.GetResult(rv), 0); | |
6776 | |
6777 RemoveMockTransaction(&mock_transaction); | |
6778 } | |
6779 | |
6780 // Verify that the entry is marked as incomplete. | |
6781 disk_cache::Entry* entry; | |
6782 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
6783 net::HttpResponseInfo response; | |
6784 bool truncated = false; | |
6785 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
6786 EXPECT_TRUE(truncated); | |
6787 entry->Close(); | |
6788 } | |
6789 | |
6790 // Tests that we handle truncated enries when StopCaching is called. | |
6791 TEST(HttpCache, StopCachingTruncatedEntry) { | |
6792 MockHttpCache cache; | |
6793 net::TestCompletionCallback callback; | |
6794 MockHttpRequest request(kRangeGET_TransactionOK); | |
6795 request.extra_headers.Clear(); | |
6796 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); | |
6797 AddMockTransaction(&kRangeGET_TransactionOK); | |
6798 | |
6799 std::string raw_headers("HTTP/1.1 200 OK\n" | |
6800 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
6801 "ETag: \"foo\"\n" | |
6802 "Accept-Ranges: bytes\n" | |
6803 "Content-Length: 80\n"); | |
6804 CreateTruncatedEntry(raw_headers, &cache); | |
6805 | |
6806 { | |
6807 // Now make a regular request. | |
6808 scoped_ptr<net::HttpTransaction> trans; | |
6809 ASSERT_EQ(net::OK, cache.CreateTransaction(&trans)); | |
6810 | |
6811 int rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
6812 EXPECT_EQ(net::OK, callback.GetResult(rv)); | |
6813 | |
6814 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); | |
6815 rv = trans->Read(buf.get(), 10, callback.callback()); | |
6816 EXPECT_EQ(callback.GetResult(rv), 10); | |
6817 | |
6818 // This is actually going to do nothing. | |
6819 trans->StopCaching(); | |
6820 | |
6821 // We should be able to keep reading. | |
6822 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6823 EXPECT_GT(callback.GetResult(rv), 0); | |
6824 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6825 EXPECT_GT(callback.GetResult(rv), 0); | |
6826 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6827 EXPECT_EQ(callback.GetResult(rv), 0); | |
6828 } | |
6829 | |
6830 // Verify that the disk entry was updated. | |
6831 disk_cache::Entry* entry; | |
6832 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | |
6833 EXPECT_EQ(80, entry->GetDataSize(1)); | |
6834 bool truncated = true; | |
6835 net::HttpResponseInfo response; | |
6836 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
6837 EXPECT_FALSE(truncated); | |
6838 entry->Close(); | |
6839 | |
6840 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
6841 } | |
6842 | |
6843 // Tests that we detect truncated resources from the net when there is | |
6844 // a Content-Length header. | |
6845 TEST(HttpCache, TruncatedByContentLength) { | |
6846 MockHttpCache cache; | |
6847 net::TestCompletionCallback callback; | |
6848 | |
6849 MockTransaction transaction(kSimpleGET_Transaction); | |
6850 AddMockTransaction(&transaction); | |
6851 transaction.response_headers = "Cache-Control: max-age=10000\n" | |
6852 "Content-Length: 100\n"; | |
6853 RunTransactionTest(cache.http_cache(), transaction); | |
6854 RemoveMockTransaction(&transaction); | |
6855 | |
6856 // Read from the cache. | |
6857 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
6858 | |
6859 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6860 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6861 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
6862 } | |
6863 | |
6864 // Tests that we actually flag entries as truncated when we detect an error | |
6865 // from the net. | |
6866 TEST(HttpCache, TruncatedByContentLength2) { | |
6867 MockHttpCache cache; | |
6868 net::TestCompletionCallback callback; | |
6869 | |
6870 MockTransaction transaction(kSimpleGET_Transaction); | |
6871 AddMockTransaction(&transaction); | |
6872 transaction.response_headers = "Cache-Control: max-age=10000\n" | |
6873 "Content-Length: 100\n" | |
6874 "Etag: \"foo\"\n"; | |
6875 RunTransactionTest(cache.http_cache(), transaction); | |
6876 RemoveMockTransaction(&transaction); | |
6877 | |
6878 // Verify that the entry is marked as incomplete. | |
6879 disk_cache::Entry* entry; | |
6880 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
6881 net::HttpResponseInfo response; | |
6882 bool truncated = false; | |
6883 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
6884 EXPECT_TRUE(truncated); | |
6885 entry->Close(); | |
6886 } | |
6887 | |
6888 // Make sure that calling SetPriority on a cache transaction passes on | |
6889 // its priority updates to its underlying network transaction. | |
6890 TEST(HttpCache, SetPriority) { | |
6891 MockHttpCache cache; | |
6892 | |
6893 scoped_ptr<net::HttpTransaction> trans; | |
6894 ASSERT_EQ(net::OK, cache.http_cache()->CreateTransaction(net::IDLE, &trans)); | |
6895 | |
6896 // Shouldn't crash, but doesn't do anything either. | |
6897 trans->SetPriority(net::LOW); | |
6898 | |
6899 EXPECT_FALSE(cache.network_layer()->last_transaction()); | |
6900 EXPECT_EQ(net::DEFAULT_PRIORITY, | |
6901 cache.network_layer()->last_create_transaction_priority()); | |
6902 | |
6903 net::HttpRequestInfo info; | |
6904 info.url = GURL(kSimpleGET_Transaction.url); | |
6905 net::TestCompletionCallback callback; | |
6906 EXPECT_EQ(net::ERR_IO_PENDING, | |
6907 trans->Start(&info, callback.callback(), net::BoundNetLog())); | |
6908 | |
6909 EXPECT_TRUE(cache.network_layer()->last_transaction()); | |
6910 if (cache.network_layer()->last_transaction()) { | |
6911 EXPECT_EQ(net::LOW, | |
6912 cache.network_layer()->last_create_transaction_priority()); | |
6913 EXPECT_EQ(net::LOW, | |
6914 cache.network_layer()->last_transaction()->priority()); | |
6915 } | |
6916 | |
6917 trans->SetPriority(net::HIGHEST); | |
6918 | |
6919 if (cache.network_layer()->last_transaction()) { | |
6920 EXPECT_EQ(net::LOW, | |
6921 cache.network_layer()->last_create_transaction_priority()); | |
6922 EXPECT_EQ(net::HIGHEST, | |
6923 cache.network_layer()->last_transaction()->priority()); | |
6924 } | |
6925 | |
6926 EXPECT_EQ(net::OK, callback.WaitForResult()); | |
6927 } | |
6928 | |
6929 // Make sure that calling SetWebSocketHandshakeStreamCreateHelper on a cache | |
6930 // transaction passes on its argument to the underlying network transaction. | |
6931 TEST(HttpCache, SetWebSocketHandshakeStreamCreateHelper) { | |
6932 MockHttpCache cache; | |
6933 | |
6934 FakeWebSocketHandshakeStreamCreateHelper create_helper; | |
6935 scoped_ptr<net::HttpTransaction> trans; | |
6936 ASSERT_EQ(net::OK, cache.http_cache()->CreateTransaction(net::IDLE, &trans)); | |
6937 | |
6938 EXPECT_FALSE(cache.network_layer()->last_transaction()); | |
6939 | |
6940 net::HttpRequestInfo info; | |
6941 info.url = GURL(kSimpleGET_Transaction.url); | |
6942 net::TestCompletionCallback callback; | |
6943 EXPECT_EQ(net::ERR_IO_PENDING, | |
6944 trans->Start(&info, callback.callback(), net::BoundNetLog())); | |
6945 | |
6946 ASSERT_TRUE(cache.network_layer()->last_transaction()); | |
6947 EXPECT_FALSE(cache.network_layer()->last_transaction()-> | |
6948 websocket_handshake_stream_create_helper()); | |
6949 trans->SetWebSocketHandshakeStreamCreateHelper(&create_helper); | |
6950 EXPECT_EQ(&create_helper, | |
6951 cache.network_layer()->last_transaction()-> | |
6952 websocket_handshake_stream_create_helper()); | |
6953 EXPECT_EQ(net::OK, callback.WaitForResult()); | |
6954 } | |
6955 | |
6956 // Make sure that a cache transaction passes on its priority to | |
6957 // newly-created network transactions. | |
6958 TEST(HttpCache, SetPriorityNewTransaction) { | |
6959 MockHttpCache cache; | |
6960 AddMockTransaction(&kRangeGET_TransactionOK); | |
6961 | |
6962 std::string raw_headers("HTTP/1.1 200 OK\n" | |
6963 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | |
6964 "ETag: \"foo\"\n" | |
6965 "Accept-Ranges: bytes\n" | |
6966 "Content-Length: 80\n"); | |
6967 CreateTruncatedEntry(raw_headers, &cache); | |
6968 | |
6969 // Now make a regular request. | |
6970 std::string headers; | |
6971 MockTransaction transaction(kRangeGET_TransactionOK); | |
6972 transaction.request_headers = EXTRA_HEADER; | |
6973 transaction.data = kFullRangeData; | |
6974 | |
6975 scoped_ptr<net::HttpTransaction> trans; | |
6976 ASSERT_EQ(net::OK, | |
6977 cache.http_cache()->CreateTransaction(net::MEDIUM, &trans)); | |
6978 EXPECT_EQ(net::DEFAULT_PRIORITY, | |
6979 cache.network_layer()->last_create_transaction_priority()); | |
6980 | |
6981 MockHttpRequest info(transaction); | |
6982 net::TestCompletionCallback callback; | |
6983 EXPECT_EQ(net::ERR_IO_PENDING, | |
6984 trans->Start(&info, callback.callback(), net::BoundNetLog())); | |
6985 EXPECT_EQ(net::OK, callback.WaitForResult()); | |
6986 | |
6987 EXPECT_EQ(net::MEDIUM, | |
6988 cache.network_layer()->last_create_transaction_priority()); | |
6989 | |
6990 trans->SetPriority(net::HIGHEST); | |
6991 // Should trigger a new network transaction and pick up the new | |
6992 // priority. | |
6993 ReadAndVerifyTransaction(trans.get(), transaction); | |
6994 | |
6995 EXPECT_EQ(net::HIGHEST, | |
6996 cache.network_layer()->last_create_transaction_priority()); | |
6997 | |
6998 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
6999 } | |
7000 | |
7001 int64 RunTransactionAndGetReceivedBytes( | |
7002 MockHttpCache& cache, | |
7003 const MockTransaction& trans_info) { | |
7004 int64 received_bytes = -1; | |
7005 RunTransactionTestBase(cache.http_cache(), trans_info, | |
7006 MockHttpRequest(trans_info), NULL, net::BoundNetLog(), | |
7007 NULL, &received_bytes); | |
7008 return received_bytes; | |
7009 } | |
7010 | |
7011 int64 TransactionSize(const MockTransaction& transaction) { | |
7012 return strlen(transaction.status) + strlen(transaction.response_headers) + | |
7013 strlen(transaction.data); | |
7014 } | |
7015 | |
7016 TEST(HttpCache, ReceivedBytesCacheMissAndThenHit) { | |
7017 MockHttpCache cache; | |
7018 | |
7019 MockTransaction transaction(kSimpleGET_Transaction); | |
7020 int64 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | |
7021 EXPECT_EQ(TransactionSize(transaction), received_bytes); | |
7022 | |
7023 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | |
7024 EXPECT_EQ(0, received_bytes); | |
7025 } | |
7026 | |
7027 TEST(HttpCache, ReceivedBytesConditionalRequest304) { | |
7028 MockHttpCache cache; | |
7029 | |
7030 ScopedMockTransaction transaction(kETagGET_Transaction); | |
7031 int64 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | |
7032 EXPECT_EQ(TransactionSize(transaction), received_bytes); | |
7033 | |
7034 transaction.load_flags = net::LOAD_VALIDATE_CACHE; | |
7035 transaction.handler = ETagGet_ConditionalRequest_Handler; | |
7036 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | |
7037 EXPECT_EQ(TransactionSize(transaction), received_bytes); | |
7038 } | |
7039 | |
7040 TEST(HttpCache, ReceivedBytesConditionalRequest200) { | |
7041 MockHttpCache cache; | |
7042 | |
7043 MockTransaction transaction(kTypicalGET_Transaction); | |
7044 transaction.request_headers = "Foo: bar\r\n"; | |
7045 transaction.response_headers = | |
7046 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | |
7047 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | |
7048 "Etag: \"foopy\"\n" | |
7049 "Cache-Control: max-age=0\n" | |
7050 "Vary: Foo\n"; | |
7051 AddMockTransaction(&transaction); | |
7052 int64 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | |
7053 EXPECT_EQ(TransactionSize(transaction), received_bytes); | |
7054 | |
7055 RevalidationServer server; | |
7056 transaction.handler = server.Handler; | |
7057 transaction.request_headers = "Foo: none\r\n"; | |
7058 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | |
7059 EXPECT_EQ(TransactionSize(transaction), received_bytes); | |
7060 | |
7061 RemoveMockTransaction(&transaction); | |
7062 } | |
7063 | |
7064 TEST(HttpCache, ReceivedBytesRange) { | |
7065 MockHttpCache cache; | |
7066 AddMockTransaction(&kRangeGET_TransactionOK); | |
7067 MockTransaction transaction(kRangeGET_TransactionOK); | |
7068 | |
7069 // Read bytes 40-49 from the network. | |
7070 int64 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | |
7071 int64 range_response_size = TransactionSize(transaction); | |
7072 EXPECT_EQ(range_response_size, received_bytes); | |
7073 | |
7074 // Read bytes 40-49 from the cache. | |
7075 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | |
7076 EXPECT_EQ(0, received_bytes); | |
7077 base::MessageLoop::current()->RunUntilIdle(); | |
7078 | |
7079 // Read bytes 30-39 from the network. | |
7080 transaction.request_headers = "Range: bytes = 30-39\r\n" EXTRA_HEADER; | |
7081 transaction.data = "rg: 30-39 "; | |
7082 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | |
7083 EXPECT_EQ(range_response_size, received_bytes); | |
7084 base::MessageLoop::current()->RunUntilIdle(); | |
7085 | |
7086 // Read bytes 20-29 and 50-59 from the network, bytes 30-49 from the cache. | |
7087 transaction.request_headers = "Range: bytes = 20-59\r\n" EXTRA_HEADER; | |
7088 transaction.data = "rg: 20-29 rg: 30-39 rg: 40-49 rg: 50-59 "; | |
7089 received_bytes = RunTransactionAndGetReceivedBytes(cache, transaction); | |
7090 EXPECT_EQ(range_response_size * 2, received_bytes); | |
7091 | |
7092 RemoveMockTransaction(&kRangeGET_TransactionOK); | |
7093 } | |
7094 | |
7095 class HttpCachePrefetchValidationTest : public ::testing::Test { | |
7096 protected: | |
7097 static const int kMaxAgeSecs = 100; | |
7098 static const int kRequireValidationSecs = kMaxAgeSecs + 1; | |
7099 | |
7100 HttpCachePrefetchValidationTest() : transaction_(kSimpleGET_Transaction) { | |
7101 DCHECK_LT(kMaxAgeSecs, prefetch_reuse_mins() * net::kNumSecondsPerMinute); | |
7102 | |
7103 clock_ = new base::SimpleTestClock(); | |
7104 cache_.http_cache()->SetClockForTesting(make_scoped_ptr(clock_)); | |
7105 cache_.network_layer()->SetClock(clock_); | |
7106 | |
7107 transaction_.response_headers = "Cache-Control: max-age=100\n"; | |
7108 } | |
7109 | |
7110 bool TransactionRequiredNetwork(int load_flags) { | |
7111 int pre_transaction_count = transaction_count(); | |
7112 transaction_.load_flags = load_flags; | |
7113 RunTransactionTest(cache_.http_cache(), transaction_); | |
7114 return pre_transaction_count != transaction_count(); | |
7115 } | |
7116 | |
7117 void AdvanceTime(int seconds) { | |
7118 clock_->Advance(base::TimeDelta::FromSeconds(seconds)); | |
7119 } | |
7120 | |
7121 int prefetch_reuse_mins() { return net::HttpCache::kPrefetchReuseMins; } | |
7122 | |
7123 // How many times this test has sent requests to the (fake) origin | |
7124 // server. Every test case needs to make at least one request to initialise | |
7125 // the cache. | |
7126 int transaction_count() { | |
7127 return cache_.network_layer()->transaction_count(); | |
7128 } | |
7129 | |
7130 MockHttpCache cache_; | |
7131 ScopedMockTransaction transaction_; | |
7132 std::string response_headers_; | |
7133 base::SimpleTestClock* clock_; | |
7134 }; | |
7135 | |
7136 TEST_F(HttpCachePrefetchValidationTest, SkipValidationShortlyAfterPrefetch) { | |
7137 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7138 AdvanceTime(kRequireValidationSecs); | |
7139 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7140 } | |
7141 | |
7142 TEST_F(HttpCachePrefetchValidationTest, ValidateLongAfterPrefetch) { | |
7143 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7144 AdvanceTime(prefetch_reuse_mins() * net::kNumSecondsPerMinute); | |
7145 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7146 } | |
7147 | |
7148 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceOnly) { | |
7149 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7150 AdvanceTime(kRequireValidationSecs); | |
7151 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7152 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7153 } | |
7154 | |
7155 TEST_F(HttpCachePrefetchValidationTest, SkipValidationOnceReadOnly) { | |
7156 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7157 AdvanceTime(kRequireValidationSecs); | |
7158 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_ONLY_FROM_CACHE)); | |
7159 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7160 } | |
7161 | |
7162 TEST_F(HttpCachePrefetchValidationTest, BypassCacheOverwritesPrefetch) { | |
7163 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7164 AdvanceTime(kRequireValidationSecs); | |
7165 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_BYPASS_CACHE)); | |
7166 AdvanceTime(kRequireValidationSecs); | |
7167 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7168 } | |
7169 | |
7170 TEST_F(HttpCachePrefetchValidationTest, | |
7171 SkipValidationOnExistingEntryThatNeedsValidation) { | |
7172 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7173 AdvanceTime(kRequireValidationSecs); | |
7174 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7175 AdvanceTime(kRequireValidationSecs); | |
7176 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7177 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7178 } | |
7179 | |
7180 TEST_F(HttpCachePrefetchValidationTest, | |
7181 SkipValidationOnExistingEntryThatDoesNotNeedValidation) { | |
7182 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7183 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7184 AdvanceTime(kRequireValidationSecs); | |
7185 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7186 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7187 } | |
7188 | |
7189 TEST_F(HttpCachePrefetchValidationTest, PrefetchMultipleTimes) { | |
7190 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7191 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7192 AdvanceTime(kRequireValidationSecs); | |
7193 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7194 } | |
7195 | |
7196 TEST_F(HttpCachePrefetchValidationTest, ValidateOnDelayedSecondPrefetch) { | |
7197 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7198 AdvanceTime(kRequireValidationSecs); | |
7199 EXPECT_TRUE(TransactionRequiredNetwork(net::LOAD_PREFETCH)); | |
7200 AdvanceTime(kRequireValidationSecs); | |
7201 EXPECT_FALSE(TransactionRequiredNetwork(net::LOAD_NORMAL)); | |
7202 } | |
7203 | |
7204 // Framework for tests of stale-while-revalidate related functionality. With | |
7205 // the default settings (age=3601,stale-while-revalidate=7200,max-age=3600) it | |
7206 // will trigger the stale-while-revalidate asynchronous revalidation. Setting | |
7207 // |age_| to < 3600 will prevent any revalidation, and |age_| > 10800 will cause | |
7208 // synchronous revalidation. | |
7209 class HttpCacheStaleWhileRevalidateTest : public ::testing::Test { | |
7210 protected: | |
7211 HttpCacheStaleWhileRevalidateTest() | |
7212 : transaction_(kSimpleGET_Transaction), | |
7213 age_(3601), | |
7214 stale_while_revalidate_(7200), | |
7215 validator_("Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT") { | |
7216 cache_.http_cache()->set_use_stale_while_revalidate_for_testing(true); | |
7217 } | |
7218 | |
7219 // RunTransactionTest() with the arguments from this fixture. | |
7220 void RunFixtureTransactionTest() { | |
7221 std::string response_headers = base::StringPrintf( | |
7222 "%s\n" | |
7223 "Age: %d\n" | |
7224 "Cache-Control: max-age=3600,stale-while-revalidate=%d\n", | |
7225 validator_.c_str(), | |
7226 age_, | |
7227 stale_while_revalidate_); | |
7228 transaction_.response_headers = response_headers.c_str(); | |
7229 RunTransactionTest(cache_.http_cache(), transaction_); | |
7230 transaction_.response_headers = ""; | |
7231 } | |
7232 | |
7233 // How many times this test has sent requests to the (fake) origin | |
7234 // server. Every test case needs to make at least one request to initialise | |
7235 // the cache. | |
7236 int transaction_count() { | |
7237 return cache_.network_layer()->transaction_count(); | |
7238 } | |
7239 | |
7240 // How many times an existing cache entry was opened during the test case. | |
7241 int open_count() { return cache_.disk_cache()->open_count(); } | |
7242 | |
7243 MockHttpCache cache_; | |
7244 ScopedMockTransaction transaction_; | |
7245 int age_; | |
7246 int stale_while_revalidate_; | |
7247 std::string validator_; | |
7248 }; | |
7249 | |
7250 static void CheckResourceFreshnessHeader(const net::HttpRequestInfo* request, | |
7251 std::string* response_status, | |
7252 std::string* response_headers, | |
7253 std::string* response_data) { | |
7254 std::string value; | |
7255 EXPECT_TRUE(request->extra_headers.GetHeader("Resource-Freshness", &value)); | |
7256 EXPECT_EQ("max-age=3600,stale-while-revalidate=7200,age=10801", value); | |
7257 } | |
7258 | |
7259 // Verify that the Resource-Freshness header is sent on a revalidation if the | |
7260 // stale-while-revalidate directive was on the response. | |
7261 TEST_F(HttpCacheStaleWhileRevalidateTest, ResourceFreshnessHeaderSent) { | |
7262 age_ = 10801; // Outside the stale-while-revalidate window. | |
7263 | |
7264 // Write to the cache. | |
7265 RunFixtureTransactionTest(); | |
7266 | |
7267 EXPECT_EQ(1, transaction_count()); | |
7268 | |
7269 // Send the request again and check that Resource-Freshness header is added. | |
7270 transaction_.handler = CheckResourceFreshnessHeader; | |
7271 | |
7272 RunFixtureTransactionTest(); | |
7273 | |
7274 EXPECT_EQ(2, transaction_count()); | |
7275 } | |
7276 | |
7277 static void CheckResourceFreshnessAbsent(const net::HttpRequestInfo* request, | |
7278 std::string* response_status, | |
7279 std::string* response_headers, | |
7280 std::string* response_data) { | |
7281 EXPECT_FALSE(request->extra_headers.HasHeader("Resource-Freshness")); | |
7282 } | |
7283 | |
7284 // Verify that the Resource-Freshness header is not sent when | |
7285 // stale-while-revalidate is 0. | |
7286 TEST_F(HttpCacheStaleWhileRevalidateTest, ResourceFreshnessHeaderNotSent) { | |
7287 age_ = 10801; | |
7288 stale_while_revalidate_ = 0; | |
7289 | |
7290 // Write to the cache. | |
7291 RunFixtureTransactionTest(); | |
7292 | |
7293 EXPECT_EQ(1, transaction_count()); | |
7294 | |
7295 // Send the request again and check that Resource-Freshness header is absent. | |
7296 transaction_.handler = CheckResourceFreshnessAbsent; | |
7297 | |
7298 RunFixtureTransactionTest(); | |
7299 | |
7300 EXPECT_EQ(2, transaction_count()); | |
7301 } | |
7302 | |
7303 // Verify that when stale-while-revalidate applies the response is read from | |
7304 // cache. | |
7305 TEST_F(HttpCacheStaleWhileRevalidateTest, ReadFromCache) { | |
7306 // Write to the cache. | |
7307 RunFixtureTransactionTest(); | |
7308 | |
7309 EXPECT_EQ(0, open_count()); | |
7310 EXPECT_EQ(1, transaction_count()); | |
7311 | |
7312 // Read back from the cache. | |
7313 RunFixtureTransactionTest(); | |
7314 | |
7315 EXPECT_EQ(1, open_count()); | |
7316 EXPECT_EQ(1, transaction_count()); | |
7317 } | |
7318 | |
7319 // Verify that when stale-while-revalidate applies an asynchronous request is | |
7320 // sent. | |
7321 TEST_F(HttpCacheStaleWhileRevalidateTest, AsyncRequestSent) { | |
7322 // Write to the cache. | |
7323 RunFixtureTransactionTest(); | |
7324 | |
7325 EXPECT_EQ(1, transaction_count()); | |
7326 | |
7327 // Read back from the cache. | |
7328 RunFixtureTransactionTest(); | |
7329 | |
7330 EXPECT_EQ(1, transaction_count()); | |
7331 | |
7332 // Let the async request execute. | |
7333 base::RunLoop().RunUntilIdle(); | |
7334 EXPECT_EQ(2, transaction_count()); | |
7335 } | |
7336 | |
7337 // Verify that tearing down the HttpCache with an async revalidation in progress | |
7338 // does not break anything (this test is most likely to find problems when run | |
7339 // with a memory checker such as AddressSanitizer). | |
7340 TEST_F(HttpCacheStaleWhileRevalidateTest, AsyncTearDown) { | |
7341 // Write to the cache. | |
7342 RunFixtureTransactionTest(); | |
7343 | |
7344 // Read back from the cache. | |
7345 RunFixtureTransactionTest(); | |
7346 } | |
7347 | |
7348 static void CheckIfModifiedSinceHeader(const net::HttpRequestInfo* request, | |
7349 std::string* response_status, | |
7350 std::string* response_headers, | |
7351 std::string* response_data) { | |
7352 std::string value; | |
7353 EXPECT_TRUE(request->extra_headers.GetHeader("If-Modified-Since", &value)); | |
7354 EXPECT_EQ("Sat, 18 Apr 2007 01:10:43 GMT", value); | |
7355 } | |
7356 | |
7357 // Verify that the async revalidation contains an If-Modified-Since header. | |
7358 TEST_F(HttpCacheStaleWhileRevalidateTest, AsyncRequestIfModifiedSince) { | |
7359 // Write to the cache. | |
7360 RunFixtureTransactionTest(); | |
7361 | |
7362 transaction_.handler = CheckIfModifiedSinceHeader; | |
7363 | |
7364 // Read back from the cache. | |
7365 RunFixtureTransactionTest(); | |
7366 } | |
7367 | |
7368 static void CheckIfNoneMatchHeader(const net::HttpRequestInfo* request, | |
7369 std::string* response_status, | |
7370 std::string* response_headers, | |
7371 std::string* response_data) { | |
7372 std::string value; | |
7373 EXPECT_TRUE(request->extra_headers.GetHeader("If-None-Match", &value)); | |
7374 EXPECT_EQ("\"40a1-1320-4f6adefa22a40\"", value); | |
7375 } | |
7376 | |
7377 // If the response had ETag rather than Last-Modified, then that is used to | |
7378 // conditionalise the response. | |
7379 TEST_F(HttpCacheStaleWhileRevalidateTest, AsyncRequestIfNoneMatch) { | |
7380 validator_ = "Etag: \"40a1-1320-4f6adefa22a40\""; | |
7381 | |
7382 // Write to the cache. | |
7383 RunFixtureTransactionTest(); | |
7384 | |
7385 transaction_.handler = CheckIfNoneMatchHeader; | |
7386 | |
7387 // Read back from the cache. | |
7388 RunFixtureTransactionTest(); | |
7389 } | |
7390 | |
7391 static void CheckResourceFreshnessHeaderPresent( | |
7392 const net::HttpRequestInfo* request, | |
7393 std::string* response_status, | |
7394 std::string* response_headers, | |
7395 std::string* response_data) { | |
7396 EXPECT_TRUE(request->extra_headers.HasHeader("Resource-Freshness")); | |
7397 } | |
7398 | |
7399 TEST_F(HttpCacheStaleWhileRevalidateTest, AsyncRequestHasResourceFreshness) { | |
7400 // Write to the cache. | |
7401 RunFixtureTransactionTest(); | |
7402 | |
7403 transaction_.handler = CheckResourceFreshnessHeaderPresent; | |
7404 | |
7405 // Read back from the cache. | |
7406 RunFixtureTransactionTest(); | |
7407 } | |
7408 | |
7409 // Verify that when age > max-age + stale-while-revalidate stale results are | |
7410 // not returned. | |
7411 TEST_F(HttpCacheStaleWhileRevalidateTest, NotAppliedIfTooStale) { | |
7412 age_ = 10801; | |
7413 | |
7414 // Write to the cache. | |
7415 RunFixtureTransactionTest(); | |
7416 | |
7417 EXPECT_EQ(0, open_count()); | |
7418 EXPECT_EQ(1, transaction_count()); | |
7419 | |
7420 // Reading back reads from the network. | |
7421 RunFixtureTransactionTest(); | |
7422 | |
7423 EXPECT_EQ(1, open_count()); | |
7424 EXPECT_EQ(2, transaction_count()); | |
7425 } | |
7426 | |
7427 // HEAD requests should be able to take advantage of stale-while-revalidate. | |
7428 TEST_F(HttpCacheStaleWhileRevalidateTest, WorksForHeadMethod) { | |
7429 // Write to the cache. This has to be a GET request; HEAD requests don't | |
7430 // create new cache entries. | |
7431 RunFixtureTransactionTest(); | |
7432 | |
7433 EXPECT_EQ(0, open_count()); | |
7434 EXPECT_EQ(1, transaction_count()); | |
7435 | |
7436 // Read back from the cache, and trigger an asynchronous HEAD request. | |
7437 transaction_.method = "HEAD"; | |
7438 transaction_.data = ""; | |
7439 | |
7440 RunFixtureTransactionTest(); | |
7441 | |
7442 EXPECT_EQ(1, open_count()); | |
7443 EXPECT_EQ(1, transaction_count()); | |
7444 | |
7445 // Let the network request proceed. | |
7446 base::RunLoop().RunUntilIdle(); | |
7447 | |
7448 EXPECT_EQ(2, transaction_count()); | |
7449 } | |
7450 | |
7451 // POST requests should not use stale-while-revalidate. | |
7452 TEST_F(HttpCacheStaleWhileRevalidateTest, NotAppliedToPost) { | |
7453 transaction_ = ScopedMockTransaction(kSimplePOST_Transaction); | |
7454 | |
7455 // Write to the cache. | |
7456 RunFixtureTransactionTest(); | |
7457 | |
7458 EXPECT_EQ(0, open_count()); | |
7459 EXPECT_EQ(1, transaction_count()); | |
7460 | |
7461 // Reading back reads from the network. | |
7462 RunFixtureTransactionTest(); | |
7463 | |
7464 EXPECT_EQ(0, open_count()); | |
7465 EXPECT_EQ(2, transaction_count()); | |
7466 } | |
7467 | |
7468 static void CheckUrlMatches(const net::HttpRequestInfo* request, | |
7469 std::string* response_status, | |
7470 std::string* response_headers, | |
7471 std::string* response_data) { | |
7472 EXPECT_EQ("http://www.google.com/", request->url.spec()); | |
7473 } | |
7474 | |
7475 // Async revalidation is issued to the original URL. | |
7476 TEST_F(HttpCacheStaleWhileRevalidateTest, AsyncRequestUrlMatches) { | |
7477 transaction_.url = "http://www.google.com/"; | |
7478 // Write to the cache. | |
7479 RunFixtureTransactionTest(); | |
7480 | |
7481 // Read back from the cache. | |
7482 RunFixtureTransactionTest(); | |
7483 | |
7484 EXPECT_EQ(1, transaction_count()); | |
7485 | |
7486 transaction_.handler = CheckUrlMatches; | |
7487 | |
7488 // Let the async request execute and perform the check. | |
7489 base::RunLoop().RunUntilIdle(); | |
7490 EXPECT_EQ(2, transaction_count()); | |
7491 } | |
7492 | |
7493 class SyncLoadFlagTest : public HttpCacheStaleWhileRevalidateTest, | |
7494 public ::testing::WithParamInterface<int> {}; | |
7495 | |
7496 // Flags which should always cause the request to be synchronous. | |
7497 TEST_P(SyncLoadFlagTest, MustBeSynchronous) { | |
7498 transaction_.load_flags |= GetParam(); | |
7499 // Write to the cache. | |
7500 RunFixtureTransactionTest(); | |
7501 | |
7502 EXPECT_EQ(1, transaction_count()); | |
7503 | |
7504 // Reading back reads from the network. | |
7505 RunFixtureTransactionTest(); | |
7506 | |
7507 EXPECT_EQ(2, transaction_count()); | |
7508 } | |
7509 | |
7510 INSTANTIATE_TEST_CASE_P(HttpCacheStaleWhileRevalidate, | |
7511 SyncLoadFlagTest, | |
7512 ::testing::Values(net::LOAD_VALIDATE_CACHE, | |
7513 net::LOAD_BYPASS_CACHE, | |
7514 net::LOAD_DISABLE_CACHE)); | |
7515 | |
7516 TEST_F(HttpCacheStaleWhileRevalidateTest, | |
7517 PreferringCacheDoesNotTriggerAsyncRequest) { | |
7518 transaction_.load_flags |= net::LOAD_PREFERRING_CACHE; | |
7519 // Write to the cache. | |
7520 RunFixtureTransactionTest(); | |
7521 | |
7522 EXPECT_EQ(1, transaction_count()); | |
7523 | |
7524 // Reading back reads from the cache. | |
7525 RunFixtureTransactionTest(); | |
7526 | |
7527 EXPECT_EQ(1, transaction_count()); | |
7528 | |
7529 // If there was an async transaction created, it would run now. | |
7530 base::RunLoop().RunUntilIdle(); | |
7531 | |
7532 // There was no async transaction. | |
7533 EXPECT_EQ(1, transaction_count()); | |
7534 } | |
7535 | |
7536 TEST_F(HttpCacheStaleWhileRevalidateTest, NotUsedWhenDisabled) { | |
7537 cache_.http_cache()->set_use_stale_while_revalidate_for_testing(false); | |
7538 // Write to the cache. | |
7539 RunFixtureTransactionTest(); | |
7540 | |
7541 EXPECT_EQ(1, transaction_count()); | |
7542 | |
7543 // A synchronous revalidation is performed. | |
7544 RunFixtureTransactionTest(); | |
7545 | |
7546 EXPECT_EQ(2, transaction_count()); | |
7547 } | |
7548 | |
7549 TEST_F(HttpCacheStaleWhileRevalidateTest, | |
7550 OnlyFromCacheDoesNotTriggerAsyncRequest) { | |
7551 transaction_.load_flags |= net::LOAD_ONLY_FROM_CACHE; | |
7552 transaction_.return_code = net::ERR_CACHE_MISS; | |
7553 | |
7554 // Writing to the cache should fail, because we are avoiding the network. | |
7555 RunFixtureTransactionTest(); | |
7556 | |
7557 EXPECT_EQ(0, transaction_count()); | |
7558 | |
7559 base::RunLoop().RunUntilIdle(); | |
7560 | |
7561 // Still nothing. | |
7562 EXPECT_EQ(0, transaction_count()); | |
7563 } | |
7564 | |
7565 // A certificate error during an asynchronous fetch should cause the next fetch | |
7566 // to proceed synchronously. | |
7567 // TODO(ricea): In future, only certificate errors which require user | |
7568 // interaction should fail the asynchronous revalidation, and they should cause | |
7569 // the next revalidation to be synchronous rather than requiring a total | |
7570 // refetch. This test will need to be updated appropriately. | |
7571 TEST_F(HttpCacheStaleWhileRevalidateTest, CertificateErrorCausesRefetch) { | |
7572 // Write to the cache. | |
7573 RunFixtureTransactionTest(); | |
7574 | |
7575 EXPECT_EQ(1, transaction_count()); | |
7576 | |
7577 // Now read back. RunTransactionTestBase() expects to receive the network | |
7578 // error back from the HttpCache::Transaction, but since the cache request | |
7579 // will return OK we need to duplicate some of its implementation here. | |
7580 transaction_.return_code = net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED; | |
7581 net::TestCompletionCallback callback; | |
7582 scoped_ptr<net::HttpTransaction> trans; | |
7583 int rv = | |
7584 cache_.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, &trans); | |
7585 EXPECT_EQ(net::OK, rv); | |
7586 ASSERT_TRUE(trans.get()); | |
7587 | |
7588 MockHttpRequest request(transaction_); | |
7589 rv = trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
7590 ASSERT_EQ(net::ERR_IO_PENDING, rv); | |
7591 ASSERT_EQ(net::OK, callback.WaitForResult()); | |
7592 ReadAndVerifyTransaction(trans.get(), transaction_); | |
7593 | |
7594 EXPECT_EQ(1, transaction_count()); | |
7595 | |
7596 // Allow the asynchronous fetch to run. | |
7597 base::RunLoop().RunUntilIdle(); | |
7598 | |
7599 EXPECT_EQ(2, transaction_count()); | |
7600 | |
7601 // Now run the transaction again. It should run synchronously. | |
7602 transaction_.return_code = net::OK; | |
7603 RunFixtureTransactionTest(); | |
7604 | |
7605 EXPECT_EQ(3, transaction_count()); | |
7606 } | |
7607 | |
7608 // Ensure that the response cached by the asynchronous request is not truncated, | |
7609 // even if the server is slow. | |
7610 TEST_F(HttpCacheStaleWhileRevalidateTest, EntireResponseCached) { | |
7611 transaction_.test_mode = TEST_MODE_SLOW_READ; | |
7612 // Write to the cache. | |
7613 RunFixtureTransactionTest(); | |
7614 | |
7615 // Read back from the cache. | |
7616 RunFixtureTransactionTest(); | |
7617 | |
7618 // Let the async request execute. | |
7619 base::RunLoop().RunUntilIdle(); | |
7620 | |
7621 // The cache entry should still be complete. | |
7622 transaction_.load_flags = net::LOAD_ONLY_FROM_CACHE; | |
7623 RunFixtureTransactionTest(); | |
7624 } | |
7625 | |
7626 // Verify that there are no race conditions in the completely synchronous case. | |
7627 TEST_F(HttpCacheStaleWhileRevalidateTest, SynchronousCaseWorks) { | |
7628 transaction_.test_mode = TEST_MODE_SYNC_ALL; | |
7629 // Write to the cache. | |
7630 RunFixtureTransactionTest(); | |
7631 | |
7632 EXPECT_EQ(1, transaction_count()); | |
7633 | |
7634 // Read back from the cache. | |
7635 RunFixtureTransactionTest(); | |
7636 | |
7637 EXPECT_EQ(1, transaction_count()); | |
7638 | |
7639 // Let the async request execute. | |
7640 base::RunLoop().RunUntilIdle(); | |
7641 EXPECT_EQ(2, transaction_count()); | |
7642 } | |
7643 | |
7644 static void CheckLoadFlagsAsyncRevalidation(const net::HttpRequestInfo* request, | |
7645 std::string* response_status, | |
7646 std::string* response_headers, | |
7647 std::string* response_data) { | |
7648 EXPECT_EQ(net::LOAD_ASYNC_REVALIDATION, request->load_flags); | |
7649 } | |
7650 | |
7651 // Check that the load flags on the async request are the same as the load flags | |
7652 // on the original request, plus LOAD_ASYNC_REVALIDATION. | |
7653 TEST_F(HttpCacheStaleWhileRevalidateTest, LoadFlagsAsyncRevalidation) { | |
7654 transaction_.load_flags = net::LOAD_NORMAL; | |
7655 // Write to the cache. | |
7656 RunFixtureTransactionTest(); | |
7657 | |
7658 EXPECT_EQ(1, transaction_count()); | |
7659 | |
7660 // Read back from the cache. | |
7661 RunFixtureTransactionTest(); | |
7662 | |
7663 EXPECT_EQ(1, transaction_count()); | |
7664 | |
7665 transaction_.handler = CheckLoadFlagsAsyncRevalidation; | |
7666 // Let the async request execute. | |
7667 base::RunLoop().RunUntilIdle(); | |
7668 EXPECT_EQ(2, transaction_count()); | |
7669 } | |
7670 | |
7671 static void SimpleMockAuthHandler(const net::HttpRequestInfo* request, | |
7672 std::string* response_status, | |
7673 std::string* response_headers, | |
7674 std::string* response_data) { | |
7675 if (request->extra_headers.HasHeader("X-Require-Mock-Auth") && | |
7676 !request->extra_headers.HasHeader("Authorization")) { | |
7677 response_status->assign("HTTP/1.1 401 Unauthorized"); | |
7678 response_headers->assign("WWW-Authenticate: Basic realm=\"mars\"\n"); | |
7679 return; | |
7680 } | |
7681 response_status->assign("HTTP/1.1 200 OK"); | |
7682 } | |
7683 | |
7684 TEST_F(HttpCacheStaleWhileRevalidateTest, RestartForAuth) { | |
7685 // Write to the cache. | |
7686 RunFixtureTransactionTest(); | |
7687 | |
7688 EXPECT_EQ(1, transaction_count()); | |
7689 | |
7690 // Now make the transaction require auth. | |
7691 transaction_.request_headers = "X-Require-Mock-Auth: dummy\r\n\r\n"; | |
7692 transaction_.handler = SimpleMockAuthHandler; | |
7693 | |
7694 // Read back from the cache. | |
7695 RunFixtureTransactionTest(); | |
7696 | |
7697 EXPECT_EQ(1, transaction_count()); | |
7698 | |
7699 // Let the async request execute. | |
7700 base::RunLoop().RunUntilIdle(); | |
7701 | |
7702 EXPECT_EQ(2, transaction_count()); | |
7703 } | |
7704 | |
7705 // Tests that we allow multiple simultaneous, non-overlapping transactions to | |
7706 // take place on a sparse entry. | |
7707 TEST(HttpCache, RangeGET_MultipleRequests) { | |
7708 MockHttpCache cache; | |
7709 | |
7710 // Create a transaction for bytes 0-9. | |
7711 MockHttpRequest request(kRangeGET_TransactionOK); | |
7712 MockTransaction transaction(kRangeGET_TransactionOK); | |
7713 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; | |
7714 transaction.data = "rg: 00-09 "; | |
7715 AddMockTransaction(&transaction); | |
7716 | |
7717 net::TestCompletionCallback callback; | |
7718 scoped_ptr<net::HttpTransaction> trans; | |
7719 int rv = cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, &trans); | |
7720 EXPECT_EQ(net::OK, rv); | |
7721 ASSERT_TRUE(trans.get()); | |
7722 | |
7723 // Start our transaction. | |
7724 trans->Start(&request, callback.callback(), net::BoundNetLog()); | |
7725 | |
7726 // A second transaction on a different part of the file (the default | |
7727 // kRangeGET_TransactionOK requests 40-49) should not be blocked by | |
7728 // the already pending transaction. | |
7729 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | |
7730 | |
7731 // Let the first transaction complete. | |
7732 callback.WaitForResult(); | |
7733 | |
7734 RemoveMockTransaction(&transaction); | |
7735 } | |
7736 | |
7737 // Makes sure that a request stops using the cache when the response headers | |
7738 // with "Cache-Control: no-store" arrives. That means that another request for | |
7739 // the same URL can be processed before the response body of the original | |
7740 // request arrives. | |
7741 TEST(HttpCache, NoStoreResponseShouldNotBlockFollowingRequests) { | |
7742 MockHttpCache cache; | |
7743 ScopedMockTransaction mock_transaction(kSimpleGET_Transaction); | |
7744 mock_transaction.response_headers = "Cache-Control: no-store\n"; | |
7745 MockHttpRequest request(mock_transaction); | |
7746 | |
7747 scoped_ptr<Context> first(new Context); | |
7748 first->result = cache.CreateTransaction(&first->trans); | |
7749 ASSERT_EQ(net::OK, first->result); | |
7750 EXPECT_EQ(net::LOAD_STATE_IDLE, first->trans->GetLoadState()); | |
7751 first->result = first->trans->Start( | |
7752 &request, first->callback.callback(), net::BoundNetLog()); | |
7753 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, first->trans->GetLoadState()); | |
7754 | |
7755 base::MessageLoop::current()->RunUntilIdle(); | |
7756 EXPECT_EQ(net::LOAD_STATE_IDLE, first->trans->GetLoadState()); | |
7757 ASSERT_TRUE(first->trans->GetResponseInfo()); | |
7758 EXPECT_TRUE(first->trans->GetResponseInfo()->headers->HasHeaderValue( | |
7759 "Cache-Control", "no-store")); | |
7760 // Here we have read the response header but not read the response body yet. | |
7761 | |
7762 // Let us create the second (read) transaction. | |
7763 scoped_ptr<Context> second(new Context); | |
7764 second->result = cache.CreateTransaction(&second->trans); | |
7765 ASSERT_EQ(net::OK, second->result); | |
7766 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); | |
7767 second->result = second->trans->Start( | |
7768 &request, second->callback.callback(), net::BoundNetLog()); | |
7769 | |
7770 // Here the second transaction proceeds without reading the first body. | |
7771 EXPECT_EQ(net::LOAD_STATE_WAITING_FOR_CACHE, second->trans->GetLoadState()); | |
7772 base::MessageLoop::current()->RunUntilIdle(); | |
7773 EXPECT_EQ(net::LOAD_STATE_IDLE, second->trans->GetLoadState()); | |
7774 ASSERT_TRUE(second->trans->GetResponseInfo()); | |
7775 EXPECT_TRUE(second->trans->GetResponseInfo()->headers->HasHeaderValue( | |
7776 "Cache-Control", "no-store")); | |
7777 ReadAndVerifyTransaction(second->trans.get(), kSimpleGET_Transaction); | |
7778 } | |
OLD | NEW |