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/strings/string_number_conversions.h"
11 #include "base/strings/string_piece.h"
12 #include "net/tools/flip_server/balsa_headers.h"
13 #include "net/tools/quic/quic_in_memory_cache.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using base::IntToString
;
17 using base::StringPiece
;
23 class QuicInMemoryCacheTest
: public ::testing::Test
{
25 QuicInMemoryCacheTest() {
27 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
28 path
= path
.AppendASCII("net").AppendASCII("data")
29 .AppendASCII("quic_in_memory_cache_data");
30 // The file path is known to be an ascii string.
31 FLAGS_quic_in_memory_cache_dir
= path
.MaybeAsASCII();
34 void CreateRequest(std::string host
,
36 net::BalsaHeaders
* headers
) {
37 headers
->SetRequestFirstlineFromStringPieces("GET", path
, "HTTP/1.1");
38 headers
->ReplaceOrAppendHeader("host", host
);
41 virtual void SetUp() {
42 QuicInMemoryCache::GetInstance()->ResetForTests();
45 // This method was copied from end_to_end_test.cc in this directory.
46 void AddToCache(const StringPiece
& method
,
47 const StringPiece
& path
,
48 const StringPiece
& version
,
49 const StringPiece
& response_code
,
50 const StringPiece
& response_detail
,
51 const StringPiece
& body
) {
52 BalsaHeaders request_headers
, response_headers
;
53 request_headers
.SetRequestFirstlineFromStringPieces(method
,
56 response_headers
.SetRequestFirstlineFromStringPieces(version
,
59 response_headers
.AppendHeader("content-length",
60 base::IntToString(body
.length()));
62 // Check if response already exists and matches.
63 QuicInMemoryCache
* cache
= QuicInMemoryCache::GetInstance();
64 const QuicInMemoryCache::Response
* cached_response
=
65 cache
->GetResponse(request_headers
);
66 if (cached_response
!= NULL
) {
67 std::string cached_response_headers_str
, response_headers_str
;
68 cached_response
->headers().DumpToString(&cached_response_headers_str
);
69 response_headers
.DumpToString(&response_headers_str
);
70 CHECK_EQ(cached_response_headers_str
, response_headers_str
);
71 CHECK_EQ(cached_response
->body(), body
);
74 cache
->AddResponse(request_headers
, response_headers
, body
);
78 TEST_F(QuicInMemoryCacheTest
, AddResponseGetResponse
) {
79 std::string
response_body("hello response");
80 AddToCache("GET", "https://www.google.com/bar",
81 "HTTP/1.1", "200", "OK", response_body
);
82 net::BalsaHeaders request_headers
;
83 CreateRequest("www.google.com", "/bar", &request_headers
);
84 QuicInMemoryCache
* cache
= QuicInMemoryCache::GetInstance();
85 const QuicInMemoryCache::Response
* response
=
86 cache
->GetResponse(request_headers
);
87 ASSERT_TRUE(response
);
88 EXPECT_EQ("200", response
->headers().response_code());
89 EXPECT_EQ(response_body
.size(), response
->body().length());
91 CreateRequest("", "https://www.google.com/bar", &request_headers
);
92 response
= cache
->GetResponse(request_headers
);
93 ASSERT_TRUE(response
);
94 EXPECT_EQ("200", response
->headers().response_code());
95 EXPECT_EQ(response_body
.size(), response
->body().length());
98 TEST_F(QuicInMemoryCacheTest
, ReadsCacheDir
) {
99 net::BalsaHeaders request_headers
;
100 CreateRequest("quic.test.url", "/index.html", &request_headers
);
102 const QuicInMemoryCache::Response
* response
=
103 QuicInMemoryCache::GetInstance()->GetResponse(request_headers
);
104 ASSERT_TRUE(response
);
106 response
->headers().GetAllOfHeaderAsString("Connection", &value
);
107 EXPECT_EQ("200", response
->headers().response_code());
108 EXPECT_EQ("Keep-Alive", value
);
109 EXPECT_LT(0U, response
->body().length());
112 TEST_F(QuicInMemoryCacheTest
, ReadsCacheDirHttp
) {
113 net::BalsaHeaders request_headers
;
114 CreateRequest("", "http://quic.test.url/index.html", &request_headers
);
116 const QuicInMemoryCache::Response
* response
=
117 QuicInMemoryCache::GetInstance()->GetResponse(request_headers
);
118 ASSERT_TRUE(response
);
120 response
->headers().GetAllOfHeaderAsString("Connection", &value
);
121 EXPECT_EQ("200", response
->headers().response_code());
122 EXPECT_EQ("Keep-Alive", value
);
123 EXPECT_LT(0U, response
->body().length());
126 TEST_F(QuicInMemoryCacheTest
, GetResponseNoMatch
) {
127 net::BalsaHeaders request_headers
;
128 CreateRequest("www.google.com", "/index.html", &request_headers
);
130 const QuicInMemoryCache::Response
* response
=
131 QuicInMemoryCache::GetInstance()->GetResponse(request_headers
);
132 ASSERT_FALSE(response
);