| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/memory/singleton.h" | |
| 9 #include "base/path_service.h" | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/strings/string_piece.h" | |
| 12 #include "net/tools/balsa/balsa_headers.h" | |
| 13 #include "net/tools/quic/quic_in_memory_cache.h" | |
| 14 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 using base::IntToString; | |
| 18 using base::StringPiece; | |
| 19 | |
| 20 namespace net { | |
| 21 namespace tools { | |
| 22 namespace test { | |
| 23 | |
| 24 class QuicInMemoryCacheTest : public ::testing::Test { | |
| 25 protected: | |
| 26 QuicInMemoryCacheTest() { | |
| 27 base::FilePath path; | |
| 28 PathService::Get(base::DIR_SOURCE_ROOT, &path); | |
| 29 path = path.AppendASCII("net").AppendASCII("data") | |
| 30 .AppendASCII("quic_in_memory_cache_data"); | |
| 31 // The file path is known to be an ascii string. | |
| 32 FLAGS_quic_in_memory_cache_dir = path.MaybeAsASCII(); | |
| 33 } | |
| 34 | |
| 35 void CreateRequest(std::string host, | |
| 36 std::string path, | |
| 37 net::BalsaHeaders* headers) { | |
| 38 headers->SetRequestFirstlineFromStringPieces("GET", path, "HTTP/1.1"); | |
| 39 headers->ReplaceOrAppendHeader("host", host); | |
| 40 } | |
| 41 | |
| 42 void SetUp() override { QuicInMemoryCachePeer::ResetForTests(); } | |
| 43 | |
| 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 }; | |
| 76 | |
| 77 TEST_F(QuicInMemoryCacheTest, AddResponseGetResponse) { | |
| 78 std::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(); | |
| 84 const QuicInMemoryCache::Response* response = | |
| 85 cache->GetResponse(request_headers); | |
| 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); | |
| 93 EXPECT_EQ("200", response->headers().response_code()); | |
| 94 EXPECT_EQ(response_body.size(), response->body().length()); | |
| 95 } | |
| 96 | |
| 97 TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) { | |
| 98 net::BalsaHeaders request_headers; | |
| 99 CreateRequest("quic.test.url", "/index.html", &request_headers); | |
| 100 | |
| 101 const QuicInMemoryCache::Response* response = | |
| 102 QuicInMemoryCache::GetInstance()->GetResponse(request_headers); | |
| 103 ASSERT_TRUE(response); | |
| 104 std::string value; | |
| 105 response->headers().GetAllOfHeaderAsString("Connection", &value); | |
| 106 EXPECT_EQ("200", response->headers().response_code()); | |
| 107 EXPECT_EQ("Keep-Alive", value); | |
| 108 EXPECT_LT(0U, response->body().length()); | |
| 109 } | |
| 110 | |
| 111 TEST_F(QuicInMemoryCacheTest, ReadsCacheDirHttp) { | |
| 112 net::BalsaHeaders request_headers; | |
| 113 CreateRequest("", "http://quic.test.url/index.html", &request_headers); | |
| 114 | |
| 115 const QuicInMemoryCache::Response* response = | |
| 116 QuicInMemoryCache::GetInstance()->GetResponse(request_headers); | |
| 117 ASSERT_TRUE(response); | |
| 118 std::string value; | |
| 119 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()); | |
| 123 } | |
| 124 | |
| 125 TEST_F(QuicInMemoryCacheTest, GetResponseNoMatch) { | |
| 126 net::BalsaHeaders request_headers; | |
| 127 CreateRequest("www.google.com", "/index.html", &request_headers); | |
| 128 | |
| 129 const QuicInMemoryCache::Response* response = | |
| 130 QuicInMemoryCache::GetInstance()->GetResponse(request_headers); | |
| 131 ASSERT_FALSE(response); | |
| 132 } | |
| 133 | |
| 134 } // namespace test | |
| 135 } // namespace tools | |
| 136 } // namespace net | |
| OLD | NEW |