Index: third_party/WebKit/Source/core/fetch/ResourceFetcherTest.cpp |
diff --git a/third_party/WebKit/Source/core/fetch/ResourceFetcherTest.cpp b/third_party/WebKit/Source/core/fetch/ResourceFetcherTest.cpp |
index b17a699bbb0edf5c6103119e474e3621949084fb..8a6a94152487b5972c91806cac71d246c768216c 100644 |
--- a/third_party/WebKit/Source/core/fetch/ResourceFetcherTest.cpp |
+++ b/third_party/WebKit/Source/core/fetch/ResourceFetcherTest.cpp |
@@ -38,7 +38,10 @@ |
#include "core/fetch/ResourceLoader.h" |
#include "platform/exported/WrappedResourceResponse.h" |
#include "platform/heap/Handle.h" |
+#include "platform/heap/HeapAllocator.h" |
+#include "platform/heap/Member.h" |
#include "platform/network/ResourceRequest.h" |
+#include "platform/network/ResourceTimingInfo.h" |
#include "platform/testing/URLTestHelpers.h" |
#include "platform/weborigin/KURL.h" |
#include "public/platform/Platform.h" |
@@ -46,13 +49,18 @@ |
#include "public/platform/WebURLLoaderMockFactory.h" |
#include "public/platform/WebURLResponse.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+#include "wtf/Allocator.h" |
#include "wtf/PtrUtil.h" |
+#include "wtf/Vector.h" |
#include <memory> |
namespace blink { |
namespace { |
+const char testImageFilename[] = "white-1x1.png"; |
+const int testImageSize = 103; // size of web/tests/data/white-1x1.png |
+ |
class MockTaskRunner : public blink::WebTaskRunner { |
void postTask(const WebTraceLocation&, Task*) override { } |
void postDelayedTask(const WebTraceLocation&, Task*, double) override { } |
@@ -82,16 +90,21 @@ public: |
void setLoadComplete(bool complete) { m_complete = complete; } |
bool isLoadComplete() const override { return m_complete; } |
+ void addResourceTiming(const ResourceTimingInfo& resourceTimingInfo) override { m_transferSize = resourceTimingInfo.transferSize(); } |
+ long long getTransferSize() const { return m_transferSize; } |
+ |
private: |
ResourceFetcherTestMockFetchContext() |
: m_policy(CachePolicyVerify) |
, m_runner(wrapUnique(new MockTaskRunner)) |
, m_complete(false) |
+ , m_transferSize(-1) |
{ } |
CachePolicy m_policy; |
std::unique_ptr<MockTaskRunner> m_runner; |
bool m_complete; |
+ long long m_transferSize; |
}; |
class ResourceFetcherTest : public ::testing::Test { |
@@ -205,7 +218,7 @@ TEST_F(ResourceFetcherTest, VaryImage) |
response.setHTTPStatusCode(200); |
response.setHTTPHeaderField(HTTPNames::Cache_Control, "max-age=3600"); |
response.setHTTPHeaderField(HTTPNames::Vary, "*"); |
- URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, "white-1x1.png", WebString::fromUTF8(""), WrappedResourceResponse(response)); |
+ URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, testImageFilename, WebString::fromUTF8(""), WrappedResourceResponse(response)); |
FetchRequest fetchRequestOriginal = FetchRequest(url, FetchInitiatorInfo()); |
Resource* resource = fetcher->requestResource(fetchRequestOriginal, TestResourceFactory(Resource::Image)); |
@@ -379,7 +392,7 @@ TEST_F(ResourceFetcherTest, ResponseOnCancel) |
ResourceResponse response; |
response.setURL(url); |
response.setHTTPStatusCode(200); |
- URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, "white-1x1.png", WebString::fromUTF8(""), WrappedResourceResponse(response)); |
+ URLTestHelpers::registerMockedURLLoadWithCustomResponse(url, testImageFilename, WebString::fromUTF8(""), WrappedResourceResponse(response)); |
ResourceFetcher* fetcher = ResourceFetcher::create(ResourceFetcherTestMockFetchContext::create()); |
FetchRequest fetchRequest = FetchRequest(url, FetchInitiatorInfo()); |
@@ -391,4 +404,115 @@ TEST_F(ResourceFetcherTest, ResponseOnCancel) |
Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
} |
+class ScopedMockRedirectRequester { |
+ STACK_ALLOCATED(); |
+ WTF_MAKE_NONCOPYABLE(ScopedMockRedirectRequester); |
+ |
+public: |
+ ScopedMockRedirectRequester() |
+ : m_context(nullptr) |
+ { |
+ } |
+ |
+ ~ScopedMockRedirectRequester() |
+ { |
+ cleanUp(); |
+ } |
+ |
+ void registerRedirect(const WebString& fromURL, const WebString& toURL) |
+ { |
+ KURL redirectURL(ParsedURLString, fromURL); |
+ WebURLResponse redirectResponse; |
+ redirectResponse.initialize(); |
+ redirectResponse.setURL(redirectURL); |
+ redirectResponse.setHTTPStatusCode(301); |
+ redirectResponse.setHTTPHeaderField(HTTPNames::Location, toURL); |
+ Platform::current()->getURLLoaderMockFactory()->registerURL(redirectURL, redirectResponse, ""); |
+ m_registeredURLs.append(redirectURL); |
+ } |
+ |
+ void registerFinalResource(const WebString& url) |
+ { |
+ KURL finalURL(ParsedURLString, url); |
+ WebURLResponse finalResponse; |
+ finalResponse.initialize(); |
+ finalResponse.setURL(finalURL); |
+ finalResponse.setHTTPStatusCode(200); |
+ URLTestHelpers::registerMockedURLLoadWithCustomResponse(finalURL, testImageFilename, "", finalResponse); |
+ m_registeredURLs.append(finalURL); |
+ } |
+ |
+ void request(const WebString& url) |
+ { |
+ DCHECK(!m_context); |
+ m_context = ResourceFetcherTestMockFetchContext::create(); |
+ ResourceFetcher* fetcher = ResourceFetcher::create(m_context); |
+ FetchRequest fetchRequest = FetchRequest(ResourceRequest(url), FetchInitiatorInfo()); |
+ Resource* resource = fetcher->requestResource(fetchRequest, TestResourceFactory()); |
+ m_cachedResources.append(resource); |
+ Platform::current()->getURLLoaderMockFactory()->serveAsynchronousRequests(); |
+ } |
+ |
+ void cleanUp() |
+ { |
+ for (const KURL& url : m_registeredURLs) { |
+ Platform::current()->getURLLoaderMockFactory()->unregisterURL(url); |
+ } |
+ m_registeredURLs.clear(); |
+ for (const auto& resource : m_cachedResources) { |
+ memoryCache()->remove(resource); |
+ } |
+ m_cachedResources.clear(); |
+ } |
+ |
+ ResourceFetcherTestMockFetchContext* context() const { return m_context; } |
+ |
+private: |
+ Vector<KURL> m_registeredURLs; |
+ HeapVector<Member<Resource>> m_cachedResources; |
+ Member<ResourceFetcherTestMockFetchContext> m_context; |
+}; |
+ |
+TEST_F(ResourceFetcherTest, SameOriginRedirect) |
+{ |
+ const char redirectURL[] = "http://127.0.0.1:8000/redirect.html"; |
+ const char finalURL[] = "http://127.0.0.1:8000/final.html"; |
+ ScopedMockRedirectRequester requester; |
+ requester.registerRedirect(redirectURL, finalURL); |
+ requester.registerFinalResource(finalURL); |
+ requester.request(redirectURL); |
+ |
+ // The magic number "300" is specified as the redirect overhead in |
Kunihiko Sakamoto
2016/07/01 10:12:35
Can you define this magic number as a constant?
Adam Rice
2016/07/04 02:44:18
Done.
|
+ // WebURLLoaderMock::ServeRedirect |
+ EXPECT_EQ(300 + testImageSize, requester.context()->getTransferSize()); |
+} |
+ |
+TEST_F(ResourceFetcherTest, CrossOriginRedirect) |
+{ |
+ const char redirectURL[] = "http://otherorigin.test/redirect.html"; |
+ const char finalURL[] = "http://127.0.0.1:8000/final.html"; |
+ ScopedMockRedirectRequester requester; |
+ requester.registerRedirect(redirectURL, finalURL); |
+ requester.registerFinalResource(finalURL); |
+ requester.request(redirectURL); |
+ |
+ EXPECT_EQ(testImageSize, requester.context()->getTransferSize()); |
+} |
+ |
+TEST_F(ResourceFetcherTest, ComplexCrossOriginRedirect) |
+{ |
+ const char redirectURL1[] = "http://127.0.0.1:8000/redirect1.html"; |
+ const char redirectURL2[] = "http://otherorigin.test/redirect2.html"; |
+ const char redirectURL3[] = "http://127.0.0.1:8000/redirect3.html"; |
+ const char finalURL[] = "http://127.0.0.1:8000/final.html"; |
+ ScopedMockRedirectRequester requester; |
+ requester.registerRedirect(redirectURL1, redirectURL2); |
+ requester.registerRedirect(redirectURL2, redirectURL3); |
+ requester.registerRedirect(redirectURL3, finalURL); |
+ requester.registerFinalResource(finalURL); |
+ requester.request(redirectURL1); |
+ |
+ EXPECT_EQ(testImageSize, requester.context()->getTransferSize()); |
+} |
+ |
} // namespace blink |