Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: net/tools/quic/quic_http_response_cache_test.cc

Issue 2547583002: Landing Recent QUIC changes until Fri Nov 18 23:21:04 2016 +0000 (Closed)
Patch Set: Remove explicit HTTP/2 enum usage Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/tools/quic/quic_http_response_cache.cc ('k') | net/tools/quic/quic_in_memory_cache.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <string> 5 #include <string>
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/memory/singleton.h" 8 #include "base/memory/singleton.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_piece.h" 12 #include "base/strings/string_piece.h"
13 #include "net/spdy/spdy_framer.h" 13 #include "net/spdy/spdy_framer.h"
14 #include "net/tools/quic/quic_in_memory_cache.h" 14 #include "net/tools/quic/quic_http_response_cache.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 using base::ContainsKey; 17 using base::ContainsKey;
18 using base::IntToString; 18 using base::IntToString;
19 using base::StringPiece; 19 using base::StringPiece;
20 using net::SpdyHeaderBlock; 20 using net::SpdyHeaderBlock;
21 using std::list; 21 using std::list;
22 using std::string; 22 using std::string;
23 23
24 namespace net { 24 namespace net {
25 namespace test { 25 namespace test {
26 26
27 namespace { 27 namespace {
28 typedef QuicInMemoryCache::Response Response; 28 typedef QuicHttpResponseCache::Response Response;
29 typedef QuicInMemoryCache::ServerPushInfo ServerPushInfo; 29 typedef QuicHttpResponseCache::ServerPushInfo ServerPushInfo;
30 }; // namespace 30 }; // namespace
31 31
32 class QuicInMemoryCacheTest : public ::testing::Test { 32 class QuicHttpResponseCacheTest : public ::testing::Test {
33 protected: 33 protected:
34 void CreateRequest(string host, string path, SpdyHeaderBlock* headers) { 34 void CreateRequest(string host, string path, SpdyHeaderBlock* headers) {
35 (*headers)[":method"] = "GET"; 35 (*headers)[":method"] = "GET";
36 (*headers)[":path"] = path; 36 (*headers)[":path"] = path;
37 (*headers)[":authority"] = host; 37 (*headers)[":authority"] = host;
38 (*headers)[":scheme"] = "https"; 38 (*headers)[":scheme"] = "https";
39 } 39 }
40 40
41 string CacheDirectory() { 41 string CacheDirectory() {
42 base::FilePath path; 42 base::FilePath path;
43 PathService::Get(base::DIR_SOURCE_ROOT, &path); 43 PathService::Get(base::DIR_SOURCE_ROOT, &path);
44 path = path.AppendASCII("net").AppendASCII("data").AppendASCII( 44 path = path.AppendASCII("net").AppendASCII("data").AppendASCII(
45 "quic_in_memory_cache_data"); 45 "quic_http_response_cache_data");
46 // The file path is known to be an ascii string. 46 // The file path is known to be an ascii string.
47 return path.MaybeAsASCII(); 47 return path.MaybeAsASCII();
48 } 48 }
49 49
50 QuicInMemoryCache cache_; 50 QuicHttpResponseCache cache_;
51 }; 51 };
52 52
53 TEST_F(QuicInMemoryCacheTest, GetResponseNoMatch) { 53 TEST_F(QuicHttpResponseCacheTest, GetResponseNoMatch) {
54 const QuicInMemoryCache::Response* response = 54 const QuicHttpResponseCache::Response* response =
55 cache_.GetResponse("mail.google.com", "/index.html"); 55 cache_.GetResponse("mail.google.com", "/index.html");
56 ASSERT_FALSE(response); 56 ASSERT_FALSE(response);
57 } 57 }
58 58
59 TEST_F(QuicInMemoryCacheTest, AddSimpleResponseGetResponse) { 59 TEST_F(QuicHttpResponseCacheTest, AddSimpleResponseGetResponse) {
60 string response_body("hello response"); 60 string response_body("hello response");
61 cache_.AddSimpleResponse("www.google.com", "/", 200, response_body); 61 cache_.AddSimpleResponse("www.google.com", "/", 200, response_body);
62 62
63 SpdyHeaderBlock request_headers; 63 SpdyHeaderBlock request_headers;
64 CreateRequest("www.google.com", "/", &request_headers); 64 CreateRequest("www.google.com", "/", &request_headers);
65 const QuicInMemoryCache::Response* response = 65 const QuicHttpResponseCache::Response* response =
66 cache_.GetResponse("www.google.com", "/"); 66 cache_.GetResponse("www.google.com", "/");
67 ASSERT_TRUE(response); 67 ASSERT_TRUE(response);
68 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); 68 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
69 EXPECT_EQ("200", response->headers().find(":status")->second); 69 EXPECT_EQ("200", response->headers().find(":status")->second);
70 EXPECT_EQ(response_body.size(), response->body().length()); 70 EXPECT_EQ(response_body.size(), response->body().length());
71 } 71 }
72 72
73 TEST_F(QuicInMemoryCacheTest, AddResponse) { 73 TEST_F(QuicHttpResponseCacheTest, AddResponse) {
74 const string kRequestHost = "www.foo.com"; 74 const string kRequestHost = "www.foo.com";
75 const string kRequestPath = "/"; 75 const string kRequestPath = "/";
76 const string kResponseBody("hello response"); 76 const string kResponseBody("hello response");
77 77
78 SpdyHeaderBlock response_headers; 78 SpdyHeaderBlock response_headers;
79 response_headers[":version"] = "HTTP/1.1"; 79 response_headers[":version"] = "HTTP/1.1";
80 response_headers[":status"] = "200"; 80 response_headers[":status"] = "200";
81 response_headers["content-length"] = IntToString(kResponseBody.size()); 81 response_headers["content-length"] = IntToString(kResponseBody.size());
82 82
83 SpdyHeaderBlock response_trailers; 83 SpdyHeaderBlock response_trailers;
84 response_trailers["key-1"] = "value-1"; 84 response_trailers["key-1"] = "value-1";
85 response_trailers["key-2"] = "value-2"; 85 response_trailers["key-2"] = "value-2";
86 response_trailers["key-3"] = "value-3"; 86 response_trailers["key-3"] = "value-3";
87 87
88 cache_.AddResponse(kRequestHost, "/", response_headers.Clone(), kResponseBody, 88 cache_.AddResponse(kRequestHost, "/", response_headers.Clone(), kResponseBody,
89 response_trailers.Clone()); 89 response_trailers.Clone());
90 90
91 const QuicInMemoryCache::Response* response = 91 const QuicHttpResponseCache::Response* response =
92 cache_.GetResponse(kRequestHost, kRequestPath); 92 cache_.GetResponse(kRequestHost, kRequestPath);
93 EXPECT_EQ(response->headers(), response_headers); 93 EXPECT_EQ(response->headers(), response_headers);
94 EXPECT_EQ(response->body(), kResponseBody); 94 EXPECT_EQ(response->body(), kResponseBody);
95 EXPECT_EQ(response->trailers(), response_trailers); 95 EXPECT_EQ(response->trailers(), response_trailers);
96 } 96 }
97 97
98 TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) { 98 TEST_F(QuicHttpResponseCacheTest, ReadsCacheDir) {
99 cache_.InitializeFromDirectory(CacheDirectory()); 99 cache_.InitializeFromDirectory(CacheDirectory());
100 const QuicInMemoryCache::Response* response = 100 const QuicHttpResponseCache::Response* response =
101 cache_.GetResponse("quic.test.url", "/index.html"); 101 cache_.GetResponse("quic.test.url", "/index.html");
102 ASSERT_TRUE(response); 102 ASSERT_TRUE(response);
103 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); 103 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
104 EXPECT_EQ("200", response->headers().find(":status")->second); 104 EXPECT_EQ("200", response->headers().find(":status")->second);
105 // Connection headers are not valid in HTTP/2. 105 // Connection headers are not valid in HTTP/2.
106 EXPECT_FALSE(ContainsKey(response->headers(), "connection")); 106 EXPECT_FALSE(ContainsKey(response->headers(), "connection"));
107 EXPECT_LT(0U, response->body().length()); 107 EXPECT_LT(0U, response->body().length());
108 } 108 }
109 109
110 TEST_F(QuicInMemoryCacheTest, ReadsCacheDirWithServerPushResource) { 110 TEST_F(QuicHttpResponseCacheTest, ReadsCacheDirWithServerPushResource) {
111 cache_.InitializeFromDirectory(CacheDirectory() + "_with_push"); 111 cache_.InitializeFromDirectory(CacheDirectory() + "_with_push");
112 list<ServerPushInfo> resources = 112 list<ServerPushInfo> resources =
113 cache_.GetServerPushResources("quic.test.url/"); 113 cache_.GetServerPushResources("quic.test.url/");
114 ASSERT_EQ(1UL, resources.size()); 114 ASSERT_EQ(1UL, resources.size());
115 } 115 }
116 116
117 TEST_F(QuicInMemoryCacheTest, ReadsCacheDirWithServerPushResources) { 117 TEST_F(QuicHttpResponseCacheTest, ReadsCacheDirWithServerPushResources) {
118 cache_.InitializeFromDirectory(CacheDirectory() + "_with_push"); 118 cache_.InitializeFromDirectory(CacheDirectory() + "_with_push");
119 list<ServerPushInfo> resources = 119 list<ServerPushInfo> resources =
120 cache_.GetServerPushResources("quic.test.url/index2.html"); 120 cache_.GetServerPushResources("quic.test.url/index2.html");
121 ASSERT_EQ(2UL, resources.size()); 121 ASSERT_EQ(2UL, resources.size());
122 } 122 }
123 123
124 TEST_F(QuicInMemoryCacheTest, UsesOriginalUrl) { 124 TEST_F(QuicHttpResponseCacheTest, UsesOriginalUrl) {
125 cache_.InitializeFromDirectory(CacheDirectory()); 125 cache_.InitializeFromDirectory(CacheDirectory());
126 const QuicInMemoryCache::Response* response = 126 const QuicHttpResponseCache::Response* response =
127 cache_.GetResponse("quic.test.url", "/index.html"); 127 cache_.GetResponse("quic.test.url", "/index.html");
128 ASSERT_TRUE(response); 128 ASSERT_TRUE(response);
129 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); 129 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
130 EXPECT_EQ("200", response->headers().find(":status")->second); 130 EXPECT_EQ("200", response->headers().find(":status")->second);
131 // Connection headers are not valid in HTTP/2. 131 // Connection headers are not valid in HTTP/2.
132 EXPECT_FALSE(ContainsKey(response->headers(), "connection")); 132 EXPECT_FALSE(ContainsKey(response->headers(), "connection"));
133 EXPECT_LT(0U, response->body().length()); 133 EXPECT_LT(0U, response->body().length());
134 } 134 }
135 135
136 TEST_F(QuicInMemoryCacheTest, DefaultResponse) { 136 TEST_F(QuicHttpResponseCacheTest, DefaultResponse) {
137 // Verify GetResponse returns nullptr when no default is set. 137 // Verify GetResponse returns nullptr when no default is set.
138 const QuicInMemoryCache::Response* response = 138 const QuicHttpResponseCache::Response* response =
139 cache_.GetResponse("www.google.com", "/"); 139 cache_.GetResponse("www.google.com", "/");
140 ASSERT_FALSE(response); 140 ASSERT_FALSE(response);
141 141
142 // Add a default response. 142 // Add a default response.
143 SpdyHeaderBlock response_headers; 143 SpdyHeaderBlock response_headers;
144 response_headers[":version"] = "HTTP/1.1"; 144 response_headers[":version"] = "HTTP/1.1";
145 response_headers[":status"] = "200"; 145 response_headers[":status"] = "200";
146 response_headers["content-length"] = "0"; 146 response_headers["content-length"] = "0";
147 QuicInMemoryCache::Response* default_response = 147 QuicHttpResponseCache::Response* default_response =
148 new QuicInMemoryCache::Response; 148 new QuicHttpResponseCache::Response;
149 default_response->set_headers(std::move(response_headers)); 149 default_response->set_headers(std::move(response_headers));
150 cache_.AddDefaultResponse(default_response); 150 cache_.AddDefaultResponse(default_response);
151 151
152 // Now we should get the default response for the original request. 152 // Now we should get the default response for the original request.
153 response = cache_.GetResponse("www.google.com", "/"); 153 response = cache_.GetResponse("www.google.com", "/");
154 ASSERT_TRUE(response); 154 ASSERT_TRUE(response);
155 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); 155 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
156 EXPECT_EQ("200", response->headers().find(":status")->second); 156 EXPECT_EQ("200", response->headers().find(":status")->second);
157 157
158 // Now add a set response for / and make sure it is returned 158 // Now add a set response for / and make sure it is returned
159 cache_.AddSimpleResponse("www.google.com", "/", 302, ""); 159 cache_.AddSimpleResponse("www.google.com", "/", 302, "");
160 response = cache_.GetResponse("www.google.com", "/"); 160 response = cache_.GetResponse("www.google.com", "/");
161 ASSERT_TRUE(response); 161 ASSERT_TRUE(response);
162 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); 162 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
163 EXPECT_EQ("302", response->headers().find(":status")->second); 163 EXPECT_EQ("302", response->headers().find(":status")->second);
164 164
165 // We should get the default response for other requests. 165 // We should get the default response for other requests.
166 response = cache_.GetResponse("www.google.com", "/asd"); 166 response = cache_.GetResponse("www.google.com", "/asd");
167 ASSERT_TRUE(response); 167 ASSERT_TRUE(response);
168 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); 168 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
169 EXPECT_EQ("200", response->headers().find(":status")->second); 169 EXPECT_EQ("200", response->headers().find(":status")->second);
170 } 170 }
171 171
172 TEST_F(QuicInMemoryCacheTest, AddSimpleResponseWithServerPushResources) { 172 TEST_F(QuicHttpResponseCacheTest, AddSimpleResponseWithServerPushResources) {
173 string request_host = "www.foo.com"; 173 string request_host = "www.foo.com";
174 string response_body("hello response"); 174 string response_body("hello response");
175 const size_t kNumResources = 5; 175 const size_t kNumResources = 5;
176 int NumResources = 5; 176 int NumResources = 5;
177 list<QuicInMemoryCache::ServerPushInfo> push_resources; 177 list<QuicHttpResponseCache::ServerPushInfo> push_resources;
178 string scheme = "http"; 178 string scheme = "http";
179 for (int i = 0; i < NumResources; ++i) { 179 for (int i = 0; i < NumResources; ++i) {
180 string path = "/server_push_src" + base::IntToString(i); 180 string path = "/server_push_src" + base::IntToString(i);
181 string url = scheme + "://" + request_host + path; 181 string url = scheme + "://" + request_host + path;
182 GURL resource_url(url); 182 GURL resource_url(url);
183 string body = "This is server push response body for " + path; 183 string body = "This is server push response body for " + path;
184 SpdyHeaderBlock response_headers; 184 SpdyHeaderBlock response_headers;
185 response_headers[":version"] = "HTTP/1.1"; 185 response_headers[":version"] = "HTTP/1.1";
186 response_headers[":status"] = "200"; 186 response_headers[":status"] = "200";
187 response_headers["content-length"] = base::UintToString(body.size()); 187 response_headers["content-length"] = base::UintToString(body.size());
188 push_resources.push_back( 188 push_resources.push_back(
189 ServerPushInfo(resource_url, response_headers.Clone(), i, body)); 189 ServerPushInfo(resource_url, response_headers.Clone(), i, body));
190 } 190 }
191 191
192 cache_.AddSimpleResponseWithServerPushResources( 192 cache_.AddSimpleResponseWithServerPushResources(
193 request_host, "/", 200, response_body, push_resources); 193 request_host, "/", 200, response_body, push_resources);
194 string request_url = request_host + "/"; 194 string request_url = request_host + "/";
195 list<ServerPushInfo> resources = cache_.GetServerPushResources(request_url); 195 list<ServerPushInfo> resources = cache_.GetServerPushResources(request_url);
196 ASSERT_EQ(kNumResources, resources.size()); 196 ASSERT_EQ(kNumResources, resources.size());
197 for (const auto& push_resource : push_resources) { 197 for (const auto& push_resource : push_resources) {
198 ServerPushInfo resource = resources.front(); 198 ServerPushInfo resource = resources.front();
199 EXPECT_EQ(resource.request_url.spec(), push_resource.request_url.spec()); 199 EXPECT_EQ(resource.request_url.spec(), push_resource.request_url.spec());
200 EXPECT_EQ(resource.priority, push_resource.priority); 200 EXPECT_EQ(resource.priority, push_resource.priority);
201 resources.pop_front(); 201 resources.pop_front();
202 } 202 }
203 } 203 }
204 204
205 TEST_F(QuicInMemoryCacheTest, GetServerPushResourcesAndPushResponses) { 205 TEST_F(QuicHttpResponseCacheTest, GetServerPushResourcesAndPushResponses) {
206 string request_host = "www.foo.com"; 206 string request_host = "www.foo.com";
207 string response_body("hello response"); 207 string response_body("hello response");
208 const size_t kNumResources = 4; 208 const size_t kNumResources = 4;
209 int NumResources = 4; 209 int NumResources = 4;
210 string scheme = "http"; 210 string scheme = "http";
211 string push_response_status[kNumResources] = {"200", "200", "301", "404"}; 211 string push_response_status[kNumResources] = {"200", "200", "301", "404"};
212 list<QuicInMemoryCache::ServerPushInfo> push_resources; 212 list<QuicHttpResponseCache::ServerPushInfo> push_resources;
213 for (int i = 0; i < NumResources; ++i) { 213 for (int i = 0; i < NumResources; ++i) {
214 string path = "/server_push_src" + base::IntToString(i); 214 string path = "/server_push_src" + base::IntToString(i);
215 string url = scheme + "://" + request_host + path; 215 string url = scheme + "://" + request_host + path;
216 GURL resource_url(url); 216 GURL resource_url(url);
217 string body = "This is server push response body for " + path; 217 string body = "This is server push response body for " + path;
218 SpdyHeaderBlock response_headers; 218 SpdyHeaderBlock response_headers;
219 response_headers[":version"] = "HTTP/1.1"; 219 response_headers[":version"] = "HTTP/1.1";
220 response_headers[":status"] = push_response_status[i]; 220 response_headers[":status"] = push_response_status[i];
221 response_headers["content-length"] = base::UintToString(body.size()); 221 response_headers["content-length"] = base::UintToString(body.size());
222 push_resources.push_back( 222 push_resources.push_back(
223 ServerPushInfo(resource_url, response_headers.Clone(), i, body)); 223 ServerPushInfo(resource_url, response_headers.Clone(), i, body));
224 } 224 }
225 cache_.AddSimpleResponseWithServerPushResources( 225 cache_.AddSimpleResponseWithServerPushResources(
226 request_host, "/", 200, response_body, push_resources); 226 request_host, "/", 200, response_body, push_resources);
227 string request_url = request_host + "/"; 227 string request_url = request_host + "/";
228 list<ServerPushInfo> resources = cache_.GetServerPushResources(request_url); 228 list<ServerPushInfo> resources = cache_.GetServerPushResources(request_url);
229 ASSERT_EQ(kNumResources, resources.size()); 229 ASSERT_EQ(kNumResources, resources.size());
230 int i = 0; 230 int i = 0;
231 for (const auto& push_resource : push_resources) { 231 for (const auto& push_resource : push_resources) {
232 GURL url = resources.front().request_url; 232 GURL url = resources.front().request_url;
233 string host = url.host(); 233 string host = url.host();
234 string path = url.path(); 234 string path = url.path();
235 const QuicInMemoryCache::Response* response = 235 const QuicHttpResponseCache::Response* response =
236 cache_.GetResponse(host, path); 236 cache_.GetResponse(host, path);
237 ASSERT_TRUE(response); 237 ASSERT_TRUE(response);
238 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); 238 ASSERT_TRUE(ContainsKey(response->headers(), ":status"));
239 EXPECT_EQ(push_response_status[i++], 239 EXPECT_EQ(push_response_status[i++],
240 response->headers().find(":status")->second); 240 response->headers().find(":status")->second);
241 EXPECT_EQ(push_resource.body, response->body()); 241 EXPECT_EQ(push_resource.body, response->body());
242 resources.pop_front(); 242 resources.pop_front();
243 } 243 }
244 } 244 }
245 245
246 } // namespace test 246 } // namespace test
247 } // namespace net 247 } // namespace net
OLDNEW
« no previous file with comments | « net/tools/quic/quic_http_response_cache.cc ('k') | net/tools/quic/quic_in_memory_cache.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698