1 // Copyright (c) 2012 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.
5 #include "base/compiler_specific.h"
6 #include "content/public/test/render_view_test.h"
7 #include "ppapi/proxy/connection.h"
8 #include "ppapi/proxy/url_request_info_resource.h"
9 #include "ppapi/shared_impl/test_globals.h"
10 #include "ppapi/shared_impl/url_request_info_data.h"
11 #include "ppapi/thunk/thunk.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLRequest.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrameClient.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
17 #include "webkit/plugins/ppapi/url_request_info_util.h"
18 #include "webkit/user_agent/user_agent.h"
19 #include "webkit/user_agent/user_agent_util.h"
21 // This test is a end-to-end test from the resource to the WebKit request
22 // object. The actual resource implementation is so simple, it makes sense to
23 // test it by making sure the conversion routines actually work at the same
26 using WebKit::WebCString
;
27 using WebKit::WebFrame
;
28 using WebKit::WebFrameClient
;
29 using WebKit::WebString
;
30 using WebKit::WebView
;
32 using WebKit::WebURLRequest
;
36 bool IsExpected(const WebCString
& web_string
, const char* expected
) {
37 const char* result
= web_string
.data();
38 return strcmp(result
, expected
) == 0;
41 bool IsExpected(const WebString
& web_string
, const char* expected
) {
42 return IsExpected(web_string
.utf8(), expected
);
45 // The base class destructor is protected, so derive.
46 class TestWebFrameClient
: public WebFrameClient
{
51 using ppapi::proxy::URLRequestInfoResource
;
52 using ppapi::URLRequestInfoData
;
54 // TODO(brettw) move to content namespace when url_request_info_util.h is moved
55 // to this directory. This file used to be in webkit/plugins/ppapi and had to
56 // be moved in advance of the rest of the files to make things compile.
60 class URLRequestInfoTest
: public content::RenderViewTest
{
62 URLRequestInfoTest() : pp_instance_(1234) {
65 virtual void SetUp() OVERRIDE
{
66 RenderViewTest::SetUp();
68 test_globals_
.GetResourceTracker()->DidCreateInstance(pp_instance_
);
70 // This resource doesn't do IPC, so a null connection is fine.
71 info_
= new URLRequestInfoResource(::ppapi::proxy::Connection(),
73 URLRequestInfoData());
76 virtual void TearDown() OVERRIDE
{
77 test_globals_
.GetResourceTracker()->DidDeleteInstance(pp_instance_
);
78 RenderViewTest::TearDown();
81 bool GetDownloadToFile() {
82 WebURLRequest web_request
;
83 URLRequestInfoData data
= info_
->GetData();
84 if (!CreateWebURLRequest(&data
, GetMainFrame(), &web_request
))
86 return web_request
.downloadToFile();
90 WebURLRequest web_request
;
91 URLRequestInfoData data
= info_
->GetData();
92 if (!CreateWebURLRequest(&data
, GetMainFrame(), &web_request
))
94 return web_request
.url().spec();
97 WebString
GetMethod() {
98 WebURLRequest web_request
;
99 URLRequestInfoData data
= info_
->GetData();
100 if (!CreateWebURLRequest(&data
, GetMainFrame(), &web_request
))
102 return web_request
.httpMethod();
105 WebString
GetHeaderValue(const char* field
) {
106 WebURLRequest web_request
;
107 URLRequestInfoData data
= info_
->GetData();
108 if (!CreateWebURLRequest(&data
, GetMainFrame(), &web_request
))
110 return web_request
.httpHeaderField(WebString::fromUTF8(field
));
113 bool SetBooleanProperty(PP_URLRequestProperty prop
, bool b
) {
114 return info_
->SetBooleanProperty(prop
, b
);
116 bool SetStringProperty(PP_URLRequestProperty prop
, const std::string
& s
) {
117 return info_
->SetStringProperty(prop
, s
);
120 PP_Instance pp_instance_
;
122 // Needs to be alive for resource tracking to work.
123 ::ppapi::TestGlobals test_globals_
;
125 scoped_refptr
<URLRequestInfoResource
> info_
;
128 TEST_F(URLRequestInfoTest
, GetInterface
) {
129 const PPB_URLRequestInfo
* request_info
=
130 ::ppapi::thunk::GetPPB_URLRequestInfo_1_0_Thunk();
131 EXPECT_TRUE(request_info
);
132 EXPECT_TRUE(request_info
->Create
);
133 EXPECT_TRUE(request_info
->IsURLRequestInfo
);
134 EXPECT_TRUE(request_info
->SetProperty
);
135 EXPECT_TRUE(request_info
->AppendDataToBody
);
136 EXPECT_TRUE(request_info
->AppendFileToBody
);
139 TEST_F(URLRequestInfoTest
, AsURLRequestInfo
) {
140 EXPECT_EQ(info_
, info_
->AsPPB_URLRequestInfo_API());
143 TEST_F(URLRequestInfoTest
, StreamToFile
) {
144 SetStringProperty(PP_URLREQUESTPROPERTY_URL
, "http://www.google.com");
146 EXPECT_FALSE(GetDownloadToFile());
148 EXPECT_TRUE(SetBooleanProperty(
149 PP_URLREQUESTPROPERTY_STREAMTOFILE
, true));
150 EXPECT_TRUE(GetDownloadToFile());
152 EXPECT_TRUE(SetBooleanProperty(
153 PP_URLREQUESTPROPERTY_STREAMTOFILE
, false));
154 EXPECT_FALSE(GetDownloadToFile());
157 TEST_F(URLRequestInfoTest
, FollowRedirects
) {
158 EXPECT_TRUE(info_
->GetData().follow_redirects
);
160 EXPECT_TRUE(SetBooleanProperty(
161 PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS
, false));
162 EXPECT_FALSE(info_
->GetData().follow_redirects
);
164 EXPECT_TRUE(SetBooleanProperty(
165 PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS
, true));
166 EXPECT_TRUE(info_
->GetData().follow_redirects
);
169 TEST_F(URLRequestInfoTest
, RecordDownloadProgress
) {
170 EXPECT_FALSE(info_
->GetData().record_download_progress
);
172 EXPECT_TRUE(SetBooleanProperty(
173 PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS
, true));
174 EXPECT_TRUE(info_
->GetData().record_download_progress
);
176 EXPECT_TRUE(SetBooleanProperty(
177 PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS
, false));
178 EXPECT_FALSE(info_
->GetData().record_download_progress
);
181 TEST_F(URLRequestInfoTest
, RecordUploadProgress
) {
182 EXPECT_FALSE(info_
->GetData().record_upload_progress
);
184 EXPECT_TRUE(SetBooleanProperty(
185 PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS
, true));
186 EXPECT_TRUE(info_
->GetData().record_upload_progress
);
188 EXPECT_TRUE(SetBooleanProperty(
189 PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS
, false));
190 EXPECT_FALSE(info_
->GetData().record_upload_progress
);
193 TEST_F(URLRequestInfoTest
, AllowCrossOriginRequests
) {
194 EXPECT_FALSE(info_
->GetData().allow_cross_origin_requests
);
196 EXPECT_TRUE(SetBooleanProperty(
197 PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS
, true));
198 EXPECT_TRUE(info_
->GetData().allow_cross_origin_requests
);
200 EXPECT_TRUE(SetBooleanProperty(
201 PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS
, false));
202 EXPECT_FALSE(info_
->GetData().allow_cross_origin_requests
);
205 TEST_F(URLRequestInfoTest
, AllowCredentials
) {
206 EXPECT_FALSE(info_
->GetData().allow_credentials
);
208 EXPECT_TRUE(SetBooleanProperty(
209 PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS
, true));
210 EXPECT_TRUE(info_
->GetData().allow_credentials
);
212 EXPECT_TRUE(SetBooleanProperty(
213 PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS
, false));
214 EXPECT_FALSE(info_
->GetData().allow_credentials
);
217 TEST_F(URLRequestInfoTest
, SetURL
) {
218 const char* url
= "http://www.google.com/";
219 EXPECT_TRUE(SetStringProperty(
220 PP_URLREQUESTPROPERTY_URL
, url
));
221 EXPECT_TRUE(IsExpected(GetURL(), url
));
224 TEST_F(URLRequestInfoTest
, JavascriptURL
) {
225 const char* url
= "javascript:foo = bar";
226 EXPECT_FALSE(URLRequestRequiresUniversalAccess(info_
->GetData()));
227 SetStringProperty(PP_URLREQUESTPROPERTY_URL
, url
);
228 EXPECT_TRUE(URLRequestRequiresUniversalAccess(info_
->GetData()));
231 TEST_F(URLRequestInfoTest
, SetMethod
) {
232 // Test default method is "GET".
233 EXPECT_TRUE(IsExpected(GetMethod(), "GET"));
234 EXPECT_TRUE(SetStringProperty(
235 PP_URLREQUESTPROPERTY_METHOD
, "POST"));
236 EXPECT_TRUE(IsExpected(GetMethod(), "POST"));
239 TEST_F(URLRequestInfoTest
, SetHeaders
) {
240 // Test default header field.
241 EXPECT_TRUE(IsExpected(
242 GetHeaderValue("foo"), ""));
243 // Test that we can set a header field.
244 EXPECT_TRUE(SetStringProperty(
245 PP_URLREQUESTPROPERTY_HEADERS
, "foo: bar"));
246 EXPECT_TRUE(IsExpected(
247 GetHeaderValue("foo"), "bar"));
248 // Test that we can set multiple header fields using \n delimiter.
249 EXPECT_TRUE(SetStringProperty(
250 PP_URLREQUESTPROPERTY_HEADERS
, "foo: bar\nbar: baz"));
251 EXPECT_TRUE(IsExpected(
252 GetHeaderValue("foo"), "bar"));
253 EXPECT_TRUE(IsExpected(
254 GetHeaderValue("bar"), "baz"));
257 // TODO(bbudge) Unit tests for AppendDataToBody, AppendFileToBody.
260 } // namespace webkit