diff options
author | honghaiz@chromium.org <honghaiz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-05 17:43:22 +0000 |
---|---|---|
committer | honghaiz@chromium.org <honghaiz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-05 17:43:22 +0000 |
commit | 743e776594f0e25adef25ff727cd1fc2e474844e (patch) | |
tree | 1a03afd788e1acb0ec1742105dbcc1ce8b730bb8 /net/tools | |
parent | 05140d67e678a57c0d84bf5e989e81570c86cc5c (diff) | |
download | chromium_src-743e776594f0e25adef25ff727cd1fc2e474844e.zip chromium_src-743e776594f0e25adef25ff727cd1fc2e474844e.tar.gz chromium_src-743e776594f0e25adef25ff727cd1fc2e474844e.tar.bz2 |
Add a quic_in_memory_cache_test data file.
Review URL: https://chromiumcodereview.appspot.com/18655004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@210314 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/tools')
-rw-r--r-- | net/tools/quic/quic_in_memory_cache.cc | 28 | ||||
-rw-r--r-- | net/tools/quic/quic_in_memory_cache.h | 1 | ||||
-rw-r--r-- | net/tools/quic/quic_in_memory_cache_test.cc | 68 |
3 files changed, 81 insertions, 16 deletions
diff --git a/net/tools/quic/quic_in_memory_cache.cc b/net/tools/quic/quic_in_memory_cache.cc index 5c02ca6..bc277c5 100644 --- a/net/tools/quic/quic_in_memory_cache.cc +++ b/net/tools/quic/quic_in_memory_cache.cc @@ -86,16 +86,7 @@ QuicInMemoryCache* QuicInMemoryCache::GetInstance() { const QuicInMemoryCache::Response* QuicInMemoryCache::GetResponse( const BalsaHeaders& request_headers) const { - string key = GetKey(request_headers); - StringPiece url(key); - // Removing the leading https:// or http://. - if (StringPieceUtils::StartsWithIgnoreCase(url, "https://")) { - url.remove_prefix(8); - } else if (StringPieceUtils::StartsWithIgnoreCase(url, "http://")) { - url.remove_prefix(7); - } - - ResponseMap::const_iterator it = responses_.find(url.as_string()); + ResponseMap::const_iterator it = responses_.find(GetKey(request_headers)); if (it == responses_.end()) { return NULL; } @@ -117,9 +108,14 @@ void QuicInMemoryCache::AddResponse(const BalsaHeaders& request_headers, void QuicInMemoryCache::ResetForTests() { STLDeleteValues(&responses_); + Initialize(); } QuicInMemoryCache::QuicInMemoryCache() { + Initialize(); +} + +void QuicInMemoryCache::Initialize() { // If there's no defined cache dir, we have no initialization to do. if (FLAGS_quic_in_memory_cache_dir.empty()) { LOG(WARNING) << "No cache directory found. Skipping initialization."; @@ -207,8 +203,16 @@ QuicInMemoryCache::~QuicInMemoryCache() { } string QuicInMemoryCache::GetKey(const BalsaHeaders& request_headers) const { - return request_headers.GetHeader("host").as_string() + - request_headers.request_uri().as_string(); + StringPiece uri = request_headers.request_uri(); + StringPiece host; + if (uri[0] == '/') { + host = request_headers.GetHeader("host"); + } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "https://")) { + uri.remove_prefix(8); + } else if (StringPieceUtils::StartsWithIgnoreCase(uri, "http://")) { + uri.remove_prefix(7); + } + return host.as_string() + uri.as_string(); } } // namespace tools diff --git a/net/tools/quic/quic_in_memory_cache.h b/net/tools/quic/quic_in_memory_cache.h index 600bb19..6322e2d 100644 --- a/net/tools/quic/quic_in_memory_cache.h +++ b/net/tools/quic/quic_in_memory_cache.h @@ -73,6 +73,7 @@ class QuicInMemoryCache { friend struct DefaultSingletonTraits<QuicInMemoryCache>; ~QuicInMemoryCache(); + void Initialize(); std::string GetKey(const BalsaHeaders& response_headers) const; // Cached responses. diff --git a/net/tools/quic/quic_in_memory_cache_test.cc b/net/tools/quic/quic_in_memory_cache_test.cc index 438f24c..065ecc3 100644 --- a/net/tools/quic/quic_in_memory_cache_test.cc +++ b/net/tools/quic/quic_in_memory_cache_test.cc @@ -2,18 +2,23 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "net/tools/quic/quic_in_memory_cache.h" +#include <string> #include "base/files/file_path.h" #include "base/memory/singleton.h" #include "base/path_service.h" +#include "base/strings/string_number_conversions.h" +#include "base/strings/string_piece.h" #include "net/tools/flip_server/balsa_headers.h" +#include "net/tools/quic/quic_in_memory_cache.h" #include "testing/gtest/include/gtest/gtest.h" +using base::IntToString; +using base::StringPiece; + namespace net { namespace tools { namespace test { -namespace { class QuicInMemoryCacheTest : public ::testing::Test { protected: @@ -32,8 +37,64 @@ class QuicInMemoryCacheTest : public ::testing::Test { headers->SetRequestFirstlineFromStringPieces("GET", path, "HTTP/1.1"); headers->ReplaceOrAppendHeader("host", host); } + + virtual void SetUp() { + QuicInMemoryCache::GetInstance()->ResetForTests(); + } + + // This method was copied from end_to_end_test.cc in this directory. + void AddToCache(const StringPiece& method, + const StringPiece& path, + const StringPiece& version, + const StringPiece& response_code, + const StringPiece& response_detail, + const StringPiece& body) { + BalsaHeaders request_headers, response_headers; + request_headers.SetRequestFirstlineFromStringPieces(method, + path, + version); + response_headers.SetRequestFirstlineFromStringPieces(version, + response_code, + response_detail); + response_headers.AppendHeader("content-length", + base::IntToString(body.length())); + + // Check if response already exists and matches. + QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); + const QuicInMemoryCache::Response* cached_response = + cache->GetResponse(request_headers); + if (cached_response != NULL) { + std::string cached_response_headers_str, response_headers_str; + cached_response->headers().DumpToString(&cached_response_headers_str); + response_headers.DumpToString(&response_headers_str); + CHECK_EQ(cached_response_headers_str, response_headers_str); + CHECK_EQ(cached_response->body(), body); + return; + } + cache->AddResponse(request_headers, response_headers, body); + } }; +TEST_F(QuicInMemoryCacheTest, AddResponseGetResponse) { + std::string response_body("hello response"); + AddToCache("GET", "https://www.google.com/bar", + "HTTP/1.1", "200", "OK", response_body); + net::BalsaHeaders request_headers; + CreateRequest("www.google.com", "/bar", &request_headers); + QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); + const QuicInMemoryCache::Response* response = + cache->GetResponse(request_headers); + ASSERT_TRUE(response); + EXPECT_EQ("200", response->headers().response_code()); + EXPECT_EQ(response_body.size(), response->body().length()); + + CreateRequest("", "https://www.google.com/bar", &request_headers); + response = cache->GetResponse(request_headers); + ASSERT_TRUE(response); + EXPECT_EQ("200", response->headers().response_code()); + EXPECT_EQ(response_body.size(), response->body().length()); +} + TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) { net::BalsaHeaders request_headers; CreateRequest("quic.test.url", "/index.html", &request_headers); @@ -50,7 +111,7 @@ TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) { TEST_F(QuicInMemoryCacheTest, ReadsCacheDirHttp) { net::BalsaHeaders request_headers; - CreateRequest("http://quic.test.url", "/index.html", &request_headers); + CreateRequest("", "http://quic.test.url/index.html", &request_headers); const QuicInMemoryCache::Response* response = QuicInMemoryCache::GetInstance()->GetResponse(request_headers); @@ -71,7 +132,6 @@ TEST_F(QuicInMemoryCacheTest, GetResponseNoMatch) { ASSERT_FALSE(response); } -} // namespace } // namespace test } // namespace tools } // namespace net |