Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
mmenke
2014/12/09 21:00:03
-(c), update year
mef
2014/12/09 22:20:59
Done.
| |
| 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 <vector> | |
|
mmenke
2014/12/09 21:00:03
Not used.
mef
2014/12/09 22:20:59
Done.
| |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/compiler_specific.h" | |
|
mmenke
2014/12/09 21:00:04
Not used, I believe.
mef
2014/12/09 22:20:59
Done.
| |
| 11 #include "base/memory/ref_counted_memory.h" | |
|
mmenke
2014/12/09 21:00:04
Not used.
mef
2014/12/09 22:20:59
Done.
| |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/strings/string_number_conversions.h" | |
| 14 #include "net/base/io_buffer.h" | |
| 15 #include "net/base/net_errors.h" | |
|
mmenke
2014/12/09 21:00:04
Not used
mef
2014/12/09 22:20:59
Done.
| |
| 16 #include "net/base/url_util.h" | |
| 17 #include "net/http/http_request_headers.h" | |
| 18 #include "net/http/http_util.h" | |
|
mmenke
2014/12/09 21:00:03
Is http util used?
mef
2014/12/09 22:20:59
Done.
| |
| 19 #include "net/url_request/url_request_filter.h" | |
| 20 #include "net/url_request/url_request_status.h" | |
|
mmenke
2014/12/09 21:00:04
Not used.
mef
2014/12/09 22:20:59
Done.
| |
| 21 | |
| 22 namespace net { | |
| 23 namespace { | |
| 24 | |
| 25 const char kMockHostname[] = "mock.data"; | |
| 26 | |
| 27 // Gets the data from URL of the form: | |
| 28 // scheme://kMockHostname/?data=abc&repeat_count=nnn. | |
| 29 std::string GetDataFromRequest(net::URLRequest* request) { | |
|
mmenke
2014/12/09 21:00:03
const net::URLRequest&, here and below?
mef
2014/12/09 22:20:59
Done.
| |
| 30 std::string value; | |
| 31 if (!GetValueForKeyInQuery(request->url(), "data", &value)) | |
| 32 return "default_data"; | |
| 33 return value; | |
| 34 } | |
| 35 | |
| 36 // Gets the numeric repeat count from URL of the form: | |
| 37 // scheme://kMockHostname/?data=abc&repeat_count=nnn. | |
| 38 unsigned GetRepeatCountFromRequest(net::URLRequest* request) { | |
| 39 std::string value; | |
| 40 if (!GetValueForKeyInQuery(request->url(), "repeat", &value)) | |
| 41 return 1; | |
| 42 | |
| 43 unsigned repeat_count = 1; | |
| 44 if (!base::StringToUint(value, &repeat_count)) | |
| 45 return 1; | |
| 46 | |
| 47 return repeat_count; | |
| 48 } | |
| 49 | |
| 50 GURL GetMockUrl(const std::string& scheme, | |
| 51 const std::string& hostname, | |
| 52 const std::string& data, | |
| 53 unsigned data_repeat_count) { | |
| 54 std::string url(scheme + "://" + hostname + "/"); | |
| 55 url.append("?data="); | |
| 56 url.append(data); | |
| 57 url.append("&repeat="); | |
| 58 url.append(base::UintToString(data_repeat_count)); | |
| 59 return GURL(url); | |
| 60 } | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 64 URLRequestMockDataJob::URLRequestMockDataJob(URLRequest* request, | |
| 65 NetworkDelegate* network_delegate, | |
| 66 const std::string& data, | |
| 67 unsigned data_repeat_count) | |
| 68 : URLRequestJob(request, network_delegate), | |
| 69 data_offset_(0), | |
| 70 weak_factory_(this) { | |
| 71 data_.reserve(data_repeat_count*data.length()); | |
|
mmenke
2014/12/09 21:00:03
Space before/after "*".
Actually, since this this
mef
2014/12/09 22:20:59
Done.
| |
| 72 for (unsigned i = 0; i < data_repeat_count; ++i) { | |
| 73 data_ += data; | |
|
mmenke
2014/12/09 21:00:04
Suggest using .append - think it's a little cleane
mef
2014/12/09 22:20:59
Done.
| |
| 74 } | |
| 75 } | |
| 76 | |
| 77 void URLRequestMockDataJob::Start() { | |
| 78 // Start reading asynchronously so that all error reporting and data | |
| 79 // callbacks happen as they would for network requests. | |
| 80 base::MessageLoop::current()->PostTask( | |
| 81 FROM_HERE, | |
| 82 base::Bind(&URLRequestMockDataJob::StartAsync, | |
| 83 weak_factory_.GetWeakPtr())); | |
| 84 } | |
| 85 | |
| 86 bool URLRequestMockDataJob::GetMimeType(std::string* mime_type) const { | |
| 87 *mime_type = mime_type_; | |
|
mmenke
2014/12/09 21:00:03
Can we just use the default implementation of this
mef
2014/12/09 22:20:59
Done.
| |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 bool URLRequestMockDataJob::GetCharset(std::string* charset) { | |
| 92 *charset = charset_; | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 96 URLRequestMockDataJob::~URLRequestMockDataJob() {} | |
| 97 | |
| 98 bool URLRequestMockDataJob::ReadRawData(IOBuffer* buf, int buf_size, | |
| 99 int* bytes_read) { | |
|
mmenke
2014/12/09 21:00:03
Fix indent
mef
2014/12/09 22:20:59
Done.
| |
| 100 DCHECK(bytes_read); | |
| 101 buf_size = static_cast<int>(std::min( | |
|
mmenke
2014/12/09 21:00:04
This is a misnomer. Suggest just setting *bytes_r
mef
2014/12/09 22:20:59
Done.
| |
| 102 static_cast<int64>(buf_size), | |
| 103 data_.length() - data_offset_ + 1)); | |
|
mmenke
2014/12/09 21:00:04
Remove the "+1". If they're the same, you're at t
mef
2014/12/09 22:20:59
Done.
| |
| 104 memcpy(buf->data(), data_.c_str() + data_offset_, buf_size); | |
| 105 data_offset_ += buf_size; | |
| 106 *bytes_read = buf_size; | |
| 107 return true; | |
| 108 } | |
| 109 | |
| 110 void URLRequestMockDataJob::StartAsync() { | |
| 111 if (!request_) | |
| 112 return; | |
| 113 | |
| 114 set_expected_content_size(data_.length()); | |
| 115 NotifyHeadersComplete(); | |
| 116 } | |
| 117 | |
| 118 // static | |
| 119 void URLRequestMockDataJob::AddUrlHandler() { | |
| 120 return AddUrlHandlerForHostname(kMockHostname); | |
| 121 } | |
| 122 | |
| 123 // static | |
| 124 void URLRequestMockDataJob::AddUrlHandlerForHostname( | |
| 125 const std::string& hostname) { | |
| 126 // Add |hostname| to net::URLRequestFilter for HTTP and HTTPS. | |
| 127 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | |
| 128 filter->AddHostnameHandler("http", hostname, URLRequestMockDataJob::Factory); | |
| 129 filter->AddHostnameHandler("https", hostname, URLRequestMockDataJob::Factory); | |
|
mmenke
2014/12/09 21:00:03
Should use AddHostnameInterceptor instead - URLReq
mef
2014/12/09 22:20:59
Done.
| |
| 130 } | |
| 131 | |
| 132 // static | |
| 133 GURL URLRequestMockDataJob::GetMockHttpUrl(const std::string& data, | |
| 134 int repeat_count) { | |
| 135 return GetMockHttpUrlForHostname(kMockHostname, data, repeat_count); | |
| 136 } | |
| 137 | |
| 138 // static | |
| 139 GURL URLRequestMockDataJob::GetMockHttpsUrl(const std::string& data, | |
| 140 int repeat_count) { | |
| 141 return GetMockHttpsUrlForHostname(kMockHostname, data, repeat_count); | |
| 142 } | |
| 143 | |
| 144 // static | |
| 145 GURL URLRequestMockDataJob::GetMockHttpUrlForHostname( | |
| 146 const std::string& hostname, | |
| 147 const std::string& data, | |
| 148 int repeat_count) { | |
| 149 return GetMockUrl("http", hostname, data, repeat_count); | |
| 150 } | |
| 151 | |
| 152 // static | |
| 153 GURL URLRequestMockDataJob::GetMockHttpsUrlForHostname( | |
| 154 const std::string& hostname, | |
| 155 const std::string& data, | |
| 156 int repeat_count) { | |
| 157 return GetMockUrl("https", hostname, data, repeat_count); | |
|
mmenke
2014/12/09 21:00:04
repeat_count is an int, GetMockUrl is defined as t
mef
2014/12/09 22:20:59
Done.
| |
| 158 } | |
| 159 | |
| 160 // static | |
| 161 net::URLRequestJob* URLRequestMockDataJob::Factory( | |
| 162 net::URLRequest* request, | |
| 163 net::NetworkDelegate* network_delegate, | |
| 164 const std::string& scheme) { | |
| 165 return new URLRequestMockDataJob( | |
| 166 request, network_delegate, GetDataFromRequest(request), | |
| 167 GetRepeatCountFromRequest(request)); | |
| 168 } | |
| 169 | |
| 170 } // namespace net | |
| OLD | NEW |