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 "net/tools/quic/quic_in_memory_cache.h" | 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" |
| 11 #include "base/strings/string_piece.h" |
10 #include "net/tools/flip_server/balsa_headers.h" | 12 #include "net/tools/flip_server/balsa_headers.h" |
| 13 #include "net/tools/quic/quic_in_memory_cache.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
12 | 15 |
| 16 using base::IntToString; |
| 17 using base::StringPiece; |
| 18 |
13 namespace net { | 19 namespace net { |
14 namespace tools { | 20 namespace tools { |
15 namespace test { | 21 namespace test { |
16 namespace { | |
17 | 22 |
18 class QuicInMemoryCacheTest : public ::testing::Test { | 23 class QuicInMemoryCacheTest : public ::testing::Test { |
19 protected: | 24 protected: |
20 QuicInMemoryCacheTest() { | 25 QuicInMemoryCacheTest() { |
21 base::FilePath path; | 26 base::FilePath path; |
22 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 27 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
23 path = path.AppendASCII("net").AppendASCII("data") | 28 path = path.AppendASCII("net").AppendASCII("data") |
24 .AppendASCII("quic_in_memory_cache_data"); | 29 .AppendASCII("quic_in_memory_cache_data"); |
25 // The file path is known to be an ascii string. | 30 // The file path is known to be an ascii string. |
26 FLAGS_quic_in_memory_cache_dir = path.MaybeAsASCII(); | 31 FLAGS_quic_in_memory_cache_dir = path.MaybeAsASCII(); |
27 } | 32 } |
28 | 33 |
29 void CreateRequest(std::string host, | 34 void CreateRequest(std::string host, |
30 std::string path, | 35 std::string path, |
31 net::BalsaHeaders* headers) { | 36 net::BalsaHeaders* headers) { |
32 headers->SetRequestFirstlineFromStringPieces("GET", path, "HTTP/1.1"); | 37 headers->SetRequestFirstlineFromStringPieces("GET", path, "HTTP/1.1"); |
33 headers->ReplaceOrAppendHeader("host", host); | 38 headers->ReplaceOrAppendHeader("host", host); |
34 } | 39 } |
| 40 |
| 41 virtual void SetUp() { |
| 42 QuicInMemoryCache::GetInstance()->ResetForTests(); |
| 43 } |
| 44 |
| 45 // This method was copied from end_to_end_test.cc in this directory. |
| 46 void AddToCache(const StringPiece& method, |
| 47 const StringPiece& path, |
| 48 const StringPiece& version, |
| 49 const StringPiece& response_code, |
| 50 const StringPiece& response_detail, |
| 51 const StringPiece& body) { |
| 52 BalsaHeaders request_headers, response_headers; |
| 53 request_headers.SetRequestFirstlineFromStringPieces(method, |
| 54 path, |
| 55 version); |
| 56 response_headers.SetRequestFirstlineFromStringPieces(version, |
| 57 response_code, |
| 58 response_detail); |
| 59 response_headers.AppendHeader("content-length", |
| 60 base::IntToString(body.length())); |
| 61 |
| 62 // Check if response already exists and matches. |
| 63 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); |
| 64 const QuicInMemoryCache::Response* cached_response = |
| 65 cache->GetResponse(request_headers); |
| 66 if (cached_response != NULL) { |
| 67 std::string cached_response_headers_str, response_headers_str; |
| 68 cached_response->headers().DumpToString(&cached_response_headers_str); |
| 69 response_headers.DumpToString(&response_headers_str); |
| 70 CHECK_EQ(cached_response_headers_str, response_headers_str); |
| 71 CHECK_EQ(cached_response->body(), body); |
| 72 return; |
| 73 } |
| 74 cache->AddResponse(request_headers, response_headers, body); |
| 75 } |
35 }; | 76 }; |
36 | 77 |
| 78 TEST_F(QuicInMemoryCacheTest, AddResponseGetResponse) { |
| 79 std::string response_body("hello response"); |
| 80 AddToCache("GET", "https://www.google.com/bar", |
| 81 "HTTP/1.1", "200", "OK", response_body); |
| 82 net::BalsaHeaders request_headers; |
| 83 CreateRequest("www.google.com", "/bar", &request_headers); |
| 84 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); |
| 85 const QuicInMemoryCache::Response* response = |
| 86 cache->GetResponse(request_headers); |
| 87 ASSERT_TRUE(response); |
| 88 EXPECT_EQ("200", response->headers().response_code()); |
| 89 EXPECT_EQ(response_body.size(), response->body().length()); |
| 90 |
| 91 CreateRequest("", "https://www.google.com/bar", &request_headers); |
| 92 response = cache->GetResponse(request_headers); |
| 93 ASSERT_TRUE(response); |
| 94 EXPECT_EQ("200", response->headers().response_code()); |
| 95 EXPECT_EQ(response_body.size(), response->body().length()); |
| 96 } |
| 97 |
37 TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) { | 98 TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) { |
38 net::BalsaHeaders request_headers; | 99 net::BalsaHeaders request_headers; |
39 CreateRequest("quic.test.url", "/index.html", &request_headers); | 100 CreateRequest("quic.test.url", "/index.html", &request_headers); |
40 | 101 |
41 const QuicInMemoryCache::Response* response = | 102 const QuicInMemoryCache::Response* response = |
42 QuicInMemoryCache::GetInstance()->GetResponse(request_headers); | 103 QuicInMemoryCache::GetInstance()->GetResponse(request_headers); |
43 ASSERT_TRUE(response); | 104 ASSERT_TRUE(response); |
44 std::string value; | 105 std::string value; |
45 response->headers().GetAllOfHeaderAsString("Connection", &value); | 106 response->headers().GetAllOfHeaderAsString("Connection", &value); |
46 EXPECT_EQ("200", response->headers().response_code()); | 107 EXPECT_EQ("200", response->headers().response_code()); |
47 EXPECT_EQ("Keep-Alive", value); | 108 EXPECT_EQ("Keep-Alive", value); |
48 EXPECT_LT(0U, response->body().length()); | 109 EXPECT_LT(0U, response->body().length()); |
49 } | 110 } |
50 | 111 |
51 TEST_F(QuicInMemoryCacheTest, ReadsCacheDirHttp) { | 112 TEST_F(QuicInMemoryCacheTest, ReadsCacheDirHttp) { |
52 net::BalsaHeaders request_headers; | 113 net::BalsaHeaders request_headers; |
53 CreateRequest("http://quic.test.url", "/index.html", &request_headers); | 114 CreateRequest("", "http://quic.test.url/index.html", &request_headers); |
54 | 115 |
55 const QuicInMemoryCache::Response* response = | 116 const QuicInMemoryCache::Response* response = |
56 QuicInMemoryCache::GetInstance()->GetResponse(request_headers); | 117 QuicInMemoryCache::GetInstance()->GetResponse(request_headers); |
57 ASSERT_TRUE(response); | 118 ASSERT_TRUE(response); |
58 std::string value; | 119 std::string value; |
59 response->headers().GetAllOfHeaderAsString("Connection", &value); | 120 response->headers().GetAllOfHeaderAsString("Connection", &value); |
60 EXPECT_EQ("200", response->headers().response_code()); | 121 EXPECT_EQ("200", response->headers().response_code()); |
61 EXPECT_EQ("Keep-Alive", value); | 122 EXPECT_EQ("Keep-Alive", value); |
62 EXPECT_LT(0U, response->body().length()); | 123 EXPECT_LT(0U, response->body().length()); |
63 } | 124 } |
64 | 125 |
65 TEST_F(QuicInMemoryCacheTest, GetResponseNoMatch) { | 126 TEST_F(QuicInMemoryCacheTest, GetResponseNoMatch) { |
66 net::BalsaHeaders request_headers; | 127 net::BalsaHeaders request_headers; |
67 CreateRequest("www.google.com", "/index.html", &request_headers); | 128 CreateRequest("www.google.com", "/index.html", &request_headers); |
68 | 129 |
69 const QuicInMemoryCache::Response* response = | 130 const QuicInMemoryCache::Response* response = |
70 QuicInMemoryCache::GetInstance()->GetResponse(request_headers); | 131 QuicInMemoryCache::GetInstance()->GetResponse(request_headers); |
71 ASSERT_FALSE(response); | 132 ASSERT_FALSE(response); |
72 } | 133 } |
73 | 134 |
74 } // namespace | |
75 } // namespace test | 135 } // namespace test |
76 } // namespace tools | 136 } // namespace tools |
77 } // namespace net | 137 } // namespace net |
OLD | NEW |