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

Side by Side Diff: net/url_request/url_request_unittest.cc

Issue 1017583002: Set Origin header to "null" for cross origin redirects. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Addressed nits Created 5 years, 9 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #include <shlobj.h> 9 #include <shlobj.h>
10 #endif 10 #endif
(...skipping 2835 matching lines...) Expand 10 before | Expand all | Expand 10 after
2846 if (request_method == redirect_method) { 2846 if (request_method == redirect_method) {
2847 EXPECT_EQ(kData, d.data_received()); 2847 EXPECT_EQ(kData, d.data_received());
2848 } else { 2848 } else {
2849 EXPECT_NE(kData, d.data_received()); 2849 EXPECT_NE(kData, d.data_received());
2850 } 2850 }
2851 } 2851 }
2852 if (HasFailure()) 2852 if (HasFailure())
2853 LOG(WARNING) << "Request method was: " << request_method; 2853 LOG(WARNING) << "Request method was: " << request_method;
2854 } 2854 }
2855 2855
2856 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2857 // |request_method| is the method to use for the initial request.
2858 // |redirect_method| is the method that is expected to be used for the second
2859 // request, after redirection.
2860 // |origin_value| is the expected value for the Origin header after
2861 // redirection. If empty, expects that there will be no Origin header.
2862 void HTTPRedirectOriginHeaderTest(const GURL& redirect_url,
2863 const std::string& request_method,
2864 const std::string& redirect_method,
2865 const std::string& origin_value) {
2866 TestDelegate d;
2867 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
2868 redirect_url, DEFAULT_PRIORITY, &d, NULL));
davidben 2015/03/24 23:47:38 Note: you'll need to remove that trailing NULL on
jww 2015/03/27 22:16:15 Will do.
2869 req->set_method(request_method);
2870 req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin,
2871 redirect_url.GetOrigin().spec(), false);
2872 req->Start();
2873
2874 base::RunLoop().Run();
2875
2876 EXPECT_EQ(redirect_method, req->method());
2877 // Note that there is no check for request success here because, for
2878 // purposes of testing, the request very well may fail. For example, if the
2879 // test redirects to an FTP server so it is cross origin, there is no such
2880 // FTP server, so the request would fail. However, that's fine, as long as
2881 // the request headers are in order and pass the checks below.
davidben 2015/03/24 23:47:38 It's sort of confusing to have these tests use FTP
jww 2015/03/27 22:16:15 Agreed. I've rejiggered all of these new tests to
2882 if (origin_value.empty()) {
2883 EXPECT_FALSE(
2884 req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin));
2885 } else {
2886 std::string origin_header;
2887 EXPECT_TRUE(req->extra_request_headers().GetHeader(
2888 HttpRequestHeaders::kOrigin, &origin_header));
2889 EXPECT_EQ(origin_value, origin_header);
2890 }
2891 }
2892
2856 void HTTPUploadDataOperationTest(const std::string& method) { 2893 void HTTPUploadDataOperationTest(const std::string& method) {
2857 const int kMsgSize = 20000; // multiple of 10 2894 const int kMsgSize = 20000; // multiple of 10
2858 const int kIterations = 50; 2895 const int kIterations = 50;
2859 char* uploadBytes = new char[kMsgSize+1]; 2896 char* uploadBytes = new char[kMsgSize+1];
2860 char* ptr = uploadBytes; 2897 char* ptr = uploadBytes;
2861 char marker = 'a'; 2898 char marker = 'a';
2862 for (int idx = 0; idx < kMsgSize/10; idx++) { 2899 for (int idx = 0; idx < kMsgSize/10; idx++) {
2863 memcpy(ptr, "----------", 10); 2900 memcpy(ptr, "----------", 10);
2864 ptr += 10; 2901 ptr += 10;
2865 if (idx % 100 == 0) { 2902 if (idx % 100 == 0) {
(...skipping 3363 matching lines...) Expand 10 before | Expand all | Expand 10 after
6229 EXPECT_FALSE(ContainsString(data, "Content-Length:")); 6266 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
6230 EXPECT_FALSE(ContainsString(data, "Content-Type:")); 6267 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
6231 EXPECT_FALSE(ContainsString(data, "Origin:")); 6268 EXPECT_FALSE(ContainsString(data, "Origin:"));
6232 6269
6233 // These extra request headers should not have been stripped. 6270 // These extra request headers should not have been stripped.
6234 EXPECT_TRUE(ContainsString(data, "Accept:")); 6271 EXPECT_TRUE(ContainsString(data, "Accept:"));
6235 EXPECT_TRUE(ContainsString(data, "Accept-Language:")); 6272 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
6236 EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); 6273 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
6237 } 6274 }
6238 6275
6239 // The following tests check that we handle mutating the request method for 6276 // The following tests check that we handle mutating the request method for
davidben 2015/03/24 23:47:38 Nit: "request method" -> "request", since you've m
jww 2015/03/27 22:16:15 Done.
6240 // HTTP redirects as expected. 6277 // HTTP redirects as expected.
6241 // See http://crbug.com/56373 and http://crbug.com/102130. 6278 // See http://crbug.com/56373 and http://crbug.com/102130.
davidben 2015/03/24 23:47:38 Nit: I guess cite your bug too?
jww 2015/03/27 22:16:15 Done.
6242 6279
6243 TEST_F(URLRequestTestHTTP, Redirect301Tests) { 6280 TEST_F(URLRequestTestHTTP, Redirect301Tests) {
6244 ASSERT_TRUE(test_server_.Start()); 6281 ASSERT_TRUE(test_server_.Start());
6245 6282
6246 const GURL url = test_server_.GetURL("files/redirect301-to-echo"); 6283 const GURL url = test_server_.GetURL("files/redirect301-to-echo");
6284 const GURL ftp_redirect_url = test_server_.GetURL("files/redirect301-to-ftp");
6247 6285
6248 HTTPRedirectMethodTest(url, "POST", "GET", true); 6286 HTTPRedirectMethodTest(url, "POST", "GET", true);
6249 HTTPRedirectMethodTest(url, "PUT", "PUT", true); 6287 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6250 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 6288 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6289
6290 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6291 HTTPRedirectOriginHeaderTest(ftp_redirect_url, "GET", "GET", "null");
6292 HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string());
davidben 2015/03/24 23:47:38 Aside: does the spec say to drop the Origin header
jww 2015/03/27 22:16:15 No, RFC 6454 does not say to do so, nor can I find
6293 HTTPRedirectOriginHeaderTest(ftp_redirect_url, "POST", "GET", std::string());
6251 } 6294 }
6252 6295
6253 TEST_F(URLRequestTestHTTP, Redirect302Tests) { 6296 TEST_F(URLRequestTestHTTP, Redirect302Tests) {
6254 ASSERT_TRUE(test_server_.Start()); 6297 ASSERT_TRUE(test_server_.Start());
6255 6298
6256 const GURL url = test_server_.GetURL("files/redirect302-to-echo"); 6299 const GURL url = test_server_.GetURL("files/redirect302-to-echo");
6300 const GURL ftp_redirect_url = test_server_.GetURL("files/redirect302-to-ftp");
6257 6301
6258 HTTPRedirectMethodTest(url, "POST", "GET", true); 6302 HTTPRedirectMethodTest(url, "POST", "GET", true);
6259 HTTPRedirectMethodTest(url, "PUT", "PUT", true); 6303 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6260 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 6304 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6305
6306 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6307 HTTPRedirectOriginHeaderTest(ftp_redirect_url, "GET", "GET", "null");
6308 HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string());
6309 HTTPRedirectOriginHeaderTest(ftp_redirect_url, "POST", "GET", std::string());
6261 } 6310 }
6262 6311
6263 TEST_F(URLRequestTestHTTP, Redirect303Tests) { 6312 TEST_F(URLRequestTestHTTP, Redirect303Tests) {
6264 ASSERT_TRUE(test_server_.Start()); 6313 ASSERT_TRUE(test_server_.Start());
6265 6314
6266 const GURL url = test_server_.GetURL("files/redirect303-to-echo"); 6315 const GURL url = test_server_.GetURL("files/redirect303-to-echo");
6316 const GURL ftp_redirect_url = test_server_.GetURL("files/redirect303-to-ftp");
6267 6317
6268 HTTPRedirectMethodTest(url, "POST", "GET", true); 6318 HTTPRedirectMethodTest(url, "POST", "GET", true);
6269 HTTPRedirectMethodTest(url, "PUT", "GET", true); 6319 HTTPRedirectMethodTest(url, "PUT", "GET", true);
6270 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 6320 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6321
6322 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6323 HTTPRedirectOriginHeaderTest(ftp_redirect_url, "GET", "GET", "null");
6324 HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string());
6325 HTTPRedirectOriginHeaderTest(ftp_redirect_url, "POST", "GET", std::string());
6271 } 6326 }
6272 6327
6273 TEST_F(URLRequestTestHTTP, Redirect307Tests) { 6328 TEST_F(URLRequestTestHTTP, Redirect307Tests) {
6274 ASSERT_TRUE(test_server_.Start()); 6329 ASSERT_TRUE(test_server_.Start());
6275 6330
6276 const GURL url = test_server_.GetURL("files/redirect307-to-echo"); 6331 const GURL url = test_server_.GetURL("files/redirect307-to-echo");
6332 const GURL ftp_redirect_url = test_server_.GetURL("files/redirect307-to-ftp");
6277 6333
6278 HTTPRedirectMethodTest(url, "POST", "POST", true); 6334 HTTPRedirectMethodTest(url, "POST", "POST", true);
6279 HTTPRedirectMethodTest(url, "PUT", "PUT", true); 6335 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6280 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 6336 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6337
6338 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6339 HTTPRedirectOriginHeaderTest(ftp_redirect_url, "GET", "GET", "null");
6340 HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec());
6341 HTTPRedirectOriginHeaderTest(ftp_redirect_url, "POST", "POST", "null");
6281 } 6342 }
6282 6343
6283 TEST_F(URLRequestTestHTTP, Redirect308Tests) { 6344 TEST_F(URLRequestTestHTTP, Redirect308Tests) {
6284 ASSERT_TRUE(test_server_.Start()); 6345 ASSERT_TRUE(test_server_.Start());
6285 6346
6286 const GURL url = test_server_.GetURL("files/redirect308-to-echo"); 6347 const GURL url = test_server_.GetURL("files/redirect308-to-echo");
6348 const GURL ftp_redirect_url = test_server_.GetURL("files/redirect308-to-ftp");
6287 6349
6288 HTTPRedirectMethodTest(url, "POST", "POST", true); 6350 HTTPRedirectMethodTest(url, "POST", "POST", true);
6289 HTTPRedirectMethodTest(url, "PUT", "PUT", true); 6351 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6290 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 6352 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6353
6354 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6355 HTTPRedirectOriginHeaderTest(ftp_redirect_url, "GET", "GET", "null");
6356 HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec());
6357 HTTPRedirectOriginHeaderTest(ftp_redirect_url, "POST", "POST", "null");
6291 } 6358 }
6292 6359
6293 // Make sure that 308 responses without bodies are not treated as redirects. 6360 // Make sure that 308 responses without bodies are not treated as redirects.
6294 // Certain legacy apis that pre-date the response code expect this behavior 6361 // Certain legacy apis that pre-date the response code expect this behavior
6295 // (Like Google Drive). 6362 // (Like Google Drive).
6296 TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { 6363 TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) {
6297 ASSERT_TRUE(test_server_.Start()); 6364 ASSERT_TRUE(test_server_.Start());
6298 6365
6299 TestDelegate d; 6366 TestDelegate d;
6300 const GURL url = test_server_.GetURL("files/308-without-location-header"); 6367 const GURL url = test_server_.GetURL("files/308-without-location-header");
(...skipping 2654 matching lines...) Expand 10 before | Expand all | Expand 10 after
8955 9022
8956 EXPECT_FALSE(r->is_pending()); 9023 EXPECT_FALSE(r->is_pending());
8957 EXPECT_EQ(1, d->response_started_count()); 9024 EXPECT_EQ(1, d->response_started_count());
8958 EXPECT_FALSE(d->received_data_before_response()); 9025 EXPECT_FALSE(d->received_data_before_response());
8959 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 9026 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8960 } 9027 }
8961 } 9028 }
8962 #endif // !defined(DISABLE_FTP_SUPPORT) 9029 #endif // !defined(DISABLE_FTP_SUPPORT)
8963 9030
8964 } // namespace net 9031 } // namespace net
OLDNEW
« net/url_request/url_request.cc ('K') | « net/url_request/url_request.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698