Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / test / url_request / url_request_mock_data_job.cc
blob5425816bb0574af0b757c9c9b9786d14c8530c83
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"
7 #include "base/bind.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"
18 namespace net {
19 namespace {
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) {
26 std::string value;
27 if (!GetValueForKeyInQuery(request.url(), "data", &value))
28 return "default_data";
29 return value;
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) {
35 std::string value;
36 if (!GetValueForKeyInQuery(request.url(), "repeat", &value))
37 return 1;
39 int repeat_count;
40 if (!base::StringToInt(value, &repeat_count))
41 return 1;
43 DCHECK_GT(repeat_count, 0);
45 return repeat_count;
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 + "/");
54 url.append("?data=");
55 url.append(data);
56 url.append("&repeat=");
57 url.append(base::IntToString(data_repeat_count));
58 return GURL(url);
61 class MockJobInterceptor : public URLRequestInterceptor {
62 public:
63 MockJobInterceptor() {}
64 ~MockJobInterceptor() override {}
66 // URLRequestInterceptor implementation
67 URLRequestJob* MaybeInterceptRequest(
68 URLRequest* request,
69 NetworkDelegate* network_delegate) const override {
70 return new URLRequestMockDataJob(request, network_delegate,
71 GetDataFromRequest(*request),
72 GetRepeatCountFromRequest(*request));
75 private:
76 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor);
79 } // namespace
81 URLRequestMockDataJob::URLRequestMockDataJob(URLRequest* request,
82 NetworkDelegate* network_delegate,
83 const std::string& data,
84 int data_repeat_count)
85 : URLRequestJob(request, network_delegate),
86 data_offset_(0),
87 weak_factory_(this) {
88 DCHECK_GT(data_repeat_count, 0);
89 for (int i = 0; i < data_repeat_count; ++i) {
90 data_.append(data);
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,
106 int buf_size,
107 int* bytes_read) {
108 DCHECK(bytes_read);
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;
113 return true;
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;
132 raw_headers.append(
133 "HTTP/1.1 200 OK\n"
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() {
142 if (!request_)
143 return;
145 set_expected_content_size(data_.length());
146 NotifyHeadersComplete();
149 // static
150 void URLRequestMockDataJob::AddUrlHandler() {
151 return AddUrlHandlerForHostname(kMockHostname);
154 // static
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()));
165 // static
166 GURL URLRequestMockDataJob::GetMockHttpUrl(const std::string& data,
167 int repeat_count) {
168 return GetMockHttpUrlForHostname(kMockHostname, data, repeat_count);
171 // static
172 GURL URLRequestMockDataJob::GetMockHttpsUrl(const std::string& data,
173 int repeat_count) {
174 return GetMockHttpsUrlForHostname(kMockHostname, data, repeat_count);
177 // static
178 GURL URLRequestMockDataJob::GetMockHttpUrlForHostname(
179 const std::string& hostname,
180 const std::string& data,
181 int repeat_count) {
182 return GetMockUrl("http", hostname, data, repeat_count);
185 // static
186 GURL URLRequestMockDataJob::GetMockHttpsUrlForHostname(
187 const std::string& hostname,
188 const std::string& data,
189 int repeat_count) {
190 return GetMockUrl("https", hostname, data, repeat_count);
193 } // namespace net