| OLD | NEW |
| 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" | |
| 12 #include "base/strings/string_piece.h" | |
| 13 #include "net/quic/platform/api/quic_str_cat.h" | 11 #include "net/quic/platform/api/quic_str_cat.h" |
| 12 #include "net/quic/platform/api/quic_text_utils.h" |
| 14 #include "net/spdy/spdy_framer.h" | 13 #include "net/spdy/spdy_framer.h" |
| 15 #include "net/tools/quic/quic_http_response_cache.h" | 14 #include "net/tools/quic/quic_http_response_cache.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 16 |
| 18 using base::ContainsKey; | 17 using base::ContainsKey; |
| 19 using base::IntToString; | |
| 20 using base::StringPiece; | 18 using base::StringPiece; |
| 21 using net::SpdyHeaderBlock; | 19 using net::SpdyHeaderBlock; |
| 22 using std::string; | 20 using std::string; |
| 23 | 21 |
| 24 namespace net { | 22 namespace net { |
| 25 namespace test { | 23 namespace test { |
| 26 | 24 |
| 27 namespace { | 25 namespace { |
| 28 typedef QuicHttpResponseCache::Response Response; | 26 typedef QuicHttpResponseCache::Response Response; |
| 29 typedef QuicHttpResponseCache::ServerPushInfo ServerPushInfo; | 27 typedef QuicHttpResponseCache::ServerPushInfo ServerPushInfo; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 } | 69 } |
| 72 | 70 |
| 73 TEST_F(QuicHttpResponseCacheTest, AddResponse) { | 71 TEST_F(QuicHttpResponseCacheTest, AddResponse) { |
| 74 const string kRequestHost = "www.foo.com"; | 72 const string kRequestHost = "www.foo.com"; |
| 75 const string kRequestPath = "/"; | 73 const string kRequestPath = "/"; |
| 76 const string kResponseBody("hello response"); | 74 const string kResponseBody("hello response"); |
| 77 | 75 |
| 78 SpdyHeaderBlock response_headers; | 76 SpdyHeaderBlock response_headers; |
| 79 response_headers[":version"] = "HTTP/1.1"; | 77 response_headers[":version"] = "HTTP/1.1"; |
| 80 response_headers[":status"] = "200"; | 78 response_headers[":status"] = "200"; |
| 81 response_headers["content-length"] = IntToString(kResponseBody.size()); | 79 response_headers["content-length"] = |
| 80 QuicTextUtils::Uint64ToString(kResponseBody.size()); |
| 82 | 81 |
| 83 SpdyHeaderBlock response_trailers; | 82 SpdyHeaderBlock response_trailers; |
| 84 response_trailers["key-1"] = "value-1"; | 83 response_trailers["key-1"] = "value-1"; |
| 85 response_trailers["key-2"] = "value-2"; | 84 response_trailers["key-2"] = "value-2"; |
| 86 response_trailers["key-3"] = "value-3"; | 85 response_trailers["key-3"] = "value-3"; |
| 87 | 86 |
| 88 cache_.AddResponse(kRequestHost, "/", response_headers.Clone(), kResponseBody, | 87 cache_.AddResponse(kRequestHost, "/", response_headers.Clone(), kResponseBody, |
| 89 response_trailers.Clone()); | 88 response_trailers.Clone()); |
| 90 | 89 |
| 91 const QuicHttpResponseCache::Response* response = | 90 const QuicHttpResponseCache::Response* response = |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 } | 169 } |
| 171 | 170 |
| 172 TEST_F(QuicHttpResponseCacheTest, AddSimpleResponseWithServerPushResources) { | 171 TEST_F(QuicHttpResponseCacheTest, AddSimpleResponseWithServerPushResources) { |
| 173 string request_host = "www.foo.com"; | 172 string request_host = "www.foo.com"; |
| 174 string response_body("hello response"); | 173 string response_body("hello response"); |
| 175 const size_t kNumResources = 5; | 174 const size_t kNumResources = 5; |
| 176 int NumResources = 5; | 175 int NumResources = 5; |
| 177 std::list<QuicHttpResponseCache::ServerPushInfo> push_resources; | 176 std::list<QuicHttpResponseCache::ServerPushInfo> push_resources; |
| 178 string scheme = "http"; | 177 string scheme = "http"; |
| 179 for (int i = 0; i < NumResources; ++i) { | 178 for (int i = 0; i < NumResources; ++i) { |
| 180 string path = "/server_push_src" + base::IntToString(i); | 179 string path = "/server_push_src" + QuicTextUtils::Uint64ToString(i); |
| 181 string url = scheme + "://" + request_host + path; | 180 string url = scheme + "://" + request_host + path; |
| 182 GURL resource_url(url); | 181 GURL resource_url(url); |
| 183 string body = QuicStrCat("This is server push response body for ", path); | 182 string body = QuicStrCat("This is server push response body for ", path); |
| 184 SpdyHeaderBlock response_headers; | 183 SpdyHeaderBlock response_headers; |
| 185 response_headers[":version"] = "HTTP/1.1"; | 184 response_headers[":version"] = "HTTP/1.1"; |
| 186 response_headers[":status"] = "200"; | 185 response_headers[":status"] = "200"; |
| 187 response_headers["content-length"] = base::UintToString(body.size()); | 186 response_headers["content-length"] = |
| 187 QuicTextUtils::Uint64ToString(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 std::list<ServerPushInfo> resources = | 195 std::list<ServerPushInfo> resources = |
| 196 cache_.GetServerPushResources(request_url); | 196 cache_.GetServerPushResources(request_url); |
| 197 ASSERT_EQ(kNumResources, resources.size()); | 197 ASSERT_EQ(kNumResources, resources.size()); |
| 198 for (const auto& push_resource : push_resources) { | 198 for (const auto& push_resource : push_resources) { |
| 199 ServerPushInfo resource = resources.front(); | 199 ServerPushInfo resource = resources.front(); |
| 200 EXPECT_EQ(resource.request_url.spec(), push_resource.request_url.spec()); | 200 EXPECT_EQ(resource.request_url.spec(), push_resource.request_url.spec()); |
| 201 EXPECT_EQ(resource.priority, push_resource.priority); | 201 EXPECT_EQ(resource.priority, push_resource.priority); |
| 202 resources.pop_front(); | 202 resources.pop_front(); |
| 203 } | 203 } |
| 204 } | 204 } |
| 205 | 205 |
| 206 TEST_F(QuicHttpResponseCacheTest, GetServerPushResourcesAndPushResponses) { | 206 TEST_F(QuicHttpResponseCacheTest, GetServerPushResourcesAndPushResponses) { |
| 207 string request_host = "www.foo.com"; | 207 string request_host = "www.foo.com"; |
| 208 string response_body("hello response"); | 208 string response_body("hello response"); |
| 209 const size_t kNumResources = 4; | 209 const size_t kNumResources = 4; |
| 210 int NumResources = 4; | 210 int NumResources = 4; |
| 211 string scheme = "http"; | 211 string scheme = "http"; |
| 212 string push_response_status[kNumResources] = {"200", "200", "301", "404"}; | 212 string push_response_status[kNumResources] = {"200", "200", "301", "404"}; |
| 213 std::list<QuicHttpResponseCache::ServerPushInfo> push_resources; | 213 std::list<QuicHttpResponseCache::ServerPushInfo> push_resources; |
| 214 for (int i = 0; i < NumResources; ++i) { | 214 for (int i = 0; i < NumResources; ++i) { |
| 215 string path = "/server_push_src" + base::IntToString(i); | 215 string path = "/server_push_src" + QuicTextUtils::Uint64ToString(i); |
| 216 string url = scheme + "://" + request_host + path; | 216 string url = scheme + "://" + request_host + path; |
| 217 GURL resource_url(url); | 217 GURL resource_url(url); |
| 218 string body = "This is server push response body for " + path; | 218 string body = "This is server push response body for " + path; |
| 219 SpdyHeaderBlock response_headers; | 219 SpdyHeaderBlock response_headers; |
| 220 response_headers[":version"] = "HTTP/1.1"; | 220 response_headers[":version"] = "HTTP/1.1"; |
| 221 response_headers[":status"] = push_response_status[i]; | 221 response_headers[":status"] = push_response_status[i]; |
| 222 response_headers["content-length"] = base::UintToString(body.size()); | 222 response_headers["content-length"] = |
| 223 QuicTextUtils::Uint64ToString(body.size()); |
| 223 push_resources.push_back( | 224 push_resources.push_back( |
| 224 ServerPushInfo(resource_url, response_headers.Clone(), i, body)); | 225 ServerPushInfo(resource_url, response_headers.Clone(), i, body)); |
| 225 } | 226 } |
| 226 cache_.AddSimpleResponseWithServerPushResources( | 227 cache_.AddSimpleResponseWithServerPushResources( |
| 227 request_host, "/", 200, response_body, push_resources); | 228 request_host, "/", 200, response_body, push_resources); |
| 228 string request_url = request_host + "/"; | 229 string request_url = request_host + "/"; |
| 229 std::list<ServerPushInfo> resources = | 230 std::list<ServerPushInfo> resources = |
| 230 cache_.GetServerPushResources(request_url); | 231 cache_.GetServerPushResources(request_url); |
| 231 ASSERT_EQ(kNumResources, resources.size()); | 232 ASSERT_EQ(kNumResources, resources.size()); |
| 232 int i = 0; | 233 int i = 0; |
| 233 for (const auto& push_resource : push_resources) { | 234 for (const auto& push_resource : push_resources) { |
| 234 GURL url = resources.front().request_url; | 235 GURL url = resources.front().request_url; |
| 235 string host = url.host(); | 236 string host = url.host(); |
| 236 string path = url.path(); | 237 string path = url.path(); |
| 237 const QuicHttpResponseCache::Response* response = | 238 const QuicHttpResponseCache::Response* response = |
| 238 cache_.GetResponse(host, path); | 239 cache_.GetResponse(host, path); |
| 239 ASSERT_TRUE(response); | 240 ASSERT_TRUE(response); |
| 240 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 241 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); |
| 241 EXPECT_EQ(push_response_status[i++], | 242 EXPECT_EQ(push_response_status[i++], |
| 242 response->headers().find(":status")->second); | 243 response->headers().find(":status")->second); |
| 243 EXPECT_EQ(push_resource.body, response->body()); | 244 EXPECT_EQ(push_resource.body, response->body()); |
| 244 resources.pop_front(); | 245 resources.pop_front(); |
| 245 } | 246 } |
| 246 } | 247 } |
| 247 | 248 |
| 248 } // namespace test | 249 } // namespace test |
| 249 } // namespace net | 250 } // namespace net |
| OLD | NEW |