redirect_info_unittest.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/url_request/redirect_info.h"
  5. #include "base/memory/ref_counted.h"
  6. #include "base/strings/string_number_conversions.h"
  7. #include "net/http/http_response_headers.h"
  8. #include "net/http/http_util.h"
  9. #include "net/url_request/redirect_util.h"
  10. #include "net/url_request/referrer_policy.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. #include "url/gurl.h"
  13. namespace net {
  14. namespace {
  15. TEST(RedirectInfoTest, MethodForRedirect) {
  16. struct TestCase {
  17. const char* original_method;
  18. int http_status_code;
  19. const char* expected_new_method;
  20. };
  21. const TestCase kTests[] = {
  22. {"GET", 301, "GET"}, {"GET", 302, "GET"}, {"GET", 303, "GET"},
  23. {"GET", 307, "GET"}, {"GET", 308, "GET"}, {"HEAD", 301, "HEAD"},
  24. {"HEAD", 302, "HEAD"}, {"HEAD", 303, "HEAD"}, {"HEAD", 307, "HEAD"},
  25. {"HEAD", 308, "HEAD"}, {"POST", 301, "GET"}, {"POST", 302, "GET"},
  26. {"POST", 303, "GET"}, {"POST", 307, "POST"}, {"POST", 308, "POST"},
  27. {"PUT", 301, "PUT"}, {"PUT", 302, "PUT"}, {"PUT", 303, "GET"},
  28. {"PUT", 307, "PUT"}, {"PUT", 308, "PUT"},
  29. };
  30. const GURL kOriginalUrl = GURL("https://foo.test/original");
  31. const SiteForCookies kOriginalSiteForCookies =
  32. SiteForCookies::FromUrl(GURL("https://foo.test/"));
  33. const url::Origin kOriginalTopFrameOrigin = url::Origin::Create(kOriginalUrl);
  34. const RedirectInfo::FirstPartyURLPolicy kOriginalFirstPartyUrlPolicy =
  35. RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL;
  36. const ReferrerPolicy kOriginalReferrerPolicy = ReferrerPolicy::NEVER_CLEAR;
  37. const std::string kOriginalReferrer = "";
  38. const GURL kNewLocation = GURL("https://foo.test/redirected");
  39. const bool kInsecureSchemeWasUpgraded = false;
  40. const bool kCopyFragment = true;
  41. for (const auto& test : kTests) {
  42. SCOPED_TRACE(::testing::Message()
  43. << "original_method: " << test.original_method
  44. << " http_status_code: " << test.http_status_code);
  45. RedirectInfo redirect_info = RedirectInfo::ComputeRedirectInfo(
  46. test.original_method, kOriginalUrl, kOriginalSiteForCookies,
  47. kOriginalFirstPartyUrlPolicy, kOriginalReferrerPolicy,
  48. kOriginalReferrer, test.http_status_code, kNewLocation,
  49. absl::nullopt /* referrer_policy_header */, kInsecureSchemeWasUpgraded,
  50. kCopyFragment);
  51. EXPECT_EQ(test.expected_new_method, redirect_info.new_method);
  52. EXPECT_EQ(test.http_status_code, redirect_info.status_code);
  53. EXPECT_EQ(kNewLocation, redirect_info.new_url);
  54. }
  55. }
  56. TEST(RedirectInfoTest, CopyFragment) {
  57. struct TestCase {
  58. bool copy_fragment;
  59. const char* original_url;
  60. const char* new_location;
  61. const char* expected_new_url;
  62. };
  63. const TestCase kTests[] = {
  64. {true, "http://foo.test/original", "http://foo.test/redirected",
  65. "http://foo.test/redirected"},
  66. {true, "http://foo.test/original#1", "http://foo.test/redirected",
  67. "http://foo.test/redirected#1"},
  68. {true, "http://foo.test/original#1", "http://foo.test/redirected#2",
  69. "http://foo.test/redirected#2"},
  70. {false, "http://foo.test/original", "http://foo.test/redirected",
  71. "http://foo.test/redirected"},
  72. {false, "http://foo.test/original#1", "http://foo.test/redirected",
  73. "http://foo.test/redirected"},
  74. {false, "http://foo.test/original#1", "http://foo.test/redirected#2",
  75. "http://foo.test/redirected#2"},
  76. };
  77. const std::string kOriginalMethod = "GET";
  78. const SiteForCookies kOriginalSiteForCookies =
  79. SiteForCookies::FromUrl(GURL("https://foo.test/"));
  80. const RedirectInfo::FirstPartyURLPolicy kOriginalFirstPartyUrlPolicy =
  81. RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL;
  82. const ReferrerPolicy kOriginalReferrerPolicy = ReferrerPolicy::NEVER_CLEAR;
  83. const std::string kOriginalReferrer = "";
  84. const int kHttpStatusCode = 301;
  85. const bool kInsecureSchemeWasUpgraded = false;
  86. for (const auto& test : kTests) {
  87. SCOPED_TRACE(::testing::Message()
  88. << "copy_fragment: " << test.copy_fragment
  89. << " original_url: " << test.original_url
  90. << " new_location: " << test.new_location);
  91. RedirectInfo redirect_info = RedirectInfo::ComputeRedirectInfo(
  92. kOriginalMethod, GURL(test.original_url), kOriginalSiteForCookies,
  93. kOriginalFirstPartyUrlPolicy, kOriginalReferrerPolicy,
  94. kOriginalReferrer, kHttpStatusCode, GURL(test.new_location),
  95. absl::nullopt /* referrer_policy_header */, kInsecureSchemeWasUpgraded,
  96. test.copy_fragment);
  97. EXPECT_EQ(GURL(test.expected_new_url), redirect_info.new_url);
  98. }
  99. }
  100. TEST(RedirectInfoTest, FirstPartyURLPolicy) {
  101. struct TestCase {
  102. RedirectInfo::FirstPartyURLPolicy original_first_party_url_policy;
  103. const char* expected_new_site_for_cookies;
  104. };
  105. const TestCase kTests[] = {
  106. {RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL,
  107. "https://foo.test/"},
  108. {RedirectInfo::FirstPartyURLPolicy::UPDATE_URL_ON_REDIRECT,
  109. "https://foo.test/redirected"},
  110. };
  111. const std::string kOriginalMethod = "GET";
  112. const GURL kOriginalUrl = GURL("https://foo.test/");
  113. const SiteForCookies kOriginalSiteForCookies =
  114. SiteForCookies::FromUrl(GURL("https://foo.test/"));
  115. const ReferrerPolicy kOriginalReferrerPolicy = ReferrerPolicy::NEVER_CLEAR;
  116. const std::string kOriginalReferrer = "";
  117. const GURL kNewLocation = GURL("https://foo.test/redirected");
  118. const bool kInsecureSchemeWasUpgraded = false;
  119. const int kHttpStatusCode = 301;
  120. const bool kCopyFragment = true;
  121. for (const auto& test : kTests) {
  122. SCOPED_TRACE(::testing::Message()
  123. << "original_first_party_url_policy: "
  124. << static_cast<int>(test.original_first_party_url_policy));
  125. RedirectInfo redirect_info = RedirectInfo::ComputeRedirectInfo(
  126. kOriginalMethod, kOriginalUrl, kOriginalSiteForCookies,
  127. test.original_first_party_url_policy, kOriginalReferrerPolicy,
  128. kOriginalReferrer, kHttpStatusCode, kNewLocation,
  129. absl::nullopt /* referrer_policy_header */, kInsecureSchemeWasUpgraded,
  130. kCopyFragment);
  131. EXPECT_TRUE(redirect_info.new_site_for_cookies.IsEquivalent(
  132. SiteForCookies::FromUrl(GURL(test.expected_new_site_for_cookies))));
  133. }
  134. }
  135. TEST(RedirectInfoTest, ReferrerPolicy) {
  136. struct TestCase {
  137. const char* original_url;
  138. const char* original_referrer;
  139. const char* response_headers;
  140. ReferrerPolicy original_referrer_policy;
  141. ReferrerPolicy expected_new_referrer_policy;
  142. const char* expected_referrer;
  143. };
  144. const TestCase kTests[] = {
  145. // If a redirect serves 'Referrer-Policy: no-referrer', then the referrer
  146. // should be cleared.
  147. {"http://foo.test/one" /* original url */,
  148. "http://foo.test/one" /* original referrer */,
  149. "Location: http://foo.test/test\n"
  150. "Referrer-Policy: no-referrer\n",
  151. // original policy
  152. ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
  153. ReferrerPolicy::NO_REFERRER /* expected new policy */,
  154. "" /* expected new referrer */},
  155. // Same as above but for the legacy keyword 'never', which should not be
  156. // supported.
  157. {"http://foo.test/one" /* original url */,
  158. "http://foo.test/one" /* original referrer */,
  159. "Location: http://foo.test/test\nReferrer-Policy: never\n",
  160. // original policy
  161. ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
  162. // expected new policy
  163. ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
  164. "http://foo.test/one" /* expected new referrer */},
  165. // If a redirect serves 'Referrer-Policy: no-referrer-when-downgrade',
  166. // then the referrer should be cleared on downgrade, even if the original
  167. // request's policy specified that the referrer should never be cleared.
  168. {"https://foo.test/one" /* original url */,
  169. "https://foo.test/one" /* original referrer */,
  170. "Location: http://foo.test\n"
  171. "Referrer-Policy: no-referrer-when-downgrade\n",
  172. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  173. // expected new policy
  174. ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
  175. "" /* expected new referrer */},
  176. // Same as above but for the legacy keyword 'default', which should not be
  177. // supported.
  178. {"https://foo.test/one" /* original url */,
  179. "https://foo.test/one" /* original referrer */,
  180. "Location: http://foo.test\n"
  181. "Referrer-Policy: default\n",
  182. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  183. // expected new policy
  184. ReferrerPolicy::NEVER_CLEAR,
  185. "https://foo.test/one" /* expected new referrer */},
  186. // If a redirect serves 'Referrer-Policy: no-referrer-when-downgrade',
  187. // the referrer should not be cleared for a non-downgrading redirect. But
  188. // the policy should be updated.
  189. {"https://foo.test/one" /* original url */,
  190. "https://foo.test/one" /* original referrer */,
  191. "Location: https://foo.test\n"
  192. "Referrer-Policy: no-referrer-when-downgrade\n",
  193. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  194. // expected new policy
  195. ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
  196. "https://foo.test/one" /* expected new referrer */},
  197. // If a redirect serves 'Referrer-Policy: origin', then the referrer
  198. // should be stripped to its origin, even if the original request's policy
  199. // specified that the referrer should never be cleared.
  200. {"https://foo.test/one" /* original url */,
  201. "https://foo.test/one" /* original referrer */,
  202. "Location: https://foo.test/two\n"
  203. "Referrer-Policy: origin\n",
  204. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  205. ReferrerPolicy::ORIGIN /* expected new policy */,
  206. "https://foo.test/" /* expected new referrer */},
  207. // If a redirect serves 'Referrer-Policy: origin-when-cross-origin', then
  208. // the referrer should be untouched for a same-origin redirect...
  209. {"https://foo.test/one" /* original url */,
  210. "https://foo.test/referrer" /* original referrer */,
  211. "Location: https://foo.test/two\n"
  212. "Referrer-Policy: origin-when-cross-origin\n",
  213. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  214. ReferrerPolicy::
  215. ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected new policy */,
  216. "https://foo.test/referrer" /* expected new referrer */},
  217. // ... but should be stripped to the origin for a cross-origin redirect.
  218. {"https://foo.test/one" /* original url */,
  219. "https://foo.test/one" /* original referrer */,
  220. "Location: https://bar.test/two\n"
  221. "Referrer-Policy: origin-when-cross-origin\n",
  222. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  223. ReferrerPolicy::
  224. ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected new policy */,
  225. "https://foo.test/" /* expected new referrer */},
  226. // If a redirect serves 'Referrer-Policy: same-origin', then the referrer
  227. // should be untouched for a same-origin redirect,
  228. {"https://foo.test/one" /* original url */,
  229. "https://foo.test/referrer" /* original referrer */,
  230. "Location: https://foo.test/two\n"
  231. "Referrer-Policy: same-origin\n",
  232. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  233. ReferrerPolicy::CLEAR_ON_TRANSITION_CROSS_ORIGIN /* new policy */
  234. ,
  235. "https://foo.test/referrer" /* expected new referrer */},
  236. // ... but should be cleared for a cross-origin redirect.
  237. {"https://foo.test/one" /* original url */,
  238. "https://foo.test/referrer" /* original referrer */,
  239. "Location: https://bar.test/two\n"
  240. "Referrer-Policy: same-origin\n",
  241. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  242. ReferrerPolicy::CLEAR_ON_TRANSITION_CROSS_ORIGIN,
  243. "" /* expected new referrer */},
  244. // If a redirect serves 'Referrer-Policy: strict-origin', then the
  245. // referrer should be the origin only for a cross-origin non-downgrading
  246. // redirect,
  247. {"https://foo.test/one" /* original url */,
  248. "https://foo.test/referrer" /* original referrer */,
  249. "Location: https://bar.test/two\n"
  250. "Referrer-Policy: strict-origin\n",
  251. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  252. ReferrerPolicy::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
  253. "https://foo.test/" /* expected new referrer */},
  254. {"http://foo.test/one" /* original url */,
  255. "http://foo.test/referrer" /* original referrer */,
  256. "Location: http://bar.test/two\n"
  257. "Referrer-Policy: strict-origin\n",
  258. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  259. ReferrerPolicy::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
  260. "http://foo.test/" /* expected new referrer */},
  261. // ... but should be cleared for a downgrading redirect.
  262. {"https://foo.test/one" /* original url */,
  263. "https://foo.test/referrer" /* original referrer */,
  264. "Location: http://foo.test/two\n"
  265. "Referrer-Policy: strict-origin\n",
  266. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  267. ReferrerPolicy::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
  268. "" /* expected new referrer */},
  269. // If a redirect serves 'Referrer-Policy:
  270. // strict-origin-when-cross-origin', then the referrer should be preserved
  271. // for a same-origin redirect,
  272. {"https://foo.test/one" /* original url */,
  273. "https://foo.test/referrer" /* original referrer */,
  274. "Location: https://foo.test/two\n"
  275. "Referrer-Policy: strict-origin-when-cross-origin\n",
  276. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  277. ReferrerPolicy::REDUCE_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
  278. "https://foo.test/referrer" /* expected new referrer */},
  279. {"http://foo.test/one" /* original url */,
  280. "http://foo.test/referrer" /* original referrer */,
  281. "Location: http://foo.test/two\n"
  282. "Referrer-Policy: strict-origin-when-cross-origin\n",
  283. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  284. ReferrerPolicy::REDUCE_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
  285. "http://foo.test/referrer" /* expected new referrer */},
  286. // ... but should be stripped to the origin for a cross-origin
  287. // non-downgrading redirect,
  288. {"https://foo.test/one" /* original url */,
  289. "https://foo.test/referrer" /* original referrer */,
  290. "Location: https://bar.test/two\n"
  291. "Referrer-Policy: strict-origin-when-cross-origin\n",
  292. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  293. ReferrerPolicy::REDUCE_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
  294. "https://foo.test/" /* expected new referrer */},
  295. {"http://foo.test/one" /* original url */,
  296. "http://foo.test/referrer" /* original referrer */,
  297. "Location: http://bar.test/two\n"
  298. "Referrer-Policy: strict-origin-when-cross-origin\n",
  299. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  300. ReferrerPolicy::REDUCE_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
  301. "http://foo.test/" /* expected new referrer */},
  302. // ... and should be cleared for a downgrading redirect.
  303. {"https://foo.test/one" /* original url */,
  304. "https://foo.test/referrer" /* original referrer */,
  305. "Location: http://foo.test/two\n"
  306. "Referrer-Policy: strict-origin-when-cross-origin\n",
  307. ReferrerPolicy::NEVER_CLEAR /* original policy */,
  308. ReferrerPolicy::REDUCE_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
  309. "" /* expected new referrer */},
  310. // If a redirect serves 'Referrer-Policy: unsafe-url', then the referrer
  311. // should remain, even if originally set to clear on downgrade.
  312. {"https://foo.test/one" /* original url */,
  313. "https://foo.test/one" /* original referrer */,
  314. "Location: http://bar.test/two\n"
  315. "Referrer-Policy: unsafe-url\n",
  316. ReferrerPolicy::
  317. ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
  318. ReferrerPolicy::NEVER_CLEAR /* expected new policy */,
  319. "https://foo.test/one" /* expected new referrer */},
  320. // Same as above but for the legacy keyword 'always', which should not be
  321. // supported.
  322. {"https://foo.test/one" /* original url */,
  323. "https://foo.test/one" /* original referrer */,
  324. "Location: http://bar.test/two\n"
  325. "Referrer-Policy: always\n",
  326. ReferrerPolicy::
  327. ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
  328. ReferrerPolicy::
  329. ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected new policy */,
  330. "https://foo.test/" /* expected new referrer */},
  331. // An invalid keyword should leave the policy untouched.
  332. {"https://foo.test/one" /* original url */,
  333. "https://foo.test/one" /* original referrer */,
  334. "Location: https://bar.test/two\n"
  335. "Referrer-Policy: not-a-valid-policy\n",
  336. ReferrerPolicy::
  337. ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
  338. ReferrerPolicy::
  339. ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected new policy */,
  340. "https://foo.test/" /* expected new referrer */},
  341. {"https://foo.test/one" /* original url */,
  342. "https://foo.test/one" /* original referrer */,
  343. "Location: http://bar.test/two\n"
  344. "Referrer-Policy: not-a-valid-policy\n",
  345. // original policy
  346. ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
  347. // expected new policy
  348. ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
  349. "" /* expected new referrer */},
  350. // The last valid keyword should take precedence.
  351. {"https://foo.test/one" /* original url */,
  352. "https://foo.test/one" /* original referrer */,
  353. "Location: https://bar.test/two\n"
  354. "Referrer-Policy: unsafe-url\n"
  355. "Referrer-Policy: not-a-valid-policy\n",
  356. ReferrerPolicy::
  357. ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
  358. ReferrerPolicy::NEVER_CLEAR /* expected new policy */,
  359. "https://foo.test/one" /* expected new referrer */},
  360. {"https://foo.test/one" /* original url */,
  361. "https://foo.test/one" /* original referrer */,
  362. "Location: https://bar.test/two\n"
  363. "Referrer-Policy: unsafe-url\n"
  364. "Referrer-Policy: origin\n",
  365. ReferrerPolicy::
  366. ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
  367. ReferrerPolicy::ORIGIN /* expected new policy */,
  368. "https://foo.test/" /* expected new referrer */},
  369. // An empty header should not affect the request.
  370. {"https://foo.test/one" /* original url */,
  371. "https://foo.test/one" /* original referrer */,
  372. "Location: https://bar.test/two\n"
  373. "Referrer-Policy: \n",
  374. ReferrerPolicy::
  375. ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
  376. ReferrerPolicy::
  377. ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected new policy */,
  378. "https://foo.test/" /* expected new referrer */},
  379. // A redirect response without Referrer-Policy header should not affect
  380. // the policy and the referrer.
  381. {"http://foo.test/one" /* original url */,
  382. "http://foo.test/one" /* original referrer */,
  383. "Location: http://foo.test/test\n",
  384. // original policy
  385. ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
  386. // expected new policy
  387. ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
  388. "http://foo.test/one" /* expected new referrer */},
  389. };
  390. const std::string kOriginalMethod = "GET";
  391. const SiteForCookies kOriginalSiteForCookies =
  392. SiteForCookies::FromUrl(GURL("https://foo.test/"));
  393. const RedirectInfo::FirstPartyURLPolicy kOriginalFirstPartyUrlPolicy =
  394. RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL;
  395. const bool kInsecureSchemeWasUpgraded = false;
  396. const bool kCopyFragment = true;
  397. for (const auto& test : kTests) {
  398. SCOPED_TRACE(::testing::Message()
  399. << "original_url: " << test.original_url
  400. << " original_referrer: " << test.original_referrer
  401. << " response_headers: " << test.response_headers
  402. << " original_referrer_policy: "
  403. << static_cast<int>(test.original_referrer_policy));
  404. std::string response_header_text =
  405. "HTTP/1.1 302 Redirect\n" + std::string(test.response_headers);
  406. std::string raw_headers =
  407. HttpUtil::AssembleRawHeaders(response_header_text);
  408. auto response_headers =
  409. base::MakeRefCounted<HttpResponseHeaders>(raw_headers);
  410. EXPECT_EQ(302, response_headers->response_code());
  411. std::string location_string;
  412. EXPECT_TRUE(response_headers->IsRedirect(&location_string));
  413. const GURL original_url = GURL(test.original_url);
  414. const GURL new_location = original_url.Resolve(location_string);
  415. RedirectInfo redirect_info = RedirectInfo::ComputeRedirectInfo(
  416. kOriginalMethod, original_url, kOriginalSiteForCookies,
  417. kOriginalFirstPartyUrlPolicy, test.original_referrer_policy,
  418. test.original_referrer, response_headers->response_code(), new_location,
  419. RedirectUtil::GetReferrerPolicyHeader(response_headers.get()),
  420. kInsecureSchemeWasUpgraded, kCopyFragment);
  421. EXPECT_EQ(test.expected_new_referrer_policy,
  422. redirect_info.new_referrer_policy);
  423. EXPECT_EQ(test.expected_referrer, redirect_info.new_referrer);
  424. }
  425. }
  426. } // namespace
  427. } // namespace net