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" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 TEST_F(QuicInMemoryCacheTest, AddSimpleResponseGetResponse) { | 62 TEST_F(QuicInMemoryCacheTest, AddSimpleResponseGetResponse) { |
63 string response_body("hello response"); | 63 string response_body("hello response"); |
64 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); | 64 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); |
65 cache->AddSimpleResponse("www.google.com", "/", 200, response_body); | 65 cache->AddSimpleResponse("www.google.com", "/", 200, response_body); |
66 | 66 |
67 BalsaHeaders request_headers; | 67 BalsaHeaders request_headers; |
68 CreateRequest("www.google.com", "/", &request_headers); | 68 CreateRequest("www.google.com", "/", &request_headers); |
69 const QuicInMemoryCache::Response* response = | 69 const QuicInMemoryCache::Response* response = |
70 cache->GetResponse("www.google.com", "/"); | 70 cache->GetResponse("www.google.com", "/"); |
71 ASSERT_TRUE(response); | 71 ASSERT_TRUE(response); |
72 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 72 ASSERT_TRUE(base::ContainsKey(response->headers(), ":status")); |
73 EXPECT_EQ("200", response->headers().find(":status")->second); | 73 EXPECT_EQ("200", response->headers().find(":status")->second); |
74 EXPECT_EQ(response_body.size(), response->body().length()); | 74 EXPECT_EQ(response_body.size(), response->body().length()); |
75 } | 75 } |
76 | 76 |
77 TEST_F(QuicInMemoryCacheTest, AddResponse) { | 77 TEST_F(QuicInMemoryCacheTest, AddResponse) { |
78 const string kRequestHost = "www.foo.com"; | 78 const string kRequestHost = "www.foo.com"; |
79 const string kRequestPath = "/"; | 79 const string kRequestPath = "/"; |
80 const string kResponseBody("hello response"); | 80 const string kResponseBody("hello response"); |
81 | 81 |
82 SpdyHeaderBlock response_headers; | 82 SpdyHeaderBlock response_headers; |
(...skipping 16 matching lines...) Expand all Loading... |
99 EXPECT_EQ(response->body(), kResponseBody); | 99 EXPECT_EQ(response->body(), kResponseBody); |
100 EXPECT_EQ(response->trailers(), response_trailers); | 100 EXPECT_EQ(response->trailers(), response_trailers); |
101 } | 101 } |
102 | 102 |
103 TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) { | 103 TEST_F(QuicInMemoryCacheTest, ReadsCacheDir) { |
104 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory()); | 104 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory()); |
105 const QuicInMemoryCache::Response* response = | 105 const QuicInMemoryCache::Response* response = |
106 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url", | 106 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url", |
107 "/index.html"); | 107 "/index.html"); |
108 ASSERT_TRUE(response); | 108 ASSERT_TRUE(response); |
109 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 109 ASSERT_TRUE(base::ContainsKey(response->headers(), ":status")); |
110 EXPECT_EQ("200", response->headers().find(":status")->second); | 110 EXPECT_EQ("200", response->headers().find(":status")->second); |
111 ASSERT_TRUE(ContainsKey(response->headers(), "connection")); | 111 ASSERT_TRUE(base::ContainsKey(response->headers(), "connection")); |
112 EXPECT_EQ("close", response->headers().find("connection")->second); | 112 EXPECT_EQ("close", response->headers().find("connection")->second); |
113 EXPECT_LT(0U, response->body().length()); | 113 EXPECT_LT(0U, response->body().length()); |
114 } | 114 } |
115 | 115 |
116 TEST_F(QuicInMemoryCacheTest, ReadsCacheDirWithServerPushResource) { | 116 TEST_F(QuicInMemoryCacheTest, ReadsCacheDirWithServerPushResource) { |
117 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory() + | 117 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory() + |
118 "_with_push"); | 118 "_with_push"); |
119 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); | 119 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); |
120 list<ServerPushInfo> resources = | 120 list<ServerPushInfo> resources = |
121 cache->GetServerPushResources("quic.test.url/"); | 121 cache->GetServerPushResources("quic.test.url/"); |
122 ASSERT_EQ(1UL, resources.size()); | 122 ASSERT_EQ(1UL, resources.size()); |
123 } | 123 } |
124 | 124 |
125 TEST_F(QuicInMemoryCacheTest, ReadsCacheDirWithServerPushResources) { | 125 TEST_F(QuicInMemoryCacheTest, ReadsCacheDirWithServerPushResources) { |
126 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory() + | 126 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory() + |
127 "_with_push"); | 127 "_with_push"); |
128 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); | 128 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); |
129 list<ServerPushInfo> resources = | 129 list<ServerPushInfo> resources = |
130 cache->GetServerPushResources("quic.test.url/index2.html"); | 130 cache->GetServerPushResources("quic.test.url/index2.html"); |
131 ASSERT_EQ(2UL, resources.size()); | 131 ASSERT_EQ(2UL, resources.size()); |
132 } | 132 } |
133 | 133 |
134 TEST_F(QuicInMemoryCacheTest, UsesOriginalUrl) { | 134 TEST_F(QuicInMemoryCacheTest, UsesOriginalUrl) { |
135 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory()); | 135 QuicInMemoryCache::GetInstance()->InitializeFromDirectory(CacheDirectory()); |
136 const QuicInMemoryCache::Response* response = | 136 const QuicInMemoryCache::Response* response = |
137 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url", | 137 QuicInMemoryCache::GetInstance()->GetResponse("quic.test.url", |
138 "/index.html"); | 138 "/index.html"); |
139 ASSERT_TRUE(response); | 139 ASSERT_TRUE(response); |
140 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 140 ASSERT_TRUE(base::ContainsKey(response->headers(), ":status")); |
141 EXPECT_EQ("200", response->headers().find(":status")->second); | 141 EXPECT_EQ("200", response->headers().find(":status")->second); |
142 ASSERT_TRUE(ContainsKey(response->headers(), "connection")); | 142 ASSERT_TRUE(base::ContainsKey(response->headers(), "connection")); |
143 EXPECT_EQ("close", response->headers().find("connection")->second); | 143 EXPECT_EQ("close", response->headers().find("connection")->second); |
144 EXPECT_LT(0U, response->body().length()); | 144 EXPECT_LT(0U, response->body().length()); |
145 } | 145 } |
146 | 146 |
147 TEST_F(QuicInMemoryCacheTest, DefaultResponse) { | 147 TEST_F(QuicInMemoryCacheTest, DefaultResponse) { |
148 // Verify GetResponse returns nullptr when no default is set. | 148 // Verify GetResponse returns nullptr when no default is set. |
149 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); | 149 QuicInMemoryCache* cache = QuicInMemoryCache::GetInstance(); |
150 const QuicInMemoryCache::Response* response = | 150 const QuicInMemoryCache::Response* response = |
151 cache->GetResponse("www.google.com", "/"); | 151 cache->GetResponse("www.google.com", "/"); |
152 ASSERT_FALSE(response); | 152 ASSERT_FALSE(response); |
153 | 153 |
154 // Add a default response. | 154 // Add a default response. |
155 SpdyHeaderBlock response_headers; | 155 SpdyHeaderBlock response_headers; |
156 response_headers[":version"] = "HTTP/1.1"; | 156 response_headers[":version"] = "HTTP/1.1"; |
157 response_headers[":status"] = "200"; | 157 response_headers[":status"] = "200"; |
158 response_headers["content-length"] = "0"; | 158 response_headers["content-length"] = "0"; |
159 QuicInMemoryCache::Response* default_response = | 159 QuicInMemoryCache::Response* default_response = |
160 new QuicInMemoryCache::Response; | 160 new QuicInMemoryCache::Response; |
161 default_response->set_headers(std::move(response_headers)); | 161 default_response->set_headers(std::move(response_headers)); |
162 cache->AddDefaultResponse(default_response); | 162 cache->AddDefaultResponse(default_response); |
163 | 163 |
164 // Now we should get the default response for the original request. | 164 // Now we should get the default response for the original request. |
165 response = cache->GetResponse("www.google.com", "/"); | 165 response = cache->GetResponse("www.google.com", "/"); |
166 ASSERT_TRUE(response); | 166 ASSERT_TRUE(response); |
167 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 167 ASSERT_TRUE(base::ContainsKey(response->headers(), ":status")); |
168 EXPECT_EQ("200", response->headers().find(":status")->second); | 168 EXPECT_EQ("200", response->headers().find(":status")->second); |
169 | 169 |
170 // Now add a set response for / and make sure it is returned | 170 // Now add a set response for / and make sure it is returned |
171 cache->AddSimpleResponse("www.google.com", "/", 302, ""); | 171 cache->AddSimpleResponse("www.google.com", "/", 302, ""); |
172 response = cache->GetResponse("www.google.com", "/"); | 172 response = cache->GetResponse("www.google.com", "/"); |
173 ASSERT_TRUE(response); | 173 ASSERT_TRUE(response); |
174 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 174 ASSERT_TRUE(base::ContainsKey(response->headers(), ":status")); |
175 EXPECT_EQ("302", response->headers().find(":status")->second); | 175 EXPECT_EQ("302", response->headers().find(":status")->second); |
176 | 176 |
177 // We should get the default response for other requests. | 177 // We should get the default response for other requests. |
178 response = cache->GetResponse("www.google.com", "/asd"); | 178 response = cache->GetResponse("www.google.com", "/asd"); |
179 ASSERT_TRUE(response); | 179 ASSERT_TRUE(response); |
180 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 180 ASSERT_TRUE(base::ContainsKey(response->headers(), ":status")); |
181 EXPECT_EQ("200", response->headers().find(":status")->second); | 181 EXPECT_EQ("200", response->headers().find(":status")->second); |
182 } | 182 } |
183 | 183 |
184 TEST_F(QuicInMemoryCacheTest, AddSimpleResponseWithServerPushResources) { | 184 TEST_F(QuicInMemoryCacheTest, AddSimpleResponseWithServerPushResources) { |
185 string request_host = "www.foo.com"; | 185 string request_host = "www.foo.com"; |
186 string response_body("hello response"); | 186 string response_body("hello response"); |
187 const size_t kNumResources = 5; | 187 const size_t kNumResources = 5; |
188 int NumResources = 5; | 188 int NumResources = 5; |
189 list<QuicInMemoryCache::ServerPushInfo> push_resources; | 189 list<QuicInMemoryCache::ServerPushInfo> push_resources; |
190 string scheme = "http"; | 190 string scheme = "http"; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 list<ServerPushInfo> resources = cache->GetServerPushResources(request_url); | 242 list<ServerPushInfo> resources = cache->GetServerPushResources(request_url); |
243 ASSERT_EQ(kNumResources, resources.size()); | 243 ASSERT_EQ(kNumResources, resources.size()); |
244 int i = 0; | 244 int i = 0; |
245 for (const auto& push_resource : push_resources) { | 245 for (const auto& push_resource : push_resources) { |
246 GURL url = resources.front().request_url; | 246 GURL url = resources.front().request_url; |
247 string host = url.host(); | 247 string host = url.host(); |
248 string path = url.path(); | 248 string path = url.path(); |
249 const QuicInMemoryCache::Response* response = | 249 const QuicInMemoryCache::Response* response = |
250 cache->GetResponse(host, path); | 250 cache->GetResponse(host, path); |
251 ASSERT_TRUE(response); | 251 ASSERT_TRUE(response); |
252 ASSERT_TRUE(ContainsKey(response->headers(), ":status")); | 252 ASSERT_TRUE(base::ContainsKey(response->headers(), ":status")); |
253 EXPECT_EQ(push_response_status[i++], | 253 EXPECT_EQ(push_response_status[i++], |
254 response->headers().find(":status")->second); | 254 response->headers().find(":status")->second); |
255 EXPECT_EQ(push_resource.body, response->body()); | 255 EXPECT_EQ(push_resource.body, response->body()); |
256 resources.pop_front(); | 256 resources.pop_front(); |
257 } | 257 } |
258 } | 258 } |
259 | 259 |
260 } // namespace test | 260 } // namespace test |
261 } // namespace net | 261 } // namespace net |
OLD | NEW |