OLD | NEW |
| (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 "core/fetch/Resource.h" | |
6 | |
7 #include "core/fetch/MemoryCache.h" | |
8 #include "core/fetch/RawResource.h" | |
9 #include "platform/SharedBuffer.h" | |
10 #include "platform/network/ResourceRequest.h" | |
11 #include "platform/network/ResourceResponse.h" | |
12 #include "platform/testing/TestingPlatformSupport.h" | |
13 #include "platform/testing/URLTestHelpers.h" | |
14 #include "public/platform/Platform.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 #include "wtf/Vector.h" | |
17 | |
18 namespace blink { | |
19 | |
20 namespace { | |
21 | |
22 class MockPlatform final : public TestingPlatformSupport { | |
23 public: | |
24 MockPlatform() {} | |
25 ~MockPlatform() override {} | |
26 | |
27 // From blink::Platform: | |
28 void cacheMetadata(const WebURL& url, int64_t, const char*, size_t) override { | |
29 m_cachedURLs.push_back(url); | |
30 } | |
31 | |
32 const Vector<WebURL>& cachedURLs() const { return m_cachedURLs; } | |
33 | |
34 private: | |
35 Vector<WebURL> m_cachedURLs; | |
36 }; | |
37 | |
38 ResourceResponse createTestResourceResponse() { | |
39 ResourceResponse response; | |
40 response.setURL(URLTestHelpers::toKURL("https://example.com/")); | |
41 response.setHTTPStatusCode(200); | |
42 return response; | |
43 } | |
44 | |
45 void createTestResourceAndSetCachedMetadata(const ResourceResponse& response) { | |
46 const char testData[] = "test data"; | |
47 Resource* resource = | |
48 RawResource::create(ResourceRequest(response.url()), Resource::Raw); | |
49 resource->setResponse(response); | |
50 resource->cacheHandler()->setCachedMetadata( | |
51 100, testData, sizeof(testData), CachedMetadataHandler::SendToPlatform); | |
52 return; | |
53 } | |
54 | |
55 } // anonymous namespace | |
56 | |
57 TEST(ResourceTest, SetCachedMetadata_SendsMetadataToPlatform) { | |
58 ScopedTestingPlatformSupport<MockPlatform> mock; | |
59 ResourceResponse response(createTestResourceResponse()); | |
60 createTestResourceAndSetCachedMetadata(response); | |
61 EXPECT_EQ(1u, mock->cachedURLs().size()); | |
62 } | |
63 | |
64 TEST( | |
65 ResourceTest, | |
66 SetCachedMetadata_DoesNotSendMetadataToPlatformWhenFetchedViaServiceWorker)
{ | |
67 ScopedTestingPlatformSupport<MockPlatform> mock; | |
68 ResourceResponse response(createTestResourceResponse()); | |
69 response.setWasFetchedViaServiceWorker(true); | |
70 createTestResourceAndSetCachedMetadata(response); | |
71 EXPECT_EQ(0u, mock->cachedURLs().size()); | |
72 } | |
73 | |
74 TEST(ResourceTest, RevalidateWithFragment) { | |
75 KURL url(ParsedURLString, "http://127.0.0.1:8000/foo.html"); | |
76 ResourceResponse response; | |
77 response.setURL(url); | |
78 response.setHTTPStatusCode(200); | |
79 Resource* resource = RawResource::create(url, Resource::Raw); | |
80 resource->responseReceived(response, nullptr); | |
81 resource->finish(); | |
82 | |
83 // Revalidating with a url that differs by only the fragment | |
84 // shouldn't trigger a securiy check. | |
85 url.setFragmentIdentifier("bar"); | |
86 resource->setRevalidatingRequest(ResourceRequest(url)); | |
87 ResourceResponse revalidatingResponse; | |
88 revalidatingResponse.setURL(url); | |
89 revalidatingResponse.setHTTPStatusCode(304); | |
90 resource->responseReceived(revalidatingResponse, nullptr); | |
91 } | |
92 | |
93 } // namespace blink | |
OLD | NEW |