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