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 #include "content/test/net/url_request_mock_http_job.h"
7 #include "base/file_util.h"
8 #include "base/message_loop.h"
9 #include "base/string_util.h"
10 #include "base/threading/thread_restrictions.h"
11 #include "base/utf_string_conversions.h"
12 #include "content/public/common/url_constants.h"
13 #include "net/base/net_util.h"
14 #include "net/http/http_response_headers.h"
15 #include "net/url_request/url_request_filter.h"
17 const char kMockHostname
[] = "mock.http";
18 const base::FilePath::CharType kMockHeaderFileSuffix
[] =
19 FILE_PATH_LITERAL(".mock-http-headers");
25 class ProtocolHandler
: public net::URLRequestJobFactory::ProtocolHandler
{
27 // When |map_all_requests_to_base_path| is true, all request should return the
28 // contents of the file at |base_path|. When |map_all_requests_to_base_path|
29 // is false, |base_path| is the file path leading to the root of the directory
30 // to use as the root of the HTTP server.
31 explicit ProtocolHandler(const base::FilePath
& base_path
,
32 bool map_all_requests_to_base_path
)
33 : base_path_(base_path
),
34 map_all_requests_to_base_path_(map_all_requests_to_base_path
) {}
35 virtual ~ProtocolHandler() {}
37 // net::URLRequestJobFactory::ProtocolHandler implementation
38 virtual net::URLRequestJob
* MaybeCreateJob(
39 net::URLRequest
* request
,
40 net::NetworkDelegate
* network_delegate
) const OVERRIDE
{
41 return new URLRequestMockHTTPJob(request
, network_delegate
,
42 map_all_requests_to_base_path_
? base_path_
: GetOnDiskPath(request
));
46 base::FilePath
GetOnDiskPath(net::URLRequest
* request
) const {
47 // Conceptually we just want to "return base_path_ + request->url().path()".
48 // But path in the request URL is in URL space (i.e. %-encoded spaces).
49 // So first we convert base FilePath to a URL, then append the URL
50 // path to that, and convert the final URL back to a FilePath.
51 GURL
file_url(net::FilePathToFileURL(base_path_
));
52 std::string url
= file_url
.spec() + request
->url().path();
53 base::FilePath file_path
;
54 net::FileURLToFilePath(GURL(url
), &file_path
);
58 const base::FilePath base_path_
;
59 const bool map_all_requests_to_base_path_
;
61 DISALLOW_COPY_AND_ASSIGN(ProtocolHandler
);
67 void URLRequestMockHTTPJob::AddUrlHandler(const base::FilePath
& base_path
) {
68 // Add kMockHostname to net::URLRequestFilter.
69 net::URLRequestFilter
* filter
= net::URLRequestFilter::GetInstance();
70 filter
->AddHostnameProtocolHandler("http", kMockHostname
,
71 CreateProtocolHandler(base_path
));
75 void URLRequestMockHTTPJob::AddHostnameToFileHandler(
76 const std::string
& hostname
,
77 const base::FilePath
& file_path
) {
78 net::URLRequestFilter
* filter
= net::URLRequestFilter::GetInstance();
79 filter
->AddHostnameProtocolHandler("http", hostname
,
80 scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
81 new ProtocolHandler(file_path
, true)));
86 GURL
URLRequestMockHTTPJob::GetMockUrl(const base::FilePath
& path
) {
87 std::string url
= "http://";
88 url
.append(kMockHostname
);
90 std::string path_str
= path
.MaybeAsASCII();
91 DCHECK(!path_str
.empty()); // We only expect ASCII paths in tests.
97 GURL
URLRequestMockHTTPJob::GetMockViewSourceUrl(const base::FilePath
& path
) {
98 std::string url
= chrome::kViewSourceScheme
;
100 url
.append(GetMockUrl(path
).spec());
105 scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>
106 URLRequestMockHTTPJob::CreateProtocolHandler(const base::FilePath
& base_path
) {
107 return scoped_ptr
<net::URLRequestJobFactory::ProtocolHandler
>(
108 new ProtocolHandler(base_path
, false));
111 URLRequestMockHTTPJob::URLRequestMockHTTPJob(
112 net::URLRequest
* request
,
113 net::NetworkDelegate
* network_delegate
,
114 const base::FilePath
& file_path
)
115 : net::URLRequestFileJob(request
, network_delegate
, file_path
) { }
117 URLRequestMockHTTPJob::~URLRequestMockHTTPJob() { }
119 // Public virtual version.
120 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo
* info
) {
121 // Forward to private const version.
122 GetResponseInfoConst(info
);
125 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL
* location
,
126 int* http_status_code
) {
127 // Override the net::URLRequestFileJob implementation to invoke the default
128 // one based on HttpResponseInfo.
129 return net::URLRequestJob::IsRedirectResponse(location
, http_status_code
);
132 // Private const version.
133 void URLRequestMockHTTPJob::GetResponseInfoConst(
134 net::HttpResponseInfo
* info
) const {
135 // We have to load our headers from disk, but we only use this class
136 // from tests, so allow these IO operations to happen on any thread.
137 base::ThreadRestrictions::ScopedAllowIO allow_io
;
139 base::FilePath header_file
=
140 base::FilePath(file_path_
.value() + kMockHeaderFileSuffix
);
141 std::string raw_headers
;
142 if (!file_util::ReadFileToString(header_file
, &raw_headers
))
145 // ParseRawHeaders expects \0 to end each header line.
146 ReplaceSubstringsAfterOffset(&raw_headers
, 0, "\n", std::string("\0", 1));
147 info
->headers
= new net::HttpResponseHeaders(raw_headers
);
150 bool URLRequestMockHTTPJob::GetMimeType(std::string
* mime_type
) const {
151 net::HttpResponseInfo info
;
152 GetResponseInfoConst(&info
);
153 return info
.headers
&& info
.headers
->GetMimeType(mime_type
);
156 int URLRequestMockHTTPJob::GetResponseCode() const {
157 net::HttpResponseInfo info
;
158 GetResponseInfoConst(&info
);
159 // If we have headers, get the response code from them.
161 return info
.headers
->response_code();
162 return net::URLRequestJob::GetResponseCode();
165 bool URLRequestMockHTTPJob::GetCharset(std::string
* charset
) {
166 net::HttpResponseInfo info
;
167 GetResponseInfo(&info
);
168 return info
.headers
&& info
.headers
->GetCharset(charset
);
171 } // namespace content