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

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

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

Powered by Google App Engine
This is Rietveld 408576698