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

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

Issue 2053593002: WIP: URLRequest-based UIR implementation. Base URL: https://chromium.googlesource.com/chromium/src.git@replicate
Patch Set: Created 4 years, 6 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_http_job.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 <memory> 5 #include <memory>
6 #include <utility> 6 #include <utility>
7 7
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "build/build_config.h" 9 #include "build/build_config.h"
10 10
(...skipping 1109 matching lines...) Expand 10 before | Expand all | Expand 10 after
1120 req->Start(); 1120 req->Start();
1121 base::RunLoop().Run(); 1121 base::RunLoop().Run();
1122 1122
1123 ASSERT_EQ(1, d.received_redirect_count()); 1123 ASSERT_EQ(1, d.received_redirect_count());
1124 ASSERT_FALSE(req->status().is_success()); 1124 ASSERT_FALSE(req->status().is_success());
1125 } 1125 }
1126 #endif // defined(OS_WIN) 1126 #endif // defined(OS_WIN)
1127 1127
1128 #endif // !defined(DISABLE_FILE_SUPPORT) 1128 #endif // !defined(DISABLE_FILE_SUPPORT)
1129 1129
1130 TEST_F(URLRequestTest, InsecureRequestPolicyTest) {
1131 TestDelegate d;
1132 GURL original_url("http://example.com/path/to/file");
1133 GURL upgraded_url("https://example.com/path/to/file");
1134 url::Origin matched_host(original_url);
1135 url::Origin mismatched_host(GURL("http://not.example.com/"));
1136 std::string upgrade_type = "Upgrade";
1137
1138 std::unique_ptr<URLRequest> r(default_context_.CreateRequest(original_url, DEF AULT_PRIORITY, &d));
1139
1140 // No upgrade:
1141 {
1142 r->set_insecure_request_policy(URLRequest::DO_NOT_UPGRADE_INSECURE_REQUESTS) ;
1143
1144 GURL redirect;
1145 std::string type;
1146 EXPECT_FALSE(r->GetSecureRedirect(&redirect, &type));
1147 EXPECT_TRUE(redirect.is_empty());
1148 EXPECT_TRUE(type.empty());
1149 }
1150
1151 // Upgrade all, matching host:
1152 {
1153 r->set_insecure_request_policy(URLRequest::UPGRADE_ALL_INSECURE_REQUESTS);
1154 r->set_initiator(matched_host);
1155
1156 GURL redirect;
1157 std::string type;
1158 EXPECT_TRUE(r->GetSecureRedirect(&redirect, &type));
1159 EXPECT_EQ(upgraded_url, redirect);
1160 EXPECT_EQ(upgrade_type, type);
1161 }
1162
1163 // Upgrade all, mismatched host:
1164 {
1165 r->set_insecure_request_policy(URLRequest::UPGRADE_ALL_INSECURE_REQUESTS);
1166 r->set_initiator(mismatched_host);
1167
1168 GURL redirect;
1169 std::string type;
1170 EXPECT_TRUE(r->GetSecureRedirect(&redirect, &type));
1171 EXPECT_EQ(upgraded_url, redirect);
1172 EXPECT_EQ(upgrade_type, type);
1173 }
1174
1175 // Upgrade same host, matching host:
1176 {
1177 r->set_insecure_request_policy(URLRequest::UPGRADE_SAME_HOST_INSECURE_REQUES TS);
1178 r->set_initiator(matched_host);
1179
1180 GURL redirect;
1181 std::string type;
1182 EXPECT_TRUE(r->GetSecureRedirect(&redirect, &type));
1183 EXPECT_EQ(upgraded_url, redirect);
1184 EXPECT_EQ(upgrade_type, type);
1185 }
1186
1187 // Upgrade same host, mismatched host:
1188 {
1189 r->set_insecure_request_policy(URLRequest::UPGRADE_SAME_HOST_INSECURE_REQUES TS);
1190 r->set_initiator(mismatched_host);
1191
1192 GURL redirect;
1193 std::string type;
1194 EXPECT_FALSE(r->GetSecureRedirect(&redirect, &type));
1195 EXPECT_TRUE(redirect.is_empty());
1196 EXPECT_TRUE(type.empty());
1197 }
1198 }
1199
1200 TEST_F(URLRequestTest, HSTSUpgradeTest) {
1201 TestURLRequestContext context(true);
1202 TestNetworkDelegate network_delegate;
1203 TestDelegate delegate;
1204
1205 TransportSecurityState transport_security_state;
1206
1207 context.set_transport_security_state(&transport_security_state);
1208 context.set_network_delegate(&network_delegate);
1209 context.Init();
1210
1211 GURL original_url("http://example.com/path/to/file");
1212 GURL upgraded_url("https://example.com/path/to/file");
1213 std::string hsts_type = "HSTS";
1214 std::string upgrade_type = "Upgrade";
1215
1216 std::unique_ptr<URLRequest> r(context.CreateRequest(original_url, DEFAULT_PRIO RITY, &delegate));
1217
1218 // No upgrade by default:
1219 {
1220 GURL redirect;
1221 std::string type;
1222 EXPECT_FALSE(r->GetSecureRedirect(&redirect, &type));
1223 EXPECT_TRUE(redirect.is_empty());
1224 EXPECT_TRUE(type.empty());
1225 }
1226
1227 // Upgrade if host is in the HSTS list:
1228 {
1229 transport_security_state.AddHSTS("example.com", base::Time::Now() + base::Ti meDelta::FromDays(1), false);
1230
1231 GURL redirect;
1232 std::string type;
1233 EXPECT_TRUE(r->GetSecureRedirect(&redirect, &type));
1234 EXPECT_EQ(upgraded_url, redirect);
1235 EXPECT_EQ(hsts_type, type);
1236 }
1237
1238 // If host is in the HSTS list, and would be upgraded by insecure request
1239 // policy, the latter is reported as the upgrade type:
1240 {
1241 transport_security_state.AddHSTS("example.com", base::Time::Now() + base::Ti meDelta::FromDays(1), false);
1242 r->set_insecure_request_policy(URLRequest::UPGRADE_ALL_INSECURE_REQUESTS);
1243
1244 GURL redirect;
1245 std::string type;
1246 EXPECT_TRUE(r->GetSecureRedirect(&redirect, &type));
1247 EXPECT_EQ(upgraded_url, redirect);
1248 EXPECT_EQ(upgrade_type, type);
1249 }
1250 }
1251
1130 TEST_F(URLRequestTest, InvalidUrlTest) { 1252 TEST_F(URLRequestTest, InvalidUrlTest) {
1131 TestDelegate d; 1253 TestDelegate d;
1132 { 1254 {
1133 std::unique_ptr<URLRequest> r(default_context_.CreateRequest( 1255 std::unique_ptr<URLRequest> r(default_context_.CreateRequest(
1134 GURL("invalid url"), DEFAULT_PRIORITY, &d)); 1256 GURL("invalid url"), DEFAULT_PRIORITY, &d));
1135 1257
1136 r->Start(); 1258 r->Start();
1137 EXPECT_TRUE(r->is_pending()); 1259 EXPECT_TRUE(r->is_pending());
1138 1260
1139 base::RunLoop().Run(); 1261 base::RunLoop().Run();
(...skipping 7419 matching lines...) Expand 10 before | Expand all | Expand 10 after
8559 std::string redirect_location; 8681 std::string redirect_location;
8560 EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location)); 8682 EXPECT_TRUE(headers->EnumerateHeader(NULL, "Location", &redirect_location));
8561 EXPECT_EQ(hsts_https_url.spec(), redirect_location); 8683 EXPECT_EQ(hsts_https_url.spec(), redirect_location);
8562 8684
8563 std::string received_cors_header; 8685 std::string received_cors_header;
8564 EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin", 8686 EXPECT_TRUE(headers->EnumerateHeader(NULL, "Access-Control-Allow-Origin",
8565 &received_cors_header)); 8687 &received_cors_header));
8566 EXPECT_EQ(kOriginHeaderValue, received_cors_header); 8688 EXPECT_EQ(kOriginHeaderValue, received_cors_header);
8567 } 8689 }
8568 8690
8569 // This just tests the behaviour of GetHSTSRedirect(). End-to-end tests of HSTS 8691 // This just tests the behaviour of GetSecureRedirect(). End-to-end tests of HST S
8570 // are performed in net/websockets/websocket_end_to_end_test.cc. 8692 // are performed in net/websockets/websocket_end_to_end_test.cc.
8571 TEST(WebSocketURLRequestTest, HSTSApplied) { 8693 TEST(WebSocketURLRequestTest, HSTSApplied) {
8572 TestNetworkDelegate network_delegate; 8694 TestNetworkDelegate network_delegate;
8573 TransportSecurityState transport_security_state; 8695 TransportSecurityState transport_security_state;
8574 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1); 8696 base::Time expiry = base::Time::Now() + base::TimeDelta::FromDays(1);
8575 bool include_subdomains = false; 8697 bool include_subdomains = false;
8576 transport_security_state.AddHSTS("example.net", expiry, include_subdomains); 8698 transport_security_state.AddHSTS("example.net", expiry, include_subdomains);
8577 TestURLRequestContext context(true); 8699 TestURLRequestContext context(true);
8578 context.set_transport_security_state(&transport_security_state); 8700 context.set_transport_security_state(&transport_security_state);
8579 context.set_network_delegate(&network_delegate); 8701 context.set_network_delegate(&network_delegate);
8580 context.Init(); 8702 context.Init();
8581 GURL ws_url("ws://example.net/echo"); 8703 GURL ws_url("ws://example.net/echo");
8582 TestDelegate delegate; 8704 TestDelegate delegate;
8583 std::unique_ptr<URLRequest> request( 8705 std::unique_ptr<URLRequest> request(
8584 context.CreateRequest(ws_url, DEFAULT_PRIORITY, &delegate)); 8706 context.CreateRequest(ws_url, DEFAULT_PRIORITY, &delegate));
8585 EXPECT_TRUE(request->GetHSTSRedirect(&ws_url)); 8707 std::string redirect_type;
8708 EXPECT_TRUE(request->GetSecureRedirect(&ws_url, &redirect_type));
8586 EXPECT_TRUE(ws_url.SchemeIs("wss")); 8709 EXPECT_TRUE(ws_url.SchemeIs("wss"));
8710 EXPECT_EQ("HSTS", redirect_type);
8587 } 8711 }
8588 8712
8589 namespace { 8713 namespace {
8590 8714
8591 class SSLClientAuthTestDelegate : public TestDelegate { 8715 class SSLClientAuthTestDelegate : public TestDelegate {
8592 public: 8716 public:
8593 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) { 8717 SSLClientAuthTestDelegate() : on_certificate_requested_count_(0) {
8594 } 8718 }
8595 void OnCertificateRequested(URLRequest* request, 8719 void OnCertificateRequested(URLRequest* request,
8596 SSLCertRequestInfo* cert_request_info) override { 8720 SSLCertRequestInfo* cert_request_info) override {
(...skipping 1444 matching lines...) Expand 10 before | Expand all | Expand 10 after
10041 AddTestInterceptor()->set_main_intercept_job(std::move(job)); 10165 AddTestInterceptor()->set_main_intercept_job(std::move(job));
10042 10166
10043 req->Start(); 10167 req->Start();
10044 req->Cancel(); 10168 req->Cancel();
10045 base::RunLoop().RunUntilIdle(); 10169 base::RunLoop().RunUntilIdle();
10046 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); 10170 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
10047 EXPECT_EQ(0, d.received_redirect_count()); 10171 EXPECT_EQ(0, d.received_redirect_count());
10048 } 10172 }
10049 10173
10050 } // namespace net 10174 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_request_http_job.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698