Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / fetch / ResourceFetcherTest.cpp
blobd41fcba891f1c8daf3dd71c216cf320cf16f40e5
1 /*
2 * Copyright (c) 2013, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "config.h"
32 #include "core/fetch/ResourceFetcher.h"
34 #include "core/fetch/FetchInitiatorInfo.h"
35 #include "core/fetch/FetchRequest.h"
36 #include "core/fetch/MemoryCache.h"
37 #include "core/fetch/ResourceLoader.h"
38 #include "core/fetch/ResourcePtr.h"
39 #include "platform/exported/WrappedResourceResponse.h"
40 #include "platform/heap/Handle.h"
41 #include "platform/network/ResourceRequest.h"
42 #include "platform/testing/URLTestHelpers.h"
43 #include "platform/weborigin/KURL.h"
44 #include "public/platform/Platform.h"
45 #include "public/platform/WebURLResponse.h"
46 #include "public/platform/WebUnitTestSupport.h"
47 #include <gtest/gtest.h>
49 namespace blink {
51 class ResourceFetcherTestMockFetchContext : public FetchContext {
52 public:
53 static ResourceFetcherTestMockFetchContext* create()
55 return new ResourceFetcherTestMockFetchContext;
58 virtual ~ResourceFetcherTestMockFetchContext() { }
60 bool allowImage(bool imagesEnabled, const KURL&) const override { return true; }
61 bool canRequest(Resource::Type, const ResourceRequest&, const KURL&, const ResourceLoaderOptions&, bool forPreload, FetchRequest::OriginRestriction) const override { return true; }
62 bool shouldLoadNewResource(Resource::Type) const override { return true; }
64 void setCachePolicy(CachePolicy policy) { m_policy = policy; }
65 CachePolicy cachePolicy() const override { return m_policy; }
67 private:
68 ResourceFetcherTestMockFetchContext()
69 : m_policy(CachePolicyVerify)
70 { }
72 CachePolicy m_policy;
75 class ResourceFetcherTest : public ::testing::Test {
78 class TestResourceFactory : public ResourceFactory {
79 public:
80 TestResourceFactory(Resource::Type type = Resource::Raw)
81 : ResourceFactory(type) { }
83 Resource* create(const ResourceRequest& request, const String& charset) const override
85 return new Resource(request, type());
89 TEST_F(ResourceFetcherTest, StartLoadAfterFrameDetach)
91 KURL secureURL(ParsedURLString, "https://secureorigin.test/image.png");
92 // Try to request a url. The request should fail, no resource should be returned,
93 // and no resource should be present in the cache.
94 ResourceFetcher* fetcher = ResourceFetcher::create(nullptr);
95 FetchRequest fetchRequest = FetchRequest(ResourceRequest(secureURL), FetchInitiatorInfo());
96 ResourcePtr<Resource> resource = fetcher->requestResource(fetchRequest, TestResourceFactory());
97 EXPECT_EQ(resource.get(), static_cast<Resource*>(nullptr));
98 EXPECT_EQ(memoryCache()->resourceForURL(secureURL), static_cast<Resource*>(nullptr));
101 TEST_F(ResourceFetcherTest, UseExistingResource)
103 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFetchContext::create());
105 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html");
106 ResourcePtr<Resource> resource = new Resource(url, Resource::Image);
107 memoryCache()->add(resource.get());
108 ResourceResponse response;
109 response.setURL(url);
110 response.setHTTPStatusCode(200);
111 response.setHTTPHeaderField("Cache-Control", "max-age=3600");
112 resource->responseReceived(response, nullptr);
113 resource->finish();
115 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo());
116 ResourcePtr<Resource> newResource = fetcher->requestResource(fetchRequest, TestResourceFactory(Resource::Image));
117 EXPECT_EQ(resource, newResource);
118 memoryCache()->remove(resource.get());
121 TEST_F(ResourceFetcherTest, Vary)
123 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html");
124 ResourcePtr<Resource> resource = new Resource(url, Resource::Raw);
125 memoryCache()->add(resource.get());
126 ResourceResponse response;
127 response.setURL(url);
128 response.setHTTPStatusCode(200);
129 response.setHTTPHeaderField("Cache-Control", "max-age=3600");
130 response.setHTTPHeaderField("Vary", "*");
131 resource->responseReceived(response, nullptr);
132 resource->finish();
133 ASSERT_TRUE(resource->hasVaryHeader());
135 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFetchContext::create());
136 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo());
137 Platform::current()->unitTestSupport()->registerMockedURL(url, WebURLResponse(), "");
138 ResourcePtr<Resource> newResource = fetcher->requestResource(fetchRequest, TestResourceFactory());
139 EXPECT_NE(resource, newResource);
140 newResource->loader()->cancel();
141 memoryCache()->remove(newResource.get());
142 Platform::current()->unitTestSupport()->unregisterMockedURL(url);
144 memoryCache()->remove(resource.get());
147 TEST_F(ResourceFetcherTest, VaryOnBack)
149 ResourceFetcherTestMockFetchContext* context = ResourceFetcherTestMockFetchContext::create();
150 context->setCachePolicy(CachePolicyHistoryBuffer);
151 ResourceFetcher* fetcher = ResourceFetcher::create(context);
153 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html");
154 ResourcePtr<Resource> resource = new Resource(url, Resource::Raw);
155 memoryCache()->add(resource.get());
156 ResourceResponse response;
157 response.setURL(url);
158 response.setHTTPStatusCode(200);
159 response.setHTTPHeaderField("Cache-Control", "max-age=3600");
160 response.setHTTPHeaderField("Vary", "*");
161 resource->responseReceived(response, nullptr);
162 resource->finish();
163 ASSERT_TRUE(resource->hasVaryHeader());
165 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo());
166 ResourcePtr<Resource> newResource = fetcher->requestResource(fetchRequest, TestResourceFactory());
167 EXPECT_EQ(resource, newResource);
169 memoryCache()->remove(newResource.get());
172 TEST_F(ResourceFetcherTest, VaryImage)
174 ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFetchContext::create());
176 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html");
177 ResourceResponse response;
178 response.setURL(url);
179 response.setHTTPStatusCode(200);
180 response.setHTTPHeaderField("Cache-Control", "max-age=3600");
181 response.setHTTPHeaderField("Vary", "*");
182 URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, "white-1x1.png", WebString::fromUTF8(""), WrappedResourceResponse(response));
184 FetchRequest fetchRequestOriginal = FetchRequest(url, FetchInitiatorInfo());
185 ResourcePtr<Resource> resource = fetcher->requestResource(fetchRequestOriginal, TestResourceFactory(Resource::Image));
186 ASSERT_TRUE(resource.get());
187 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
188 ASSERT_TRUE(resource->hasVaryHeader());
190 FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo());
191 ResourcePtr<Resource> newResource = fetcher->requestResource(fetchRequest, TestResourceFactory(Resource::Image));
192 EXPECT_EQ(resource, newResource);
194 memoryCache()->remove(newResource.get());
195 Platform::current()->unitTestSupport()->unregisterMockedURL(url);
198 TEST_F(ResourceFetcherTest, RevalidateWhileLoading)
200 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html");
201 Platform::current()->unitTestSupport()->registerMockedURL(url, WebURLResponse(), "");
203 ResourceFetcher* fetcher1 = ResourceFetcher::create(ResourceFetcherTestMockFetchContext::create());
204 ResourceRequest request1(url);
205 request1.setHTTPHeaderField("Cache-control", "no-cache");
206 FetchRequest fetchRequest1 = FetchRequest(request1, FetchInitiatorInfo());
207 ResourcePtr<Resource> resource1 = fetcher1->requestResource(fetchRequest1, TestResourceFactory(Resource::Image));
208 ResourceResponse response;
209 response.setURL(url);
210 response.setHTTPStatusCode(200);
211 response.setHTTPHeaderField("Cache-Control", "max-age=3600");
212 response.setHTTPHeaderField("etag", "1234567890");
213 resource1->responseReceived(response, nullptr);
214 resource1->finish();
216 ResourceFetcherTestMockFetchContext* context = ResourceFetcherTestMockFetchContext::create();
217 context->setCachePolicy(CachePolicyRevalidate);
218 ResourceFetcher* fetcher2 = ResourceFetcher::create(context);
219 FetchRequest fetchRequest2(url, FetchInitiatorInfo());
220 ResourcePtr<Resource> resource2 = fetcher2->requestResource(fetchRequest2, TestResourceFactory(Resource::Image));
221 EXPECT_EQ(resource1, resource2);
223 // Tidily(?) shut down the ResourceLoader.
224 resource1->loader()->cancel();
225 Platform::current()->unitTestSupport()->unregisterMockedURL(url);
228 } // namespace blink