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 "net/base/io_buffer.h"
11 #include "net/base/url_util.h"
12 #include "net/http/http_request_headers.h"
13 #include "net/url_request/url_request_filter.h"
18 const char kMockHostname
[] = "mock.data";
20 // Gets the data from URL of the form:
21 // scheme://kMockHostname/?data=abc&repeat_count=nnn.
22 std::string
GetDataFromRequest(const net::URLRequest
& request
) {
24 if (!GetValueForKeyInQuery(request
.url(), "data", &value
))
25 return "default_data";
29 // Gets the numeric repeat count from URL of the form:
30 // scheme://kMockHostname/?data=abc&repeat_count=nnn.
31 int GetRepeatCountFromRequest(const net::URLRequest
& request
) {
33 if (!GetValueForKeyInQuery(request
.url(), "repeat", &value
))
37 if (!base::StringToInt(value
, &repeat_count
))
40 DCHECK_GT(repeat_count
, 0);
45 GURL
GetMockUrl(const std::string
& scheme
,
46 const std::string
& hostname
,
47 const std::string
& data
,
48 int data_repeat_count
) {
49 DCHECK_GT(data_repeat_count
, 0);
50 std::string
url(scheme
+ "://" + hostname
+ "/");
53 url
.append("&repeat=");
54 url
.append(base::IntToString(data_repeat_count
));
58 class MockJobInterceptor
: public net::URLRequestInterceptor
{
60 MockJobInterceptor() {}
61 ~MockJobInterceptor() override
{}
63 // net::URLRequestInterceptor implementation
64 net::URLRequestJob
* MaybeInterceptRequest(
65 net::URLRequest
* request
,
66 net::NetworkDelegate
* network_delegate
) const override
{
67 return new URLRequestMockDataJob(request
, network_delegate
,
68 GetDataFromRequest(*request
),
69 GetRepeatCountFromRequest(*request
));
73 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor
);
78 URLRequestMockDataJob::URLRequestMockDataJob(URLRequest
* request
,
79 NetworkDelegate
* network_delegate
,
80 const std::string
& data
,
81 int data_repeat_count
)
82 : URLRequestJob(request
, network_delegate
),
85 DCHECK_GT(data_repeat_count
, 0);
86 for (int i
= 0; i
< data_repeat_count
; ++i
) {
91 void URLRequestMockDataJob::Start() {
92 // Start reading asynchronously so that all error reporting and data
93 // callbacks happen as they would for network requests.
94 base::MessageLoop::current()->PostTask(
95 FROM_HERE
, base::Bind(&URLRequestMockDataJob::StartAsync
,
96 weak_factory_
.GetWeakPtr()));
99 URLRequestMockDataJob::~URLRequestMockDataJob() {
102 bool URLRequestMockDataJob::ReadRawData(IOBuffer
* buf
,
106 *bytes_read
= static_cast<int>(
107 std::min(static_cast<size_t>(buf_size
), data_
.length() - data_offset_
));
108 memcpy(buf
->data(), data_
.c_str() + data_offset_
, *bytes_read
);
109 data_offset_
+= *bytes_read
;
113 void URLRequestMockDataJob::StartAsync() {
117 set_expected_content_size(data_
.length());
118 NotifyHeadersComplete();
122 void URLRequestMockDataJob::AddUrlHandler() {
123 return AddUrlHandlerForHostname(kMockHostname
);
127 void URLRequestMockDataJob::AddUrlHandlerForHostname(
128 const std::string
& hostname
) {
129 // Add |hostname| to net::URLRequestFilter for HTTP and HTTPS.
130 net::URLRequestFilter
* filter
= net::URLRequestFilter::GetInstance();
131 filter
->AddHostnameInterceptor("http", hostname
,
132 make_scoped_ptr(new MockJobInterceptor()));
133 filter
->AddHostnameInterceptor("https", hostname
,
134 make_scoped_ptr(new MockJobInterceptor()));
138 GURL
URLRequestMockDataJob::GetMockHttpUrl(const std::string
& data
,
140 return GetMockHttpUrlForHostname(kMockHostname
, data
, repeat_count
);
144 GURL
URLRequestMockDataJob::GetMockHttpsUrl(const std::string
& data
,
146 return GetMockHttpsUrlForHostname(kMockHostname
, data
, repeat_count
);
150 GURL
URLRequestMockDataJob::GetMockHttpUrlForHostname(
151 const std::string
& hostname
,
152 const std::string
& data
,
154 return GetMockUrl("http", hostname
, data
, repeat_count
);
158 GURL
URLRequestMockDataJob::GetMockHttpsUrlForHostname(
159 const std::string
& hostname
,
160 const std::string
& data
,
162 return GetMockUrl("https", hostname
, data
, repeat_count
);