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.
7 #include "base/files/file_path.h"
8 #include "base/memory/singleton.h"
9 #include "base/path_service.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_piece.h"
13 #include "net/tools/balsa/balsa_headers.h"
14 #include "net/tools/quic/quic_in_memory_cache.h"
15 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using base::IntToString
;
19 using base::StringPiece
;
26 class QuicInMemoryCacheTest
: public ::testing::Test
{
28 QuicInMemoryCacheTest() {
29 QuicInMemoryCachePeer::ResetForTests();
32 ~QuicInMemoryCacheTest() override
{ QuicInMemoryCachePeer::ResetForTests(); }
34 void CreateRequest(string host
, string path
, BalsaHeaders
* headers
) {
35 headers
->SetRequestFirstlineFromStringPieces("GET", path
, "HTTP/1.1");
36 headers
->ReplaceOrAppendHeader("host", host
);
39 string
CacheDirectory() {
41 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
42 path
= path
.AppendASCII("net").AppendASCII("data")
43 .AppendASCII("quic_in_memory_cache_data");
44 // The file path is known to be an ascii string.
45 return path
.MaybeAsASCII();
49 TEST_F(QuicInMemoryCacheTest
, GetResponseNoMatch
) {
50 const QuicInMemoryCache::Response
* response
=
51 QuicInMemoryCache::GetInstance()->GetResponse("mail.google.com",
53 ASSERT_FALSE(response
);
56 TEST_F(QuicInMemoryCacheTest
, AddSimpleResponseGetResponse
) {
57 string
response_body("hello response");
58 QuicInMemoryCache
* cache
= QuicInMemoryCache::GetInstance();
59 cache
->AddSimpleResponse("www.google.com", "/", 200, "OK", response_body
);
61 BalsaHeaders request_headers
;
62 CreateRequest("www.google.com", "/", &request_headers
);
63 const QuicInMemoryCache::Response
* response
=
64 cache
->GetResponse("www.google.com", "/");
65 ASSERT_TRUE(response
);
66 ASSERT_TRUE(ContainsKey(response
->headers(), ":status"));
67 EXPECT_EQ("200 OK", response
->headers().find(":status")->second
);
68 EXPECT_EQ(response_body
.size(), response
->body().length());
71 TEST_F(QuicInMemoryCacheTest
, ReadsCacheDir
) {
72 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory());
73 const QuicInMemoryCache::Response
* response
=
74 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url",
76 ASSERT_TRUE(response
);
77 ASSERT_TRUE(ContainsKey(response
->headers(), ":status"));
78 EXPECT_EQ("200 OK", response
->headers().find(":status")->second
);
79 ASSERT_TRUE(ContainsKey(response
->headers(), "connection"));
80 EXPECT_EQ("close", response
->headers().find("connection")->second
);
81 EXPECT_LT(0U, response
->body().length());
84 TEST_F(QuicInMemoryCacheTest
, UsesOriginalUrl
) {
85 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory());
86 const QuicInMemoryCache::Response
* response
=
87 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url",
89 ASSERT_TRUE(response
);
90 ASSERT_TRUE(ContainsKey(response
->headers(), ":status"));
91 EXPECT_EQ("200 OK", response
->headers().find(":status")->second
);
92 ASSERT_TRUE(ContainsKey(response
->headers(), "connection"));
93 EXPECT_EQ("close", response
->headers().find("connection")->second
);
94 EXPECT_LT(0U, response
->body().length());