OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 using net::test::IsOk; | 63 using net::test::IsOk; |
64 | 64 |
65 using base::Time; | 65 using base::Time; |
66 | 66 |
67 namespace net { | 67 namespace net { |
68 | 68 |
69 using CacheEntryStatus = HttpResponseInfo::CacheEntryStatus; | 69 using CacheEntryStatus = HttpResponseInfo::CacheEntryStatus; |
70 | 70 |
71 namespace { | 71 namespace { |
72 | 72 |
73 // Returns a simple text serialization of the given | |
74 // |HttpResponseHeaders|. This is used by tests to verify that an | |
75 // |HttpResponseHeaders| matches an expectation string. | |
76 // | |
77 // * One line per header, written as: | |
78 // HEADER_NAME: HEADER_VALUE\n | |
79 // * The original case of header names is preserved. | |
80 // * Whitespace around head names/values is stripped. | |
81 // * Repeated headers are not aggregated. | |
82 // * Headers are listed in their original order. | |
83 std::string ToSimpleString(const scoped_refptr<HttpResponseHeaders>& parsed) { | |
tfarina
2017/02/14 22:13:18
Sorry, I had to steal your function. Maybe we can
eroman
2017/02/16 20:37:35
Can you add a TODO explaining this is a duplicate
tfarina
2017/02/16 22:18:02
Done.
| |
84 std::string result = parsed->GetStatusLine() + "\n"; | |
85 | |
86 size_t iter = 0; | |
87 std::string name; | |
88 std::string value; | |
89 while (parsed->EnumerateHeaderLines(&iter, &name, &value)) { | |
90 std::string new_line = name + ": " + value + "\n"; | |
91 | |
92 result += new_line; | |
93 } | |
94 | |
95 return result; | |
96 } | |
97 | |
73 // Tests the load timing values of a request that goes through a | 98 // Tests the load timing values of a request that goes through a |
74 // MockNetworkTransaction. | 99 // MockNetworkTransaction. |
75 void TestLoadTimingNetworkRequest(const LoadTimingInfo& load_timing_info) { | 100 void TestLoadTimingNetworkRequest(const LoadTimingInfo& load_timing_info) { |
76 EXPECT_FALSE(load_timing_info.socket_reused); | 101 EXPECT_FALSE(load_timing_info.socket_reused); |
77 EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); | 102 EXPECT_NE(NetLogSource::kInvalidId, load_timing_info.socket_log_id); |
78 | 103 |
79 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); | 104 EXPECT_TRUE(load_timing_info.proxy_resolve_start.is_null()); |
80 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); | 105 EXPECT_TRUE(load_timing_info.proxy_resolve_end.is_null()); |
81 | 106 |
82 ExpectConnectTimingHasTimes(load_timing_info.connect_timing, | 107 ExpectConnectTimingHasTimes(load_timing_info.connect_timing, |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), | 280 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), |
256 response, log, load_timing_info, nullptr, nullptr, | 281 response, log, load_timing_info, nullptr, nullptr, |
257 nullptr); | 282 nullptr); |
258 } | 283 } |
259 | 284 |
260 void RunTransactionTestWithResponse(HttpCache* cache, | 285 void RunTransactionTestWithResponse(HttpCache* cache, |
261 const MockTransaction& trans_info, | 286 const MockTransaction& trans_info, |
262 std::string* response_headers) { | 287 std::string* response_headers) { |
263 HttpResponseInfo response; | 288 HttpResponseInfo response; |
264 RunTransactionTestWithResponseInfo(cache, trans_info, &response); | 289 RunTransactionTestWithResponseInfo(cache, trans_info, &response); |
265 response.headers->GetNormalizedHeaders(response_headers); | 290 *response_headers = ToSimpleString(response.headers); |
266 } | 291 } |
267 | 292 |
268 void RunTransactionTestWithResponseAndGetTiming( | 293 void RunTransactionTestWithResponseAndGetTiming( |
269 HttpCache* cache, | 294 HttpCache* cache, |
270 const MockTransaction& trans_info, | 295 const MockTransaction& trans_info, |
271 std::string* response_headers, | 296 std::string* response_headers, |
272 const NetLogWithSource& log, | 297 const NetLogWithSource& log, |
273 LoadTimingInfo* load_timing_info) { | 298 LoadTimingInfo* load_timing_info) { |
274 HttpResponseInfo response; | 299 HttpResponseInfo response; |
275 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), | 300 RunTransactionTestBase(cache, trans_info, MockHttpRequest(trans_info), |
276 &response, log, load_timing_info, nullptr, nullptr, | 301 &response, log, load_timing_info, nullptr, nullptr, |
277 nullptr); | 302 nullptr); |
278 response.headers->GetNormalizedHeaders(response_headers); | 303 *response_headers = ToSimpleString(response.headers); |
279 } | 304 } |
280 | 305 |
281 // This class provides a handler for kFastNoStoreGET_Transaction so that the | 306 // This class provides a handler for kFastNoStoreGET_Transaction so that the |
282 // no-store header can be included on demand. | 307 // no-store header can be included on demand. |
283 class FastTransactionServer { | 308 class FastTransactionServer { |
284 public: | 309 public: |
285 FastTransactionServer() { | 310 FastTransactionServer() { |
286 no_store = false; | 311 no_store = false; |
287 } | 312 } |
288 ~FastTransactionServer() {} | 313 ~FastTransactionServer() {} |
(...skipping 6438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
6727 | 6752 |
6728 HttpResponseInfo response; | 6753 HttpResponseInfo response; |
6729 RunTransactionTestWithResponseInfo(cache.http_cache(), request, &response); | 6754 RunTransactionTestWithResponseInfo(cache.http_cache(), request, &response); |
6730 | 6755 |
6731 // The request and response times should have been updated. | 6756 // The request and response times should have been updated. |
6732 EXPECT_EQ(request_time.ToInternalValue(), | 6757 EXPECT_EQ(request_time.ToInternalValue(), |
6733 response.request_time.ToInternalValue()); | 6758 response.request_time.ToInternalValue()); |
6734 EXPECT_EQ(response_time.ToInternalValue(), | 6759 EXPECT_EQ(response_time.ToInternalValue(), |
6735 response.response_time.ToInternalValue()); | 6760 response.response_time.ToInternalValue()); |
6736 | 6761 |
6737 std::string headers; | |
6738 response.headers->GetNormalizedHeaders(&headers); | |
6739 | |
6740 EXPECT_EQ("HTTP/1.1 200 OK\n" | 6762 EXPECT_EQ("HTTP/1.1 200 OK\n" |
6741 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" | 6763 "Date: Wed, 22 Jul 2009 03:15:26 GMT\n" |
6742 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", | 6764 "Last-Modified: Wed, 06 Feb 2008 22:38:21 GMT\n", |
6743 headers); | 6765 ToSimpleString(response.headers)); |
6744 | 6766 |
6745 RemoveMockTransaction(&mock_network_response); | 6767 RemoveMockTransaction(&mock_network_response); |
6746 } | 6768 } |
6747 | 6769 |
6748 // Tests that we can write metadata to an entry. | 6770 // Tests that we can write metadata to an entry. |
6749 TEST(HttpCache, WriteMetadata_OK) { | 6771 TEST(HttpCache, WriteMetadata_OK) { |
6750 MockHttpCache cache; | 6772 MockHttpCache cache; |
6751 | 6773 |
6752 // Write to the cache | 6774 // Write to the cache |
6753 HttpResponseInfo response; | 6775 HttpResponseInfo response; |
(...skipping 1568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
8322 RunTransactionTestWithResponseInfo(cache.http_cache(), | 8344 RunTransactionTestWithResponseInfo(cache.http_cache(), |
8323 kTypicalGET_Transaction, &response_info); | 8345 kTypicalGET_Transaction, &response_info); |
8324 | 8346 |
8325 EXPECT_FALSE(response_info.was_cached); | 8347 EXPECT_FALSE(response_info.was_cached); |
8326 EXPECT_TRUE(response_info.network_accessed); | 8348 EXPECT_TRUE(response_info.network_accessed); |
8327 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, | 8349 EXPECT_EQ(CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE, |
8328 response_info.cache_entry_status); | 8350 response_info.cache_entry_status); |
8329 } | 8351 } |
8330 | 8352 |
8331 } // namespace net | 8353 } // namespace net |
OLD | NEW |