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

Side by Side Diff: content/browser/loader/test_resource_handler.h

Issue 2557433005: Revert of Fix a pair of ResourceLoader cancellation/error bugs. (Closed)
Patch Set: Created 4 years 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 #ifndef CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_ 5 #ifndef CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_
6 #define CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_ 6 #define CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_
7 7
8 #include <memory>
9 #include <string> 8 #include <string>
10 9
11 #include "base/macros.h" 10 #include "base/macros.h"
12 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
13 #include "base/run_loop.h"
14 #include "content/browser/loader/resource_handler.h" 12 #include "content/browser/loader/resource_handler.h"
15 #include "net/base/io_buffer.h" 13 #include "net/base/io_buffer.h"
16 #include "net/base/net_errors.h" 14
17 #include "net/url_request/url_request_status.h" 15 class GURL;
18 #include "url/gurl.h"
19 16
20 namespace net { 17 namespace net {
21 class URLRequestStatus; 18 class URLRequestStatus;
22 } 19 }
23 20
24 namespace content { 21 namespace content {
25 22
26 class ResourceController;
27 class ResourceHandler; 23 class ResourceHandler;
28 struct ResourceResponse; 24 struct ResourceResponse;
29 25
30 // A test version of a ResourceHandler. It returns a configurable buffer in 26 // A test version of a ResourceHandler. It returns a configurable buffer in
31 // response to OnWillStart. It records what ResourceHandler methods are called, 27 // response to OnWillStart. It records what ResourceHandler methods are called,
32 // and verifies that they are called in the correct order. It can optionally 28 // and verifies that they are called in the correct order. It can optionally
33 // defer or fail the request at any stage, and record the response body and 29 // defer or fail the request at any stage, and record the response body and
34 // final status it sees. Redirects currently not supported. 30 // final status it sees. Redirects currently not supported.
35 class TestResourceHandler : public ResourceHandler { 31 class TestResourceHandler : public ResourceHandler {
36 public: 32 public:
(...skipping 12 matching lines...) Expand all
49 bool OnResponseStarted(ResourceResponse* response, bool* defer) override; 45 bool OnResponseStarted(ResourceResponse* response, bool* defer) override;
50 bool OnWillStart(const GURL& url, bool* defer) override; 46 bool OnWillStart(const GURL& url, bool* defer) override;
51 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, 47 bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
52 int* buf_size, 48 int* buf_size,
53 int min_size) override; 49 int min_size) override;
54 bool OnReadCompleted(int bytes_read, bool* defer) override; 50 bool OnReadCompleted(int bytes_read, bool* defer) override;
55 void OnResponseCompleted(const net::URLRequestStatus& status, 51 void OnResponseCompleted(const net::URLRequestStatus& status,
56 bool* defer) override; 52 bool* defer) override;
57 void OnDataDownloaded(int bytes_downloaded) override; 53 void OnDataDownloaded(int bytes_downloaded) override;
58 54
59 // Invoke the corresponding methods on the ResourceHandler's
60 // ResourceController.
61 void Resume();
62 void CancelWithError(net::Error net_error);
63
64 // Sets the size of the read buffer returned by OnWillRead. Releases reference 55 // Sets the size of the read buffer returned by OnWillRead. Releases reference
65 // to previous read buffer. Default size is 2048 bytes. 56 // to previous read buffer. Default size is 2048 bytes.
66 void SetBufferSize(int buffer_size); 57 void SetBufferSize(int buffer_size);
67 58
68 scoped_refptr<net::IOBuffer> buffer() const { return buffer_; } 59 scoped_refptr<net::IOBuffer> buffer() const { return buffer_; }
69 60
70 // Sets the result returned by each method. All default to returning true. 61 // Sets the result returned by each method. All default to returning true.
71 void set_on_will_start_result(bool on_will_start_result) { 62 void set_on_will_start_result(bool on_will_start_result) {
72 on_will_start_result_ = on_will_start_result; 63 on_will_start_result_ = on_will_start_result;
73 } 64 }
74 void set_on_request_redirected_result(bool on_request_redirected_result) {
75 on_request_redirected_result_ = on_request_redirected_result;
76 }
77 void set_on_response_started_result(bool on_response_started_result) { 65 void set_on_response_started_result(bool on_response_started_result) {
78 on_response_started_result_ = on_response_started_result; 66 on_response_started_result_ = on_response_started_result;
79 } 67 }
80 void set_on_will_read_result(bool on_will_read_result) { 68 void set_on_will_read_result(bool on_will_read_result) {
81 on_will_read_result_ = on_will_read_result; 69 on_will_read_result_ = on_will_read_result;
82 } 70 }
83 void set_on_read_completed_result(bool on_read_completed_result) { 71 void set_on_read_completed_result(bool on_read_completed_result) {
84 on_read_completed_result_ = on_read_completed_result; 72 on_read_completed_result_ = on_read_completed_result;
85 } 73 }
86 void set_on_on_read_eof_result(bool on_on_read_eof_result) {
87 on_on_read_eof_result_ = on_on_read_eof_result;
88 }
89 74
90 // Cause |defer| to be set to true when the specified method is invoked. The 75 // Cause |defer| to be set to true when the specified method is invoked. The
91 // test itself is responsible for resuming the request after deferral. 76 // test itself is responsible for resuming the request after deferral.
92 77
93 void set_defer_on_will_start(bool defer_on_will_start) { 78 void set_defer_on_will_start(bool defer_on_will_start) {
94 defer_on_will_start_ = defer_on_will_start; 79 defer_on_will_start_ = defer_on_will_start;
95 } 80 }
96 void set_defer_on_request_redirected(bool defer_on_request_redirected) {
97 defer_on_request_redirected_ = defer_on_request_redirected;
98 }
99 void set_defer_on_response_started(bool defer_on_response_started) { 81 void set_defer_on_response_started(bool defer_on_response_started) {
100 defer_on_response_started_ = defer_on_response_started; 82 defer_on_response_started_ = defer_on_response_started;
101 } 83 }
102 // Only the next OnReadCompleted call will set |defer| to true. 84 // Only the next OnReadCompleted call will set |defer| to true.
103 void set_defer_on_read_completed(bool defer_on_read_completed) { 85 void set_defer_on_read_completed(bool defer_on_read_completed) {
104 defer_on_read_completed_ = defer_on_read_completed; 86 defer_on_read_completed_ = defer_on_read_completed;
105 } 87 }
106 // The final-byte read will set |defer| to true.
107 void set_defer_on_read_eof(bool defer_on_read_eof) {
108 defer_on_read_eof_ = defer_on_read_eof;
109 }
110 void set_defer_on_response_completed(bool defer_on_response_completed) { 88 void set_defer_on_response_completed(bool defer_on_response_completed) {
111 defer_on_response_completed_ = defer_on_response_completed; 89 defer_on_response_completed_ = defer_on_response_completed;
112 } 90 }
113 91
114 // Set if OnDataDownloaded calls are expected instead of
115 // OnWillRead/OnReadCompleted.
116 void set_expect_on_data_downloaded(bool expect_on_data_downloaded) {
117 expect_on_data_downloaded_ = expect_on_data_downloaded;
118 }
119
120 // Sets whether to expect a final 0-byte read on success. Defaults to true.
121 void set_expect_eof_read(bool expect_eof_read) {
122 expect_eof_read_ = expect_eof_read;
123 }
124
125 // Return the number of times the corresponding method was invoked. 92 // Return the number of times the corresponding method was invoked.
126 93
127 int on_will_start_called() const { return on_will_start_called_; } 94 int on_will_start_called() const { return on_will_start_called_; }
128 int on_request_redirected_called() const { 95 // Redirection currently not supported.
129 return on_request_redirected_called_; 96 int on_request_redirected_called() const { return 0; }
130 }
131 int on_response_started_called() const { return on_response_started_called_; } 97 int on_response_started_called() const { return on_response_started_called_; }
132 int on_will_read_called() const { return on_will_read_called_; } 98 int on_will_read_called() const { return on_will_read_called_; }
133 int on_read_completed_called() const { return on_read_completed_called_; } 99 int on_read_completed_called() const { return on_read_completed_called_; }
134 int on_read_eof() const { return on_read_eof_; }
135 int on_response_completed_called() const { 100 int on_response_completed_called() const {
136 return on_response_completed_called_; 101 return on_response_completed_called_;
137 } 102 }
138 103
139 // URL passed to OnResponseStarted, if it was called.
140 const GURL& start_url() const { return start_url_; }
141
142 ResourceResponse* resource_response() { return resource_response_.get(); };
143
144 int total_bytes_downloaded() const { return total_bytes_downloaded_; }
145
146 const std::string& body() const { return body_; }
147 net::URLRequestStatus final_status() const { return final_status_; }
148
149 // Spins the message loop until the request is deferred. Using this is
150 // optional, but if used, must use it exclusively to wait for the request. If
151 // the request was deferred and then resumed/canceled without calling this
152 // method, behavior is undefined.
153 void WaitUntilDeferred();
154
155 void WaitUntilResponseComplete();
156
157 private: 104 private:
158 // TODO(mmenke): Remove these, in favor of final_status_ and body_. 105 net::URLRequestStatus* request_status_;
159 net::URLRequestStatus* request_status_ptr_; 106 std::string* body_;
160 std::string* body_ptr_;
161
162 scoped_refptr<net::IOBuffer> buffer_; 107 scoped_refptr<net::IOBuffer> buffer_;
163 size_t buffer_size_; 108 size_t buffer_size_;
164 109
165 ResourceController* controller_;
166
167 bool on_will_start_result_ = true; 110 bool on_will_start_result_ = true;
168 bool on_request_redirected_result_ = true;
169 bool on_response_started_result_ = true; 111 bool on_response_started_result_ = true;
170 bool on_will_read_result_ = true; 112 bool on_will_read_result_ = true;
171 bool on_read_completed_result_ = true; 113 bool on_read_completed_result_ = true;
172 bool on_on_read_eof_result_ = true;
173 114
174 bool defer_on_will_start_ = false; 115 bool defer_on_will_start_ = false;
175 bool defer_on_request_redirected_ = false;
176 bool defer_on_response_started_ = false; 116 bool defer_on_response_started_ = false;
177 bool defer_on_read_completed_ = false; 117 bool defer_on_read_completed_ = false;
178 bool defer_on_read_eof_ = false;
179 bool defer_on_response_completed_ = false; 118 bool defer_on_response_completed_ = false;
180 119
181 bool expect_on_data_downloaded_ = false;
182
183 bool expect_eof_read_ = true;
184
185 int on_will_start_called_ = 0; 120 int on_will_start_called_ = 0;
186 int on_request_redirected_called_ = 0;
187 int on_response_started_called_ = 0; 121 int on_response_started_called_ = 0;
188 int on_will_read_called_ = 0; 122 int on_will_read_called_ = 0;
189 int on_read_completed_called_ = 0; 123 int on_read_completed_called_ = 0;
190 int on_read_eof_ = 0;
191 int on_response_completed_called_ = 0; 124 int on_response_completed_called_ = 0;
192 125
193 GURL start_url_;
194 scoped_refptr<ResourceResponse> resource_response_;
195 int total_bytes_downloaded_ = 0;
196 std::string body_;
197 net::URLRequestStatus final_status_ =
198 net::URLRequestStatus::FromError(net::ERR_UNEXPECTED);
199 bool canceled_ = false;
200
201 std::unique_ptr<base::RunLoop> deferred_run_loop_;
202
203 base::RunLoop response_complete_run_loop_;
204
205 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler); 126 DISALLOW_COPY_AND_ASSIGN(TestResourceHandler);
206 }; 127 };
207 128
208 } // namespace content 129 } // namespace content
209 130
210 #endif // CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_ 131 #endif // CONTENT_BROWSER_LOADER_TEST_RESOURCE_HANDLER_H_
OLDNEW
« no previous file with comments | « content/browser/loader/resource_loader_unittest.cc ('k') | content/browser/loader/test_resource_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698