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_ | |
6 #define CONTENT_PUBLIC_TEST_TEST_DOWNLOAD_REQUEST_HANDLER_H_ | |
7 | |
8 #include <stdint.h> | |
9 #include <queue> | |
10 | |
11 #include "base/files/file.h" | |
12 #include "base/macros.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/memory/weak_ptr.h" | |
15 #include "net/base/net_errors.h" | |
16 #include "net/http/http_byte_range.h" | |
17 #include "net/http/http_response_headers.h" | |
18 #include "net/http/http_util.h" | |
19 #include "net/url_request/url_request_job.h" | |
20 #include "url/gurl.h" | |
21 | |
22 namespace content { | |
23 | |
24 // A request handler that can be used to mock the behavior a URLRequestJob for a | |
Randy Smith (Not in Mondays)
2015/11/12 00:57:57
nit: "of a"?
asanka
2015/11/13 21:40:02
Done.
| |
25 // download. | |
26 // | |
27 // Testing of download interruption scenarios typically involve simulating | |
28 // errors that occur: | |
29 // 1. on the client, prior to the request being sent out, | |
30 // 2. on the network, between the client and the server, | |
31 // 3. on the server, | |
32 // 4. back on the client, while writing the response to disk, | |
33 // 5. on the client, after the response has been written to disk. | |
34 // | |
35 // This test class is meant to help test failures in #2 and #3 above. The test | |
Randy Smith (Not in Mondays)
2015/11/12 00:57:57
Wouldn't errors on the server normally show up wit
asanka
2015/11/13 21:40:02
Currently, yes. But it's also controlling the enti
| |
36 // implementation depends on content::BrowserThread and assumes that the | |
37 // thread identified by BrowserThread::IO is the network task runner thread. | |
38 // | |
39 // To use the test request handler: | |
40 // | |
41 // // Define the request handler. Note that initialiation of the | |
42 // // TestDownloadRequestHandler object immediately registers it as well and is | |
43 // // a blocking operation. | |
44 // TestDownloadRequestHandler request_handler; | |
45 // | |
46 // // Set up parameters for the partial request handler. | |
47 // TestDownloadRequestHandler::Parameters parameters; | |
48 // | |
49 // // Inject an error at offset 100. | |
50 // parameters.injected_errors.push(TestDownloadRequestHandler::InjectedError( | |
51 // 100, net::ERR_CONNECTION_RESET)); | |
52 // | |
53 // // Start serving. | |
54 // request_handler.StartServing(parameters); | |
55 // | |
56 // At this point, you can initiate a URLRequest for request_handler.url(). The | |
57 // request will fail when offset 100 is reached with the error specified above. | |
58 class TestDownloadRequestHandler { | |
59 public: | |
60 // An injected error. | |
61 struct InjectedError { | |
62 InjectedError(int64_t offset, net::Error error); | |
63 | |
64 int64_t offset; | |
65 net::Error error; | |
66 }; | |
67 | |
68 // Parameters used by StartServing(). | |
69 struct Parameters { | |
70 static Parameters WithSingleInterruption(); | |
71 | |
72 Parameters(const Parameters& other); | |
Randy Smith (Not in Mondays)
2015/11/12 00:57:57
suggestion: I'm tempted to ask for a comment calli
asanka
2015/11/13 21:40:02
Thanks for calling this out. During an intermediat
Randy Smith (Not in Mondays)
2015/11/17 22:21:42
I read the style guide as recommending but not req
asanka
2015/11/23 22:04:59
Thanks for pointing this out. I've added the defau
| |
73 Parameters(const std::string& etag, | |
74 const std::string& last_modified, | |
75 const std::string& content_type, | |
76 int64_t size, | |
77 int pattern_generator_seed, | |
78 bool support_byte_ranges); | |
79 | |
80 // The default constructor initializes the parameters for serving a 100 KB | |
81 // resource with no interruptions. The response contains an ETag and a | |
82 // Last-Modified header and the server supports byte range requests. | |
83 Parameters(); | |
84 | |
85 ~Parameters(); | |
86 | |
87 // Clears the errors in injected_errors. | |
88 void ClearInjectedErrors(); | |
89 | |
90 // Contents of the ETag header field of the response. No Etag header is | |
91 // sent if this field is empty. | |
92 std::string etag; | |
93 | |
94 // Contents of the Last-Modified header field of the response. No | |
95 // Last-Modified header is sent if this field is empty. | |
96 std::string last_modified; | |
97 | |
98 // The Content-Type of the response. No Content-Type header is sent if this | |
99 // field is empty. | |
100 std::string content_type; | |
101 | |
102 // The total size of the entity. If the entire entity is requested, then | |
103 // this would be the same as the value returned in the Content-Length | |
104 // header. | |
105 int64_t size; | |
106 | |
107 // Seed for the pseudo-random sequence that defines the response body | |
108 // contents. The seed is with GetPatternBytes() to generate the body of the | |
109 // response. | |
110 int pattern_generator_seed; | |
111 | |
112 // If true, the response contains a 'Accept-Ranges: bytes' header. | |
113 bool support_byte_ranges; | |
114 | |
115 // Errors to be injected. Each injected error is defined by an offset and an | |
116 // error. Request handler will successfully fulfil requests to read up to | |
117 // |offset|. An attempt to read the byte at |offset| will result in the | |
118 // error defined by the InjectErrors object. | |
119 // | |
120 // Note that if a read spans the range containing |offset|, then the portion | |
121 // of the request preceding |offset| will succeed. The next read would start | |
122 // at |offset| and hence would result in an error. | |
123 // | |
124 // E.g.: injected_errors.push(InjectedError(100, ERR_CONNECTION_RESET)); | |
125 // | |
126 // A network read for 1024 bytes at offset 0 would result in successfully | |
127 // reading 100 bytes (bytes with offset 0-99). The next read would, | |
128 // therefore, start at offset 100 and would result in | |
129 // ERR_CONNECTION_RESET. | |
130 // | |
131 // Note that distinctions about which read requests signal the error is | |
132 // often only important at the //net layer. From //content, it would appear | |
133 // that 100 bytes were read and then request failed with | |
134 // ERR_CONNECTION_RESET. | |
135 std::queue<InjectedError> injected_errors; | |
136 }; | |
137 | |
138 // Details about completed requests returned by GetCompletedRequestInfo(). | |
139 struct CompletedRequest { | |
140 int64_t transferred_content_length = -1; | |
141 net::HttpRequestHeaders request_headers; | |
142 }; | |
143 | |
144 using CompletedRequests = std::vector<CompletedRequest>; | |
145 | |
146 // Registers a request handler at the default URL. Call url() to determine the | |
147 // URL. | |
148 // | |
149 // Notes: | |
150 // * The URL used is a constant. Constructing multiple instances of this | |
151 // for the same target URL results in unspecified behavior. (The result is | |
152 // known, but don't do it). | |
Randy Smith (Not in Mondays)
2015/11/12 00:57:57
Suggestion: Can you assert/dcheck if they do that?
asanka
2015/11/13 21:40:02
On debug builds, using the same URL to register tw
| |
153 // | |
154 // * Initialization of the handler synchronously runs a task on the | |
155 // BrowserThread::IO thread using a nested message loop. Only construct an | |
156 // instance of this object after browser threads have been initialized. | |
157 TestDownloadRequestHandler(); | |
158 | |
159 // Similar to the default constructor, but registers the handler at |url|. | |
160 TestDownloadRequestHandler(const GURL& url); | |
161 | |
162 // Destroys and posts a task to the IO thread to dismantle the registered URL | |
163 // request interceptor. Does not wait for the task to return. | |
164 ~TestDownloadRequestHandler(); | |
165 | |
166 // Returns the URL that this instance is intercepting URLRequests for. | |
167 const GURL& url() const { return url_; } | |
168 | |
169 // Start responding to URLRequests for url() with responses based on | |
170 // |parameters|. | |
171 // | |
172 // This method invocation posts a task to the IO thread to update the | |
173 // URLRequestInterceptor with the new parameters and returns immediately. URL | |
174 // interception won't be updated until the posted task executes. | |
175 // | |
176 // Calling this method does not affect URLRequests that have already started. | |
177 // The new parameters will only be used to respond to new URLRequests that are | |
178 // starting. | |
179 // | |
180 // StartServing() can be called multiple times to change the operating | |
181 // parameters of the current URL interceptor. | |
182 void StartServing(const Parameters& parameters); | |
183 | |
184 // Start responding to URLRuequests for url() with a static response | |
185 // containing the headers in |headers|. | |
186 // | |
187 // The format of |headers| should comply with the requirements for | |
188 // net::HttpUtil::AssembleRawHeaders(). | |
189 void StartServingStaticResponse(const base::StringPiece& headers); | |
190 | |
191 // Get the list of requests that have already completed. | |
192 // | |
193 // This method posts a task to the IO thread to collect the list of completed | |
194 // requests and waits for the task to complete. | |
195 // | |
196 // Requests that are currently in progress will not be reflected in | |
197 // |requests|. | |
198 void GetCompletedRequestInfo(CompletedRequests* requests); | |
199 | |
200 // Generate a pseudorandom pattern. | |
201 // | |
202 // |seed| is the seed for the pseudorandom sequence. |offset| is the byte | |
203 // offset into the sequence. |length| is a count of bytes to generate. | |
204 // |data| receives the generated bytes and should be able to store |length| | |
205 // bytes. | |
206 // | |
207 // The pattern has the following properties: | |
208 // | |
209 // * For a given |seed|, the entire sequence of bytes is fixed. Any | |
210 // subsequence can be generated by specifying the |offset| and |length|. | |
211 // | |
212 // * The sequence is aperiodic (at least for the first 1M bytes). | |
213 // | |
214 // * |seed| is chaotic. | |
215 // | |
216 // These properties make the generated bytes useful for testing partial | |
217 // requests where the response may need to be built using a sequence of | |
218 // partial requests. | |
219 static void GetPatternBytes(int seed, int64 offset, int length, char* data); | |
220 | |
221 private: | |
222 struct InterceptorProxy; | |
223 | |
224 GURL url_; | |
225 scoped_ptr<InterceptorProxy> interceptor_proxy_; | |
226 DISALLOW_COPY_AND_ASSIGN(TestDownloadRequestHandler); | |
227 }; | |
228 | |
229 } // namespace content | |
230 | |
231 #endif // CONTENT_PUBLIC_TEST_TEST_DOWNLOAD_REQUEST_HANDLER_H_ | |
OLD | NEW |