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

Side by Side Diff: content/public/test/test_download_request_handler.h

Issue 1203983004: Stop using SpawnedTestServer in DownloadContentTest.* (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 5 years, 1 month 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
(Empty)
1 // Copyright 2015 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
5 #ifndef CONTENT_PUBLIC_TEST_TEST_DOWNLOAD_REQUEST_HANDLER_H_
6 #define CONTENT_PUBLIC_TEST_TEST_DOWNLOAD_REQUEST_HANDLER_H_
7
8 #include <stdint.h>
9 #include <queue>
10
11 #include "base/callback_forward.h"
12 #include "base/files/file.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "net/base/completion_callback.h"
17 #include "net/base/net_errors.h"
18 #include "net/http/http_byte_range.h"
19 #include "net/http/http_response_headers.h"
20 #include "net/http/http_util.h"
21 #include "net/url_request/url_request_job.h"
22 #include "url/gurl.h"
23
24 namespace content {
25
26 // A request handler that can be used to mock the behavior of a URLRequestJob
27 // for a download.
28 //
29 // Testing of download interruption scenarios typically involve simulating
30 // errors that occur:
31 // 1. On the client, prior to the request being sent out,
32 // 2. On the network, between the client and the server,
33 // 3. On the server,
34 // 4. Back on the client, while writing the response to disk,
35 // 5. On the client, after the response has been written to disk.
36 //
37 // This test class is meant to help test failures in #2 and #3 above. The test
38 // implementation depends on content::BrowserThread and assumes that the
39 // thread identified by BrowserThread::IO is the network task runner thread.
40 //
41 // To use the test request handler:
42 //
43 // // Define the request handler. Note that initialization of the
44 // // TestDownloadRequestHandler object immediately registers it as well and is
45 // // a blocking operation.
46 // TestDownloadRequestHandler request_handler;
47 //
48 // // Set up parameters for the partial request handler.
49 // TestDownloadRequestHandler::Parameters parameters;
50 //
51 // // Inject an error at offset 100.
52 // parameters.injected_errors.push(TestDownloadRequestHandler::InjectedError(
53 // 100, net::ERR_CONNECTION_RESET));
54 //
55 // // Start serving.
56 // request_handler.StartServing(parameters);
57 //
58 // At this point, you can initiate a URLRequest for request_handler.url(). The
59 // request will fail when offset 100 is reached with the error specified above.
60 class TestDownloadRequestHandler {
Randy Smith (Not in Mondays) 2015/11/17 22:21:42 Is there a comment as to the thread home of this c
asanka 2015/11/23 22:04:59 Added one. It can be used on any thread, however t
61 public:
62 using OnStartResponseCallback =
63 base::Callback<void(const std::string&, net::Error)>;
64
65 // A callback of this type can be used to intercept the Start() event of a new
Randy Smith (Not in Mondays) 2015/11/17 22:21:42 nit, suggestion: "this type" -> "type OnStartHandl
asanka 2015/11/23 22:04:59 Done.
66 // URLRequest. Set it as the |on_start_handler| member of Parameters below.
67 //
68 // The callback is invoked on the UI thread (since that's where test code
69 // usually runs). Once the callback has a response ready, it can invoke the
70 // OnStartResponseCallback object. The latter can be invoked on any thread and
71 // will post back to the IO thread to continue with processing the Start()
72 // event.
73 //
74 // The parameters to the OnStartResponseCallback are:
75 //
76 // * a |const std::string&| containing the headers to be sent in response to
77 // the request. The headers should be formatted according to the
78 // requirements of net::HttpUtil::AssembleRawHeaders(). The headers are only
79 // used if the |net::Error| parameters is net::OK.
80 //
81 // * a |net::Error| indicating the result of the operation. If this parameters
82 // is not net::OK, then that error value is set as the result of the Start()
83 // operation. The headers are ignored in this case.
84 //
85 // If the error is net::OK, and the headers are empty, then the request is
86 // handled based on the remaining parameters in |Parameters|.
87 using OnStartHandler = base::Callback<void(const net::HttpRequestHeaders&,
88 const OnStartResponseCallback&)>;
89
90 // An injected error.
91 struct InjectedError {
92 InjectedError(int64_t offset, net::Error error);
93
94 int64_t offset;
95 net::Error error;
96 };
97
98 // Parameters used by StartServing().
99 struct Parameters {
100 static Parameters WithSingleInterruption();
Randy Smith (Not in Mondays) 2015/11/17 22:21:42 I think a comment for this function would be good;
asanka 2015/11/23 22:04:59 Done.
101
102 // The default constructor initializes the parameters for serving a 100 KB
103 // resource with no interruptions. The response contains an ETag and a
104 // Last-Modified header and the server supports byte range requests.
105 Parameters();
106
107 ~Parameters();
108
109 // Clears the errors in injected_errors.
110 void ClearInjectedErrors();
111
112 // Contents of the ETag header field of the response. No Etag header is
113 // sent if this field is empty.
114 std::string etag;
115
116 // Contents of the Last-Modified header field of the response. No
117 // Last-Modified header is sent if this field is empty.
118 std::string last_modified;
119
120 // The Content-Type of the response. No Content-Type header is sent if this
121 // field is empty.
122 std::string content_type;
123
124 // The total size of the entity. If the entire entity is requested, then
125 // this would be the same as the value returned in the Content-Length
126 // header.
127 int64_t size;
128
129 // Seed for the pseudo-random sequence that defines the response body
130 // contents. The seed is with GetPatternBytes() to generate the body of the
131 // response.
132 int pattern_generator_seed;
133
134 // If true, the response contains a 'Accept-Ranges: bytes' header.
135 bool support_byte_ranges;
136
137 // If on_start_handler is valid, it will be invoked when a new request is
138 // received. See details about the OnStartHandler above.
139 OnStartHandler on_start_handler;
140
141 // Errors to be injected. Each injected error is defined by an offset and an
142 // error. Request handler will successfully fulfil requests to read up to
143 // |offset|. An attempt to read the byte at |offset| will result in the
144 // error defined by the InjectErrors object.
145 //
146 // If a read spans the range containing |offset|, then the portion of the
147 // request preceding |offset| will succeed. The next read would start at
148 // |offset| and hence would result in an error.
149 //
150 // E.g.: injected_errors.push(InjectedError(100, ERR_CONNECTION_RESET));
151 //
152 // A network read for 1024 bytes at offset 0 would result in successfully
153 // reading 100 bytes (bytes with offset 0-99). The next read would,
154 // therefore, start at offset 100 and would result in
155 // ERR_CONNECTION_RESET.
156 //
157 // Injected errors are considered in the order in which they appear in
158 // |injected_errors|. When handling a network read for the range [S,E]
159 // (inclusive), all events in |injected_errors| where |offset| is less than
160 // S will be ignored. Any remaining events will trigger an error once the
161 // sequence of reads proceeds to a point where |offset| is included in
162 // [S,E].
163 //
164 // This implies that |injected_errors| must be specified in increasing order
165 // of |offset|. I.e. |injected_errors| must be sorted by |offset|.
Randy Smith (Not in Mondays) 2015/11/17 22:21:43 I didn't actually get this implication; I thought
asanka 2015/11/23 22:04:59 I tried to clarify it a little bit. See if it make
166 //
167 // Errors at relative offset 0 are ignored for a partial request. I.e. If
168 // the request is for the byte range 100-200, then an error at offset 100
169 // will not trigger. This is done so that non-overlapping continuation
170 // attempts don't require reseting parameters to succeed.
171 //
172 // E.g.: If the caller injects an error at offset 100, then a request for
173 // the entire entity will fail after reading 100 bytes (offsets 0 through
174 // 99). A subsequent request for byte range "100-" (offsets 100 through EOF)
175 // will succeed since the error at offset 100 is ignored.
176 //
177 // Notes:
178 //
179 // * Distinctions about which read requests signal the error is often only
180 // important at the //net layer. From //content, it would appear that 100
181 // bytes were read and then request failed with ERR_CONNECTION_RESET.
182 std::queue<InjectedError> injected_errors;
183 };
184
185 // Details about completed requests returned by GetCompletedRequestInfo().
186 struct CompletedRequest {
187 int64_t transferred_content_length = -1;
Randy Smith (Not in Mondays) 2015/11/17 22:21:42 nit: I'm having an instinctive question as to whet
asanka 2015/11/23 22:04:59 Good point. I renamed this to transferred_byte_cou
188 net::HttpRequestHeaders request_headers;
189 };
190
191 using CompletedRequests = std::vector<CompletedRequest>;
192
193 // Registers a request handler at the default URL. Call url() to determine the
194 // URL.
195 //
196 // Notes:
197 // * This constructor is only meant to be used for convenience when the caller
198 // is not interested in the URL used for interception. The URL used is
199 // generated at run time and should not be assumed to be the same across
200 // different runs of the same test.
201 //
202 // * Initialization of the handler synchronously runs a task on the
203 // BrowserThread::IO thread using a nested message loop. Only construct an
204 // instance of this object after browser threads have been initialized.
205 TestDownloadRequestHandler();
206
207 // Similar to the default constructor, but registers the handler at |url|.
208 //
209 // Notes:
210 // * The behavior is undefined if more than one TestDownloadRequestHandler is
211 // registered for the same URL.
212 TestDownloadRequestHandler(const GURL& url);
213
214 // Destroys and posts a task to the IO thread to dismantle the registered URL
215 // request interceptor. Does not wait for the task to return.
216 ~TestDownloadRequestHandler();
217
218 // Returns the URL that this instance is intercepting URLRequests for.
219 const GURL& url() const { return url_; }
220
221 // Start responding to URLRequests for url() with responses based on
222 // |parameters|.
223 //
224 // This method invocation posts a task to the IO thread to update the
225 // URLRequestInterceptor with the new parameters and returns immediately. URL
226 // interception won't be updated until the posted task executes.
Randy Smith (Not in Mondays) 2015/11/17 22:21:42 I presume it's post-and-forget rather than blockin
asanka 2015/11/23 22:04:59 Post-and-forget. Added a short note.
227 //
228 // Calling this method does not affect URLRequests that have already started.
229 // The new parameters will only be used to respond to new URLRequests that are
230 // starting.
231 //
232 // StartServing() can be called multiple times to change the operating
233 // parameters of the current URL interceptor.
234 void StartServing(const Parameters& parameters);
235
236 // Start responding to URLRequests for url() with a static response
237 // containing the headers in |headers|.
238 //
239 // The format of |headers| should comply with the requirements for
240 // net::HttpUtil::AssembleRawHeaders().
241 void StartServingStaticResponse(const base::StringPiece& headers);
242
243 // Get the list of requests that have already completed.
244 //
245 // This method posts a task to the IO thread to collect the list of completed
246 // requests and waits for the task to complete.
247 //
248 // Requests that are currently in progress will not be reflected in
249 // |requests|.
250 void GetCompletedRequestInfo(CompletedRequests* requests);
251
252 // Generate a pseudo random pattern.
253 //
254 // |seed| is the seed for the pseudo random sequence. |offset| is the byte
255 // offset into the sequence. |length| is a count of bytes to generate.
256 // |data| receives the generated bytes and should be able to store |length|
257 // bytes.
258 //
259 // The pattern has the following properties:
260 //
261 // * For a given |seed|, the entire sequence of bytes is fixed. Any
262 // subsequence can be generated by specifying the |offset| and |length|.
263 //
264 // * The sequence is aperiodic (at least for the first 1M bytes).
265 //
266 // * |seed| is chaotic.
Randy Smith (Not in Mondays) 2015/11/17 22:21:42 I don't know what this means? I can sorta underst
asanka 2015/11/23 22:04:59 Updated comment. Hopefully it clarifies the commen
267 //
268 // These properties make the generated bytes useful for testing partial
269 // requests where the response may need to be built using a sequence of
270 // partial requests.
271 static void GetPatternBytes(int seed, int64 offset, int length, char* data);
272
273 private:
274 struct InterceptorProxy;
275
276 GURL url_;
277 scoped_ptr<InterceptorProxy> interceptor_proxy_;
278 DISALLOW_COPY_AND_ASSIGN(TestDownloadRequestHandler);
279 };
280
281 } // namespace content
282
283 #endif // CONTENT_PUBLIC_TEST_TEST_DOWNLOAD_REQUEST_HANDLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698