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

Side by Side Diff: ios/net/protocol_handler_util_unittest.mm

Issue 1861593005: Convert //ios from scoped_ptr to std::unique_ptr. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase? Created 4 years, 8 months 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
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ios/net/protocol_handler_util.h" 5 #import "ios/net/protocol_handler_util.h"
6 6
7 #include <memory>
7 #include <utility> 8 #include <utility>
8 9
9 #include "base/mac/scoped_nsobject.h" 10 #include "base/mac/scoped_nsobject.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/ptr_util.h"
11 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 13 #include "base/run_loop.h"
13 #include "base/strings/sys_string_conversions.h" 14 #include "base/strings/sys_string_conversions.h"
14 #include "net/base/elements_upload_data_stream.h" 15 #include "net/base/elements_upload_data_stream.h"
15 #import "net/base/mac/url_conversions.h" 16 #import "net/base/mac/url_conversions.h"
16 #include "net/base/upload_bytes_element_reader.h" 17 #include "net/base/upload_bytes_element_reader.h"
17 #include "net/http/http_request_headers.h" 18 #include "net/http/http_request_headers.h"
18 #include "net/http/http_response_headers.h" 19 #include "net/http/http_response_headers.h"
19 #include "net/url_request/data_protocol_handler.h" 20 #include "net/url_request/data_protocol_handler.h"
20 #include "net/url_request/url_request.h" 21 #include "net/url_request/url_request.h"
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 return new HeadersURLRequestJob(request); 108 return new HeadersURLRequestJob(request);
108 } 109 }
109 }; 110 };
110 111
111 class ProtocolHandlerUtilTest : public testing::Test, 112 class ProtocolHandlerUtilTest : public testing::Test,
112 public URLRequest::Delegate { 113 public URLRequest::Delegate {
113 public: 114 public:
114 ProtocolHandlerUtilTest() : request_context_(new TestURLRequestContext) { 115 ProtocolHandlerUtilTest() : request_context_(new TestURLRequestContext) {
115 // Ownership of the protocol handlers is transferred to the factory. 116 // Ownership of the protocol handlers is transferred to the factory.
116 job_factory_.SetProtocolHandler("http", 117 job_factory_.SetProtocolHandler("http",
117 make_scoped_ptr(new NetProtocolHandler)); 118 base::WrapUnique(new NetProtocolHandler));
118 job_factory_.SetProtocolHandler("data", 119 job_factory_.SetProtocolHandler("data",
119 make_scoped_ptr(new DataProtocolHandler)); 120 base::WrapUnique(new DataProtocolHandler));
120 request_context_->set_job_factory(&job_factory_); 121 request_context_->set_job_factory(&job_factory_);
121 } 122 }
122 123
123 NSURLResponse* BuildDataURLResponse(const std::string& mime_type, 124 NSURLResponse* BuildDataURLResponse(const std::string& mime_type,
124 const std::string& encoding, 125 const std::string& encoding,
125 const std::string& content) { 126 const std::string& content) {
126 // Build an URL in the form "data:<mime_type>;charset=<encoding>,<content>" 127 // Build an URL in the form "data:<mime_type>;charset=<encoding>,<content>"
127 // The ';' is removed if mime_type or charset is empty. 128 // The ';' is removed if mime_type or charset is empty.
128 std::string url_string = std::string("data:") + mime_type; 129 std::string url_string = std::string("data:") + mime_type;
129 if (!encoding.empty()) 130 if (!encoding.empty())
130 url_string += ";charset=" + encoding; 131 url_string += ";charset=" + encoding;
131 url_string += ","; 132 url_string += ",";
132 GURL url(url_string); 133 GURL url(url_string);
133 134
134 scoped_ptr<URLRequest> request( 135 std::unique_ptr<URLRequest> request(
135 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this)); 136 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this));
136 request->Start(); 137 request->Start();
137 base::RunLoop loop; 138 base::RunLoop loop;
138 loop.RunUntilIdle(); 139 loop.RunUntilIdle();
139 return GetNSURLResponseForRequest(request.get()); 140 return GetNSURLResponseForRequest(request.get());
140 } 141 }
141 142
142 void CheckDataResponse(NSURLResponse* response, 143 void CheckDataResponse(NSURLResponse* response,
143 const std::string& mime_type, 144 const std::string& mime_type,
144 const std::string& encoding) { 145 const std::string& encoding) {
145 EXPECT_NSEQ(base::SysUTF8ToNSString(mime_type), [response MIMEType]); 146 EXPECT_NSEQ(base::SysUTF8ToNSString(mime_type), [response MIMEType]);
146 EXPECT_NSEQ(base::SysUTF8ToNSString(encoding), [response textEncodingName]); 147 EXPECT_NSEQ(base::SysUTF8ToNSString(encoding), [response textEncodingName]);
147 // The response class must be NSURLResponse (and not NSHTTPURLResponse) when 148 // The response class must be NSURLResponse (and not NSHTTPURLResponse) when
148 // the scheme is "data". 149 // the scheme is "data".
149 EXPECT_TRUE([response isMemberOfClass:[NSURLResponse class]]); 150 EXPECT_TRUE([response isMemberOfClass:[NSURLResponse class]]);
150 } 151 }
151 152
152 void OnResponseStarted(URLRequest* request) override {} 153 void OnResponseStarted(URLRequest* request) override {}
153 void OnReadCompleted(URLRequest* request, int bytes_read) override {} 154 void OnReadCompleted(URLRequest* request, int bytes_read) override {}
154 155
155 protected: 156 protected:
156 base::MessageLoop loop_; 157 base::MessageLoop loop_;
157 URLRequestJobFactoryImpl job_factory_; 158 URLRequestJobFactoryImpl job_factory_;
158 scoped_ptr<URLRequestContext> request_context_; 159 std::unique_ptr<URLRequestContext> request_context_;
159 }; 160 };
160 161
161 } // namespace 162 } // namespace
162 163
163 TEST_F(ProtocolHandlerUtilTest, GetResponseDataSchemeTest) { 164 TEST_F(ProtocolHandlerUtilTest, GetResponseDataSchemeTest) {
164 NSURLResponse* response; 165 NSURLResponse* response;
165 // MIME type and charset are correctly carried over. 166 // MIME type and charset are correctly carried over.
166 response = BuildDataURLResponse("#mime=type'", "$(charset-*", "content"); 167 response = BuildDataURLResponse("#mime=type'", "$(charset-*", "content");
167 CheckDataResponse(response, "#mime=type'", "$(charset-*"); 168 CheckDataResponse(response, "#mime=type'", "$(charset-*");
168 // Missing values are treated as default values. 169 // Missing values are treated as default values.
169 response = BuildDataURLResponse("", "", "content"); 170 response = BuildDataURLResponse("", "", "content");
170 CheckDataResponse(response, kTextPlain, kAscii); 171 CheckDataResponse(response, kTextPlain, kAscii);
171 } 172 }
172 173
173 TEST_F(ProtocolHandlerUtilTest, GetResponseHttpTest) { 174 TEST_F(ProtocolHandlerUtilTest, GetResponseHttpTest) {
174 // Create a request. 175 // Create a request.
175 GURL url(std::string("http://url")); 176 GURL url(std::string("http://url"));
176 scoped_ptr<URLRequest> request( 177 std::unique_ptr<URLRequest> request(
177 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this)); 178 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this));
178 request->Start(); 179 request->Start();
179 // Create a response from the request. 180 // Create a response from the request.
180 NSURLResponse* response = GetNSURLResponseForRequest(request.get()); 181 NSURLResponse* response = GetNSURLResponseForRequest(request.get());
181 EXPECT_NSEQ([NSString stringWithUTF8String:kTextHtml], [response MIMEType]); 182 EXPECT_NSEQ([NSString stringWithUTF8String:kTextHtml], [response MIMEType]);
182 ASSERT_TRUE([response isKindOfClass:[NSHTTPURLResponse class]]); 183 ASSERT_TRUE([response isKindOfClass:[NSHTTPURLResponse class]]);
183 NSHTTPURLResponse* http_response = (NSHTTPURLResponse*)response; 184 NSHTTPURLResponse* http_response = (NSHTTPURLResponse*)response;
184 NSDictionary* headers = [http_response allHeaderFields]; 185 NSDictionary* headers = [http_response allHeaderFields];
185 // Check the headers, duplicates must be appended. 186 // Check the headers, duplicates must be appended.
186 EXPECT_EQ(5u, [headers count]); 187 EXPECT_EQ(5u, [headers count]);
187 NSString* foo_header = [headers objectForKey:@"Foo"]; 188 NSString* foo_header = [headers objectForKey:@"Foo"];
188 EXPECT_NSEQ(@"A,D,E", foo_header); 189 EXPECT_NSEQ(@"A,D,E", foo_header);
189 NSString* bar_header = [headers objectForKey:@"Bar"]; 190 NSString* bar_header = [headers objectForKey:@"Bar"];
190 EXPECT_NSEQ(@"B,F", bar_header); 191 EXPECT_NSEQ(@"B,F", bar_header);
191 NSString* baz_header = [headers objectForKey:@"Baz"]; 192 NSString* baz_header = [headers objectForKey:@"Baz"];
192 EXPECT_NSEQ(@"C", baz_header); 193 EXPECT_NSEQ(@"C", baz_header);
193 NSString* cache_header = [headers objectForKey:@"Cache-Control"]; 194 NSString* cache_header = [headers objectForKey:@"Cache-Control"];
194 EXPECT_NSEQ(@"no-store", cache_header); // Cache-Control is overridden. 195 EXPECT_NSEQ(@"no-store", cache_header); // Cache-Control is overridden.
195 // Check the status. 196 // Check the status.
196 EXPECT_EQ(request->GetResponseCode(), [http_response statusCode]); 197 EXPECT_EQ(request->GetResponseCode(), [http_response statusCode]);
197 } 198 }
198 199
199 TEST_F(ProtocolHandlerUtilTest, BadHttpContentType) { 200 TEST_F(ProtocolHandlerUtilTest, BadHttpContentType) {
200 // Create a request using the magic domain that triggers a garbage 201 // Create a request using the magic domain that triggers a garbage
201 // content-type in the test framework. 202 // content-type in the test framework.
202 GURL url(std::string("http://badcontenttype")); 203 GURL url(std::string("http://badcontenttype"));
203 scoped_ptr<URLRequest> request( 204 std::unique_ptr<URLRequest> request(
204 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this)); 205 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this));
205 request->Start(); 206 request->Start();
206 // Create a response from the request. 207 // Create a response from the request.
207 @try { 208 @try {
208 GetNSURLResponseForRequest(request.get()); 209 GetNSURLResponseForRequest(request.get());
209 } 210 }
210 @catch (id exception) { 211 @catch (id exception) {
211 FAIL() << "Exception while creating response"; 212 FAIL() << "Exception while creating response";
212 } 213 }
213 } 214 }
214 215
215 TEST_F(ProtocolHandlerUtilTest, MultipleHttpContentType) { 216 TEST_F(ProtocolHandlerUtilTest, MultipleHttpContentType) {
216 // Create a request using the magic domain that triggers a garbage 217 // Create a request using the magic domain that triggers a garbage
217 // content-type in the test framework. 218 // content-type in the test framework.
218 GURL url(std::string("http://multiplecontenttype")); 219 GURL url(std::string("http://multiplecontenttype"));
219 scoped_ptr<URLRequest> request( 220 std::unique_ptr<URLRequest> request(
220 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this)); 221 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this));
221 request->Start(); 222 request->Start();
222 // Create a response from the request. 223 // Create a response from the request.
223 NSURLResponse* response = GetNSURLResponseForRequest(request.get()); 224 NSURLResponse* response = GetNSURLResponseForRequest(request.get());
224 EXPECT_NSEQ(@"text/plain", [response MIMEType]); 225 EXPECT_NSEQ(@"text/plain", [response MIMEType]);
225 EXPECT_NSEQ(@"iso-8859-4", [response textEncodingName]); 226 EXPECT_NSEQ(@"iso-8859-4", [response textEncodingName]);
226 NSHTTPURLResponse* http_response = (NSHTTPURLResponse*)response; 227 NSHTTPURLResponse* http_response = (NSHTTPURLResponse*)response;
227 NSDictionary* headers = [http_response allHeaderFields]; 228 NSDictionary* headers = [http_response allHeaderFields];
228 NSString* content_type_header = [headers objectForKey:@"Content-Type"]; 229 NSString* content_type_header = [headers objectForKey:@"Content-Type"];
229 EXPECT_NSEQ(@"text/plain; charset=iso-8859-4", content_type_header); 230 EXPECT_NSEQ(@"text/plain; charset=iso-8859-4", content_type_header);
230 } 231 }
231 232
232 TEST_F(ProtocolHandlerUtilTest, CopyHttpHeaders) { 233 TEST_F(ProtocolHandlerUtilTest, CopyHttpHeaders) {
233 GURL url(std::string("http://url")); 234 GURL url(std::string("http://url"));
234 base::scoped_nsobject<NSMutableURLRequest> in_request( 235 base::scoped_nsobject<NSMutableURLRequest> in_request(
235 [[NSMutableURLRequest alloc] initWithURL:NSURLWithGURL(url)]); 236 [[NSMutableURLRequest alloc] initWithURL:NSURLWithGURL(url)]);
236 [in_request setAllHTTPHeaderFields:@{ 237 [in_request setAllHTTPHeaderFields:@{
237 @"Referer" : @"referrer", 238 @"Referer" : @"referrer",
238 @"User-Agent" : @"secret", 239 @"User-Agent" : @"secret",
239 @"Accept" : @"money/cash", 240 @"Accept" : @"money/cash",
240 @"Foo" : @"bar", 241 @"Foo" : @"bar",
241 }]; 242 }];
242 scoped_ptr<URLRequest> out_request( 243 std::unique_ptr<URLRequest> out_request(
243 request_context_->CreateRequest(url, DEFAULT_PRIORITY, nullptr)); 244 request_context_->CreateRequest(url, DEFAULT_PRIORITY, nullptr));
244 CopyHttpHeaders(in_request, out_request.get()); 245 CopyHttpHeaders(in_request, out_request.get());
245 246
246 EXPECT_EQ("referrer", out_request->referrer()); 247 EXPECT_EQ("referrer", out_request->referrer());
247 const HttpRequestHeaders& headers = out_request->extra_request_headers(); 248 const HttpRequestHeaders& headers = out_request->extra_request_headers();
248 EXPECT_FALSE(headers.HasHeader("User-Agent")); // User agent is not copied. 249 EXPECT_FALSE(headers.HasHeader("User-Agent")); // User agent is not copied.
249 EXPECT_FALSE(headers.HasHeader("Content-Type")); // Only in POST requests. 250 EXPECT_FALSE(headers.HasHeader("Content-Type")); // Only in POST requests.
250 std::string header; 251 std::string header;
251 EXPECT_TRUE(headers.GetHeader("Accept", &header)); 252 EXPECT_TRUE(headers.GetHeader("Accept", &header));
252 EXPECT_EQ("money/cash", header); 253 EXPECT_EQ("money/cash", header);
253 EXPECT_TRUE(headers.GetHeader("Foo", &header)); 254 EXPECT_TRUE(headers.GetHeader("Foo", &header));
254 EXPECT_EQ("bar", header); 255 EXPECT_EQ("bar", header);
255 } 256 }
256 257
257 TEST_F(ProtocolHandlerUtilTest, AddMissingHeaders) { 258 TEST_F(ProtocolHandlerUtilTest, AddMissingHeaders) {
258 GURL url(std::string("http://url")); 259 GURL url(std::string("http://url"));
259 base::scoped_nsobject<NSMutableURLRequest> in_request( 260 base::scoped_nsobject<NSMutableURLRequest> in_request(
260 [[NSMutableURLRequest alloc] initWithURL:NSURLWithGURL(url)]); 261 [[NSMutableURLRequest alloc] initWithURL:NSURLWithGURL(url)]);
261 scoped_ptr<URLRequest> out_request( 262 std::unique_ptr<URLRequest> out_request(
262 request_context_->CreateRequest(url, DEFAULT_PRIORITY, nullptr)); 263 request_context_->CreateRequest(url, DEFAULT_PRIORITY, nullptr));
263 out_request->set_method("POST"); 264 out_request->set_method("POST");
264 scoped_ptr<UploadElementReader> reader( 265 std::unique_ptr<UploadElementReader> reader(
265 new UploadBytesElementReader(nullptr, 0)); 266 new UploadBytesElementReader(nullptr, 0));
266 out_request->set_upload( 267 out_request->set_upload(
267 ElementsUploadDataStream::CreateWithReader(std::move(reader), 0)); 268 ElementsUploadDataStream::CreateWithReader(std::move(reader), 0));
268 CopyHttpHeaders(in_request, out_request.get()); 269 CopyHttpHeaders(in_request, out_request.get());
269 270
270 // Some headers are added by default if missing. 271 // Some headers are added by default if missing.
271 const HttpRequestHeaders& headers = out_request->extra_request_headers(); 272 const HttpRequestHeaders& headers = out_request->extra_request_headers();
272 std::string header; 273 std::string header;
273 EXPECT_TRUE(headers.GetHeader("Accept", &header)); 274 EXPECT_TRUE(headers.GetHeader("Accept", &header));
274 EXPECT_EQ("*/*", header); 275 EXPECT_EQ("*/*", header);
275 EXPECT_TRUE(headers.GetHeader("Content-Type", &header)); 276 EXPECT_TRUE(headers.GetHeader("Content-Type", &header));
276 EXPECT_EQ("application/x-www-form-urlencoded", header); 277 EXPECT_EQ("application/x-www-form-urlencoded", header);
277 } 278 }
278 279
279 } // namespace net 280 } // namespace net
OLDNEW
« no previous file with comments | « ios/net/http_response_headers_util.h ('k') | ios/public/provider/chrome/browser/chrome_browser_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698