Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / test / url_request / url_request_mock_http_job.h
blob70839f5292ffd24d5af535e02de96e902a81f7a3
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.
7 #ifndef NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_
8 #define NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_
10 #include <string>
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"
18 namespace base {
19 class FilePath;
20 class SequencedWorkerPool;
23 namespace net {
24 class URLRequestInterceptor;
27 namespace net {
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);
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
62 // HTTP server.
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);
74 protected:
75 ~URLRequestMockHTTPJob() override;
77 private:
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);
89 } // namespace net
91 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_MOCK_HTTP_JOB_H_