OLD | NEW |
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 #include "base/mac/scoped_nsobject.h" | 5 #include "base/mac/scoped_nsobject.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #import "ios/net/protocol_handler_util.h" | 10 #import "ios/net/protocol_handler_util.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 const std::string& content) { | 120 const std::string& content) { |
121 // Build an URL in the form "data:<mime_type>;charset=<encoding>,<content>" | 121 // Build an URL in the form "data:<mime_type>;charset=<encoding>,<content>" |
122 // The ';' is removed if mime_type or charset is empty. | 122 // The ';' is removed if mime_type or charset is empty. |
123 std::string url_string = std::string("data:") + mime_type; | 123 std::string url_string = std::string("data:") + mime_type; |
124 if (!encoding.empty()) | 124 if (!encoding.empty()) |
125 url_string += ";charset=" + encoding; | 125 url_string += ";charset=" + encoding; |
126 url_string += ","; | 126 url_string += ","; |
127 GURL url(url_string); | 127 GURL url(url_string); |
128 | 128 |
129 scoped_ptr<URLRequest> request( | 129 scoped_ptr<URLRequest> request( |
130 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this, nullptr)); | 130 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this)); |
131 request->Start(); | 131 request->Start(); |
132 base::RunLoop loop; | 132 base::RunLoop loop; |
133 loop.RunUntilIdle(); | 133 loop.RunUntilIdle(); |
134 return GetNSURLResponseForRequest(request.get()); | 134 return GetNSURLResponseForRequest(request.get()); |
135 } | 135 } |
136 | 136 |
137 void CheckDataResponse(NSURLResponse* response, | 137 void CheckDataResponse(NSURLResponse* response, |
138 const std::string& mime_type, | 138 const std::string& mime_type, |
139 const std::string& encoding) { | 139 const std::string& encoding) { |
140 EXPECT_NSEQ(base::SysUTF8ToNSString(mime_type), [response MIMEType]); | 140 EXPECT_NSEQ(base::SysUTF8ToNSString(mime_type), [response MIMEType]); |
(...skipping 21 matching lines...) Expand all Loading... |
162 CheckDataResponse(response, "#mime=type'", "$(charset-*"); | 162 CheckDataResponse(response, "#mime=type'", "$(charset-*"); |
163 // Missing values are treated as default values. | 163 // Missing values are treated as default values. |
164 response = BuildDataURLResponse("", "", "content"); | 164 response = BuildDataURLResponse("", "", "content"); |
165 CheckDataResponse(response, kTextPlain, kAscii); | 165 CheckDataResponse(response, kTextPlain, kAscii); |
166 } | 166 } |
167 | 167 |
168 TEST_F(ProtocolHandlerUtilTest, GetResponseHttpTest) { | 168 TEST_F(ProtocolHandlerUtilTest, GetResponseHttpTest) { |
169 // Create a request. | 169 // Create a request. |
170 GURL url(std::string("http://url")); | 170 GURL url(std::string("http://url")); |
171 scoped_ptr<URLRequest> request( | 171 scoped_ptr<URLRequest> request( |
172 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this, nullptr)); | 172 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this)); |
173 request->Start(); | 173 request->Start(); |
174 // Create a response from the request. | 174 // Create a response from the request. |
175 NSURLResponse* response = GetNSURLResponseForRequest(request.get()); | 175 NSURLResponse* response = GetNSURLResponseForRequest(request.get()); |
176 EXPECT_NSEQ([NSString stringWithUTF8String:kTextHtml], [response MIMEType]); | 176 EXPECT_NSEQ([NSString stringWithUTF8String:kTextHtml], [response MIMEType]); |
177 ASSERT_TRUE([response isKindOfClass:[NSHTTPURLResponse class]]); | 177 ASSERT_TRUE([response isKindOfClass:[NSHTTPURLResponse class]]); |
178 NSHTTPURLResponse* http_response = (NSHTTPURLResponse*)response; | 178 NSHTTPURLResponse* http_response = (NSHTTPURLResponse*)response; |
179 NSDictionary* headers = [http_response allHeaderFields]; | 179 NSDictionary* headers = [http_response allHeaderFields]; |
180 // Check the headers, duplicates must be appended. | 180 // Check the headers, duplicates must be appended. |
181 EXPECT_EQ(5u, [headers count]); | 181 EXPECT_EQ(5u, [headers count]); |
182 NSString* foo_header = [headers objectForKey:@"Foo"]; | 182 NSString* foo_header = [headers objectForKey:@"Foo"]; |
183 EXPECT_NSEQ(@"A,D,E", foo_header); | 183 EXPECT_NSEQ(@"A,D,E", foo_header); |
184 NSString* bar_header = [headers objectForKey:@"Bar"]; | 184 NSString* bar_header = [headers objectForKey:@"Bar"]; |
185 EXPECT_NSEQ(@"B,F", bar_header); | 185 EXPECT_NSEQ(@"B,F", bar_header); |
186 NSString* baz_header = [headers objectForKey:@"Baz"]; | 186 NSString* baz_header = [headers objectForKey:@"Baz"]; |
187 EXPECT_NSEQ(@"C", baz_header); | 187 EXPECT_NSEQ(@"C", baz_header); |
188 NSString* cache_header = [headers objectForKey:@"Cache-Control"]; | 188 NSString* cache_header = [headers objectForKey:@"Cache-Control"]; |
189 EXPECT_NSEQ(@"no-store", cache_header); // Cache-Control is overridden. | 189 EXPECT_NSEQ(@"no-store", cache_header); // Cache-Control is overridden. |
190 // Check the status. | 190 // Check the status. |
191 EXPECT_EQ(request->GetResponseCode(), [http_response statusCode]); | 191 EXPECT_EQ(request->GetResponseCode(), [http_response statusCode]); |
192 } | 192 } |
193 | 193 |
194 TEST_F(ProtocolHandlerUtilTest, BadHttpContentType) { | 194 TEST_F(ProtocolHandlerUtilTest, BadHttpContentType) { |
195 // Create a request using the magic domain that triggers a garbage | 195 // Create a request using the magic domain that triggers a garbage |
196 // content-type in the test framework. | 196 // content-type in the test framework. |
197 GURL url(std::string("http://badcontenttype")); | 197 GURL url(std::string("http://badcontenttype")); |
198 scoped_ptr<URLRequest> request( | 198 scoped_ptr<URLRequest> request( |
199 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this, nullptr)); | 199 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this)); |
200 request->Start(); | 200 request->Start(); |
201 // Create a response from the request. | 201 // Create a response from the request. |
202 @try { | 202 @try { |
203 GetNSURLResponseForRequest(request.get()); | 203 GetNSURLResponseForRequest(request.get()); |
204 } | 204 } |
205 @catch (id exception) { | 205 @catch (id exception) { |
206 FAIL() << "Exception while creating response"; | 206 FAIL() << "Exception while creating response"; |
207 } | 207 } |
208 } | 208 } |
209 | 209 |
210 TEST_F(ProtocolHandlerUtilTest, MultipleHttpContentType) { | 210 TEST_F(ProtocolHandlerUtilTest, MultipleHttpContentType) { |
211 // Create a request using the magic domain that triggers a garbage | 211 // Create a request using the magic domain that triggers a garbage |
212 // content-type in the test framework. | 212 // content-type in the test framework. |
213 GURL url(std::string("http://multiplecontenttype")); | 213 GURL url(std::string("http://multiplecontenttype")); |
214 scoped_ptr<URLRequest> request( | 214 scoped_ptr<URLRequest> request( |
215 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this, nullptr)); | 215 request_context_->CreateRequest(url, DEFAULT_PRIORITY, this)); |
216 request->Start(); | 216 request->Start(); |
217 // Create a response from the request. | 217 // Create a response from the request. |
218 NSURLResponse* response = GetNSURLResponseForRequest(request.get()); | 218 NSURLResponse* response = GetNSURLResponseForRequest(request.get()); |
219 EXPECT_NSEQ(@"text/plain", [response MIMEType]); | 219 EXPECT_NSEQ(@"text/plain", [response MIMEType]); |
220 EXPECT_NSEQ(@"iso-8859-4", [response textEncodingName]); | 220 EXPECT_NSEQ(@"iso-8859-4", [response textEncodingName]); |
221 NSHTTPURLResponse* http_response = (NSHTTPURLResponse*)response; | 221 NSHTTPURLResponse* http_response = (NSHTTPURLResponse*)response; |
222 NSDictionary* headers = [http_response allHeaderFields]; | 222 NSDictionary* headers = [http_response allHeaderFields]; |
223 NSString* content_type_header = [headers objectForKey:@"Content-Type"]; | 223 NSString* content_type_header = [headers objectForKey:@"Content-Type"]; |
224 EXPECT_NSEQ(@"text/plain; charset=iso-8859-4", content_type_header); | 224 EXPECT_NSEQ(@"text/plain; charset=iso-8859-4", content_type_header); |
225 } | 225 } |
226 | 226 |
227 TEST_F(ProtocolHandlerUtilTest, CopyHttpHeaders) { | 227 TEST_F(ProtocolHandlerUtilTest, CopyHttpHeaders) { |
228 GURL url(std::string("http://url")); | 228 GURL url(std::string("http://url")); |
229 base::scoped_nsobject<NSMutableURLRequest> in_request( | 229 base::scoped_nsobject<NSMutableURLRequest> in_request( |
230 [[NSMutableURLRequest alloc] initWithURL:NSURLWithGURL(url)]); | 230 [[NSMutableURLRequest alloc] initWithURL:NSURLWithGURL(url)]); |
231 [in_request setAllHTTPHeaderFields:@{ | 231 [in_request setAllHTTPHeaderFields:@{ |
232 @"Referer" : @"referrer", | 232 @"Referer" : @"referrer", |
233 @"User-Agent" : @"secret", | 233 @"User-Agent" : @"secret", |
234 @"Accept" : @"money/cash", | 234 @"Accept" : @"money/cash", |
235 @"Foo" : @"bar", | 235 @"Foo" : @"bar", |
236 }]; | 236 }]; |
237 scoped_ptr<URLRequest> out_request( | 237 scoped_ptr<URLRequest> out_request( |
238 request_context_->CreateRequest(url, DEFAULT_PRIORITY, nullptr, nullptr)); | 238 request_context_->CreateRequest(url, DEFAULT_PRIORITY, nullptr)); |
239 CopyHttpHeaders(in_request, out_request.get()); | 239 CopyHttpHeaders(in_request, out_request.get()); |
240 | 240 |
241 EXPECT_EQ("referrer", out_request->referrer()); | 241 EXPECT_EQ("referrer", out_request->referrer()); |
242 const HttpRequestHeaders& headers = out_request->extra_request_headers(); | 242 const HttpRequestHeaders& headers = out_request->extra_request_headers(); |
243 EXPECT_FALSE(headers.HasHeader("User-Agent")); // User agent is not copied. | 243 EXPECT_FALSE(headers.HasHeader("User-Agent")); // User agent is not copied. |
244 EXPECT_FALSE(headers.HasHeader("Content-Type")); // Only in POST requests. | 244 EXPECT_FALSE(headers.HasHeader("Content-Type")); // Only in POST requests. |
245 std::string header; | 245 std::string header; |
246 EXPECT_TRUE(headers.GetHeader("Accept", &header)); | 246 EXPECT_TRUE(headers.GetHeader("Accept", &header)); |
247 EXPECT_EQ("money/cash", header); | 247 EXPECT_EQ("money/cash", header); |
248 EXPECT_TRUE(headers.GetHeader("Foo", &header)); | 248 EXPECT_TRUE(headers.GetHeader("Foo", &header)); |
249 EXPECT_EQ("bar", header); | 249 EXPECT_EQ("bar", header); |
250 } | 250 } |
251 | 251 |
252 TEST_F(ProtocolHandlerUtilTest, AddMissingHeaders) { | 252 TEST_F(ProtocolHandlerUtilTest, AddMissingHeaders) { |
253 GURL url(std::string("http://url")); | 253 GURL url(std::string("http://url")); |
254 base::scoped_nsobject<NSMutableURLRequest> in_request( | 254 base::scoped_nsobject<NSMutableURLRequest> in_request( |
255 [[NSMutableURLRequest alloc] initWithURL:NSURLWithGURL(url)]); | 255 [[NSMutableURLRequest alloc] initWithURL:NSURLWithGURL(url)]); |
256 scoped_ptr<URLRequest> out_request( | 256 scoped_ptr<URLRequest> out_request( |
257 request_context_->CreateRequest(url, DEFAULT_PRIORITY, nullptr, nullptr)); | 257 request_context_->CreateRequest(url, DEFAULT_PRIORITY, nullptr)); |
258 out_request->set_method("POST"); | 258 out_request->set_method("POST"); |
259 scoped_ptr<UploadElementReader> reader( | 259 scoped_ptr<UploadElementReader> reader( |
260 new UploadBytesElementReader(nullptr, 0)); | 260 new UploadBytesElementReader(nullptr, 0)); |
261 out_request->set_upload( | 261 out_request->set_upload( |
262 ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0)); | 262 ElementsUploadDataStream::CreateWithReader(reader.Pass(), 0)); |
263 CopyHttpHeaders(in_request, out_request.get()); | 263 CopyHttpHeaders(in_request, out_request.get()); |
264 | 264 |
265 // Some headers are added by default if missing. | 265 // Some headers are added by default if missing. |
266 const HttpRequestHeaders& headers = out_request->extra_request_headers(); | 266 const HttpRequestHeaders& headers = out_request->extra_request_headers(); |
267 std::string header; | 267 std::string header; |
268 EXPECT_TRUE(headers.GetHeader("Accept", &header)); | 268 EXPECT_TRUE(headers.GetHeader("Accept", &header)); |
269 EXPECT_EQ("*/*", header); | 269 EXPECT_EQ("*/*", header); |
270 EXPECT_TRUE(headers.GetHeader("Content-Type", &header)); | 270 EXPECT_TRUE(headers.GetHeader("Content-Type", &header)); |
271 EXPECT_EQ("application/x-www-form-urlencoded", header); | 271 EXPECT_EQ("application/x-www-form-urlencoded", header); |
272 } | 272 } |
273 | 273 |
274 } // namespace net | 274 } // namespace net |
OLD | NEW |