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

Side by Side Diff: content/browser/loader/mojo_async_resource_handler_unittest.cc

Issue 2629513003: Implement CachedMetadata handling on MojoAsyncResourceHandler (Closed)
Patch Set: rebase Created 3 years, 11 months 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "content/browser/loader/mojo_async_resource_handler.h" 5 #include "content/browser/loader/mojo_async_resource_handler.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 num_on_response_started_calls_expectation_ = expectation; 167 num_on_response_started_calls_expectation_ = expectation;
168 } 168 }
169 169
170 private: 170 private:
171 int num_on_response_started_calls_ = 0; 171 int num_on_response_started_calls_ = 0;
172 int num_on_response_started_calls_expectation_ = 0; 172 int num_on_response_started_calls_expectation_ = 0;
173 173
174 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcherHostDelegate); 174 DISALLOW_COPY_AND_ASSIGN(TestResourceDispatcherHostDelegate);
175 }; 175 };
176 176
177 class MojoAsyncResourceHandlerWithCustomDataPipeOperations 177 class MojoAsyncResourceHandlerWithStubOperations
178 : public MojoAsyncResourceHandler { 178 : public MojoAsyncResourceHandler {
179 public: 179 public:
180 MojoAsyncResourceHandlerWithCustomDataPipeOperations( 180 MojoAsyncResourceHandlerWithStubOperations(
181 net::URLRequest* request, 181 net::URLRequest* request,
182 ResourceDispatcherHostImpl* rdh, 182 ResourceDispatcherHostImpl* rdh,
183 mojom::URLLoaderAssociatedRequest mojo_request, 183 mojom::URLLoaderAssociatedRequest mojo_request,
184 mojom::URLLoaderClientAssociatedPtr url_loader_client) 184 mojom::URLLoaderClientAssociatedPtr url_loader_client)
185 : MojoAsyncResourceHandler(request, 185 : MojoAsyncResourceHandler(request,
186 rdh, 186 rdh,
187 std::move(mojo_request), 187 std::move(mojo_request),
188 std::move(url_loader_client)) {} 188 std::move(url_loader_client)) {}
189 ~MojoAsyncResourceHandlerWithCustomDataPipeOperations() override {} 189 ~MojoAsyncResourceHandlerWithStubOperations() override {}
190 190
191 void ResetBeginWriteExpectation() { is_begin_write_expectation_set_ = false; } 191 void ResetBeginWriteExpectation() { is_begin_write_expectation_set_ = false; }
192 192
193 void set_begin_write_expectation(MojoResult begin_write_expectation) { 193 void set_begin_write_expectation(MojoResult begin_write_expectation) {
194 is_begin_write_expectation_set_ = true; 194 is_begin_write_expectation_set_ = true;
195 begin_write_expectation_ = begin_write_expectation; 195 begin_write_expectation_ = begin_write_expectation;
196 } 196 }
197 void set_end_write_expectation(MojoResult end_write_expectation) { 197 void set_end_write_expectation(MojoResult end_write_expectation) {
198 is_end_write_expectation_set_ = true; 198 is_end_write_expectation_set_ = true;
199 end_write_expectation_ = end_write_expectation; 199 end_write_expectation_ = end_write_expectation;
200 } 200 }
201 bool has_received_bad_message() const { return has_received_bad_message_; } 201 bool has_received_bad_message() const { return has_received_bad_message_; }
202 void SetMetadata(scoped_refptr<net::IOBufferWithSize> metadata) {
203 metadata_ = std::move(metadata);
204 }
202 205
203 private: 206 private:
204 MojoResult BeginWrite(void** data, uint32_t* available) override { 207 MojoResult BeginWrite(void** data, uint32_t* available) override {
205 if (is_begin_write_expectation_set_) 208 if (is_begin_write_expectation_set_)
206 return begin_write_expectation_; 209 return begin_write_expectation_;
207 return MojoAsyncResourceHandler::BeginWrite(data, available); 210 return MojoAsyncResourceHandler::BeginWrite(data, available);
208 } 211 }
209 MojoResult EndWrite(uint32_t written) override { 212 MojoResult EndWrite(uint32_t written) override {
210 if (is_end_write_expectation_set_) 213 if (is_end_write_expectation_set_)
211 return end_write_expectation_; 214 return end_write_expectation_;
212 return MojoAsyncResourceHandler::EndWrite(written); 215 return MojoAsyncResourceHandler::EndWrite(written);
213 } 216 }
217 net::IOBufferWithSize* GetResponseMetadata(
218 net::URLRequest* request) override {
219 return metadata_.get();
220 }
221
214 void ReportBadMessage(const std::string& error) override { 222 void ReportBadMessage(const std::string& error) override {
215 has_received_bad_message_ = true; 223 has_received_bad_message_ = true;
216 } 224 }
217 225
218 bool is_begin_write_expectation_set_ = false; 226 bool is_begin_write_expectation_set_ = false;
219 bool is_end_write_expectation_set_ = false; 227 bool is_end_write_expectation_set_ = false;
220 bool has_received_bad_message_ = false; 228 bool has_received_bad_message_ = false;
221 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN; 229 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN;
222 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN; 230 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN;
231 scoped_refptr<net::IOBufferWithSize> metadata_;
223 232
224 DISALLOW_COPY_AND_ASSIGN( 233 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerWithStubOperations);
225 MojoAsyncResourceHandlerWithCustomDataPipeOperations);
226 }; 234 };
227 235
228 class TestURLLoaderFactory final : public mojom::URLLoaderFactory { 236 class TestURLLoaderFactory final : public mojom::URLLoaderFactory {
229 public: 237 public:
230 TestURLLoaderFactory() {} 238 TestURLLoaderFactory() {}
231 ~TestURLLoaderFactory() override {} 239 ~TestURLLoaderFactory() override {}
232 240
233 void CreateLoaderAndStart( 241 void CreateLoaderAndStart(
234 mojom::URLLoaderAssociatedRequest request, 242 mojom::URLLoaderAssociatedRequest request,
235 int32_t routing_id, 243 int32_t routing_id,
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 0, 0, request, url_loader_client_.CreateRemoteAssociatedPtrInfo( 309 0, 0, request, url_loader_client_.CreateRemoteAssociatedPtrInfo(
302 url_loader_factory_.associated_group())); 310 url_loader_factory_.associated_group()));
303 311
304 url_loader_factory_.FlushForTesting(); 312 url_loader_factory_.FlushForTesting();
305 DCHECK(weak_binding); 313 DCHECK(weak_binding);
306 TestURLLoaderFactory* factory_impl = 314 TestURLLoaderFactory* factory_impl =
307 static_cast<TestURLLoaderFactory*>(weak_binding->impl()); 315 static_cast<TestURLLoaderFactory*>(weak_binding->impl());
308 316
309 mojom::URLLoaderClientAssociatedPtr client_ptr; 317 mojom::URLLoaderClientAssociatedPtr client_ptr;
310 client_ptr.Bind(factory_impl->PassClientPtrInfo()); 318 client_ptr.Bind(factory_impl->PassClientPtrInfo());
311 handler_.reset(new MojoAsyncResourceHandlerWithCustomDataPipeOperations( 319 handler_.reset(new MojoAsyncResourceHandlerWithStubOperations(
312 request_.get(), &rdh_, factory_impl->PassLoaderRequest(), 320 request_.get(), &rdh_, factory_impl->PassLoaderRequest(),
313 std::move(client_ptr))); 321 std::move(client_ptr)));
314 mock_loader_.reset(new MockResourceLoader(handler_.get())); 322 mock_loader_.reset(new MockResourceLoader(handler_.get()));
315 } 323 }
316 324
317 virtual ~MojoAsyncResourceHandlerTestBase() { 325 virtual ~MojoAsyncResourceHandlerTestBase() {
318 MojoAsyncResourceHandler::SetAllocationSizeForTesting( 326 MojoAsyncResourceHandler::SetAllocationSizeForTesting(
319 MojoAsyncResourceHandler::kDefaultAllocationSize); 327 MojoAsyncResourceHandler::kDefaultAllocationSize);
320 base::RunLoop().RunUntilIdle(); 328 base::RunLoop().RunUntilIdle();
321 } 329 }
(...skipping 23 matching lines...) Expand all
345 353
346 TestBrowserThreadBundle thread_bundle_; 354 TestBrowserThreadBundle thread_bundle_;
347 TestResourceDispatcherHostDelegate rdh_delegate_; 355 TestResourceDispatcherHostDelegate rdh_delegate_;
348 ResourceDispatcherHostImpl rdh_; 356 ResourceDispatcherHostImpl rdh_;
349 mojom::URLLoaderFactoryPtr url_loader_factory_; 357 mojom::URLLoaderFactoryPtr url_loader_factory_;
350 mojom::URLLoaderAssociatedPtr url_loader_proxy_; 358 mojom::URLLoaderAssociatedPtr url_loader_proxy_;
351 TestURLLoaderClient url_loader_client_; 359 TestURLLoaderClient url_loader_client_;
352 std::unique_ptr<TestBrowserContext> browser_context_; 360 std::unique_ptr<TestBrowserContext> browser_context_;
353 net::TestDelegate url_request_delegate_; 361 net::TestDelegate url_request_delegate_;
354 std::unique_ptr<net::URLRequest> request_; 362 std::unique_ptr<net::URLRequest> request_;
355 std::unique_ptr<MojoAsyncResourceHandlerWithCustomDataPipeOperations> 363 std::unique_ptr<MojoAsyncResourceHandlerWithStubOperations> handler_;
356 handler_;
357 std::unique_ptr<MockResourceLoader> mock_loader_; 364 std::unique_ptr<MockResourceLoader> mock_loader_;
358 365
359 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase); 366 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase);
360 }; 367 };
361 368
362 class MojoAsyncResourceHandlerTest : public MojoAsyncResourceHandlerTestBase, 369 class MojoAsyncResourceHandlerTest : public MojoAsyncResourceHandlerTestBase,
363 public ::testing::Test {}; 370 public ::testing::Test {};
364 371
365 // This test class is parameterized with MojoAsyncResourceHandler's allocation 372 // This test class is parameterized with MojoAsyncResourceHandler's allocation
366 // size. 373 // size.
(...skipping 12 matching lines...) Expand all
379 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 386 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
380 } 387 }
381 388
382 TEST_F(MojoAsyncResourceHandlerTest, OnWillStart) { 389 TEST_F(MojoAsyncResourceHandlerTest, OnWillStart) {
383 EXPECT_EQ(MockResourceLoader::Status::IDLE, 390 EXPECT_EQ(MockResourceLoader::Status::IDLE,
384 mock_loader_->OnWillStart(request_->url())); 391 mock_loader_->OnWillStart(request_->url()));
385 } 392 }
386 393
387 TEST_F(MojoAsyncResourceHandlerTest, OnResponseStarted) { 394 TEST_F(MojoAsyncResourceHandlerTest, OnResponseStarted) {
388 rdh_delegate_.set_num_on_response_started_calls_expectation(1); 395 rdh_delegate_.set_num_on_response_started_calls_expectation(1);
396 scoped_refptr<net::IOBufferWithSize> metadata = new net::IOBufferWithSize(5);
397 strncpy(metadata->data(), "hello", 5);
dcheng 2017/01/18 00:23:52 Please use memcpy (strncpy doesn't null terminate,
yhirano 2017/01/18 06:22:42 Done.
398 handler_->SetMetadata(metadata);
399
389 ASSERT_EQ(MockResourceLoader::Status::IDLE, 400 ASSERT_EQ(MockResourceLoader::Status::IDLE,
390 mock_loader_->OnWillStart(request_->url())); 401 mock_loader_->OnWillStart(request_->url()));
391 402
392 scoped_refptr<ResourceResponse> response = new ResourceResponse(); 403 scoped_refptr<ResourceResponse> response = new ResourceResponse();
393 response->head.content_length = 99; 404 response->head.content_length = 99;
394 response->head.request_start = 405 response->head.request_start =
395 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(14); 406 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(14);
396 response->head.response_start = 407 response->head.response_start =
397 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(28); 408 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(28);
398 409
399 EXPECT_EQ(0, rdh_delegate_.num_on_response_started_calls()); 410 EXPECT_EQ(0, rdh_delegate_.num_on_response_started_calls());
400 base::TimeTicks now1 = base::TimeTicks::Now(); 411 base::TimeTicks now1 = base::TimeTicks::Now();
401 ASSERT_EQ(MockResourceLoader::Status::IDLE, 412 ASSERT_EQ(MockResourceLoader::Status::IDLE,
402 mock_loader_->OnResponseStarted(response)); 413 mock_loader_->OnResponseStarted(response));
403 base::TimeTicks now2 = base::TimeTicks::Now(); 414 base::TimeTicks now2 = base::TimeTicks::Now();
404 415
405 EXPECT_EQ(request_->creation_time(), response->head.request_start); 416 EXPECT_EQ(request_->creation_time(), response->head.request_start);
406 EXPECT_LE(now1, response->head.response_start); 417 EXPECT_LE(now1, response->head.response_start);
407 EXPECT_LE(response->head.response_start, now2); 418 EXPECT_LE(response->head.response_start, now2);
408 EXPECT_EQ(1, rdh_delegate_.num_on_response_started_calls()); 419 EXPECT_EQ(1, rdh_delegate_.num_on_response_started_calls());
409 420
410 url_loader_client_.RunUntilResponseReceived(); 421 url_loader_client_.RunUntilResponseReceived();
411 EXPECT_EQ(response->head.request_start, 422 EXPECT_EQ(response->head.request_start,
412 url_loader_client_.response_head().request_start); 423 url_loader_client_.response_head().request_start);
413 EXPECT_EQ(response->head.response_start, 424 EXPECT_EQ(response->head.response_start,
414 url_loader_client_.response_head().response_start); 425 url_loader_client_.response_head().response_start);
415 EXPECT_EQ(99, url_loader_client_.response_head().content_length); 426 EXPECT_EQ(99, url_loader_client_.response_head().content_length);
427
428 url_loader_client_.RunUntilCachedMetadataReceived();
429 EXPECT_EQ("hello",
430 std::string(reinterpret_cast<const char*>(
431 url_loader_client_.cached_metadata().data()),
432 url_loader_client_.cached_metadata().size()));
mmenke 2017/01/17 18:39:57 Suggest moving this std::string call into TestURLL
yhirano 2017/01/18 06:22:42 Done.
416 } 433 }
417 434
418 TEST_F(MojoAsyncResourceHandlerTest, OnWillReadAndInFlightRequests) { 435 TEST_F(MojoAsyncResourceHandlerTest, OnWillReadAndInFlightRequests) {
419 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 436 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
420 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 437 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
421 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(-1)); 438 ASSERT_EQ(MockResourceLoader::Status::IDLE, mock_loader_->OnWillRead(-1));
422 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing()); 439 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing());
423 handler_ = nullptr; 440 handler_ = nullptr;
424 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 441 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
425 } 442 }
(...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after
1122 } 1139 }
1123 } 1140 }
1124 EXPECT_EQ("B", body); 1141 EXPECT_EQ("B", body);
1125 } 1142 }
1126 1143
1127 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 1144 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
1128 MojoAsyncResourceHandlerWithAllocationSizeTest, 1145 MojoAsyncResourceHandlerWithAllocationSizeTest,
1129 ::testing::Values(8, 32 * 2014)); 1146 ::testing::Values(8, 32 * 2014));
1130 } // namespace 1147 } // namespace
1131 } // namespace content 1148 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698