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/common/user_agent.h"
7 #include "content/public/test/render_view_test.h"
8 #include "content/renderer/pepper/url_request_info_util.h"
9 #include "ppapi/proxy/connection.h"
10 #include "ppapi/proxy/url_request_info_resource.h"
11 #include "ppapi/shared_impl/proxy_lock.h"
12 #include "ppapi/shared_impl/test_globals.h"
13 #include "ppapi/shared_impl/url_request_info_data.h"
14 #include "ppapi/thunk/thunk.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/WebKit/public/platform/WebURLRequest.h"
17 #include "third_party/WebKit/public/web/WebFrameClient.h"
18 #include "third_party/WebKit/public/web/WebLocalFrame.h"
19 #include "third_party/WebKit/public/web/WebView.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 blink::WebCString
;
27 using blink::WebFrameClient
;
28 using blink::WebString
;
31 using blink::WebURLRequest
;
35 bool IsExpected(const WebCString
& web_string
, const char* expected
) {
36 const char* result
= web_string
.data();
37 return strcmp(result
, expected
) == 0;
40 bool IsExpected(const WebString
& web_string
, const char* expected
) {
41 return IsExpected(web_string
.utf8(), expected
);
44 // The base class destructor is protected, so derive.
45 class TestWebFrameClient
: public WebFrameClient
{};
49 using ppapi::proxy::URLRequestInfoResource
;
50 using ppapi::URLRequestInfoData
;
54 class URLRequestInfoTest
: public RenderViewTest
{
56 // Note: using -1 as the instance value allows code in
57 // url_request_info_util.cc to detect that this is a test instance.
58 URLRequestInfoTest() : pp_instance_(-1) {}
60 void SetUp() override
{
61 RenderViewTest::SetUp();
62 test_globals_
.GetResourceTracker()->DidCreateInstance(pp_instance_
);
64 // This resource doesn't do IPC, so a null connection is fine.
65 info_
= new URLRequestInfoResource(
66 ppapi::proxy::Connection(), pp_instance_
, URLRequestInfoData());
69 void TearDown() override
{
70 test_globals_
.GetResourceTracker()->DidDeleteInstance(pp_instance_
);
71 RenderViewTest::TearDown();
74 bool GetDownloadToFile() {
75 WebURLRequest web_request
;
76 URLRequestInfoData data
= info_
->GetData();
77 if (!CreateWebURLRequest(pp_instance_
, &data
, GetMainFrame(), &web_request
))
79 return web_request
.downloadToFile();
83 WebURLRequest web_request
;
84 URLRequestInfoData data
= info_
->GetData();
85 if (!CreateWebURLRequest(pp_instance_
, &data
, GetMainFrame(), &web_request
))
87 return web_request
.url().spec();
90 WebString
GetMethod() {
91 WebURLRequest web_request
;
92 URLRequestInfoData data
= info_
->GetData();
93 if (!CreateWebURLRequest(pp_instance_
, &data
, GetMainFrame(), &web_request
))
95 return web_request
.httpMethod();
98 WebString
GetHeaderValue(const char* field
) {
99 WebURLRequest web_request
;
100 URLRequestInfoData data
= info_
->GetData();
101 if (!CreateWebURLRequest(pp_instance_
, &data
, GetMainFrame(), &web_request
))
103 return web_request
.httpHeaderField(WebString::fromUTF8(field
));
106 bool SetBooleanProperty(PP_URLRequestProperty prop
, bool b
) {
107 return info_
->SetBooleanProperty(prop
, b
);
109 bool SetStringProperty(PP_URLRequestProperty prop
, const std::string
& s
) {
110 return info_
->SetStringProperty(prop
, s
);
113 PP_Instance pp_instance_
;
115 // Disables locking for the duration of the test.
116 ppapi::ProxyLock::LockingDisablerForTest disable_locking_
;
118 // Needs to be alive for resource tracking to work.
119 ppapi::TestGlobals test_globals_
;
121 scoped_refptr
<URLRequestInfoResource
> info_
;
124 TEST_F(URLRequestInfoTest
, GetInterface
) {
125 const PPB_URLRequestInfo
* request_info
=
126 ppapi::thunk::GetPPB_URLRequestInfo_1_0_Thunk();
127 EXPECT_TRUE(request_info
);
128 EXPECT_TRUE(request_info
->Create
);
129 EXPECT_TRUE(request_info
->IsURLRequestInfo
);
130 EXPECT_TRUE(request_info
->SetProperty
);
131 EXPECT_TRUE(request_info
->AppendDataToBody
);
132 EXPECT_TRUE(request_info
->AppendFileToBody
);
135 TEST_F(URLRequestInfoTest
, AsURLRequestInfo
) {
136 EXPECT_EQ(info_
.get(), info_
->AsPPB_URLRequestInfo_API());
139 TEST_F(URLRequestInfoTest
, StreamToFile
) {
140 SetStringProperty(PP_URLREQUESTPROPERTY_URL
, "http://www.google.com");
142 EXPECT_FALSE(GetDownloadToFile());
144 EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_STREAMTOFILE
, true));
145 EXPECT_TRUE(GetDownloadToFile());
147 EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_STREAMTOFILE
, false));
148 EXPECT_FALSE(GetDownloadToFile());
151 TEST_F(URLRequestInfoTest
, FollowRedirects
) {
152 EXPECT_TRUE(info_
->GetData().follow_redirects
);
154 EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS
, false));
155 EXPECT_FALSE(info_
->GetData().follow_redirects
);
157 EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS
, true));
158 EXPECT_TRUE(info_
->GetData().follow_redirects
);
161 TEST_F(URLRequestInfoTest
, RecordDownloadProgress
) {
162 EXPECT_FALSE(info_
->GetData().record_download_progress
);
165 SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS
, true));
166 EXPECT_TRUE(info_
->GetData().record_download_progress
);
169 SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS
, false));
170 EXPECT_FALSE(info_
->GetData().record_download_progress
);
173 TEST_F(URLRequestInfoTest
, RecordUploadProgress
) {
174 EXPECT_FALSE(info_
->GetData().record_upload_progress
);
177 SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS
, true));
178 EXPECT_TRUE(info_
->GetData().record_upload_progress
);
181 SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS
, false));
182 EXPECT_FALSE(info_
->GetData().record_upload_progress
);
185 TEST_F(URLRequestInfoTest
, AllowCrossOriginRequests
) {
186 EXPECT_FALSE(info_
->GetData().allow_cross_origin_requests
);
189 SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS
, true));
190 EXPECT_TRUE(info_
->GetData().allow_cross_origin_requests
);
192 EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS
,
194 EXPECT_FALSE(info_
->GetData().allow_cross_origin_requests
);
197 TEST_F(URLRequestInfoTest
, AllowCredentials
) {
198 EXPECT_FALSE(info_
->GetData().allow_credentials
);
200 EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS
, true));
201 EXPECT_TRUE(info_
->GetData().allow_credentials
);
204 SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS
, false));
205 EXPECT_FALSE(info_
->GetData().allow_credentials
);
208 TEST_F(URLRequestInfoTest
, SetURL
) {
209 const char* url
= "http://www.google.com/";
210 EXPECT_TRUE(SetStringProperty(PP_URLREQUESTPROPERTY_URL
, url
));
211 EXPECT_TRUE(IsExpected(GetURL(), url
));
214 TEST_F(URLRequestInfoTest
, JavascriptURL
) {
215 const char* url
= "javascript:foo = bar";
216 EXPECT_FALSE(URLRequestRequiresUniversalAccess(info_
->GetData()));
217 SetStringProperty(PP_URLREQUESTPROPERTY_URL
, url
);
218 EXPECT_TRUE(URLRequestRequiresUniversalAccess(info_
->GetData()));
221 TEST_F(URLRequestInfoTest
, SetMethod
) {
222 // Test default method is "GET".
223 EXPECT_TRUE(IsExpected(GetMethod(), "GET"));
224 EXPECT_TRUE(SetStringProperty(PP_URLREQUESTPROPERTY_METHOD
, "POST"));
225 EXPECT_TRUE(IsExpected(GetMethod(), "POST"));
228 TEST_F(URLRequestInfoTest
, SetHeaders
) {
229 // Test default header field.
230 EXPECT_TRUE(IsExpected(GetHeaderValue("foo"), ""));
231 // Test that we can set a header field.
232 EXPECT_TRUE(SetStringProperty(PP_URLREQUESTPROPERTY_HEADERS
, "foo: bar"));
233 EXPECT_TRUE(IsExpected(GetHeaderValue("foo"), "bar"));
234 // Test that we can set multiple header fields using \n delimiter.
236 SetStringProperty(PP_URLREQUESTPROPERTY_HEADERS
, "foo: bar\nbar: baz"));
237 EXPECT_TRUE(IsExpected(GetHeaderValue("foo"), "bar"));
238 EXPECT_TRUE(IsExpected(GetHeaderValue("bar"), "baz"));
241 // TODO(bbudge) Unit tests for AppendDataToBody, AppendFileToBody.
243 } // namespace content