OLD | NEW |
---|---|
(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 int GetRepeatCountFromRequest(const net::URLRequest& request) { | |
32 std::string value; | |
33 if (!GetValueForKeyInQuery(request.url(), "repeat", &value)) | |
34 return 1; | |
35 | |
36 int repeat_count; | |
37 if (!base::StringToInt(value, &repeat_count)) | |
38 return 1; | |
39 | |
40 DCHECK_GT(repeat_count, 0); | |
41 | |
42 return repeat_count; | |
43 } | |
44 | |
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 + "/"); | |
51 url.append("?data="); | |
52 url.append(data); | |
53 url.append("&repeat="); | |
54 url.append(base::IntToString(data_repeat_count)); | |
55 return GURL(url); | |
56 } | |
57 | |
58 class MockJobInterceptor : public net::URLRequestInterceptor { | |
59 public: | |
60 MockJobInterceptor() {} | |
61 ~MockJobInterceptor() override {} | |
62 | |
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)); | |
mmenke
2014/12/11 20:04:37
Again, you're passing an int as an unsigned here.
mef
2014/12/12 19:47:53
Done. Sorry.
| |
70 } | |
71 | |
72 private: | |
73 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor); | |
74 }; | |
75 | |
76 } // namespace | |
77 | |
78 URLRequestMockDataJob::URLRequestMockDataJob(URLRequest* request, | |
79 NetworkDelegate* network_delegate, | |
80 const std::string& data, | |
81 unsigned data_repeat_count) | |
82 : URLRequestJob(request, network_delegate), | |
83 data_offset_(0), | |
84 weak_factory_(this) { | |
85 for (unsigned i = 0; i < data_repeat_count; ++i) { | |
86 data_.append(data); | |
87 } | |
88 } | |
89 | |
90 void URLRequestMockDataJob::Start() { | |
91 // Start reading asynchronously so that all error reporting and data | |
92 // callbacks happen as they would for network requests. | |
93 base::MessageLoop::current()->PostTask( | |
94 FROM_HERE, base::Bind(&URLRequestMockDataJob::StartAsync, | |
95 weak_factory_.GetWeakPtr())); | |
96 } | |
97 | |
98 URLRequestMockDataJob::~URLRequestMockDataJob() { | |
99 } | |
100 | |
101 bool URLRequestMockDataJob::ReadRawData(IOBuffer* buf, | |
102 int buf_size, | |
103 int* bytes_read) { | |
104 DCHECK(bytes_read); | |
105 *bytes_read = static_cast<int>( | |
106 std::min(static_cast<int64>(buf_size), data_.length() - data_offset_)); | |
107 memcpy(buf->data(), data_.c_str() + data_offset_, *bytes_read); | |
108 data_offset_ += *bytes_read; | |
109 return true; | |
110 } | |
111 | |
112 void URLRequestMockDataJob::StartAsync() { | |
113 if (!request_) | |
114 return; | |
115 | |
116 set_expected_content_size(data_.length()); | |
117 NotifyHeadersComplete(); | |
118 } | |
119 | |
120 // static | |
121 void URLRequestMockDataJob::AddUrlHandler() { | |
122 return AddUrlHandlerForHostname(kMockHostname); | |
123 } | |
124 | |
125 // static | |
126 void URLRequestMockDataJob::AddUrlHandlerForHostname( | |
127 const std::string& hostname) { | |
128 // Add |hostname| to net::URLRequestFilter for HTTP and HTTPS. | |
129 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | |
130 filter->AddHostnameInterceptor("http", hostname, | |
131 make_scoped_ptr(new MockJobInterceptor())); | |
132 filter->AddHostnameInterceptor("https", hostname, | |
133 make_scoped_ptr(new MockJobInterceptor())); | |
134 } | |
135 | |
136 // static | |
137 GURL URLRequestMockDataJob::GetMockHttpUrl(const std::string& data, | |
138 int repeat_count) { | |
139 return GetMockHttpUrlForHostname(kMockHostname, data, repeat_count); | |
140 } | |
141 | |
142 // static | |
143 GURL URLRequestMockDataJob::GetMockHttpsUrl(const std::string& data, | |
144 int repeat_count) { | |
145 return GetMockHttpsUrlForHostname(kMockHostname, data, repeat_count); | |
146 } | |
147 | |
148 // static | |
149 GURL URLRequestMockDataJob::GetMockHttpUrlForHostname( | |
150 const std::string& hostname, | |
151 const std::string& data, | |
152 int repeat_count) { | |
153 return GetMockUrl("http", hostname, data, repeat_count); | |
154 } | |
155 | |
156 // static | |
157 GURL URLRequestMockDataJob::GetMockHttpsUrlForHostname( | |
158 const std::string& hostname, | |
159 const std::string& data, | |
160 int repeat_count) { | |
161 return GetMockUrl("https", hostname, data, repeat_count); | |
162 } | |
163 | |
164 } // namespace net | |
OLD | NEW |