1 // Copyright 2012 The Chromium Authors 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 <stdint.h> 11 12 #include <memory> 13 #include <string> 14 15 #include "net/test/url_request/url_request_test_job_backed_by_file.h" 16 #include "url/gurl.h" 17 18 namespace base { 19 class FilePath; 20 } 21 22 namespace net { 23 class URLRequestInterceptor; 24 } 25 26 namespace net { 27 28 class URLRequestMockHTTPJob : public URLRequestTestJobBackedByFile { 29 public: 30 // Note that all file I/O is done using ThreadPool. 31 URLRequestMockHTTPJob(URLRequest* request, 32 const base::FilePath& file_path); 33 34 URLRequestMockHTTPJob(const URLRequestMockHTTPJob&) = delete; 35 URLRequestMockHTTPJob& operator=(const URLRequestMockHTTPJob&) = delete; 36 37 ~URLRequestMockHTTPJob() override; 38 39 // URLRequestJob overrides. 40 void Start() override; 41 int64_t GetTotalReceivedBytes() const override; 42 bool GetMimeType(std::string* mime_type) const override; 43 bool GetCharset(std::string* charset) override; 44 void GetResponseInfo(HttpResponseInfo* info) override; 45 bool IsRedirectResponse(GURL* location, 46 int* http_status_code, 47 bool* insecure_scheme_was_upgraded) override; 48 49 // URLRequestFileJob overridess. 50 void OnReadComplete(net::IOBuffer* buffer, int result) override; 51 52 // Adds the testing URLs to the URLRequestFilter, both under HTTP and HTTPS. 53 static void AddUrlHandlers(const base::FilePath& base_path); 54 55 // Given the path to a file relative to the path passed to AddUrlHandler(), 56 // construct a mock URL. 57 static GURL GetMockUrl(const std::string& path); 58 static GURL GetMockHttpsUrl(const std::string& path); 59 60 // Returns a URLRequestJobFactory::ProtocolHandler that serves 61 // URLRequestMockHTTPJob's responding like an HTTP server. |base_path| is the 62 // file path leading to the root of the directory to use as the root of the 63 // HTTP server. 64 static std::unique_ptr<URLRequestInterceptor> CreateInterceptor( 65 const base::FilePath& base_path); 66 67 // Returns a URLRequestJobFactory::ProtocolHandler that serves 68 // URLRequestMockHTTPJob's responding like an HTTP server. It responds to all 69 // requests with the contents of |file|. 70 static std::unique_ptr<URLRequestInterceptor> CreateInterceptorForSingleFile( 71 const base::FilePath& file); 72 73 private: 74 void GetResponseInfoConst(HttpResponseInfo* info) const; 75 void SetHeadersAndStart(const std::string& raw_headers); 76 77 std::string raw_headers_; 78 int64_t total_received_bytes_ = 0; 79 80 base::WeakPtrFactory<URLRequestMockHTTPJob> weak_ptr_factory_{this}; 81 }; 82 83 } // namespace net 84 85 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_ 86