Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: net/test/url_request/url_request_mock_data_job.cc

Issue 743713002: Cronet Fix Channel Write after Close when request is canceled after success. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Matt's comments. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "net/test/url_request/url_request_mock_data_job.h"
6
7 #include "base/bind.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"
14
15 namespace net {
16 namespace {
17
18 const char kMockHostname[] = "mock.data";
19
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) {
23 std::string value;
24 if (!GetValueForKeyInQuery(request.url(), "data", &value))
25 return "default_data";
26 return value;
27 }
28
29 // Gets the numeric repeat count from URL of the form:
30 // scheme://kMockHostname/?data=abc&repeat_count=nnn.
31 unsigned GetRepeatCountFromRequest(const net::URLRequest& request) {
32 std::string value;
33 if (!GetValueForKeyInQuery(request.url(), "repeat", &value))
34 return 1;
35
36 int repeat_count = 1;
mmenke 2014/12/10 20:59:14 No need to initialize this, since we just return 1
mef 2014/12/10 23:23:36 Done.
37 if (!base::StringToInt(value, &repeat_count))
mmenke 2014/12/10 20:59:14 StringToInt and then returning unsigned is a bug.
mef 2014/12/10 23:23:36 Done.
38 return 1;
39
40 return repeat_count;
41 }
42
43 GURL GetMockUrl(const std::string& scheme,
44 const std::string& hostname,
45 const std::string& data,
46 int data_repeat_count) {
47 std::string url(scheme + "://" + hostname + "/");
48 url.append("?data=");
49 url.append(data);
50 url.append("&repeat=");
51 url.append(base::IntToString(data_repeat_count));
52 return GURL(url);
53 }
54
55 class MockJobInterceptor : public net::URLRequestInterceptor {
56 public:
57 MockJobInterceptor() {}
58 ~MockJobInterceptor() override {}
59
60 // net::URLRequestInterceptor implementation
61 net::URLRequestJob* MaybeInterceptRequest(
62 net::URLRequest* request,
63 net::NetworkDelegate* network_delegate) const override {
64 return new URLRequestMockDataJob(request, network_delegate,
65 GetDataFromRequest(*request),
66 GetRepeatCountFromRequest(*request));
67 }
68
69 private:
70 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor);
71 };
72
73 } // namespace
74
75 URLRequestMockDataJob::URLRequestMockDataJob(URLRequest* request,
76 NetworkDelegate* network_delegate,
77 const std::string& data,
78 unsigned data_repeat_count)
79 : URLRequestJob(request, network_delegate),
80 data_offset_(0),
81 weak_factory_(this) {
82 for (unsigned i = 0; i < data_repeat_count; ++i) {
83 data_.append(data);
84 }
85 }
86
87 void URLRequestMockDataJob::Start() {
88 // Start reading asynchronously so that all error reporting and data
89 // callbacks happen as they would for network requests.
90 base::MessageLoop::current()->PostTask(
91 FROM_HERE, base::Bind(&URLRequestMockDataJob::StartAsync,
92 weak_factory_.GetWeakPtr()));
93 }
94
95 URLRequestMockDataJob::~URLRequestMockDataJob() {
96 }
97
98 bool URLRequestMockDataJob::ReadRawData(IOBuffer* buf,
99 int buf_size,
100 int* bytes_read) {
101 DCHECK(bytes_read);
102 *bytes_read = static_cast<int>(
103 std::min(static_cast<int64>(buf_size), data_.length() - data_offset_));
104 memcpy(buf->data(), data_.c_str() + data_offset_, *bytes_read);
105 data_offset_ += *bytes_read;
106 return true;
107 }
108
109 void URLRequestMockDataJob::StartAsync() {
110 if (!request_)
111 return;
112
113 set_expected_content_size(data_.length());
114 NotifyHeadersComplete();
115 }
116
117 // static
118 void URLRequestMockDataJob::AddUrlHandler() {
119 return AddUrlHandlerForHostname(kMockHostname);
120 }
121
122 // static
123 void URLRequestMockDataJob::AddUrlHandlerForHostname(
124 const std::string& hostname) {
125 // Add |hostname| to net::URLRequestFilter for HTTP and HTTPS.
126 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
127 filter->AddHostnameInterceptor("http", hostname,
128 make_scoped_ptr(new MockJobInterceptor()));
129 filter->AddHostnameInterceptor("https", hostname,
130 make_scoped_ptr(new MockJobInterceptor()));
131 }
132
133 // static
134 GURL URLRequestMockDataJob::GetMockHttpUrl(const std::string& data,
135 int repeat_count) {
136 return GetMockHttpUrlForHostname(kMockHostname, data, repeat_count);
137 }
138
139 // static
140 GURL URLRequestMockDataJob::GetMockHttpsUrl(const std::string& data,
141 int repeat_count) {
142 return GetMockHttpsUrlForHostname(kMockHostname, data, repeat_count);
143 }
144
145 // static
146 GURL URLRequestMockDataJob::GetMockHttpUrlForHostname(
147 const std::string& hostname,
148 const std::string& data,
149 int repeat_count) {
150 return GetMockUrl("http", hostname, data, repeat_count);
151 }
152
153 // static
154 GURL URLRequestMockDataJob::GetMockHttpsUrlForHostname(
155 const std::string& hostname,
156 const std::string& data,
157 int repeat_count) {
158 return GetMockUrl("https", hostname, data, repeat_count);
159 }
160
161 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698