2 * Copyright (C) 2011 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
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
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.
34 #include "WebFrameClient.h"
35 #include "WebString.h"
37 #include "WebURLLoader.h"
38 #include "WebURLLoaderClient.h"
39 #include "WebURLLoaderOptions.h"
40 #include "WebURLRequest.h"
41 #include "WebURLResponse.h"
44 #include <googleurl/src/gurl.h>
45 #include <gtest/gtest.h>
46 #include <webkit/support/webkit_support.h>
48 using namespace WebKit;
52 class TestWebFrameClient : public WebFrameClient {
53 // Return a non-null cancellation error so the WebFrame loaders can shut down without asserting.
54 // Make 'reason' non-zero so WebURLError isn't considered null.
55 WebURLError cancelledError(WebFrame*, const WebURLRequest& request)
59 error.unreachableURL = request.url();
64 class AssociatedURLLoaderTest : public testing::Test,
65 public WebURLLoaderClient {
67 AssociatedURLLoaderTest()
68 : m_willSendRequest(false)
69 , m_didSendData(false)
70 , m_didReceiveResponse(false)
71 , m_didReceiveData(false)
72 , m_didReceiveCachedMetadata(false)
73 , m_didFinishLoading(false)
76 // Reuse one of the test files from WebFrameTest.
77 std::string filePath = webkit_support::GetWebKitRootDir().utf8();
78 filePath += "/Source/WebKit/chromium/tests/data/iframes_test.html";
79 m_frameFilePath = WebString::fromUTF8(filePath);
84 m_webView = WebView::create(0);
85 m_webView->initializeMainFrame(&m_webFrameClient);
87 // Load the frame before trying to load resources.
88 GURL url = GURL("http://www.test.com/iframes_test.html");
89 WebURLResponse response;
90 response.initialize();
91 response.setMIMEType("text/html");
92 webkit_support::RegisterMockedURL(url, response, m_frameFilePath);
94 WebURLRequest request;
97 m_webView->mainFrame()->loadRequest(request);
100 webkit_support::UnregisterMockedURL(url);
105 webkit_support::UnregisterAllMockedURLs();
111 webkit_support::ServeAsynchronousMockedRequests();
114 WebURLLoader* createAssociatedURLLoader(const WebURLLoaderOptions options = WebURLLoaderOptions())
116 return m_webView->mainFrame()->createAssociatedURLLoader(options);
119 // WebURLLoaderClient implementation.
120 void willSendRequest(WebURLLoader* loader, WebURLRequest& newRequest, const WebURLResponse& redirectResponse)
122 m_willSendRequest = true;
123 EXPECT_EQ(m_expectedLoader, loader);
124 EXPECT_EQ(m_expectedNewRequest.url(), newRequest.url());
125 EXPECT_EQ(m_expectedRedirectResponse.url(), redirectResponse.url());
126 EXPECT_EQ(m_expectedRedirectResponse.mimeType(), redirectResponse.mimeType());
127 EXPECT_EQ(m_expectedRedirectResponse.httpStatusCode(), redirectResponse.httpStatusCode());
130 void didSendData(WebURLLoader* loader, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
132 m_didSendData = true;
133 EXPECT_EQ(m_expectedLoader, loader);
136 void didReceiveResponse(WebURLLoader* loader, const WebURLResponse& response)
138 m_didReceiveResponse = true;
139 EXPECT_EQ(m_expectedLoader, loader);
140 EXPECT_EQ(m_expectedResponse.url(), response.url());
141 EXPECT_EQ(m_expectedResponse.httpStatusCode(), response.httpStatusCode());
144 void didDownloadData(WebURLLoader* loader, int dataLength)
146 m_didDownloadData = true;
147 EXPECT_EQ(m_expectedLoader, loader);
150 void didReceiveData(WebURLLoader* loader, const char* data, int dataLength, int encodedDataLength)
152 m_didReceiveData = true;
153 EXPECT_EQ(m_expectedLoader, loader);
155 EXPECT_GT(dataLength, 0);
158 void didReceiveCachedMetadata(WebURLLoader* loader, const char* data, int dataLength)
160 m_didReceiveCachedMetadata = true;
161 EXPECT_EQ(m_expectedLoader, loader);
164 void didFinishLoading(WebURLLoader* loader, double finishTime)
166 m_didFinishLoading = true;
167 EXPECT_EQ(m_expectedLoader, loader);
170 void didFail(WebURLLoader* loader, const WebURLError& error)
173 EXPECT_EQ(m_expectedLoader, loader);
174 webkit_support::QuitMessageLoop();
178 WebString m_frameFilePath;
179 TestWebFrameClient m_webFrameClient;
182 WebURLLoader* m_expectedLoader;
183 WebURLResponse m_expectedResponse;
184 WebURLRequest m_expectedNewRequest;
185 WebURLResponse m_expectedRedirectResponse;
186 bool m_willSendRequest;
188 bool m_didReceiveResponse;
189 bool m_didDownloadData;
190 bool m_didReceiveData;
191 bool m_didReceiveCachedMetadata;
192 bool m_didFinishLoading;
196 // Test a successful URL load.
197 TEST_F(AssociatedURLLoaderTest, Success)
199 GURL url = GURL("http://www.test.com/success.html");
200 WebURLRequest request;
201 request.initialize();
204 m_expectedResponse = WebURLResponse();
205 m_expectedResponse.initialize();
206 m_expectedResponse.setMIMEType("text/html");
207 webkit_support::RegisterMockedURL(url, m_expectedResponse, m_frameFilePath);
209 m_expectedLoader = createAssociatedURLLoader();
210 EXPECT_TRUE(m_expectedLoader);
211 m_expectedLoader->loadAsynchronously(request, this);
213 EXPECT_TRUE(m_didReceiveResponse);
214 EXPECT_TRUE(m_didReceiveData);
215 EXPECT_TRUE(m_didFinishLoading);
218 // Test that the same-origin restriction is the default.
219 TEST_F(AssociatedURLLoaderTest, SameOriginRestriction)
221 // This is cross-origin since the frame was loaded from www.test.com.
222 GURL url = GURL("http://www.other.com/failure.html");
223 WebURLRequest request;
224 request.initialize();
227 m_expectedLoader = createAssociatedURLLoader();
228 EXPECT_TRUE(m_expectedLoader);
229 m_expectedLoader->loadAsynchronously(request, this);
230 // Failure should not be reported synchronously.
231 EXPECT_FALSE(m_didFail);
232 // Allow the loader to return the error.
233 webkit_support::RunMessageLoop();
234 EXPECT_TRUE(m_didFail);
237 // Test a successful cross-origin load.
238 TEST_F(AssociatedURLLoaderTest, CrossOriginSuccess)
240 // This is cross-origin since the frame was loaded from www.test.com.
241 GURL url = GURL("http://www.other.com/success.html");
242 WebURLRequest request;
243 request.initialize();
246 m_expectedResponse = WebURLResponse();
247 m_expectedResponse.initialize();
248 m_expectedResponse.setMIMEType("text/html");
249 webkit_support::RegisterMockedURL(url, m_expectedResponse, m_frameFilePath);
251 WebURLLoaderOptions options;
252 options.crossOriginRequestPolicy = WebURLLoaderOptions::CrossOriginRequestPolicyAllow;
253 m_expectedLoader = createAssociatedURLLoader(options);
254 EXPECT_TRUE(m_expectedLoader);
255 m_expectedLoader->loadAsynchronously(request, this);
257 EXPECT_TRUE(m_didReceiveResponse);
258 EXPECT_TRUE(m_didReceiveData);
259 EXPECT_TRUE(m_didFinishLoading);