1 // Copyright 2014 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 "net/test/url_request/url_request_mock_data_job.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/url_util.h"
13 #include "net/http/http_request_headers.h"
14 #include "net/http/http_response_headers.h"
15 #include "net/http/http_util.h"
16 #include "net/url_request/url_request_filter.h"
21 const char kMockHostname
[] = "mock.data";
23 // Gets the data from URL of the form:
24 // scheme://kMockHostname/?data=abc&repeat_count=nnn.
25 std::string
GetDataFromRequest(const URLRequest
& request
) {
27 if (!GetValueForKeyInQuery(request
.url(), "data", &value
))
28 return "default_data";
32 // Gets the numeric repeat count from URL of the form:
33 // scheme://kMockHostname/?data=abc&repeat_count=nnn.
34 int GetRepeatCountFromRequest(const URLRequest
& request
) {
36 if (!GetValueForKeyInQuery(request
.url(), "repeat", &value
))
40 if (!base::StringToInt(value
, &repeat_count
))
43 DCHECK_GT(repeat_count
, 0);
48 GURL
GetMockUrl(const std::string
& scheme
,
49 const std::string
& hostname
,
50 const std::string
& data
,
51 int data_repeat_count
) {
52 DCHECK_GT(data_repeat_count
, 0);
53 std::string
url(scheme
+ "://" + hostname
+ "/");
56 url
.append("&repeat=");
57 url
.append(base::IntToString(data_repeat_count
));
61 class MockJobInterceptor
: public URLRequestInterceptor
{
63 MockJobInterceptor() {}
64 ~MockJobInterceptor() override
{}
66 // URLRequestInterceptor implementation
67 URLRequestJob
* MaybeInterceptRequest(
69 NetworkDelegate
* network_delegate
) const override
{
70 return new URLRequestMockDataJob(request
, network_delegate
,
71 GetDataFromRequest(*request
),
72 GetRepeatCountFromRequest(*request
));
76 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor
);
81 URLRequestMockDataJob::URLRequestMockDataJob(URLRequest
* request
,
82 NetworkDelegate
* network_delegate
,
83 const std::string
& data
,
84 int data_repeat_count
)
85 : URLRequestJob(request
, network_delegate
),
88 DCHECK_GT(data_repeat_count
, 0);
89 for (int i
= 0; i
< data_repeat_count
; ++i
) {
94 void URLRequestMockDataJob::Start() {
95 // Start reading asynchronously so that all error reporting and data
96 // callbacks happen as they would for network requests.
97 base::MessageLoop::current()->PostTask(
98 FROM_HERE
, base::Bind(&URLRequestMockDataJob::StartAsync
,
99 weak_factory_
.GetWeakPtr()));
102 URLRequestMockDataJob::~URLRequestMockDataJob() {
105 bool URLRequestMockDataJob::ReadRawData(IOBuffer
* buf
,
109 *bytes_read
= static_cast<int>(
110 std::min(static_cast<size_t>(buf_size
), data_
.length() - data_offset_
));
111 memcpy(buf
->data(), data_
.c_str() + data_offset_
, *bytes_read
);
112 data_offset_
+= *bytes_read
;
116 int URLRequestMockDataJob::GetResponseCode() const {
117 HttpResponseInfo info
;
118 GetResponseInfoConst(&info
);
119 return info
.headers
->response_code();
122 // Public virtual version.
123 void URLRequestMockDataJob::GetResponseInfo(HttpResponseInfo
* info
) {
124 // Forward to private const version.
125 GetResponseInfoConst(info
);
128 // Private const version.
129 void URLRequestMockDataJob::GetResponseInfoConst(HttpResponseInfo
* info
) const {
130 // Send back mock headers.
131 std::string raw_headers
;
134 "Content-type: text/plain\n");
135 raw_headers
.append(base::StringPrintf("Content-Length: %1d\n",
136 static_cast<int>(data_
.length())));
137 info
->headers
= new HttpResponseHeaders(HttpUtil::AssembleRawHeaders(
138 raw_headers
.c_str(), static_cast<int>(raw_headers
.length())));
141 void URLRequestMockDataJob::StartAsync() {
145 set_expected_content_size(data_
.length());
146 NotifyHeadersComplete();
150 void URLRequestMockDataJob::AddUrlHandler() {
151 return AddUrlHandlerForHostname(kMockHostname
);
155 void URLRequestMockDataJob::AddUrlHandlerForHostname(
156 const std::string
& hostname
) {
157 // Add |hostname| to URLRequestFilter for HTTP and HTTPS.
158 URLRequestFilter
* filter
= URLRequestFilter::GetInstance();
159 filter
->AddHostnameInterceptor("http", hostname
,
160 make_scoped_ptr(new MockJobInterceptor()));
161 filter
->AddHostnameInterceptor("https", hostname
,
162 make_scoped_ptr(new MockJobInterceptor()));
166 GURL
URLRequestMockDataJob::GetMockHttpUrl(const std::string
& data
,
168 return GetMockHttpUrlForHostname(kMockHostname
, data
, repeat_count
);
172 GURL
URLRequestMockDataJob::GetMockHttpsUrl(const std::string
& data
,
174 return GetMockHttpsUrlForHostname(kMockHostname
, data
, repeat_count
);
178 GURL
URLRequestMockDataJob::GetMockHttpUrlForHostname(
179 const std::string
& hostname
,
180 const std::string
& data
,
182 return GetMockUrl("http", hostname
, data
, repeat_count
);
186 GURL
URLRequestMockDataJob::GetMockHttpsUrlForHostname(
187 const std::string
& hostname
,
188 const std::string
& data
,
190 return GetMockUrl("https", hostname
, data
, repeat_count
);