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.
5 // A URLRequestJob class that pulls the net and http headers from disk.
7 #ifndef NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_
8 #define NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_
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"
20 class SequencedWorkerPool
;
24 class URLRequestInterceptor
;
29 class URLRequestMockHTTPJob
: public URLRequestFileJob
{
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
);
37 void Start() override
;
38 bool GetMimeType(std::string
* mime_type
) const override
;
39 int GetResponseCode() const override
;
40 bool GetCharset(std::string
* charset
) override
;
41 void GetResponseInfo(HttpResponseInfo
* info
) override
;
42 bool IsRedirectResponse(GURL
* location
, int* http_status_code
) override
;
44 // Adds the testing URLs to the URLRequestFilter, both under HTTP and HTTPS.
45 static void AddUrlHandlers(
46 const base::FilePath
& base_path
,
47 const scoped_refptr
<base::SequencedWorkerPool
>& worker_pool
);
49 // Given the path to a file relative to the path passed to AddUrlHandler(),
50 // construct a mock URL.
51 static GURL
GetMockUrl(const std::string
& path
);
52 static GURL
GetMockHttpsUrl(const std::string
& path
);
54 // Like above, but takes a FilePath. These methods are deprecated. Prefer the
55 // std::string versions. See http://crbug.com/496936.
56 static GURL
GetMockUrl(const base::FilePath
& path
);
57 static GURL
GetMockHttpsUrl(const base::FilePath
& path
);
59 // Returns a URLRequestJobFactory::ProtocolHandler that serves
60 // URLRequestMockHTTPJob's responding like an HTTP server. |base_path| is the
61 // file path leading to the root of the directory to use as the root of the
63 static scoped_ptr
<URLRequestInterceptor
> CreateInterceptor(
64 const base::FilePath
& base_path
,
65 const scoped_refptr
<base::SequencedWorkerPool
>& worker_pool
);
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 scoped_ptr
<URLRequestInterceptor
> CreateInterceptorForSingleFile(
71 const base::FilePath
& file
,
72 const scoped_refptr
<base::SequencedWorkerPool
>& worker_pool
);
75 ~URLRequestMockHTTPJob() override
;
78 void GetResponseInfoConst(HttpResponseInfo
* info
) const;
79 void SetHeadersAndStart(const std::string
& raw_headers
);
81 std::string raw_headers_
;
82 const scoped_refptr
<base::TaskRunner
> task_runner_
;
84 base::WeakPtrFactory
<URLRequestMockHTTPJob
> weak_ptr_factory_
;
86 DISALLOW_COPY_AND_ASSIGN(URLRequestMockHTTPJob
);
91 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_