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

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: Address nits from David Created 5 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
« no previous file with comments | « net/url_request/url_request.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 2772 matching lines...) Expand 10 before | Expand all | Expand 10 after
2783 if (request_method == redirect_method) { 2783 if (request_method == redirect_method) {
2784 EXPECT_EQ(kData, d.data_received()); 2784 EXPECT_EQ(kData, d.data_received());
2785 } else { 2785 } else {
2786 EXPECT_NE(kData, d.data_received()); 2786 EXPECT_NE(kData, d.data_received());
2787 } 2787 }
2788 } 2788 }
2789 if (HasFailure()) 2789 if (HasFailure())
2790 LOG(WARNING) << "Request method was: " << request_method; 2790 LOG(WARNING) << "Request method was: " << request_method;
2791 } 2791 }
2792 2792
2793 // Requests |redirect_url|, which must return a HTTP 3xx redirect.
2794 // |request_method| is the method to use for the initial request.
2795 // |redirect_method| is the method that is expected to be used for the second
2796 // request, after redirection.
2797 // |origin_value| is the expected value for the Origin header after
2798 // redirection. If empty, expects that there will be no Origin header.
2799 void HTTPRedirectOriginHeaderTest(const GURL& redirect_url,
2800 const std::string& request_method,
2801 const std::string& redirect_method,
2802 const std::string& origin_value) {
2803 TestDelegate d;
2804 scoped_ptr<URLRequest> req(
2805 default_context_.CreateRequest(redirect_url, DEFAULT_PRIORITY, &d));
2806 req->set_method(request_method);
2807 req->SetExtraRequestHeaderByName(HttpRequestHeaders::kOrigin,
2808 redirect_url.GetOrigin().spec(), false);
2809 req->Start();
2810
2811 base::RunLoop().Run();
2812
2813 EXPECT_EQ(redirect_method, req->method());
2814 // Note that there is no check for request success here because, for
2815 // purposes of testing, the request very well may fail. For example, if the
2816 // test redirects to an HTTPS server from an HTTP origin, thus it is cross
2817 // origin, there is not an HTTPS server in this unit test framework, so the
2818 // request would fail. However, that's fine, as long as the request headers
2819 // are in order and pass the checks below.
2820 if (origin_value.empty()) {
2821 EXPECT_FALSE(
2822 req->extra_request_headers().HasHeader(HttpRequestHeaders::kOrigin));
2823 } else {
2824 std::string origin_header;
2825 EXPECT_TRUE(req->extra_request_headers().GetHeader(
2826 HttpRequestHeaders::kOrigin, &origin_header));
2827 EXPECT_EQ(origin_value, origin_header);
2828 }
2829 }
2830
2793 void HTTPUploadDataOperationTest(const std::string& method) { 2831 void HTTPUploadDataOperationTest(const std::string& method) {
2794 const int kMsgSize = 20000; // multiple of 10 2832 const int kMsgSize = 20000; // multiple of 10
2795 const int kIterations = 50; 2833 const int kIterations = 50;
2796 char* uploadBytes = new char[kMsgSize+1]; 2834 char* uploadBytes = new char[kMsgSize+1];
2797 char* ptr = uploadBytes; 2835 char* ptr = uploadBytes;
2798 char marker = 'a'; 2836 char marker = 'a';
2799 for (int idx = 0; idx < kMsgSize/10; idx++) { 2837 for (int idx = 0; idx < kMsgSize/10; idx++) {
2800 memcpy(ptr, "----------", 10); 2838 memcpy(ptr, "----------", 10);
2801 ptr += 10; 2839 ptr += 10;
2802 if (idx % 100 == 0) { 2840 if (idx % 100 == 0) {
(...skipping 3346 matching lines...) Expand 10 before | Expand all | Expand 10 after
6149 EXPECT_FALSE(ContainsString(data, "Content-Length:")); 6187 EXPECT_FALSE(ContainsString(data, "Content-Length:"));
6150 EXPECT_FALSE(ContainsString(data, "Content-Type:")); 6188 EXPECT_FALSE(ContainsString(data, "Content-Type:"));
6151 EXPECT_FALSE(ContainsString(data, "Origin:")); 6189 EXPECT_FALSE(ContainsString(data, "Origin:"));
6152 6190
6153 // These extra request headers should not have been stripped. 6191 // These extra request headers should not have been stripped.
6154 EXPECT_TRUE(ContainsString(data, "Accept:")); 6192 EXPECT_TRUE(ContainsString(data, "Accept:"));
6155 EXPECT_TRUE(ContainsString(data, "Accept-Language:")); 6193 EXPECT_TRUE(ContainsString(data, "Accept-Language:"));
6156 EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); 6194 EXPECT_TRUE(ContainsString(data, "Accept-Charset:"));
6157 } 6195 }
6158 6196
6159 // The following tests check that we handle mutating the request method for 6197 // The following tests check that we handle mutating the request for HTTP
6160 // HTTP redirects as expected. 6198 // redirects as expected.
6161 // See http://crbug.com/56373 and http://crbug.com/102130. 6199 // See https://crbug.com/56373, https://crbug.com/102130, and
6200 // https://crbug.com/465517.
6162 6201
6163 TEST_F(URLRequestTestHTTP, Redirect301Tests) { 6202 TEST_F(URLRequestTestHTTP, Redirect301Tests) {
6164 ASSERT_TRUE(test_server_.Start()); 6203 ASSERT_TRUE(test_server_.Start());
6165 6204
6166 const GURL url = test_server_.GetURL("files/redirect301-to-echo"); 6205 const GURL url = test_server_.GetURL("files/redirect301-to-echo");
6206 const GURL https_redirect_url =
6207 test_server_.GetURL("files/redirect301-to-https");
6167 6208
6168 HTTPRedirectMethodTest(url, "POST", "GET", true); 6209 HTTPRedirectMethodTest(url, "POST", "GET", true);
6169 HTTPRedirectMethodTest(url, "PUT", "PUT", true); 6210 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6170 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 6211 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6212
6213 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6214 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6215 HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string());
6216 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET",
6217 std::string());
6171 } 6218 }
6172 6219
6173 TEST_F(URLRequestTestHTTP, Redirect302Tests) { 6220 TEST_F(URLRequestTestHTTP, Redirect302Tests) {
6174 ASSERT_TRUE(test_server_.Start()); 6221 ASSERT_TRUE(test_server_.Start());
6175 6222
6176 const GURL url = test_server_.GetURL("files/redirect302-to-echo"); 6223 const GURL url = test_server_.GetURL("files/redirect302-to-echo");
6224 const GURL https_redirect_url =
6225 test_server_.GetURL("files/redirect302-to-https");
6177 6226
6178 HTTPRedirectMethodTest(url, "POST", "GET", true); 6227 HTTPRedirectMethodTest(url, "POST", "GET", true);
6179 HTTPRedirectMethodTest(url, "PUT", "PUT", true); 6228 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6180 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 6229 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6230
6231 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6232 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6233 HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string());
6234 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET",
6235 std::string());
6181 } 6236 }
6182 6237
6183 TEST_F(URLRequestTestHTTP, Redirect303Tests) { 6238 TEST_F(URLRequestTestHTTP, Redirect303Tests) {
6184 ASSERT_TRUE(test_server_.Start()); 6239 ASSERT_TRUE(test_server_.Start());
6185 6240
6186 const GURL url = test_server_.GetURL("files/redirect303-to-echo"); 6241 const GURL url = test_server_.GetURL("files/redirect303-to-echo");
6242 const GURL https_redirect_url =
6243 test_server_.GetURL("files/redirect303-to-https");
6187 6244
6188 HTTPRedirectMethodTest(url, "POST", "GET", true); 6245 HTTPRedirectMethodTest(url, "POST", "GET", true);
6189 HTTPRedirectMethodTest(url, "PUT", "GET", true); 6246 HTTPRedirectMethodTest(url, "PUT", "GET", true);
6190 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 6247 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6248
6249 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6250 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6251 HTTPRedirectOriginHeaderTest(url, "POST", "GET", std::string());
6252 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "GET",
6253 std::string());
6191 } 6254 }
6192 6255
6193 TEST_F(URLRequestTestHTTP, Redirect307Tests) { 6256 TEST_F(URLRequestTestHTTP, Redirect307Tests) {
6194 ASSERT_TRUE(test_server_.Start()); 6257 ASSERT_TRUE(test_server_.Start());
6195 6258
6196 const GURL url = test_server_.GetURL("files/redirect307-to-echo"); 6259 const GURL url = test_server_.GetURL("files/redirect307-to-echo");
6260 const GURL https_redirect_url =
6261 test_server_.GetURL("files/redirect307-to-https");
6197 6262
6198 HTTPRedirectMethodTest(url, "POST", "POST", true); 6263 HTTPRedirectMethodTest(url, "POST", "POST", true);
6199 HTTPRedirectMethodTest(url, "PUT", "PUT", true); 6264 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6200 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 6265 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6266
6267 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6268 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6269 HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec());
6270 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null");
6201 } 6271 }
6202 6272
6203 TEST_F(URLRequestTestHTTP, Redirect308Tests) { 6273 TEST_F(URLRequestTestHTTP, Redirect308Tests) {
6204 ASSERT_TRUE(test_server_.Start()); 6274 ASSERT_TRUE(test_server_.Start());
6205 6275
6206 const GURL url = test_server_.GetURL("files/redirect308-to-echo"); 6276 const GURL url = test_server_.GetURL("files/redirect308-to-echo");
6277 const GURL https_redirect_url =
6278 test_server_.GetURL("files/redirect308-to-https");
6207 6279
6208 HTTPRedirectMethodTest(url, "POST", "POST", true); 6280 HTTPRedirectMethodTest(url, "POST", "POST", true);
6209 HTTPRedirectMethodTest(url, "PUT", "PUT", true); 6281 HTTPRedirectMethodTest(url, "PUT", "PUT", true);
6210 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false); 6282 HTTPRedirectMethodTest(url, "HEAD", "HEAD", false);
6283
6284 HTTPRedirectOriginHeaderTest(url, "GET", "GET", url.GetOrigin().spec());
6285 HTTPRedirectOriginHeaderTest(https_redirect_url, "GET", "GET", "null");
6286 HTTPRedirectOriginHeaderTest(url, "POST", "POST", url.GetOrigin().spec());
6287 HTTPRedirectOriginHeaderTest(https_redirect_url, "POST", "POST", "null");
6211 } 6288 }
6212 6289
6213 // Make sure that 308 responses without bodies are not treated as redirects. 6290 // Make sure that 308 responses without bodies are not treated as redirects.
6214 // Certain legacy apis that pre-date the response code expect this behavior 6291 // Certain legacy apis that pre-date the response code expect this behavior
6215 // (Like Google Drive). 6292 // (Like Google Drive).
6216 TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) { 6293 TEST_F(URLRequestTestHTTP, NoRedirectOn308WithoutLocationHeader) {
6217 ASSERT_TRUE(test_server_.Start()); 6294 ASSERT_TRUE(test_server_.Start());
6218 6295
6219 TestDelegate d; 6296 TestDelegate d;
6220 const GURL url = test_server_.GetURL("files/308-without-location-header"); 6297 const GURL url = test_server_.GetURL("files/308-without-location-header");
(...skipping 2641 matching lines...) Expand 10 before | Expand all | Expand 10 after
8862 8939
8863 EXPECT_FALSE(r->is_pending()); 8940 EXPECT_FALSE(r->is_pending());
8864 EXPECT_EQ(1, d->response_started_count()); 8941 EXPECT_EQ(1, d->response_started_count());
8865 EXPECT_FALSE(d->received_data_before_response()); 8942 EXPECT_FALSE(d->received_data_before_response());
8866 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 8943 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
8867 } 8944 }
8868 } 8945 }
8869 #endif // !defined(DISABLE_FTP_SUPPORT) 8946 #endif // !defined(DISABLE_FTP_SUPPORT)
8870 8947
8871 } // namespace net 8948 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_request.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698