| 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 "net/quic/platform/api/quic_map_util.h" |
| 11 #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" | 12 #include "net/quic/platform/api/quic_text_utils.h" |
| 13 #include "net/spdy/spdy_framer.h" | 13 #include "net/spdy/spdy_framer.h" |
| 14 #include "net/tools/quic/quic_http_response_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; | |
| 18 using base::StringPiece; | 17 using base::StringPiece; |
| 19 using net::SpdyHeaderBlock; | 18 using net::SpdyHeaderBlock; |
| 20 using std::string; | 19 using std::string; |
| 21 | 20 |
| 22 namespace net { | 21 namespace net { |
| 23 namespace test { | 22 namespace test { |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 typedef QuicHttpResponseCache::Response Response; | 25 typedef QuicHttpResponseCache::Response Response; |
| 27 typedef QuicHttpResponseCache::ServerPushInfo ServerPushInfo; | 26 typedef QuicHttpResponseCache::ServerPushInfo ServerPushInfo; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 56 | 55 |
| 57 TEST_F(QuicHttpResponseCacheTest, AddSimpleResponseGetResponse) { | 56 TEST_F(QuicHttpResponseCacheTest, AddSimpleResponseGetResponse) { |
| 58 string response_body("hello response"); | 57 string response_body("hello response"); |
| 59 cache_.AddSimpleResponse("www.google.com", "/", 200, response_body); | 58 cache_.AddSimpleResponse("www.google.com", "/", 200, response_body); |
| 60 | 59 |
| 61 SpdyHeaderBlock request_headers; | 60 SpdyHeaderBlock request_headers; |
| 62 CreateRequest("www.google.com", "/", &request_headers); | 61 CreateRequest("www.google.com", "/", &request_headers); |
| 63 const QuicHttpResponseCache::Response* response = | 62 const QuicHttpResponseCache::Response* response = |
| 64 cache_.GetResponse("www.google.com", "/"); | 63 cache_.GetResponse("www.google.com", "/"); |
| 65 ASSERT_TRUE(response); | 64 ASSERT_TRUE(response); |
| 66 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 65 ASSERT_TRUE(QuicContainsKey(response->headers(), ":status")); |
| 67 EXPECT_EQ("200", response->headers().find(":status")->second); | 66 EXPECT_EQ("200", response->headers().find(":status")->second); |
| 68 EXPECT_EQ(response_body.size(), response->body().length()); | 67 EXPECT_EQ(response_body.size(), response->body().length()); |
| 69 } | 68 } |
| 70 | 69 |
| 71 TEST_F(QuicHttpResponseCacheTest, AddResponse) { | 70 TEST_F(QuicHttpResponseCacheTest, AddResponse) { |
| 72 const string kRequestHost = "www.foo.com"; | 71 const string kRequestHost = "www.foo.com"; |
| 73 const string kRequestPath = "/"; | 72 const string kRequestPath = "/"; |
| 74 const string kResponseBody("hello response"); | 73 const string kResponseBody("hello response"); |
| 75 | 74 |
| 76 SpdyHeaderBlock response_headers; | 75 SpdyHeaderBlock response_headers; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 92 EXPECT_EQ(response->headers(), response_headers); | 91 EXPECT_EQ(response->headers(), response_headers); |
| 93 EXPECT_EQ(response->body(), kResponseBody); | 92 EXPECT_EQ(response->body(), kResponseBody); |
| 94 EXPECT_EQ(response->trailers(), response_trailers); | 93 EXPECT_EQ(response->trailers(), response_trailers); |
| 95 } | 94 } |
| 96 | 95 |
| 97 TEST_F(QuicHttpResponseCacheTest, ReadsCacheDir) { | 96 TEST_F(QuicHttpResponseCacheTest, ReadsCacheDir) { |
| 98 cache_.InitializeFromDirectory(CacheDirectory()); | 97 cache_.InitializeFromDirectory(CacheDirectory()); |
| 99 const QuicHttpResponseCache::Response* response = | 98 const QuicHttpResponseCache::Response* response = |
| 100 cache_.GetResponse("www.example.com", "/index.html"); | 99 cache_.GetResponse("www.example.com", "/index.html"); |
| 101 ASSERT_TRUE(response); | 100 ASSERT_TRUE(response); |
| 102 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 101 ASSERT_TRUE(QuicContainsKey(response->headers(), ":status")); |
| 103 EXPECT_EQ("200", response->headers().find(":status")->second); | 102 EXPECT_EQ("200", response->headers().find(":status")->second); |
| 104 // Connection headers are not valid in HTTP/2. | 103 // Connection headers are not valid in HTTP/2. |
| 105 EXPECT_FALSE(ContainsKey(response->headers(), "connection")); | 104 EXPECT_FALSE(QuicContainsKey(response->headers(), "connection")); |
| 106 EXPECT_LT(0U, response->body().length()); | 105 EXPECT_LT(0U, response->body().length()); |
| 107 } | 106 } |
| 108 | 107 |
| 109 TEST_F(QuicHttpResponseCacheTest, ReadsCacheDirWithServerPushResource) { | 108 TEST_F(QuicHttpResponseCacheTest, ReadsCacheDirWithServerPushResource) { |
| 110 cache_.InitializeFromDirectory(CacheDirectory() + "_with_push"); | 109 cache_.InitializeFromDirectory(CacheDirectory() + "_with_push"); |
| 111 std::list<ServerPushInfo> resources = | 110 std::list<ServerPushInfo> resources = |
| 112 cache_.GetServerPushResources("www.example.com/"); | 111 cache_.GetServerPushResources("www.example.com/"); |
| 113 ASSERT_EQ(1UL, resources.size()); | 112 ASSERT_EQ(1UL, resources.size()); |
| 114 } | 113 } |
| 115 | 114 |
| 116 TEST_F(QuicHttpResponseCacheTest, ReadsCacheDirWithServerPushResources) { | 115 TEST_F(QuicHttpResponseCacheTest, ReadsCacheDirWithServerPushResources) { |
| 117 cache_.InitializeFromDirectory(CacheDirectory() + "_with_push"); | 116 cache_.InitializeFromDirectory(CacheDirectory() + "_with_push"); |
| 118 std::list<ServerPushInfo> resources = | 117 std::list<ServerPushInfo> resources = |
| 119 cache_.GetServerPushResources("www.example.com/index2.html"); | 118 cache_.GetServerPushResources("www.example.com/index2.html"); |
| 120 ASSERT_EQ(2UL, resources.size()); | 119 ASSERT_EQ(2UL, resources.size()); |
| 121 } | 120 } |
| 122 | 121 |
| 123 TEST_F(QuicHttpResponseCacheTest, UsesOriginalUrl) { | 122 TEST_F(QuicHttpResponseCacheTest, UsesOriginalUrl) { |
| 124 cache_.InitializeFromDirectory(CacheDirectory()); | 123 cache_.InitializeFromDirectory(CacheDirectory()); |
| 125 const QuicHttpResponseCache::Response* response = | 124 const QuicHttpResponseCache::Response* response = |
| 126 cache_.GetResponse("www.example.com", "/site_map.html"); | 125 cache_.GetResponse("www.example.com", "/site_map.html"); |
| 127 ASSERT_TRUE(response); | 126 ASSERT_TRUE(response); |
| 128 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 127 ASSERT_TRUE(QuicContainsKey(response->headers(), ":status")); |
| 129 EXPECT_EQ("200", response->headers().find(":status")->second); | 128 EXPECT_EQ("200", response->headers().find(":status")->second); |
| 130 // Connection headers are not valid in HTTP/2. | 129 // Connection headers are not valid in HTTP/2. |
| 131 EXPECT_FALSE(ContainsKey(response->headers(), "connection")); | 130 EXPECT_FALSE(QuicContainsKey(response->headers(), "connection")); |
| 132 EXPECT_LT(0U, response->body().length()); | 131 EXPECT_LT(0U, response->body().length()); |
| 133 } | 132 } |
| 134 | 133 |
| 135 TEST_F(QuicHttpResponseCacheTest, DefaultResponse) { | 134 TEST_F(QuicHttpResponseCacheTest, DefaultResponse) { |
| 136 // Verify GetResponse returns nullptr when no default is set. | 135 // Verify GetResponse returns nullptr when no default is set. |
| 137 const QuicHttpResponseCache::Response* response = | 136 const QuicHttpResponseCache::Response* response = |
| 138 cache_.GetResponse("www.google.com", "/"); | 137 cache_.GetResponse("www.google.com", "/"); |
| 139 ASSERT_FALSE(response); | 138 ASSERT_FALSE(response); |
| 140 | 139 |
| 141 // Add a default response. | 140 // Add a default response. |
| 142 SpdyHeaderBlock response_headers; | 141 SpdyHeaderBlock response_headers; |
| 143 response_headers[":version"] = "HTTP/1.1"; | 142 response_headers[":version"] = "HTTP/1.1"; |
| 144 response_headers[":status"] = "200"; | 143 response_headers[":status"] = "200"; |
| 145 response_headers["content-length"] = "0"; | 144 response_headers["content-length"] = "0"; |
| 146 QuicHttpResponseCache::Response* default_response = | 145 QuicHttpResponseCache::Response* default_response = |
| 147 new QuicHttpResponseCache::Response; | 146 new QuicHttpResponseCache::Response; |
| 148 default_response->set_headers(std::move(response_headers)); | 147 default_response->set_headers(std::move(response_headers)); |
| 149 cache_.AddDefaultResponse(default_response); | 148 cache_.AddDefaultResponse(default_response); |
| 150 | 149 |
| 151 // Now we should get the default response for the original request. | 150 // Now we should get the default response for the original request. |
| 152 response = cache_.GetResponse("www.google.com", "/"); | 151 response = cache_.GetResponse("www.google.com", "/"); |
| 153 ASSERT_TRUE(response); | 152 ASSERT_TRUE(response); |
| 154 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 153 ASSERT_TRUE(QuicContainsKey(response->headers(), ":status")); |
| 155 EXPECT_EQ("200", response->headers().find(":status")->second); | 154 EXPECT_EQ("200", response->headers().find(":status")->second); |
| 156 | 155 |
| 157 // Now add a set response for / and make sure it is returned | 156 // Now add a set response for / and make sure it is returned |
| 158 cache_.AddSimpleResponse("www.google.com", "/", 302, ""); | 157 cache_.AddSimpleResponse("www.google.com", "/", 302, ""); |
| 159 response = cache_.GetResponse("www.google.com", "/"); | 158 response = cache_.GetResponse("www.google.com", "/"); |
| 160 ASSERT_TRUE(response); | 159 ASSERT_TRUE(response); |
| 161 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 160 ASSERT_TRUE(QuicContainsKey(response->headers(), ":status")); |
| 162 EXPECT_EQ("302", response->headers().find(":status")->second); | 161 EXPECT_EQ("302", response->headers().find(":status")->second); |
| 163 | 162 |
| 164 // We should get the default response for other requests. | 163 // We should get the default response for other requests. |
| 165 response = cache_.GetResponse("www.google.com", "/asd"); | 164 response = cache_.GetResponse("www.google.com", "/asd"); |
| 166 ASSERT_TRUE(response); | 165 ASSERT_TRUE(response); |
| 167 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 166 ASSERT_TRUE(QuicContainsKey(response->headers(), ":status")); |
| 168 EXPECT_EQ("200", response->headers().find(":status")->second); | 167 EXPECT_EQ("200", response->headers().find(":status")->second); |
| 169 } | 168 } |
| 170 | 169 |
| 171 TEST_F(QuicHttpResponseCacheTest, AddSimpleResponseWithServerPushResources) { | 170 TEST_F(QuicHttpResponseCacheTest, AddSimpleResponseWithServerPushResources) { |
| 172 string request_host = "www.foo.com"; | 171 string request_host = "www.foo.com"; |
| 173 string response_body("hello response"); | 172 string response_body("hello response"); |
| 174 const size_t kNumResources = 5; | 173 const size_t kNumResources = 5; |
| 175 int NumResources = 5; | 174 int NumResources = 5; |
| 176 std::list<QuicHttpResponseCache::ServerPushInfo> push_resources; | 175 std::list<QuicHttpResponseCache::ServerPushInfo> push_resources; |
| 177 string scheme = "http"; | 176 string scheme = "http"; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 cache_.GetServerPushResources(request_url); | 230 cache_.GetServerPushResources(request_url); |
| 232 ASSERT_EQ(kNumResources, resources.size()); | 231 ASSERT_EQ(kNumResources, resources.size()); |
| 233 int i = 0; | 232 int i = 0; |
| 234 for (const auto& push_resource : push_resources) { | 233 for (const auto& push_resource : push_resources) { |
| 235 GURL url = resources.front().request_url; | 234 GURL url = resources.front().request_url; |
| 236 string host = url.host(); | 235 string host = url.host(); |
| 237 string path = url.path(); | 236 string path = url.path(); |
| 238 const QuicHttpResponseCache::Response* response = | 237 const QuicHttpResponseCache::Response* response = |
| 239 cache_.GetResponse(host, path); | 238 cache_.GetResponse(host, path); |
| 240 ASSERT_TRUE(response); | 239 ASSERT_TRUE(response); |
| 241 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 240 ASSERT_TRUE(QuicContainsKey(response->headers(), ":status")); |
| 242 EXPECT_EQ(push_response_status[i++], | 241 EXPECT_EQ(push_response_status[i++], |
| 243 response->headers().find(":status")->second); | 242 response->headers().find(":status")->second); |
| 244 EXPECT_EQ(push_resource.body, response->body()); | 243 EXPECT_EQ(push_resource.body, response->body()); |
| 245 resources.pop_front(); | 244 resources.pop_front(); |
| 246 } | 245 } |
| 247 } | 246 } |
| 248 | 247 |
| 249 } // namespace test | 248 } // namespace test |
| 250 } // namespace net | 249 } // namespace net |
| OLD | NEW |