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