blob: d453d6766260b71055be12615e84db2702a79dd3 [file] [log] [blame]
yhiranof6c76cf2016-12-22 11:42:181// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
John Abd-El-Malek6b56ef712017-10-21 22:52:465#include "content/renderer/loader/test_request_peer.h"
yhiranof6c76cf2016-12-22 11:42:186
John Abd-El-Malek6b56ef712017-10-21 22:52:467#include "content/renderer/loader/resource_dispatcher.h"
yhiranof6c76cf2016-12-22 11:42:188#include "net/url_request/redirect_info.h"
Lucas Furukawa Gadani99701f72019-10-08 16:31:559#include "services/network/public/mojom/url_response_head.mojom.h"
yhiranof6c76cf2016-12-22 11:42:1810#include "testing/gtest/include/gtest/gtest.h"
Blink Reformata30d4232018-04-07 15:31:0611#include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h"
yhiranof6c76cf2016-12-22 11:42:1812
13namespace content {
14
15TestRequestPeer::TestRequestPeer(ResourceDispatcher* dispatcher,
16 Context* context)
17 : dispatcher_(dispatcher), context_(context) {}
18
19TestRequestPeer::~TestRequestPeer() = default;
20
21void TestRequestPeer::OnUploadProgress(uint64_t position, uint64_t size) {
22 EXPECT_FALSE(context_->complete);
23}
24
Randy Smithff094aa2018-01-17 18:21:2725bool TestRequestPeer::OnReceivedRedirect(
26 const net::RedirectInfo& redirect_info,
Yoav Weiss3c2baf62020-05-13 13:22:4427 network::mojom::URLResponseHeadPtr head,
28 std::vector<std::string>*) {
yhiranof6c76cf2016-12-22 11:42:1829 EXPECT_FALSE(context_->cancelled);
30 EXPECT_FALSE(context_->complete);
31 ++context_->seen_redirects;
Lucas Furukawa Gadani99701f72019-10-08 16:31:5532 context_->last_load_timing = head->load_timing;
yhiranof6c76cf2016-12-22 11:42:1833 if (context_->defer_on_redirect)
34 dispatcher_->SetDefersLoading(context_->request_id, true);
35 return context_->follow_redirects;
36}
37
Randy Smithff094aa2018-01-17 18:21:2738void TestRequestPeer::OnReceivedResponse(
Lucas Furukawa Gadani99701f72019-10-08 16:31:5539 network::mojom::URLResponseHeadPtr head) {
yhiranof6c76cf2016-12-22 11:42:1840 EXPECT_FALSE(context_->cancelled);
41 EXPECT_FALSE(context_->received_response);
42 EXPECT_FALSE(context_->complete);
43 context_->received_response = true;
Lucas Furukawa Gadani99701f72019-10-08 16:31:5544 context_->last_load_timing = head->load_timing;
yhiranof6c76cf2016-12-22 11:42:1845 if (context_->cancel_on_receive_response) {
Hajime Hoshi5d6642b02018-01-18 07:36:2946 dispatcher_->Cancel(
47 context_->request_id,
48 blink::scheduler::GetSingleThreadTaskRunnerForTesting());
yhiranof6c76cf2016-12-22 11:42:1849 context_->cancelled = true;
50 }
51}
52
Marijn Kruisselbrink090b1a4f2018-03-30 19:54:1253void TestRequestPeer::OnStartLoadingResponseBody(
54 mojo::ScopedDataPipeConsumerHandle body) {
Makoto Shimazuc0a29172019-01-25 08:55:3955 if (context_->cancelled)
56 return;
Marijn Kruisselbrink090b1a4f2018-03-30 19:54:1257 EXPECT_TRUE(context_->received_response);
Marijn Kruisselbrink090b1a4f2018-03-30 19:54:1258 EXPECT_FALSE(context_->complete);
Makoto Shimazu494ae7b52019-01-10 09:35:3259 context_->body_handle = std::move(body);
Marijn Kruisselbrink090b1a4f2018-03-30 19:54:1260}
61
yhiranof6c76cf2016-12-22 11:42:1862void TestRequestPeer::OnTransferSizeUpdated(int transfer_size_diff) {
63 EXPECT_TRUE(context_->received_response);
64 EXPECT_FALSE(context_->complete);
65 if (context_->cancelled)
66 return;
67 context_->total_encoded_data_length += transfer_size_diff;
yhirano6a0e9c72016-12-27 08:31:0468 if (context_->defer_on_transfer_size_updated)
69 dispatcher_->SetDefersLoading(context_->request_id, true);
yhiranof6c76cf2016-12-22 11:42:1870}
71
Bill Budge4405d8d52019-08-22 16:45:4572void TestRequestPeer::OnReceivedCachedMetadata(mojo_base::BigBuffer data) {
yhirano2a525f92017-01-18 08:16:4673 EXPECT_TRUE(context_->received_response);
74 EXPECT_FALSE(context_->complete);
75 if (context_->cancelled)
76 return;
Bill Budge4405d8d52019-08-22 16:45:4577 context_->cached_metadata = std::move(data);
yhirano2a525f92017-01-18 08:16:4678}
79
Takashi Toyoshimaf8716922017-11-08 04:40:5880void TestRequestPeer::OnCompletedRequest(
Takashi Toyoshimaaa278662017-11-20 11:11:2681 const network::URLLoaderCompletionStatus& status) {
yhiranof6c76cf2016-12-22 11:42:1882 if (context_->cancelled)
83 return;
84 EXPECT_TRUE(context_->received_response);
85 EXPECT_FALSE(context_->complete);
86 context_->complete = true;
Yutaka Hirano8004bde2018-05-17 14:37:3187 context_->completion_status = status;
yhiranof6c76cf2016-12-22 11:42:1888}
89
Lucas Furukawa Gadanid51ff5d62018-12-07 21:26:4990scoped_refptr<base::TaskRunner> TestRequestPeer::GetTaskRunner() {
Yutaka Hiranocecd4c6c2018-11-12 10:27:2991 return blink::scheduler::GetSingleThreadTaskRunnerForTesting();
92}
93
yhiranof6c76cf2016-12-22 11:42:1894TestRequestPeer::Context::Context() = default;
95TestRequestPeer::Context::~Context() = default;
96
97} // namespace content