Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1309)

Side by Side Diff: net/tools/quic/quic_in_memory_cache_test.cc

Issue 1037533002: Make initialization of the QuicInMemoryCache explicit to avoid spooky action at a distance which co… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/stl_util.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_piece.h" 12 #include "base/strings/string_piece.h"
13 #include "net/tools/balsa/balsa_headers.h" 13 #include "net/tools/balsa/balsa_headers.h"
14 #include "net/tools/quic/quic_in_memory_cache.h" 14 #include "net/tools/quic/quic_in_memory_cache.h"
15 #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"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 17
18 using base::IntToString; 18 using base::IntToString;
19 using base::StringPiece; 19 using base::StringPiece;
20 using std::string; 20 using std::string;
21 21
22 namespace net { 22 namespace net {
23 namespace tools { 23 namespace tools {
24 namespace test { 24 namespace test {
25 25
26 class QuicInMemoryCacheTest : public ::testing::Test { 26 class QuicInMemoryCacheTest : public ::testing::Test {
27 protected: 27 protected:
28 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() {
29 base::FilePath path; 40 base::FilePath path;
30 PathService::Get(base::DIR_SOURCE_ROOT, &path); 41 PathService::Get(base::DIR_SOURCE_ROOT, &path);
31 path = path.AppendASCII("net").AppendASCII("data") 42 path = path.AppendASCII("net").AppendASCII("data")
32 .AppendASCII("quic_in_memory_cache_data"); 43 .AppendASCII("quic_in_memory_cache_data");
33 // The file path is known to be an ascii string. 44 // The file path is known to be an ascii string.
34 FLAGS_quic_in_memory_cache_dir = path.MaybeAsASCII(); 45 return path.MaybeAsASCII();
35 } 46 }
47 };
36 48
37 ~QuicInMemoryCacheTest() override { QuicInMemoryCachePeer::ResetForTests(); } 49 TEST_F(QuicInMemoryCacheTest, GetResponseNoMatch) {
38 50 const QuicInMemoryCache::Response* response =
39 void CreateRequest(StringPiece host, 51 QuicInMemoryCache::GetInstance()->GetResponse("mail.google.com",
40 StringPiece path, 52 "/index.html");
41 BalsaHeaders* headers) { 53 ASSERT_FALSE(response);
42 headers->SetRequestFirstlineFromStringPieces("GET", path, "HTTP/1.1"); 54 }
43 headers->ReplaceOrAppendHeader("host", host);
44 }
45
46 void SetUp() override { QuicInMemoryCachePeer::ResetForTests(); }
47
48 };
49 55
50 TEST_F(QuicInMemoryCacheTest, AddSimpleResponseGetResponse) { 56 TEST_F(QuicInMemoryCacheTest, AddSimpleResponseGetResponse) {
51 string response_body("hello response"); 57 string response_body("hello response");
52 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); 58 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance();
53 cache->AddSimpleResponse("www.google.com", "/", 200, "OK", response_body); 59 cache->AddSimpleResponse("www.google.com", "/", 200, "OK", response_body);
54 60
55 BalsaHeaders request_headers; 61 BalsaHeaders request_headers;
56 CreateRequest("www.google.com", "/", &request_headers); 62 CreateRequest("www.google.com", "/", &request_headers);
57 const QuicInMemoryCache::Response* response = 63 const QuicInMemoryCache::Response* response =
58 cache->GetResponse("www.google.com", "/"); 64 cache->GetResponse("www.google.com", "/");
59 ASSERT_TRUE(response); 65 ASSERT_TRUE(response);
60 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); 66 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
61 EXPECT_EQ("200 OK", response->headers().find(":status")->second); 67 EXPECT_EQ("200 OK", response->headers().find(":status")->second);
62 EXPECT_EQ(response_body.size(), response->body().length()); 68 EXPECT_EQ(response_body.size(), response->body().length());
63 } 69 }
64 70
65 TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) { 71 TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) {
72 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory());
66 const QuicInMemoryCache::Response* response = 73 const QuicInMemoryCache::Response* response =
67 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url", 74 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url",
68 "/index.html"); 75 "/index.html");
69 ASSERT_TRUE(response); 76 ASSERT_TRUE(response);
70 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); 77 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
71 EXPECT_EQ("200 OK", response->headers().find(":status")->second); 78 EXPECT_EQ("200 OK", response->headers().find(":status")->second);
72 ASSERT_TRUE(ContainsKey(response->headers(), "connection")); 79 ASSERT_TRUE(ContainsKey(response->headers(), "connection"));
73 EXPECT_EQ("close", response->headers().find("connection")->second); 80 EXPECT_EQ("close", response->headers().find("connection")->second);
74 EXPECT_LT(0U, response->body().length()); 81 EXPECT_LT(0U, response->body().length());
75 } 82 }
76 83
77 TEST_F(QuicInMemoryCacheTest, UsesOriginalUrl) { 84 TEST_F(QuicInMemoryCacheTest, UsesOriginalUrl) {
85 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory());
78 const QuicInMemoryCache::Response* response = 86 const QuicInMemoryCache::Response* response =
79 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url", 87 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url",
80 "/index.html"); 88 "/index.html");
81 ASSERT_TRUE(response); 89 ASSERT_TRUE(response);
82 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); 90 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
83 EXPECT_EQ("200 OK", response->headers().find(":status")->second); 91 EXPECT_EQ("200 OK", response->headers().find(":status")->second);
84 ASSERT_TRUE(ContainsKey(response->headers(), "connection")); 92 ASSERT_TRUE(ContainsKey(response->headers(), "connection"));
85 EXPECT_EQ("close", response->headers().find("connection")->second); 93 EXPECT_EQ("close", response->headers().find("connection")->second);
86 EXPECT_LT(0U, response->body().length()); 94 EXPECT_LT(0U, response->body().length());
87 } 95 }
88 96
89 TEST_F(QuicInMemoryCacheTest, GetResponseNoMatch) {
90 const QuicInMemoryCache::Response* response =
91 QuicInMemoryCache::GetInstance()->GetResponse("mail.google.com",
92 "/index.html");
93 ASSERT_FALSE(response);
94 }
95
96 } // namespace test 97 } // namespace test
97 } // namespace tools 98 } // namespace tools
98 } // namespace net 99 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698