initial import
[vuplus_webkit] / Source / WebKit / chromium / tests / AssociatedURLLoaderTest.cpp
1 /*
2  * Copyright (C) 2011 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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.
17  *
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.
29  */
30
31 #include "config.h"
32
33 #include "WebFrame.h"
34 #include "WebFrameClient.h"
35 #include "WebString.h"
36 #include "WebURL.h"
37 #include "WebURLLoader.h"
38 #include "WebURLLoaderClient.h"
39 #include "WebURLLoaderOptions.h"
40 #include "WebURLRequest.h"
41 #include "WebURLResponse.h"
42 #include "WebView.h"
43
44 #include <googleurl/src/gurl.h>
45 #include <gtest/gtest.h>
46 #include <webkit/support/webkit_support.h>
47
48 using namespace WebKit;
49
50 namespace {
51
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)
56     {
57         WebURLError error;
58         error.reason = 1;
59         error.unreachableURL = request.url();
60         return error;
61     }
62 };
63
64 class AssociatedURLLoaderTest : public testing::Test,
65                                 public WebURLLoaderClient {
66 public:
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)
74         ,  m_didFail(false)
75     {
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);
80     }
81
82     void SetUp()
83     {
84         m_webView = WebView::create(0);
85         m_webView->initializeMainFrame(&m_webFrameClient);
86
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);
93
94         WebURLRequest request;
95         request.initialize();
96         request.setURL(url);
97         m_webView->mainFrame()->loadRequest(request);
98         serveRequests();
99
100         webkit_support::UnregisterMockedURL(url);
101     }
102
103     void TearDown()
104     {
105         webkit_support::UnregisterAllMockedURLs();
106         m_webView->close();
107     }
108
109     void serveRequests()
110     {
111         webkit_support::ServeAsynchronousMockedRequests();
112     }
113
114     WebURLLoader* createAssociatedURLLoader(const WebURLLoaderOptions options = WebURLLoaderOptions())
115     {
116         return m_webView->mainFrame()->createAssociatedURLLoader(options);
117     }
118
119     // WebURLLoaderClient implementation.
120     void willSendRequest(WebURLLoader* loader, WebURLRequest& newRequest, const WebURLResponse& redirectResponse)
121     {
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());
128     }
129
130     void didSendData(WebURLLoader* loader, unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
131     {
132         m_didSendData = true;
133         EXPECT_EQ(m_expectedLoader, loader);
134     }
135
136     void didReceiveResponse(WebURLLoader* loader, const WebURLResponse& response)
137     {
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());
142     }
143
144     void didDownloadData(WebURLLoader* loader, int dataLength)
145     {
146         m_didDownloadData = true;
147         EXPECT_EQ(m_expectedLoader, loader);
148     }
149
150     void didReceiveData(WebURLLoader* loader, const char* data, int dataLength, int encodedDataLength)
151     {
152         m_didReceiveData = true;
153         EXPECT_EQ(m_expectedLoader, loader);
154         EXPECT_TRUE(data);
155         EXPECT_GT(dataLength, 0);
156     }
157
158     void didReceiveCachedMetadata(WebURLLoader* loader, const char* data, int dataLength)
159     {
160         m_didReceiveCachedMetadata = true;
161         EXPECT_EQ(m_expectedLoader, loader);
162     }
163
164     void didFinishLoading(WebURLLoader* loader, double finishTime)
165     {
166         m_didFinishLoading = true;
167         EXPECT_EQ(m_expectedLoader, loader);
168     }
169
170     void didFail(WebURLLoader* loader, const WebURLError& error)
171     {
172         m_didFail = true;
173         EXPECT_EQ(m_expectedLoader, loader);
174         webkit_support::QuitMessageLoop();
175     }
176
177 protected:
178     WebString m_frameFilePath;
179     TestWebFrameClient m_webFrameClient;
180     WebView* m_webView;
181
182     WebURLLoader* m_expectedLoader;
183     WebURLResponse m_expectedResponse;
184     WebURLRequest m_expectedNewRequest;
185     WebURLResponse m_expectedRedirectResponse;
186     bool m_willSendRequest;
187     bool m_didSendData;
188     bool m_didReceiveResponse;
189     bool m_didDownloadData;
190     bool m_didReceiveData;
191     bool m_didReceiveCachedMetadata;
192     bool m_didFinishLoading;
193     bool m_didFail;
194 };
195
196 // Test a successful URL load.
197 TEST_F(AssociatedURLLoaderTest, Success)
198 {
199     GURL url = GURL("http://www.test.com/success.html");
200     WebURLRequest request;
201     request.initialize();
202     request.setURL(url);
203
204     m_expectedResponse = WebURLResponse();
205     m_expectedResponse.initialize();
206     m_expectedResponse.setMIMEType("text/html");
207     webkit_support::RegisterMockedURL(url, m_expectedResponse, m_frameFilePath);
208
209     m_expectedLoader = createAssociatedURLLoader();
210     EXPECT_TRUE(m_expectedLoader);
211     m_expectedLoader->loadAsynchronously(request, this);
212     serveRequests();
213     EXPECT_TRUE(m_didReceiveResponse);
214     EXPECT_TRUE(m_didReceiveData);
215     EXPECT_TRUE(m_didFinishLoading);
216 }
217
218 // Test that the same-origin restriction is the default.
219 TEST_F(AssociatedURLLoaderTest, SameOriginRestriction)
220 {
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();
225     request.setURL(url);
226
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);
235 }
236
237 // Test a successful cross-origin load.
238 TEST_F(AssociatedURLLoaderTest, CrossOriginSuccess)
239 {
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();
244     request.setURL(url);
245
246     m_expectedResponse = WebURLResponse();
247     m_expectedResponse.initialize();
248     m_expectedResponse.setMIMEType("text/html");
249     webkit_support::RegisterMockedURL(url, m_expectedResponse, m_frameFilePath);
250
251     WebURLLoaderOptions options;
252     options.crossOriginRequestPolicy = WebURLLoaderOptions::CrossOriginRequestPolicyAllow;
253     m_expectedLoader = createAssociatedURLLoader(options);
254     EXPECT_TRUE(m_expectedLoader);
255     m_expectedLoader->loadAsynchronously(request, this);
256     serveRequests();
257     EXPECT_TRUE(m_didReceiveResponse);
258     EXPECT_TRUE(m_didReceiveData);
259     EXPECT_TRUE(m_didFinishLoading);
260 }
261
262 }