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

Side by Side Diff: third_party/WebKit/Source/core/loader/FrameFetchContextTest.cpp

Issue 2555713002: Don't use FetchRequest in FrameFetchContext (Closed)
Patch Set: a Created 3 years, 11 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 /* 1 /*
tyoshino (SeeGerritForStatus) 2017/01/10 09:55:56 Removed dependency to FetchRequest in this file wh
2 * Copyright (c) 2015, Google Inc. All rights reserved. 2 * Copyright (c) 2015, Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer 11 * copyright notice, this list of conditions and the following disclaimer
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 WebURLRequest::FrameTypeNone, expected); 165 WebURLRequest::FrameTypeNone, expected);
166 } 166 }
167 167
168 void expectUpgrade(const char* input, 168 void expectUpgrade(const char* input,
169 WebURLRequest::RequestContext requestContext, 169 WebURLRequest::RequestContext requestContext,
170 WebURLRequest::FrameType frameType, 170 WebURLRequest::FrameType frameType,
171 const char* expected) { 171 const char* expected) {
172 KURL inputURL(ParsedURLString, input); 172 KURL inputURL(ParsedURLString, input);
173 KURL expectedURL(ParsedURLString, expected); 173 KURL expectedURL(ParsedURLString, expected);
174 174
175 FetchRequest fetchRequest = 175 ResourceRequest resourceRequest(inputURL);
176 FetchRequest(ResourceRequest(inputURL), FetchInitiatorInfo()); 176 resourceRequest.setRequestContext(requestContext);
177 fetchRequest.mutableResourceRequest().setRequestContext(requestContext); 177 resourceRequest.setFrameType(frameType);
178 fetchRequest.mutableResourceRequest().setFrameType(frameType);
179 178
180 fetchContext->modifyRequestForCSP(fetchRequest.mutableResourceRequest()); 179 fetchContext->modifyRequestForCSP(resourceRequest);
181 180
182 EXPECT_EQ(expectedURL.getString(), 181 EXPECT_EQ(expectedURL.getString(), resourceRequest.url().getString());
183 fetchRequest.resourceRequest().url().getString()); 182 EXPECT_EQ(expectedURL.protocol(), resourceRequest.url().protocol());
184 EXPECT_EQ(expectedURL.protocol(), 183 EXPECT_EQ(expectedURL.host(), resourceRequest.url().host());
185 fetchRequest.resourceRequest().url().protocol()); 184 EXPECT_EQ(expectedURL.port(), resourceRequest.url().port());
186 EXPECT_EQ(expectedURL.host(), fetchRequest.resourceRequest().url().host()); 185 EXPECT_EQ(expectedURL.hasPort(), resourceRequest.url().hasPort());
187 EXPECT_EQ(expectedURL.port(), fetchRequest.resourceRequest().url().port()); 186 EXPECT_EQ(expectedURL.path(), resourceRequest.url().path());
188 EXPECT_EQ(expectedURL.hasPort(),
189 fetchRequest.resourceRequest().url().hasPort());
190 EXPECT_EQ(expectedURL.path(), fetchRequest.resourceRequest().url().path());
191 } 187 }
192 188
193 void expectUpgradeInsecureRequestHeader(const char* input, 189 void expectUpgradeInsecureRequestHeader(const char* input,
194 WebURLRequest::FrameType frameType, 190 WebURLRequest::FrameType frameType,
195 bool shouldPrefer) { 191 bool shouldPrefer) {
196 KURL inputURL(ParsedURLString, input); 192 KURL inputURL(ParsedURLString, input);
197 193
198 FetchRequest fetchRequest = 194 ResourceRequest resourceRequest(inputURL);
199 FetchRequest(ResourceRequest(inputURL), FetchInitiatorInfo()); 195 resourceRequest.setRequestContext(WebURLRequest::RequestContextScript);
200 fetchRequest.mutableResourceRequest().setRequestContext( 196 resourceRequest.setFrameType(frameType);
201 WebURLRequest::RequestContextScript);
202 fetchRequest.mutableResourceRequest().setFrameType(frameType);
203 197
204 fetchContext->modifyRequestForCSP(fetchRequest.mutableResourceRequest()); 198 fetchContext->modifyRequestForCSP(resourceRequest);
205 199
206 EXPECT_EQ(shouldPrefer ? String("1") : String(), 200 EXPECT_EQ(
207 fetchRequest.resourceRequest().httpHeaderField( 201 shouldPrefer ? String("1") : String(),
208 HTTPNames::Upgrade_Insecure_Requests)); 202 resourceRequest.httpHeaderField(HTTPNames::Upgrade_Insecure_Requests));
209 203
210 // Calling modifyRequestForCSP more than once shouldn't affect the 204 // Calling modifyRequestForCSP more than once shouldn't affect the
211 // header. 205 // header.
212 if (shouldPrefer) { 206 if (shouldPrefer) {
213 fetchContext->modifyRequestForCSP(fetchRequest.mutableResourceRequest()); 207 fetchContext->modifyRequestForCSP(resourceRequest);
214 EXPECT_EQ("1", fetchRequest.resourceRequest().httpHeaderField( 208 EXPECT_EQ("1", resourceRequest.httpHeaderField(
215 HTTPNames::Upgrade_Insecure_Requests)); 209 HTTPNames::Upgrade_Insecure_Requests));
216 } 210 }
217 } 211 }
218 212
219 void expectSetEmbeddingCSPRequestHeader( 213 void expectSetEmbeddingCSPRequestHeader(
220 const char* input, 214 const char* input,
221 WebURLRequest::FrameType frameType, 215 WebURLRequest::FrameType frameType,
222 const AtomicString& expectedEmbeddingCSP) { 216 const AtomicString& expectedEmbeddingCSP) {
223 KURL inputURL(ParsedURLString, input); 217 KURL inputURL(ParsedURLString, input);
218 ResourceRequest resourceRequest(inputURL);
219 resourceRequest.setRequestContext(WebURLRequest::RequestContextScript);
220 resourceRequest.setFrameType(frameType);
224 221
225 FetchRequest fetchRequest = 222 fetchContext->modifyRequestForCSP(resourceRequest);
226 FetchRequest(ResourceRequest(inputURL), FetchInitiatorInfo());
227 fetchRequest.mutableResourceRequest().setRequestContext(
228 WebURLRequest::RequestContextScript);
229 fetchRequest.mutableResourceRequest().setFrameType(frameType);
230
231 fetchContext->modifyRequestForCSP(fetchRequest.mutableResourceRequest());
232 223
233 EXPECT_EQ(expectedEmbeddingCSP, 224 EXPECT_EQ(expectedEmbeddingCSP,
234 fetchRequest.resourceRequest().httpHeaderField( 225 resourceRequest.httpHeaderField(HTTPNames::Embedding_CSP));
235 HTTPNames::Embedding_CSP));
236 } 226 }
237 227
238 void setFrameOwnerBasedOnFrameType(WebURLRequest::FrameType frameType, 228 void setFrameOwnerBasedOnFrameType(WebURLRequest::FrameType frameType,
239 HTMLIFrameElement* iframe, 229 HTMLIFrameElement* iframe,
240 const AtomicString& potentialValue) { 230 const AtomicString& potentialValue) {
241 if (frameType != WebURLRequest::FrameTypeNested) { 231 if (frameType != WebURLRequest::FrameTypeNested) {
242 document->frame()->setOwner(nullptr); 232 document->frame()->setOwner(nullptr);
243 return; 233 return;
244 } 234 }
245 235
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 class FrameFetchContextHintsTest : public FrameFetchContextTest { 402 class FrameFetchContextHintsTest : public FrameFetchContextTest {
413 public: 403 public:
414 FrameFetchContextHintsTest() {} 404 FrameFetchContextHintsTest() {}
415 405
416 protected: 406 protected:
417 void expectHeader(const char* input, 407 void expectHeader(const char* input,
418 const char* headerName, 408 const char* headerName,
419 bool isPresent, 409 bool isPresent,
420 const char* headerValue, 410 const char* headerValue,
421 float width = 0) { 411 float width = 0) {
422 KURL inputURL(ParsedURLString, input); 412 ClientHintsPreferences hintsPreferences;
423 FetchRequest fetchRequest = 413
424 FetchRequest(ResourceRequest(inputURL), FetchInitiatorInfo()); 414 FetchRequest::ResourceWidth resourceWidth;
425 if (width > 0) { 415 if (width > 0) {
426 FetchRequest::ResourceWidth resourceWidth;
427 resourceWidth.width = width; 416 resourceWidth.width = width;
428 resourceWidth.isSet = true; 417 resourceWidth.isSet = true;
429 fetchRequest.setResourceWidth(resourceWidth);
430 } 418 }
431 fetchContext->addClientHintsIfNecessary(fetchRequest); 419
420 KURL inputURL(ParsedURLString, input);
421 ResourceRequest resourceRequest(inputURL);
422
423 fetchContext->addClientHintsIfNecessary(hintsPreferences, resourceWidth,
424 resourceRequest);
432 425
433 EXPECT_EQ(isPresent ? String(headerValue) : String(), 426 EXPECT_EQ(isPresent ? String(headerValue) : String(),
434 fetchRequest.resourceRequest().httpHeaderField(headerName)); 427 resourceRequest.httpHeaderField(headerName));
435 } 428 }
436 }; 429 };
437 430
438 TEST_F(FrameFetchContextHintsTest, MonitorDPRHints) { 431 TEST_F(FrameFetchContextHintsTest, MonitorDPRHints) {
439 expectHeader("http://www.example.com/1.gif", "DPR", false, ""); 432 expectHeader("http://www.example.com/1.gif", "DPR", false, "");
440 ClientHintsPreferences preferences; 433 ClientHintsPreferences preferences;
441 preferences.setShouldSendDPR(true); 434 preferences.setShouldSendDPR(true);
442 document->clientHintsPreferences().updateFrom(preferences); 435 document->clientHintsPreferences().updateFrom(preferences);
443 expectHeader("http://www.example.com/1.gif", "DPR", true, "1"); 436 expectHeader("http://www.example.com/1.gif", "DPR", true, "1");
444 dummyPageHolder->page().setDeviceScaleFactor(2.5); 437 dummyPageHolder->page().setDeviceScaleFactor(2.5);
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 childFetchContext->resourceRequestCachePolicy( 531 childFetchContext->resourceRequestCachePolicy(
539 request, Resource::MainResource, FetchRequest::NoDefer)); 532 request, Resource::MainResource, FetchRequest::NoDefer));
540 533
541 // Child frame as part of reload bypassing cache 534 // Child frame as part of reload bypassing cache
542 document->frame()->loader().setLoadType(FrameLoadTypeReloadBypassingCache); 535 document->frame()->loader().setLoadType(FrameLoadTypeReloadBypassingCache);
543 EXPECT_EQ(WebCachePolicy::BypassingCache, 536 EXPECT_EQ(WebCachePolicy::BypassingCache,
544 childFetchContext->resourceRequestCachePolicy( 537 childFetchContext->resourceRequestCachePolicy(
545 request, Resource::MainResource, FetchRequest::NoDefer)); 538 request, Resource::MainResource, FetchRequest::NoDefer));
546 } 539 }
547 540
548 TEST_F(FrameFetchContextTest, PopulateRequestData) { 541 TEST_F(FrameFetchContextTest, SetFirstPartyCookieAndRequestorOrigin) {
549 struct TestCase { 542 struct TestCase {
550 const char* documentURL; 543 const char* documentURL;
551 bool documentSandboxed; 544 bool documentSandboxed;
552 const char* requestorOrigin; // "" => unique origin 545 const char* requestorOrigin; // "" => unique origin
553 WebURLRequest::FrameType frameType; 546 WebURLRequest::FrameType frameType;
554 const char* serializedOrigin; // "" => unique origin 547 const char* serializedOrigin; // "" => unique origin
555 } cases[] = { 548 } cases[] = {
556 // No document origin => unique request origin 549 // No document origin => unique request origin
557 {"", false, "", WebURLRequest::FrameTypeNone, "null"}, 550 {"", false, "", WebURLRequest::FrameTypeNone, "null"},
558 {"", true, "", WebURLRequest::FrameTypeNone, "null"}, 551 {"", true, "", WebURLRequest::FrameTypeNone, "null"},
559 552
560 // Document origin => request origin 553 // Document origin => request origin
561 {"http://example.test", false, "", WebURLRequest::FrameTypeNone, 554 {"http://example.test", false, "", WebURLRequest::FrameTypeNone,
562 "http://example.test"}, 555 "http://example.test"},
563 {"http://example.test", true, "", WebURLRequest::FrameTypeNone, 556 {"http://example.test", true, "", WebURLRequest::FrameTypeNone,
564 "http://example.test"}, 557 "http://example.test"},
565 558
566 // If the request already has a requestor origin, then 559 // If the request already has a requestor origin, then
567 // 'populateRequestData' leaves it alone: 560 // 'setFirstPartyCookieAndRequestorOrigin' leaves it alone:
568 {"http://example.test", false, "http://not-example.test", 561 {"http://example.test", false, "http://not-example.test",
569 WebURLRequest::FrameTypeNone, "http://not-example.test"}, 562 WebURLRequest::FrameTypeNone, "http://not-example.test"},
570 {"http://example.test", true, "http://not-example.test", 563 {"http://example.test", true, "http://not-example.test",
571 WebURLRequest::FrameTypeNone, "http://not-example.test"}, 564 WebURLRequest::FrameTypeNone, "http://not-example.test"},
572 565
573 // If the request's frame type is not 'none', then 'populateRequestData' 566 // If the request's frame type is not 'none', then
567 // 'setFirstPartyCookieAndRequestorOrigin'
574 // leaves it alone: 568 // leaves it alone:
575 {"http://example.test", false, "", WebURLRequest::FrameTypeTopLevel, ""}, 569 {"http://example.test", false, "", WebURLRequest::FrameTypeTopLevel, ""},
576 {"http://example.test", false, "", WebURLRequest::FrameTypeAuxiliary, ""}, 570 {"http://example.test", false, "", WebURLRequest::FrameTypeAuxiliary, ""},
577 {"http://example.test", false, "", WebURLRequest::FrameTypeNested, ""}, 571 {"http://example.test", false, "", WebURLRequest::FrameTypeNested, ""},
578 }; 572 };
579 573
580 for (const auto& test : cases) { 574 for (const auto& test : cases) {
581 SCOPED_TRACE(::testing::Message() << test.documentURL << " => " 575 SCOPED_TRACE(::testing::Message() << test.documentURL << " => "
582 << test.serializedOrigin); 576 << test.serializedOrigin);
583 // Set up a new document to ensure sandbox flags are cleared: 577 // Set up a new document to ensure sandbox flags are cleared:
(...skipping 10 matching lines...) Expand all
594 document->enforceSandboxFlags(SandboxOrigin); 588 document->enforceSandboxFlags(SandboxOrigin);
595 589
596 ResourceRequest request("http://example.test/"); 590 ResourceRequest request("http://example.test/");
597 request.setFrameType(test.frameType); 591 request.setFrameType(test.frameType);
598 if (strlen(test.requestorOrigin) > 0) { 592 if (strlen(test.requestorOrigin) > 0) {
599 request.setRequestorOrigin( 593 request.setRequestorOrigin(
600 SecurityOrigin::create(KURL(ParsedURLString, test.requestorOrigin))); 594 SecurityOrigin::create(KURL(ParsedURLString, test.requestorOrigin)));
601 } 595 }
602 596
603 // Compare the populated |requestorOrigin| against |test.serializedOrigin| 597 // Compare the populated |requestorOrigin| against |test.serializedOrigin|
604 fetchContext->populateRequestData(request); 598 fetchContext->setFirstPartyCookieAndRequestorOrigin(request);
605 if (strlen(test.serializedOrigin) == 0) { 599 if (strlen(test.serializedOrigin) == 0) {
606 EXPECT_TRUE(request.requestorOrigin()->isUnique()); 600 EXPECT_TRUE(request.requestorOrigin()->isUnique());
607 } else { 601 } else {
608 EXPECT_EQ(String(test.serializedOrigin), 602 EXPECT_EQ(String(test.serializedOrigin),
609 request.requestorOrigin()->toString()); 603 request.requestorOrigin()->toString());
610 } 604 }
611 605
612 EXPECT_EQ(document->firstPartyForCookies(), request.firstPartyForCookies()); 606 EXPECT_EQ(document->firstPartyForCookies(), request.firstPartyForCookies());
613 } 607 }
614 } 608 }
615 609
616 TEST_F(FrameFetchContextTest, ModifyPriorityForLowPriorityIframes) { 610 TEST_F(FrameFetchContextTest, ModifyPriorityForLowPriorityIframes) {
617 Settings* settings = document->frame()->settings(); 611 Settings* settings = document->frame()->settings();
618 settings->setLowPriorityIframes(false); 612 settings->setLowPriorityIframes(false);
619 FetchRequest request(ResourceRequest("http://www.example.com"),
620 FetchInitiatorInfo());
621 FrameFetchContext* childFetchContext = createChildFrame(); 613 FrameFetchContext* childFetchContext = createChildFrame();
622 614
623 // No low priority iframes, expect default values. 615 // No low priority iframes, expect default values.
624 EXPECT_EQ(ResourceLoadPriorityVeryHigh, 616 EXPECT_EQ(ResourceLoadPriorityVeryHigh,
625 childFetchContext->modifyPriorityForExperiments( 617 childFetchContext->modifyPriorityForExperiments(
626 ResourceLoadPriorityVeryHigh)); 618 ResourceLoadPriorityVeryHigh));
627 EXPECT_EQ(ResourceLoadPriorityMedium, 619 EXPECT_EQ(ResourceLoadPriorityMedium,
628 childFetchContext->modifyPriorityForExperiments( 620 childFetchContext->modifyPriorityForExperiments(
629 ResourceLoadPriorityMedium)); 621 ResourceLoadPriorityMedium));
630 622
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
811 fetchContext->addAdditionalRequestHeaders(mainRequest, FetchMainResource); 803 fetchContext->addAdditionalRequestHeaders(mainRequest, FetchMainResource);
812 EXPECT_EQ(test.isExternalExpectation, mainRequest.isExternalRequest()); 804 EXPECT_EQ(test.isExternalExpectation, mainRequest.isExternalRequest());
813 805
814 ResourceRequest subRequest(test.url); 806 ResourceRequest subRequest(test.url);
815 fetchContext->addAdditionalRequestHeaders(subRequest, FetchSubresource); 807 fetchContext->addAdditionalRequestHeaders(subRequest, FetchSubresource);
816 EXPECT_EQ(test.isExternalExpectation, subRequest.isExternalRequest()); 808 EXPECT_EQ(test.isExternalExpectation, subRequest.isExternalRequest());
817 } 809 }
818 } 810 }
819 811
820 } // namespace blink 812 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/loader/FrameFetchContext.cpp ('k') | third_party/WebKit/Source/core/loader/PingLoader.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698