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