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