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 // Note that all file IO is done using |worker_pool|. |
| 32 URLRequestMockHTTPJob( |
| 33 URLRequest* request, |
| 34 NetworkDelegate* network_delegate, |
| 35 const base::FilePath& file_path, |
| 36 const scoped_refptr<base::TaskRunner>& task_runner); |
| 37 |
| 38 virtual void Start() OVERRIDE; |
| 39 virtual bool GetMimeType(std::string* mime_type) const OVERRIDE; |
| 40 virtual int GetResponseCode() const OVERRIDE; |
| 41 virtual bool GetCharset(std::string* charset) OVERRIDE; |
| 42 virtual void GetResponseInfo(HttpResponseInfo* info) OVERRIDE; |
| 43 virtual bool IsRedirectResponse(GURL* location, |
| 44 int* http_status_code) OVERRIDE; |
| 45 |
| 46 // Adds the testing URLs to the URLRequestFilter. |
| 47 static void AddUrlHandler( |
| 48 const base::FilePath& base_path, |
| 49 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); |
| 50 |
| 51 // Respond to all HTTP requests of |hostname| with contents of the file |
| 52 // located at |file_path|. |
| 53 static void AddHostnameToFileHandler( |
| 54 const std::string& hostname, |
| 55 const base::FilePath& file, |
| 56 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); |
| 57 |
| 58 // Given the path to a file relative to the path passed to AddUrlHandler(), |
| 59 // construct a mock URL. |
| 60 static GURL GetMockUrl(const base::FilePath& path); |
| 61 |
| 62 // Returns a URLRequestJobFactory::ProtocolHandler that serves |
| 63 // URLRequestMockHTTPJob's responding like an HTTP server. |base_path| is the |
| 64 // file path leading to the root of the directory to use as the root of the |
| 65 // HTTP server. |
| 66 static scoped_ptr<URLRequestInterceptor> |
| 67 CreateInterceptor( |
| 68 const base::FilePath& base_path, |
| 69 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); |
| 70 |
| 71 // Returns a URLRequestJobFactory::ProtocolHandler that serves |
| 72 // URLRequestMockHTTPJob's responding like an HTTP server. It responds to all |
| 73 // requests with the contents of |file|. |
| 74 static scoped_ptr<URLRequestInterceptor> |
| 75 CreateInterceptorForSingleFile( |
| 76 const base::FilePath& file, |
| 77 const scoped_refptr<base::SequencedWorkerPool>& worker_pool); |
| 78 |
| 79 protected: |
| 80 virtual ~URLRequestMockHTTPJob(); |
| 81 |
| 82 private: |
| 83 void GetResponseInfoConst(HttpResponseInfo* info) const; |
| 84 void GetRawHeaders(std::string raw_headers); |
| 85 std::string raw_headers_; |
| 86 const scoped_refptr<base::TaskRunner> task_runner_; |
| 87 |
| 88 base::WeakPtrFactory<URLRequestMockHTTPJob> weak_ptr_factory_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(URLRequestMockHTTPJob); |
| 91 }; |
| 92 |
| 93 } // namespace net |
| 94 |
| 95 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_ |
OLD | NEW |