Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / tools / quic / quic_in_memory_cache_test.cc
blob534edfff27e15fadfd90441af4c6770f5e4c2996
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/stl_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_piece.h"
13 #include "net/spdy/spdy_framer.h"
14 #include "net/tools/balsa/balsa_headers.h"
15 #include "net/tools/quic/quic_in_memory_cache.h"
16 #include "net/tools/quic/test_tools/quic_in_memory_cache_peer.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using base::IntToString;
20 using base::StringPiece;
21 using std::string;
23 namespace net {
24 namespace tools {
25 namespace test {
27 class QuicInMemoryCacheTest : public ::testing::Test {
28 protected:
29 QuicInMemoryCacheTest() {
30 QuicInMemoryCachePeer::ResetForTests();
33 ~QuicInMemoryCacheTest() override { QuicInMemoryCachePeer::ResetForTests(); }
35 void CreateRequest(string host, string path, BalsaHeaders* headers) {
36 headers->SetRequestFirstlineFromStringPieces("GET", path, "HTTP/1.1");
37 headers->ReplaceOrAppendHeader("host", host);
40 string CacheDirectory() {
41 base::FilePath path;
42 PathService::Get(base::DIR_SOURCE_ROOT, &path);
43 path = path.AppendASCII("net").AppendASCII("data")
44 .AppendASCII("quic_in_memory_cache_data");
45 // The file path is known to be an ascii string.
46 return path.MaybeAsASCII();
50 TEST_F(QuicInMemoryCacheTest, GetResponseNoMatch) {
51 const QuicInMemoryCache::Response* response =
52 QuicInMemoryCache::GetInstance()->GetResponse("mail.google.com",
53 "/index.html");
54 ASSERT_FALSE(response);
57 TEST_F(QuicInMemoryCacheTest, AddSimpleResponseGetResponse) {
58 string response_body("hello response");
59 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance();
60 cache->AddSimpleResponse("www.google.com", "/", 200, "OK", response_body);
62 BalsaHeaders request_headers;
63 CreateRequest("www.google.com", "/", &request_headers);
64 const QuicInMemoryCache::Response* response =
65 cache->GetResponse("www.google.com", "/");
66 ASSERT_TRUE(response);
67 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
68 EXPECT_EQ("200 OK", response->headers().find(":status")->second);
69 EXPECT_EQ(response_body.size(), response->body().length());
72 TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) {
73 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory());
74 const QuicInMemoryCache::Response* response =
75 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url",
76 "/index.html");
77 ASSERT_TRUE(response);
78 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
79 EXPECT_EQ("200 OK", response->headers().find(":status")->second);
80 ASSERT_TRUE(ContainsKey(response->headers(), "connection"));
81 EXPECT_EQ("close", response->headers().find("connection")->second);
82 EXPECT_LT(0U, response->body().length());
85 TEST_F(QuicInMemoryCacheTest, UsesOriginalUrl) {
86 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory());
87 const QuicInMemoryCache::Response* response =
88 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url",
89 "/index.html");
90 ASSERT_TRUE(response);
91 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
92 EXPECT_EQ("200 OK", response->headers().find(":status")->second);
93 ASSERT_TRUE(ContainsKey(response->headers(), "connection"));
94 EXPECT_EQ("close", response->headers().find("connection")->second);
95 EXPECT_LT(0U, response->body().length());
98 TEST_F(QuicInMemoryCacheTest, DefaultResponse) {
99 // Verify GetResponse returns nullptr when no default is set.
100 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance();
101 const QuicInMemoryCache::Response* response =
102 cache->GetResponse("www.google.com", "/");
103 ASSERT_FALSE(response);
105 // Add a default response.
106 SpdyHeaderBlock response_headers;
107 response_headers[":version"] = "HTTP/1.1";
108 response_headers[":status"] = "200 OK";
109 response_headers["content-length"] = "0";
110 QuicInMemoryCache::Response* default_response =
111 new QuicInMemoryCache::Response;
112 default_response->set_headers(response_headers);
113 cache->AddDefaultResponse(default_response);
115 // Now we should get the default response for the original request.
116 response = cache->GetResponse("www.google.com", "/");
117 ASSERT_TRUE(response);
118 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
119 EXPECT_EQ("200 OK", response->headers().find(":status")->second);
121 // Now add a set response for / and make sure it is returned
122 cache->AddSimpleResponse("www.google.com", "/", 302, "foo", "");
123 response = cache->GetResponse("www.google.com", "/");
124 ASSERT_TRUE(response);
125 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
126 EXPECT_EQ("302 foo", response->headers().find(":status")->second);
128 // We should get the default response for other requests.
129 response = cache->GetResponse("www.google.com", "/asd");
130 ASSERT_TRUE(response);
131 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
132 EXPECT_EQ("200 OK", response->headers().find(":status")->second);
135 } // namespace test
136 } // namespace tools
137 } // namespace net