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

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

Issue 2629513003: Implement CachedMetadata handling on MojoAsyncResourceHandler (Closed)
Patch Set: fix 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 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 216
217 private: 217 private:
218 bool is_cancel_with_error_called_ = false; 218 bool is_cancel_with_error_called_ = false;
219 int error_ = net::OK; 219 int error_ = net::OK;
220 int num_resume_calls_ = 0; 220 int num_resume_calls_ = 0;
221 base::Closure quit_closure_; 221 base::Closure quit_closure_;
222 222
223 DISALLOW_COPY_AND_ASSIGN(TestResourceController); 223 DISALLOW_COPY_AND_ASSIGN(TestResourceController);
224 }; 224 };
225 225
226 class MojoAsyncResourceHandlerWithCustomDataPipeOperations 226 class MojoAsyncResourceHandlerWithStubOperations
227 : public MojoAsyncResourceHandler { 227 : public MojoAsyncResourceHandler {
228 public: 228 public:
229 MojoAsyncResourceHandlerWithCustomDataPipeOperations( 229 MojoAsyncResourceHandlerWithStubOperations(
230 net::URLRequest* request, 230 net::URLRequest* request,
231 ResourceDispatcherHostImpl* rdh, 231 ResourceDispatcherHostImpl* rdh,
232 mojom::URLLoaderAssociatedRequest mojo_request, 232 mojom::URLLoaderAssociatedRequest mojo_request,
233 mojom::URLLoaderClientAssociatedPtr url_loader_client) 233 mojom::URLLoaderClientAssociatedPtr url_loader_client)
234 : MojoAsyncResourceHandler(request, 234 : MojoAsyncResourceHandler(request,
235 rdh, 235 rdh,
236 std::move(mojo_request), 236 std::move(mojo_request),
237 std::move(url_loader_client)) {} 237 std::move(url_loader_client)) {}
238 ~MojoAsyncResourceHandlerWithCustomDataPipeOperations() override {} 238 ~MojoAsyncResourceHandlerWithStubOperations() override {}
239 239
240 void ResetBeginWriteExpectation() { is_begin_write_expectation_set_ = false; } 240 void ResetBeginWriteExpectation() { is_begin_write_expectation_set_ = false; }
241 241
242 void set_begin_write_expectation(MojoResult begin_write_expectation) { 242 void set_begin_write_expectation(MojoResult begin_write_expectation) {
243 is_begin_write_expectation_set_ = true; 243 is_begin_write_expectation_set_ = true;
244 begin_write_expectation_ = begin_write_expectation; 244 begin_write_expectation_ = begin_write_expectation;
245 } 245 }
246 void set_end_write_expectation(MojoResult end_write_expectation) { 246 void set_end_write_expectation(MojoResult end_write_expectation) {
247 is_end_write_expectation_set_ = true; 247 is_end_write_expectation_set_ = true;
248 end_write_expectation_ = end_write_expectation; 248 end_write_expectation_ = end_write_expectation;
249 } 249 }
250 bool has_received_bad_message() const { return has_received_bad_message_; } 250 bool has_received_bad_message() const { return has_received_bad_message_; }
251 void SetMetadata(scoped_refptr<net::IOBufferWithSize> metadata) {
252 metadata_ = metadata;
tzik 2017/01/13 02:41:23 std::move()
yhirano 2017/01/13 12:36:13 Done.
253 }
251 254
252 private: 255 private:
253 MojoResult BeginWrite(void** data, uint32_t* available) override { 256 MojoResult BeginWrite(void** data, uint32_t* available) override {
254 if (is_begin_write_expectation_set_) 257 if (is_begin_write_expectation_set_)
255 return begin_write_expectation_; 258 return begin_write_expectation_;
256 return MojoAsyncResourceHandler::BeginWrite(data, available); 259 return MojoAsyncResourceHandler::BeginWrite(data, available);
257 } 260 }
258 MojoResult EndWrite(uint32_t written) override { 261 MojoResult EndWrite(uint32_t written) override {
259 if (is_end_write_expectation_set_) 262 if (is_end_write_expectation_set_)
260 return end_write_expectation_; 263 return end_write_expectation_;
261 return MojoAsyncResourceHandler::EndWrite(written); 264 return MojoAsyncResourceHandler::EndWrite(written);
262 } 265 }
266 net::IOBufferWithSize* GetResponseMetadata(
267 net::URLRequest* request) override {
268 return metadata_.get();
269 }
270
263 void ReportBadMessage(const std::string& error) override { 271 void ReportBadMessage(const std::string& error) override {
264 has_received_bad_message_ = true; 272 has_received_bad_message_ = true;
265 } 273 }
266 274
267 bool is_begin_write_expectation_set_ = false; 275 bool is_begin_write_expectation_set_ = false;
268 bool is_end_write_expectation_set_ = false; 276 bool is_end_write_expectation_set_ = false;
269 bool has_received_bad_message_ = false; 277 bool has_received_bad_message_ = false;
270 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN; 278 MojoResult begin_write_expectation_ = MOJO_RESULT_UNKNOWN;
271 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN; 279 MojoResult end_write_expectation_ = MOJO_RESULT_UNKNOWN;
280 scoped_refptr<net::IOBufferWithSize> metadata_;
272 281
273 DISALLOW_COPY_AND_ASSIGN( 282 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerWithStubOperations);
274 MojoAsyncResourceHandlerWithCustomDataPipeOperations);
275 }; 283 };
276 284
277 class TestURLLoaderFactory final : public mojom::URLLoaderFactory { 285 class TestURLLoaderFactory final : public mojom::URLLoaderFactory {
278 public: 286 public:
279 TestURLLoaderFactory() {} 287 TestURLLoaderFactory() {}
280 ~TestURLLoaderFactory() override {} 288 ~TestURLLoaderFactory() override {}
281 289
282 void CreateLoaderAndStart( 290 void CreateLoaderAndStart(
283 mojom::URLLoaderAssociatedRequest request, 291 mojom::URLLoaderAssociatedRequest request,
284 int32_t routing_id, 292 int32_t routing_id,
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 0, 0, request, url_loader_client_.CreateRemoteAssociatedPtrInfo( 358 0, 0, request, url_loader_client_.CreateRemoteAssociatedPtrInfo(
351 url_loader_factory_.associated_group())); 359 url_loader_factory_.associated_group()));
352 360
353 url_loader_factory_.FlushForTesting(); 361 url_loader_factory_.FlushForTesting();
354 DCHECK(weak_binding); 362 DCHECK(weak_binding);
355 TestURLLoaderFactory* factory_impl = 363 TestURLLoaderFactory* factory_impl =
356 static_cast<TestURLLoaderFactory*>(weak_binding->impl()); 364 static_cast<TestURLLoaderFactory*>(weak_binding->impl());
357 365
358 mojom::URLLoaderClientAssociatedPtr client_ptr; 366 mojom::URLLoaderClientAssociatedPtr client_ptr;
359 client_ptr.Bind(factory_impl->PassClientPtrInfo()); 367 client_ptr.Bind(factory_impl->PassClientPtrInfo());
360 handler_.reset(new MojoAsyncResourceHandlerWithCustomDataPipeOperations( 368 handler_.reset(new MojoAsyncResourceHandlerWithStubOperations(
361 request_.get(), &rdh_, factory_impl->PassLoaderRequest(), 369 request_.get(), &rdh_, factory_impl->PassLoaderRequest(),
362 std::move(client_ptr))); 370 std::move(client_ptr)));
363 handler_->SetController(&resource_controller_); 371 handler_->SetController(&resource_controller_);
364 } 372 }
365 373
366 virtual ~MojoAsyncResourceHandlerTestBase() { 374 virtual ~MojoAsyncResourceHandlerTestBase() {
367 net::URLRequestFilter::GetInstance()->ClearHandlers(); 375 net::URLRequestFilter::GetInstance()->ClearHandlers();
368 MojoAsyncResourceHandler::SetAllocationSizeForTesting( 376 MojoAsyncResourceHandler::SetAllocationSizeForTesting(
369 MojoAsyncResourceHandler::kDefaultAllocationSize); 377 MojoAsyncResourceHandler::kDefaultAllocationSize);
370 base::RunLoop().RunUntilIdle(); 378 base::RunLoop().RunUntilIdle();
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 TestBrowserThreadBundle thread_bundle_; 419 TestBrowserThreadBundle thread_bundle_;
412 TestResourceDispatcherHostDelegate rdh_delegate_; 420 TestResourceDispatcherHostDelegate rdh_delegate_;
413 ResourceDispatcherHostImpl rdh_; 421 ResourceDispatcherHostImpl rdh_;
414 mojom::URLLoaderFactoryPtr url_loader_factory_; 422 mojom::URLLoaderFactoryPtr url_loader_factory_;
415 mojom::URLLoaderAssociatedPtr url_loader_proxy_; 423 mojom::URLLoaderAssociatedPtr url_loader_proxy_;
416 TestURLLoaderClient url_loader_client_; 424 TestURLLoaderClient url_loader_client_;
417 TestResourceController resource_controller_; 425 TestResourceController resource_controller_;
418 std::unique_ptr<TestBrowserContext> browser_context_; 426 std::unique_ptr<TestBrowserContext> browser_context_;
419 std::unique_ptr<net::TestDelegate> url_request_delegate_; 427 std::unique_ptr<net::TestDelegate> url_request_delegate_;
420 std::unique_ptr<net::URLRequest> request_; 428 std::unique_ptr<net::URLRequest> request_;
421 std::unique_ptr<MojoAsyncResourceHandlerWithCustomDataPipeOperations> 429 std::unique_ptr<MojoAsyncResourceHandlerWithStubOperations> handler_;
422 handler_;
423 430
424 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase); 431 DISALLOW_COPY_AND_ASSIGN(MojoAsyncResourceHandlerTestBase);
425 }; 432 };
426 433
427 class MojoAsyncResourceHandlerTest : public MojoAsyncResourceHandlerTestBase, 434 class MojoAsyncResourceHandlerTest : public MojoAsyncResourceHandlerTestBase,
428 public ::testing::Test {}; 435 public ::testing::Test {};
429 436
430 // This test class is parameterized with MojoAsyncResourceHandler's allocation 437 // This test class is parameterized with MojoAsyncResourceHandler's allocation
431 // size. 438 // size.
432 class MojoAsyncResourceHandlerWithAllocationSizeTest 439 class MojoAsyncResourceHandlerWithAllocationSizeTest
(...skipping 12 matching lines...) Expand all
445 } 452 }
446 453
447 TEST_F(MojoAsyncResourceHandlerTest, OnWillStart) { 454 TEST_F(MojoAsyncResourceHandlerTest, OnWillStart) {
448 bool defer = false; 455 bool defer = false;
449 EXPECT_TRUE(handler_->OnWillStart(request_->url(), &defer)); 456 EXPECT_TRUE(handler_->OnWillStart(request_->url(), &defer));
450 EXPECT_FALSE(defer); 457 EXPECT_FALSE(defer);
451 } 458 }
452 459
453 TEST_F(MojoAsyncResourceHandlerTest, OnResponseStarted) { 460 TEST_F(MojoAsyncResourceHandlerTest, OnResponseStarted) {
454 rdh_delegate_.set_num_on_response_started_calls_expectation(1); 461 rdh_delegate_.set_num_on_response_started_calls_expectation(1);
462 scoped_refptr<net::IOBufferWithSize> metadata = new net::IOBufferWithSize(5);
463 strncpy(metadata->data(), "hello", 5);
464
465 handler_->SetMetadata(metadata);
466
455 ASSERT_TRUE(CallOnWillStart()); 467 ASSERT_TRUE(CallOnWillStart());
456 468
457 scoped_refptr<ResourceResponse> response = new ResourceResponse(); 469 scoped_refptr<ResourceResponse> response = new ResourceResponse();
458 response->head.content_length = 99; 470 response->head.content_length = 99;
459 response->head.request_start = 471 response->head.request_start =
460 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(14); 472 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(14);
461 response->head.response_start = 473 response->head.response_start =
462 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(28); 474 base::TimeTicks::UnixEpoch() + base::TimeDelta::FromDays(28);
463 475
464 bool defer = false; 476 bool defer = false;
465 477
466 EXPECT_EQ(0, rdh_delegate_.num_on_response_started_calls()); 478 EXPECT_EQ(0, rdh_delegate_.num_on_response_started_calls());
467 base::TimeTicks now1 = base::TimeTicks::Now(); 479 base::TimeTicks now1 = base::TimeTicks::Now();
468 ASSERT_TRUE(handler_->OnResponseStarted(response.get(), &defer)); 480 ASSERT_TRUE(handler_->OnResponseStarted(response.get(), &defer));
469 base::TimeTicks now2 = base::TimeTicks::Now(); 481 base::TimeTicks now2 = base::TimeTicks::Now();
470 482
471 EXPECT_FALSE(defer); 483 EXPECT_FALSE(defer);
472 EXPECT_EQ(request_->creation_time(), response->head.request_start); 484 EXPECT_EQ(request_->creation_time(), response->head.request_start);
473 EXPECT_LE(now1, response->head.response_start); 485 EXPECT_LE(now1, response->head.response_start);
474 EXPECT_LE(response->head.response_start, now2); 486 EXPECT_LE(response->head.response_start, now2);
475 EXPECT_EQ(1, rdh_delegate_.num_on_response_started_calls()); 487 EXPECT_EQ(1, rdh_delegate_.num_on_response_started_calls());
476 488
477 url_loader_client_.RunUntilResponseReceived(); 489 url_loader_client_.RunUntilResponseReceived();
478 EXPECT_EQ(response->head.request_start, 490 EXPECT_EQ(response->head.request_start,
479 url_loader_client_.response_head().request_start); 491 url_loader_client_.response_head().request_start);
480 EXPECT_EQ(response->head.response_start, 492 EXPECT_EQ(response->head.response_start,
481 url_loader_client_.response_head().response_start); 493 url_loader_client_.response_head().response_start);
482 EXPECT_EQ(99, url_loader_client_.response_head().content_length); 494 EXPECT_EQ(99, url_loader_client_.response_head().content_length);
495
496 url_loader_client_.RunUntilCachedMetadataReceived();
497 EXPECT_EQ("hello",
498 std::string(reinterpret_cast<const char*>(
499 url_loader_client_.cached_metadata().data()),
500 url_loader_client_.cached_metadata().size()));
483 } 501 }
484 502
485 TEST_F(MojoAsyncResourceHandlerTest, OnWillReadAndInFlightRequests) { 503 TEST_F(MojoAsyncResourceHandlerTest, OnWillReadAndInFlightRequests) {
486 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted()); 504 ASSERT_TRUE(CallOnWillStartAndOnResponseStarted());
487 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing()); 505 EXPECT_EQ(0, rdh_.num_in_flight_requests_for_testing());
488 scoped_refptr<net::IOBuffer> io_buffer; 506 scoped_refptr<net::IOBuffer> io_buffer;
489 int io_buffer_size = 0; 507 int io_buffer_size = 0;
490 EXPECT_TRUE(handler_->OnWillRead(&io_buffer, &io_buffer_size, -1)); 508 EXPECT_TRUE(handler_->OnWillRead(&io_buffer, &io_buffer_size, -1));
491 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing()); 509 EXPECT_EQ(1, rdh_.num_in_flight_requests_for_testing());
492 handler_ = nullptr; 510 handler_ = nullptr;
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after
1271 } 1289 }
1272 } 1290 }
1273 EXPECT_EQ("B", body); 1291 EXPECT_EQ("B", body);
1274 } 1292 }
1275 1293
1276 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest, 1294 INSTANTIATE_TEST_CASE_P(MojoAsyncResourceHandlerWithAllocationSizeTest,
1277 MojoAsyncResourceHandlerWithAllocationSizeTest, 1295 MojoAsyncResourceHandlerWithAllocationSizeTest,
1278 ::testing::Values(8, 32 * 2014)); 1296 ::testing::Values(8, 32 * 2014));
1279 } // namespace 1297 } // namespace
1280 } // namespace content 1298 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/loader/mojo_async_resource_handler.cc ('k') | content/browser/loader/test_url_loader_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698