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/stl_util.h" |
10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
12 #include "net/tools/balsa/balsa_headers.h" | 13 #include "net/tools/balsa/balsa_headers.h" |
13 #include "net/tools/quic/quic_in_memory_cache.h" | 14 #include "net/tools/quic/quic_in_memory_cache.h" |
14 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h" | 15 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 using base::IntToString; | 18 using base::IntToString; |
18 using base::StringPiece; | 19 using base::StringPiece; |
19 using std::string; | 20 using std::string; |
20 | 21 |
21 namespace net { | 22 namespace net { |
22 namespace tools { | 23 namespace tools { |
23 namespace test { | 24 namespace test { |
24 | 25 |
25 class QuicInMemoryCacheTest : public ::testing::Test { | 26 class QuicInMemoryCacheTest : public ::testing::Test { |
26 protected: | 27 protected: |
27 QuicInMemoryCacheTest() { | 28 QuicInMemoryCacheTest() { |
| 29 QuicInMemoryCachePeer::ResetForTests(); |
| 30 } |
| 31 |
| 32 ~QuicInMemoryCacheTest() override { QuicInMemoryCachePeer::ResetForTests(); } |
| 33 |
| 34 void CreateRequest(string host, string path, BalsaHeaders* headers) { |
| 35 headers->SetRequestFirstlineFromStringPieces("GET", path, "HTTP/1.1"); |
| 36 headers->ReplaceOrAppendHeader("host", host); |
| 37 } |
| 38 |
| 39 string CacheDirectory() { |
28 base::FilePath path; | 40 base::FilePath path; |
29 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 41 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
30 path = path.AppendASCII("net").AppendASCII("data") | 42 path = path.AppendASCII("net").AppendASCII("data") |
31 .AppendASCII("quic_in_memory_cache_data"); | 43 .AppendASCII("quic_in_memory_cache_data"); |
32 // The file path is known to be an ascii string. | 44 // The file path is known to be an ascii string. |
33 FLAGS_quic_in_memory_cache_dir = path.MaybeAsASCII(); | 45 return path.MaybeAsASCII(); |
34 } | 46 } |
| 47 }; |
35 | 48 |
36 ~QuicInMemoryCacheTest() override { QuicInMemoryCachePeer::ResetForTests(); } | 49 TEST_F(QuicInMemoryCacheTest, GetResponseNoMatch) { |
37 | 50 const QuicInMemoryCache::Response* response = |
38 void CreateRequest(StringPiece host, | 51 QuicInMemoryCache::GetInstance()->GetResponse("mail.google.com", |
39 StringPiece path, | 52 "/index.html"); |
40 BalsaHeaders* headers) { | 53 ASSERT_FALSE(response); |
41 headers->SetRequestFirstlineFromStringPieces("GET", path, "HTTP/1.1"); | 54 } |
42 headers->ReplaceOrAppendHeader("host", host); | |
43 } | |
44 | |
45 void SetUp() override { QuicInMemoryCachePeer::ResetForTests(); } | |
46 | |
47 }; | |
48 | 55 |
49 TEST_F(QuicInMemoryCacheTest, AddSimpleResponseGetResponse) { | 56 TEST_F(QuicInMemoryCacheTest, AddSimpleResponseGetResponse) { |
50 string response_body("hello response"); | 57 string response_body("hello response"); |
51 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); | 58 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); |
52 cache->AddSimpleResponse("www.google.com", "/", 200, "OK", response_body); | 59 cache->AddSimpleResponse("www.google.com", "/", 200, "OK", response_body); |
53 | 60 |
54 BalsaHeaders request_headers; | 61 BalsaHeaders request_headers; |
55 CreateRequest("www.google.com", "/", &request_headers); | 62 CreateRequest("www.google.com", "/", &request_headers); |
56 const QuicInMemoryCache::Response* response = | 63 const QuicInMemoryCache::Response* response = |
57 cache->GetResponse("www.google.com", "/"); | 64 cache->GetResponse("www.google.com", "/"); |
58 ASSERT_TRUE(response); | 65 ASSERT_TRUE(response); |
59 EXPECT_EQ("200", response->headers().response_code()); | 66 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); |
| 67 EXPECT_EQ("200 OK", response->headers().find(":status")->second); |
60 EXPECT_EQ(response_body.size(), response->body().length()); | 68 EXPECT_EQ(response_body.size(), response->body().length()); |
61 } | 69 } |
62 | 70 |
63 TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) { | 71 TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) { |
| 72 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory()); |
64 const QuicInMemoryCache::Response* response = | 73 const QuicInMemoryCache::Response* response = |
65 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url", | 74 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url", |
66 "/index.html"); | 75 "/index.html"); |
67 ASSERT_TRUE(response); | 76 ASSERT_TRUE(response); |
68 string value; | 77 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); |
69 response->headers().GetAllOfHeaderAsString("Connection", &value); | 78 EXPECT_EQ("200 OK", response->headers().find(":status")->second); |
70 EXPECT_EQ("200", response->headers().response_code()); | 79 ASSERT_TRUE(ContainsKey(response->headers(), "connection")); |
71 EXPECT_EQ("Keep-Alive", value); | 80 EXPECT_EQ("close", response->headers().find("connection")->second); |
72 EXPECT_LT(0U, response->body().length()); | 81 EXPECT_LT(0U, response->body().length()); |
73 } | 82 } |
74 | 83 |
75 TEST_F(QuicInMemoryCacheTest, UsesOriginalUrl) { | 84 TEST_F(QuicInMemoryCacheTest, UsesOriginalUrl) { |
| 85 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory()); |
76 const QuicInMemoryCache::Response* response = | 86 const QuicInMemoryCache::Response* response = |
77 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url", | 87 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url", |
78 "/index.html"); | 88 "/index.html"); |
79 ASSERT_TRUE(response); | 89 ASSERT_TRUE(response); |
80 EXPECT_EQ("200", response->headers().response_code()); | 90 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); |
81 string value; | 91 EXPECT_EQ("200 OK", response->headers().find(":status")->second); |
82 response->headers().GetAllOfHeaderAsString("Connection", &value); | 92 ASSERT_TRUE(ContainsKey(response->headers(), "connection")); |
| 93 EXPECT_EQ("close", response->headers().find("connection")->second); |
83 EXPECT_LT(0U, response->body().length()); | 94 EXPECT_LT(0U, response->body().length()); |
84 } | 95 } |
85 | 96 |
86 TEST_F(QuicInMemoryCacheTest, GetResponseNoMatch) { | |
87 const QuicInMemoryCache::Response* response = | |
88 QuicInMemoryCache::GetInstance()->GetResponse("mail.google.com", | |
89 "/index.html"); | |
90 ASSERT_FALSE(response); | |
91 } | |
92 | |
93 } // namespace test | 97 } // namespace test |
94 } // namespace tools | 98 } // namespace tools |
95 } // namespace net | 99 } // namespace net |
OLD | NEW |