Index: Source/core/loader/LinkLoaderTest.cpp |
diff --git a/Source/core/loader/LinkLoaderTest.cpp b/Source/core/loader/LinkLoaderTest.cpp |
index 2f7931c35460ba7b3c192646fd4679975ce33fdc..34b2df1f1a8fab56076da2b30a6ddf8e92bd4fd0 100644 |
--- a/Source/core/loader/LinkLoaderTest.cpp |
+++ b/Source/core/loader/LinkLoaderTest.cpp |
@@ -6,8 +6,10 @@ |
#include "core/loader/LinkLoader.h" |
#include "core/fetch/ResourceFetcher.h" |
+#include "core/frame/Settings.h" |
#include "core/html/LinkRelAttribute.h" |
#include "core/loader/LinkLoaderClient.h" |
+#include "core/loader/NetworkHintsInterface.h" |
#include "core/testing/DummyPageHolder.h" |
#include "platform/network/ResourceLoadPriority.h" |
@@ -39,6 +41,39 @@ private: |
bool m_shouldLoad; |
}; |
+class NetworkHintsMock : public NetworkHintsInterface { |
+public: |
+ NetworkHintsMock() |
+ : m_didDnsPrefetch(false) |
+ , m_didPreconnect(false) |
+ { |
+ } |
+ |
+ void dnsPrefetchHost(const String& host) const override |
+ { |
+ m_didDnsPrefetch = true; |
+ } |
+ |
+ void preconnectHost(const KURL& host, const CrossOriginAttributeValue crossOrigin) const override |
+ { |
+ m_didPreconnect = true; |
+ m_isHTTPS = host.protocolIs("https"); |
+ m_isCrossOrigin = (crossOrigin == CrossOriginAttributeAnonymous); |
+ } |
+ |
+ bool didDnsPrefetch() { return m_didDnsPrefetch; } |
+ bool didPreconnect() { return m_didPreconnect; } |
+ bool isHTTPS() { return m_isHTTPS; } |
+ bool isCrossOrigin() { return m_isCrossOrigin; } |
+ |
+private: |
+ mutable bool m_didDnsPrefetch; |
+ mutable bool m_didPreconnect; |
+ mutable bool m_isHTTPS; |
+ mutable bool m_isCrossOrigin; |
+ |
+}; |
+ |
TEST(LinkLoaderTest, Preload) |
{ |
struct TestCase { |
@@ -65,7 +100,8 @@ TEST(LinkLoaderTest, Preload) |
String(), |
testCase.as, |
hrefURL, |
- dummyPageHolder->document()); |
+ dummyPageHolder->document(), |
+ NetworkHintsMock()); |
if (testCase.priority == ResourceLoadPriorityUnresolved) { |
ASSERT(!loader.resource()); |
} else { |
@@ -75,4 +111,72 @@ TEST(LinkLoaderTest, Preload) |
} |
} |
+TEST(LinkLoaderTest, DNSPrefetch) |
+{ |
+ struct { |
+ const char* href; |
+ const bool shouldLoad; |
+ } cases[] = { |
+ {"http://example.com/", true}, |
+ {"https://example.com/", true}, |
+ {"//example.com/", true}, |
+ }; |
+ |
+ // TODO(yoav): Test (and fix) shouldLoad = false |
+ |
+ // Test the cases with a single header |
+ for (const auto& testCase : cases) { |
+ OwnPtr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); |
+ dummyPageHolder->document().settings()->setDNSPrefetchingEnabled(true); |
+ MockLinkLoaderClient loaderClient(testCase.shouldLoad); |
+ LinkLoader loader(&loaderClient); |
+ KURL hrefURL = KURL(KURL(ParsedURLStringTag(), String("http://example.com")), testCase.href); |
+ NetworkHintsMock networkHints; |
+ loader.loadLink(LinkRelAttribute("dns-prefetch"), |
+ AtomicString(), |
+ String(), |
+ String(), |
+ hrefURL, |
+ dummyPageHolder->document(), |
+ networkHints); |
+ ASSERT_FALSE(networkHints.didPreconnect()); |
+ ASSERT_EQ(testCase.shouldLoad, networkHints.didDnsPrefetch()); |
+ } |
+} |
+ |
+TEST(LinkLoaderTest, Preconnect) |
+{ |
+ struct { |
+ const char* href; |
+ const char* crossOrigin; |
+ const bool shouldLoad; |
+ const bool isHTTPS; |
+ const bool isCrossOrigin; |
+ } cases[] = { |
+ {"http://example.com/", nullptr, true, false, false}, |
+ {"https://example.com/", nullptr, true, true, false}, |
+ {"http://example.com/", "anonymous", true, false, true}, |
+ {"//example.com/", nullptr, true, false, false}, |
+ }; |
+ |
+ // Test the cases with a single header |
+ for (const auto& testCase : cases) { |
+ OwnPtr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); |
+ MockLinkLoaderClient loaderClient(testCase.shouldLoad); |
+ LinkLoader loader(&loaderClient); |
+ KURL hrefURL = KURL(KURL(ParsedURLStringTag(), String("http://example.com")), testCase.href); |
+ NetworkHintsMock networkHints; |
+ loader.loadLink(LinkRelAttribute("preconnect"), |
+ testCase.crossOrigin, |
+ String(), |
+ String(), |
+ hrefURL, |
+ dummyPageHolder->document(), |
+ networkHints); |
+ ASSERT_EQ(testCase.shouldLoad, networkHints.didPreconnect()); |
+ ASSERT_EQ(testCase.isHTTPS, networkHints.isHTTPS()); |
+ ASSERT_EQ(testCase.isCrossOrigin, networkHints.isCrossOrigin()); |
+ } |
+} |
+ |
} // namespace blink |