OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/memory/singleton.h" | 8 #include "base/memory/singleton.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
12 #include "net/tools/balsa/balsa_headers.h" | 12 #include "net/tools/balsa/balsa_headers.h" |
13 #include "net/tools/quic/quic_in_memory_cache.h" | 13 #include "net/tools/quic/quic_in_memory_cache.h" |
14 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h" | 14 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 using base::IntToString; | 17 using base::IntToString; |
18 using base::StringPiece; | 18 using base::StringPiece; |
19 | 19 |
20 namespace net { | 20 namespace net { |
21 namespace tools { | 21 namespace tools { |
22 namespace test { | 22 namespace test { |
23 | 23 |
24 class QuicInMemoryCacheTest : public ::testing::Test { | 24 class QuicInMemoryCacheTest : public ::testing::Test { |
25 protected: | 25 protected: |
26 QuicInMemoryCacheTest() { | 26 QuicInMemoryCacheTest() { |
27 base::FilePath path; | 27 base::FilePath path; |
28 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 28 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
29 path = path.AppendASCII("net").AppendASCII("data") | 29 path = path.AppendASCII("net").AppendASCII("data").AppendASCII( |
30 .AppendASCII("quic_in_memory_cache_data"); | 30 "quic_in_memory_cache_data"); |
31 // The file path is known to be an ascii string. | 31 // The file path is known to be an ascii string. |
32 FLAGS_quic_in_memory_cache_dir = path.MaybeAsASCII(); | 32 FLAGS_quic_in_memory_cache_dir = path.MaybeAsASCII(); |
33 } | 33 } |
34 | 34 |
35 void CreateRequest(std::string host, | 35 void CreateRequest(std::string host, |
36 std::string path, | 36 std::string path, |
37 net::BalsaHeaders* headers) { | 37 net::BalsaHeaders* headers) { |
38 headers->SetRequestFirstlineFromStringPieces("GET", path, "HTTP/1.1"); | 38 headers->SetRequestFirstlineFromStringPieces("GET", path, "HTTP/1.1"); |
39 headers->ReplaceOrAppendHeader("host", host); | 39 headers->ReplaceOrAppendHeader("host", host); |
40 } | 40 } |
41 | 41 |
42 virtual void SetUp() { | 42 virtual void SetUp() { QuicInMemoryCachePeer::ResetForTests(); } |
43 QuicInMemoryCachePeer::ResetForTests(); | |
44 } | |
45 | 43 |
46 // This method was copied from end_to_end_test.cc in this directory. | 44 // This method was copied from end_to_end_test.cc in this directory. |
47 void AddToCache(const StringPiece& method, | 45 void AddToCache(const StringPiece& method, |
48 const StringPiece& path, | 46 const StringPiece& path, |
49 const StringPiece& version, | 47 const StringPiece& version, |
50 const StringPiece& response_code, | 48 const StringPiece& response_code, |
51 const StringPiece& response_detail, | 49 const StringPiece& response_detail, |
52 const StringPiece& body) { | 50 const StringPiece& body) { |
53 BalsaHeaders request_headers, response_headers; | 51 BalsaHeaders request_headers, response_headers; |
54 request_headers.SetRequestFirstlineFromStringPieces(method, | 52 request_headers.SetRequestFirstlineFromStringPieces(method, path, version); |
55 path, | 53 response_headers.SetRequestFirstlineFromStringPieces( |
56 version); | 54 version, response_code, response_detail); |
57 response_headers.SetRequestFirstlineFromStringPieces(version, | |
58 response_code, | |
59 response_detail); | |
60 response_headers.AppendHeader("content-length", | 55 response_headers.AppendHeader("content-length", |
61 base::IntToString(body.length())); | 56 base::IntToString(body.length())); |
62 | 57 |
63 // Check if response already exists and matches. | 58 // Check if response already exists and matches. |
64 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); | 59 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); |
65 const QuicInMemoryCache::Response* cached_response = | 60 const QuicInMemoryCache::Response* cached_response = |
66 cache->GetResponse(request_headers); | 61 cache->GetResponse(request_headers); |
67 if (cached_response != NULL) { | 62 if (cached_response != NULL) { |
68 std::string cached_response_headers_str, response_headers_str; | 63 std::string cached_response_headers_str, response_headers_str; |
69 cached_response->headers().DumpToString(&cached_response_headers_str); | 64 cached_response->headers().DumpToString(&cached_response_headers_str); |
70 response_headers.DumpToString(&response_headers_str); | 65 response_headers.DumpToString(&response_headers_str); |
71 CHECK_EQ(cached_response_headers_str, response_headers_str); | 66 CHECK_EQ(cached_response_headers_str, response_headers_str); |
72 CHECK_EQ(cached_response->body(), body); | 67 CHECK_EQ(cached_response->body(), body); |
73 return; | 68 return; |
74 } | 69 } |
75 cache->AddResponse(request_headers, response_headers, body); | 70 cache->AddResponse(request_headers, response_headers, body); |
76 } | 71 } |
77 }; | 72 }; |
78 | 73 |
79 TEST_F(QuicInMemoryCacheTest, AddResponseGetResponse) { | 74 TEST_F(QuicInMemoryCacheTest, AddResponseGetResponse) { |
80 std::string response_body("hello response"); | 75 std::string response_body("hello response"); |
81 AddToCache("GET", "https://www.google.com/bar", | 76 AddToCache("GET", |
82 "HTTP/1.1", "200", "OK", response_body); | 77 "https://www.google.com/bar", |
| 78 "HTTP/1.1", |
| 79 "200", |
| 80 "OK", |
| 81 response_body); |
83 net::BalsaHeaders request_headers; | 82 net::BalsaHeaders request_headers; |
84 CreateRequest("www.google.com", "/bar", &request_headers); | 83 CreateRequest("www.google.com", "/bar", &request_headers); |
85 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); | 84 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); |
86 const QuicInMemoryCache::Response* response = | 85 const QuicInMemoryCache::Response* response = |
87 cache->GetResponse(request_headers); | 86 cache->GetResponse(request_headers); |
88 ASSERT_TRUE(response); | 87 ASSERT_TRUE(response); |
89 EXPECT_EQ("200", response->headers().response_code()); | 88 EXPECT_EQ("200", response->headers().response_code()); |
90 EXPECT_EQ(response_body.size(), response->body().length()); | 89 EXPECT_EQ(response_body.size(), response->body().length()); |
91 | 90 |
92 CreateRequest("", "https://www.google.com/bar", &request_headers); | 91 CreateRequest("", "https://www.google.com/bar", &request_headers); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 CreateRequest("www.google.com", "/index.html", &request_headers); | 128 CreateRequest("www.google.com", "/index.html", &request_headers); |
130 | 129 |
131 const QuicInMemoryCache::Response* response = | 130 const QuicInMemoryCache::Response* response = |
132 QuicInMemoryCache::GetInstance()->GetResponse(request_headers); | 131 QuicInMemoryCache::GetInstance()->GetResponse(request_headers); |
133 ASSERT_FALSE(response); | 132 ASSERT_FALSE(response); |
134 } | 133 } |
135 | 134 |
136 } // namespace test | 135 } // namespace test |
137 } // namespace tools | 136 } // namespace tools |
138 } // namespace net | 137 } // namespace net |
OLD | NEW |