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 virtual void SetUp() override
{
61 RenderViewTest::SetUp();
62 ppapi::ProxyLock::DisableLockingOnThreadForTest();
64 test_globals_
.GetResourceTracker()->DidCreateInstance(pp_instance_
);
66 // This resource doesn't do IPC, so a null connection is fine.
67 info_
= new URLRequestInfoResource(
68 ppapi::proxy::Connection(), pp_instance_
, URLRequestInfoData());
71 virtual void TearDown() override
{
72 test_globals_
.GetResourceTracker()->DidDeleteInstance(pp_instance_
);
73 RenderViewTest::TearDown();
76 bool GetDownloadToFile() {
77 WebURLRequest web_request
;
78 URLRequestInfoData data
= info_
->GetData();
79 if (!CreateWebURLRequest(pp_instance_
, &data
, GetMainFrame(), &web_request
))
81 return web_request
.downloadToFile();
85 WebURLRequest web_request
;
86 URLRequestInfoData data
= info_
->GetData();
87 if (!CreateWebURLRequest(pp_instance_
, &data
, GetMainFrame(), &web_request
))
89 return web_request
.url().spec();
92 WebString
GetMethod() {
93 WebURLRequest web_request
;
94 URLRequestInfoData data
= info_
->GetData();
95 if (!CreateWebURLRequest(pp_instance_
, &data
, GetMainFrame(), &web_request
))
97 return web_request
.httpMethod();
100 WebString
GetHeaderValue(const char* field
) {
101 WebURLRequest web_request
;
102 URLRequestInfoData data
= info_
->GetData();
103 if (!CreateWebURLRequest(pp_instance_
, &data
, GetMainFrame(), &web_request
))
105 return web_request
.httpHeaderField(WebString::fromUTF8(field
));
108 bool SetBooleanProperty(PP_URLRequestProperty prop
, bool b
) {
109 return info_
->SetBooleanProperty(prop
, b
);
111 bool SetStringProperty(PP_URLRequestProperty prop
, const std::string
& s
) {
112 return info_
->SetStringProperty(prop
, s
);
115 PP_Instance pp_instance_
;
117 // Needs to be alive for resource tracking to work.
118 ppapi::TestGlobals test_globals_
;
120 scoped_refptr
<URLRequestInfoResource
> info_
;
123 TEST_F(URLRequestInfoTest
, GetInterface
) {
124 const PPB_URLRequestInfo
* request_info
=
125 ppapi::thunk::GetPPB_URLRequestInfo_1_0_Thunk();
126 EXPECT_TRUE(request_info
);
127 EXPECT_TRUE(request_info
->Create
);
128 EXPECT_TRUE(request_info
->IsURLRequestInfo
);
129 EXPECT_TRUE(request_info
->SetProperty
);
130 EXPECT_TRUE(request_info
->AppendDataToBody
);
131 EXPECT_TRUE(request_info
->AppendFileToBody
);
134 TEST_F(URLRequestInfoTest
, AsURLRequestInfo
) {
135 EXPECT_EQ(info_
.get(), info_
->AsPPB_URLRequestInfo_API());
138 TEST_F(URLRequestInfoTest
, StreamToFile
) {
139 SetStringProperty(PP_URLREQUESTPROPERTY_URL
, "http://www.google.com");
141 EXPECT_FALSE(GetDownloadToFile());
143 EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_STREAMTOFILE
, true));
144 EXPECT_TRUE(GetDownloadToFile());
146 EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_STREAMTOFILE
, false));
147 EXPECT_FALSE(GetDownloadToFile());
150 TEST_F(URLRequestInfoTest
, FollowRedirects
) {
151 EXPECT_TRUE(info_
->GetData().follow_redirects
);
153 EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS
, false));
154 EXPECT_FALSE(info_
->GetData().follow_redirects
);
156 EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS
, true));
157 EXPECT_TRUE(info_
->GetData().follow_redirects
);
160 TEST_F(URLRequestInfoTest
, RecordDownloadProgress
) {
161 EXPECT_FALSE(info_
->GetData().record_download_progress
);
164 SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS
, true));
165 EXPECT_TRUE(info_
->GetData().record_download_progress
);
168 SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS
, false));
169 EXPECT_FALSE(info_
->GetData().record_download_progress
);
172 TEST_F(URLRequestInfoTest
, RecordUploadProgress
) {
173 EXPECT_FALSE(info_
->GetData().record_upload_progress
);
176 SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS
, true));
177 EXPECT_TRUE(info_
->GetData().record_upload_progress
);
180 SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS
, false));
181 EXPECT_FALSE(info_
->GetData().record_upload_progress
);
184 TEST_F(URLRequestInfoTest
, AllowCrossOriginRequests
) {
185 EXPECT_FALSE(info_
->GetData().allow_cross_origin_requests
);
188 SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS
, true));
189 EXPECT_TRUE(info_
->GetData().allow_cross_origin_requests
);
191 EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS
,
193 EXPECT_FALSE(info_
->GetData().allow_cross_origin_requests
);
196 TEST_F(URLRequestInfoTest
, AllowCredentials
) {
197 EXPECT_FALSE(info_
->GetData().allow_credentials
);
199 EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS
, true));
200 EXPECT_TRUE(info_
->GetData().allow_credentials
);
203 SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS
, false));
204 EXPECT_FALSE(info_
->GetData().allow_credentials
);
207 TEST_F(URLRequestInfoTest
, SetURL
) {
208 const char* url
= "http://www.google.com/";
209 EXPECT_TRUE(SetStringProperty(PP_URLREQUESTPROPERTY_URL
, url
));
210 EXPECT_TRUE(IsExpected(GetURL(), url
));
213 TEST_F(URLRequestInfoTest
, JavascriptURL
) {
214 const char* url
= "javascript:foo = bar";
215 EXPECT_FALSE(URLRequestRequiresUniversalAccess(info_
->GetData()));
216 SetStringProperty(PP_URLREQUESTPROPERTY_URL
, url
);
217 EXPECT_TRUE(URLRequestRequiresUniversalAccess(info_
->GetData()));
220 TEST_F(URLRequestInfoTest
, SetMethod
) {
221 // Test default method is "GET".
222 EXPECT_TRUE(IsExpected(GetMethod(), "GET"));
223 EXPECT_TRUE(SetStringProperty(PP_URLREQUESTPROPERTY_METHOD
, "POST"));
224 EXPECT_TRUE(IsExpected(GetMethod(), "POST"));
227 TEST_F(URLRequestInfoTest
, SetHeaders
) {
228 // Test default header field.
229 EXPECT_TRUE(IsExpected(GetHeaderValue("foo"), ""));
230 // Test that we can set a header field.
231 EXPECT_TRUE(SetStringProperty(PP_URLREQUESTPROPERTY_HEADERS
, "foo: bar"));
232 EXPECT_TRUE(IsExpected(GetHeaderValue("foo"), "bar"));
233 // Test that we can set multiple header fields using \n delimiter.
235 SetStringProperty(PP_URLREQUESTPROPERTY_HEADERS
, "foo: bar\nbar: baz"));
236 EXPECT_TRUE(IsExpected(GetHeaderValue("foo"), "bar"));
237 EXPECT_TRUE(IsExpected(GetHeaderValue("bar"), "baz"));
240 // TODO(bbudge) Unit tests for AppendDataToBody, AppendFileToBody.
242 } // namespace content