OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 // A URLRequestJob class that pulls the net and http headers from disk. | |
6 | |
7 #ifndef NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_ | |
8 #define NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_ | |
9 | |
10 #include <string> | |
11 | |
12 #include "base/macros.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "net/url_request/url_request_file_job.h" | |
16 #include "url/gurl.h" | |
17 | |
18 namespace base { | |
19 class FilePath; | |
20 class SequencedWorkerPool; | |
21 } | |
22 | |
23 namespace net { | |
24 class URLRequestInterceptor; | |
25 } | |
26 | |
27 namespace net { | |
28 | |
29 class URLRequestMockHTTPJob : public URLRequestFileJob { | |
30 public: | |
31 enum FailurePhase { | |
32 START = 0, | |
33 READ_ASYNC = 1, | |
34 READ_SYNC = 2, | |
35 MAX_FAILURE_PHASE = 3, | |
36 }; | |
37 | |
38 // Note that all file IO is done using |worker_pool|. | |
39 URLRequestMockHTTPJob(URLRequest* request, | |
40 NetworkDelegate* network_delegate, | |
41 const base::FilePath& file_path, | |
42 const scoped_refptr<base::TaskRunner>& task_runner); | |
43 | |
44 void Start() override; | |
45 bool ReadRawData(IOBuffer* buf, int buf_size, int* bytes_read) override; | |
46 bool GetMimeType(std::string* mime_type) const override; | |
47 int GetResponseCode() const override; | |
48 bool GetCharset(std::string* charset) override; | |
49 void GetResponseInfo(HttpResponseInfo* info) override; | |
50 bool IsRedirectResponse(GURL* location, int* http_status_code) override; | |
51 | |
52 // Adds the testing URLs to the URLRequestFilter, both under HTTP and HTTPS. | |
53 static void AddUrlHandlers( | |
54 const base::FilePath& base_path, | |
55 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); | |
56 | |
57 // Given the path to a file relative to the path passed to AddUrlHandler(), | |
58 // construct a mock URL. | |
59 static GURL GetMockUrl(const base::FilePath& path); | |
60 static GURL GetMockHttpsUrl(const base::FilePath& path); | |
61 | |
62 // Given the path to a file relative to the path passed to AddUrlHandler(), | |
63 // construct a mock URL that reports |net_error| at given |phase| of the | |
64 // request. Reporting |net_error| ERR_IO_PENDING results in a hung request. | |
65 static GURL GetMockUrlWithFailure(const base::FilePath& path, | |
66 FailurePhase phase, | |
67 int net_error); | |
68 | |
69 // Returns a URLRequestJobFactory::ProtocolHandler that serves | |
70 // URLRequestMockHTTPJob's responding like an HTTP server. |base_path| is the | |
71 // file path leading to the root of the directory to use as the root of the | |
72 // HTTP server. | |
73 static scoped_ptr<URLRequestInterceptor> CreateInterceptor( | |
74 const base::FilePath& base_path, | |
75 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); | |
76 | |
77 // Returns a URLRequestJobFactory::ProtocolHandler that serves | |
78 // URLRequestMockHTTPJob's responding like an HTTP server. It responds to all | |
79 // requests with the contents of |file|. | |
80 static scoped_ptr<URLRequestInterceptor> CreateInterceptorForSingleFile( | |
81 const base::FilePath& file, | |
82 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); | |
83 | |
84 protected: | |
85 ~URLRequestMockHTTPJob() override; | |
86 | |
87 private: | |
88 void GetResponseInfoConst(HttpResponseInfo* info) const; | |
89 void SetHeadersAndStart(const std::string& raw_headers); | |
90 // Checks query part of request url, and reports an error if it matches. | |
91 // Error is parsed out from the query and is reported synchronously. | |
92 // Reporting ERR_IO_PENDING results in a hung request. | |
93 // The "readasync" error is posted asynchronously. | |
94 bool MaybeReportErrorOnPhase(FailurePhase phase); | |
95 | |
96 std::string raw_headers_; | |
97 const scoped_refptr<base::TaskRunner> task_runner_; | |
98 | |
99 base::WeakPtrFactory<URLRequestMockHTTPJob> weak_ptr_factory_; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(URLRequestMockHTTPJob); | |
102 }; | |
103 | |
104 } // namespace net | |
105 | |
106 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_ | |
OLD | NEW |