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

Side by Side Diff: Source/core/html/parser/HTMLResourcePreloaderTest.cpp

Issue 1112833004: Add HTMLResourcePreloader preconnect unit tests (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Fixed crash Created 5 years, 4 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
(Empty)
1 // Copyright 2015 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
5 #include "config.h"
6 #include "core/html/parser/HTMLResourcePreloader.h"
7
8 #include "core/html/parser/PreloadRequest.h"
9 #include "core/testing/DummyPageHolder.h"
10 #include <gtest/gtest.h>
11
12 namespace blink {
13
14 struct PreconnectTestCase {
15 const char* baseURL;
16 const char* url;
17 bool isCORS;
18 bool isHTTPS;
19 };
20
21 class PreloaderNetworkHintsMock : public NetworkHintsInterface {
22 public:
23 PreloaderNetworkHintsMock()
24 : m_didPreconnect(false)
25 {
26 }
27
28 void dnsPrefetchHost(const String& host) const { }
29 void preconnectHost(const KURL& host, const CrossOriginAttributeValue crossO rigin) const override
30 {
31 m_didPreconnect = true;
32 m_isHTTPS = host.protocolIs("https");
33 m_isCrossOrigin = (crossOrigin == CrossOriginAttributeAnonymous);
34 }
35
36 bool didPreconnect() { return m_didPreconnect; }
37 bool isHTTPS() { return m_isHTTPS; }
38 bool isCrossOrigin() { return m_isCrossOrigin; }
39
40 private:
41 mutable bool m_didPreconnect;
42 mutable bool m_isHTTPS;
43 mutable bool m_isCrossOrigin;
44 };
45
46 class HTMLResourcePreloaderTest : public testing::Test {
47 protected:
48 HTMLResourcePreloaderTest()
49 : m_dummyPageHolder(DummyPageHolder::create())
50 {
51 }
52
53 void test(PreconnectTestCase testCase)
54 {
55 // TODO(yoav): Need a mock loader here to verify things are happenning b eyond preconnect.
56 PreloaderNetworkHintsMock networkHints;
57 OwnPtr<PreloadRequest> preloadRequest = PreloadRequest::create(String(),
58 TextPosition(),
59 testCase.url,
60 KURL(ParsedURLStringTag(), testCase.baseURL),
61 Resource::Image,
62 ReferrerPolicy(),
63 FetchRequest::ResourceWidth(),
64 ClientHintsPreferences(),
65 PreloadRequest::RequestTypePreconnect);
66 if (testCase.isCORS)
67 preloadRequest->setCrossOriginEnabled(DoNotAllowStoredCredentials);
68 OwnPtrWillBeRawPtr<HTMLResourcePreloader> preloader = HTMLResourcePreloa der::create(m_dummyPageHolder->document());
69 preloader->preload(preloadRequest.release(), networkHints);
70 ASSERT_TRUE(networkHints.didPreconnect());
71 ASSERT_EQ(testCase.isCORS, networkHints.isCrossOrigin());
72 ASSERT_EQ(testCase.isHTTPS, networkHints.isHTTPS());
73 }
74
75 private:
76 OwnPtr<DummyPageHolder> m_dummyPageHolder;
77 };
78
79 TEST_F(HTMLResourcePreloaderTest, testPreconnect)
80 {
81 PreconnectTestCase testCases[] = {
82 { "http://example.test", "http://example.com", false, false },
83 { "http://example.test", "http://example.com", true, false },
84 { "http://example.test", "https://example.com", true, true },
85 { "http://example.test", "https://example.com", false, true },
86 { "http://example.test", "//example.com", false, false },
87 { "http://example.test", "//example.com", true, false },
88 { "https://example.test", "//example.com", false, true },
89 { "https://example.test", "//example.com", true, true },
90 };
91
92 for (const auto& testCase : testCases)
93 test(testCase);
94 }
95
96 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/html/parser/HTMLResourcePreloader.cpp ('k') | Source/core/html/parser/ResourcePreloader.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698