Windows should animate when they are about to get docked at screen edges.
[chromium-blink-merge.git] / net / tools / quic / quic_in_memory_cache_test.cc
blob065ecc3b4372aa484ce69ee4680a2dba77dfd252
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.
5 #include <string>
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;
19 namespace net {
20 namespace tools {
21 namespace test {
23 class QuicInMemoryCacheTest : public ::testing::Test {
24 protected:
25 QuicInMemoryCacheTest() {
26 base::FilePath path;
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,
35 std::string path,
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,
54 path,
55 version);
56 response_headers.SetRequestFirstlineFromStringPieces(version,
57 response_code,
58 response_detail);
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);
72 return;
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);
105 std::string value;
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);
119 std::string value;
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);
135 } // namespace test
136 } // namespace tools
137 } // namespace net