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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 const scoped_refptr<base::TaskRunner>& task_runner); | 42 const scoped_refptr<base::TaskRunner>& task_runner); |
43 | 43 |
44 void Start() override; | 44 void Start() override; |
45 bool ReadRawData(IOBuffer* buf, int buf_size, int* bytes_read) override; | 45 bool ReadRawData(IOBuffer* buf, int buf_size, int* bytes_read) override; |
46 bool GetMimeType(std::string* mime_type) const override; | 46 bool GetMimeType(std::string* mime_type) const override; |
47 int GetResponseCode() const override; | 47 int GetResponseCode() const override; |
48 bool GetCharset(std::string* charset) override; | 48 bool GetCharset(std::string* charset) override; |
49 void GetResponseInfo(HttpResponseInfo* info) override; | 49 void GetResponseInfo(HttpResponseInfo* info) override; |
50 bool IsRedirectResponse(GURL* location, int* http_status_code) override; | 50 bool IsRedirectResponse(GURL* location, int* http_status_code) override; |
51 | 51 |
52 // Adds the testing URLs to the URLRequestFilter. | 52 // Adds the testing URLs to the URLRequestFilter, both under HTTP and HTTPS. |
53 static void AddUrlHandler( | 53 static void AddUrlHandlers( |
54 const base::FilePath& base_path, | 54 const base::FilePath& base_path, |
55 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); | 55 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); |
56 | 56 |
57 // Respond to all HTTP requests of |hostname| with contents of the file | 57 // Respond to all HTTP requests of |hostname| with contents of the file |
58 // located at |file_path|. | 58 // located at |file_path|. |
59 static void AddHostnameToFileHandler( | 59 static void AddHostnameToFileHandler( |
60 const std::string& hostname, | 60 const std::string& hostname, |
61 const base::FilePath& file, | 61 const base::FilePath& file, |
62 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); | 62 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); |
63 | 63 |
64 // Given the path to a file relative to the path passed to AddUrlHandler(), | 64 // Given the path to a file relative to the path passed to AddUrlHandler(), |
65 // construct a mock URL. | 65 // construct a mock URL. |
66 static GURL GetMockUrl(const base::FilePath& path); | 66 static GURL GetMockUrl(const base::FilePath& path); |
| 67 static GURL GetMockHttpsUrl(const base::FilePath& path); |
67 | 68 |
68 // Given the path to a file relative to the path passed to AddUrlHandler(), | 69 // Given the path to a file relative to the path passed to AddUrlHandler(), |
69 // construct a mock URL that reports |net_error| at given |phase| of the | 70 // construct a mock URL that reports |net_error| at given |phase| of the |
70 // request. Reporting |net_error| ERR_IO_PENDING results in a hung request. | 71 // request. Reporting |net_error| ERR_IO_PENDING results in a hung request. |
71 static GURL GetMockUrlWithFailure(const base::FilePath& path, | 72 static GURL GetMockUrlWithFailure(const base::FilePath& path, |
72 FailurePhase phase, | 73 FailurePhase phase, |
73 int net_error); | 74 int net_error); |
74 | 75 |
75 // Returns a URLRequestJobFactory::ProtocolHandler that serves | 76 // Returns a URLRequestJobFactory::ProtocolHandler that serves |
76 // URLRequestMockHTTPJob's responding like an HTTP server. |base_path| is the | 77 // URLRequestMockHTTPJob's responding like an HTTP server. |base_path| is the |
77 // file path leading to the root of the directory to use as the root of the | 78 // file path leading to the root of the directory to use as the root of the |
78 // HTTP server. | 79 // HTTP server. |
79 static scoped_ptr<URLRequestInterceptor> CreateInterceptor( | 80 static scoped_ptr<URLRequestInterceptor> CreateInterceptor( |
80 const base::FilePath& base_path, | 81 const base::FilePath& base_path, |
81 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); | 82 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); |
82 | 83 |
83 // Returns a URLRequestJobFactory::ProtocolHandler that serves | 84 // Returns a URLRequestJobFactory::ProtocolHandler that serves |
84 // URLRequestMockHTTPJob's responding like an HTTP server. It responds to all | 85 // URLRequestMockHTTPJob's responding like an HTTP server. It responds to all |
85 // requests with the contents of |file|. | 86 // requests with the contents of |file|. |
86 static scoped_ptr<URLRequestInterceptor> CreateInterceptorForSingleFile( | 87 static scoped_ptr<URLRequestInterceptor> CreateInterceptorForSingleFile( |
87 const base::FilePath& file, | 88 const base::FilePath& file, |
88 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); | 89 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); |
89 | 90 |
90 protected: | 91 protected: |
91 ~URLRequestMockHTTPJob() override; | 92 ~URLRequestMockHTTPJob() override; |
92 | 93 |
93 private: | 94 private: |
| 95 static GURL GetMockUrlForScheme(const base::FilePath& path, |
| 96 const std::string& scheme); |
94 void GetResponseInfoConst(HttpResponseInfo* info) const; | 97 void GetResponseInfoConst(HttpResponseInfo* info) const; |
95 void SetHeadersAndStart(const std::string& raw_headers); | 98 void SetHeadersAndStart(const std::string& raw_headers); |
96 // Checks query part of request url, and reports an error if it matches. | 99 // Checks query part of request url, and reports an error if it matches. |
97 // Error is parsed out from the query and is reported synchronously. | 100 // Error is parsed out from the query and is reported synchronously. |
98 // Reporting ERR_IO_PENDING results in a hung request. | 101 // Reporting ERR_IO_PENDING results in a hung request. |
99 // The "readasync" error is posted asynchronously. | 102 // The "readasync" error is posted asynchronously. |
100 bool MaybeReportErrorOnPhase(FailurePhase phase); | 103 bool MaybeReportErrorOnPhase(FailurePhase phase); |
101 | 104 |
102 std::string raw_headers_; | 105 std::string raw_headers_; |
103 const scoped_refptr<base::TaskRunner> task_runner_; | 106 const scoped_refptr<base::TaskRunner> task_runner_; |
104 | 107 |
105 base::WeakPtrFactory<URLRequestMockHTTPJob> weak_ptr_factory_; | 108 base::WeakPtrFactory<URLRequestMockHTTPJob> weak_ptr_factory_; |
106 | 109 |
107 DISALLOW_COPY_AND_ASSIGN(URLRequestMockHTTPJob); | 110 DISALLOW_COPY_AND_ASSIGN(URLRequestMockHTTPJob); |
108 }; | 111 }; |
109 | 112 |
110 } // namespace net | 113 } // namespace net |
111 | 114 |
112 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_ | 115 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_ |
OLD | NEW |