• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
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"
20 
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
24 // time.
25 
26 using blink::WebCString;
27 using blink::WebFrameClient;
28 using blink::WebString;
29 using blink::WebView;
30 using blink::WebURL;
31 using blink::WebURLRequest;
32 
33 namespace {
34 
IsExpected(const WebCString & web_string,const char * expected)35 bool IsExpected(const WebCString& web_string, const char* expected) {
36   const char* result = web_string.data();
37   return strcmp(result, expected) == 0;
38 }
39 
IsExpected(const WebString & web_string,const char * expected)40 bool IsExpected(const WebString& web_string, const char* expected) {
41   return IsExpected(web_string.utf8(), expected);
42 }
43 
44 // The base class destructor is protected, so derive.
45 class TestWebFrameClient : public WebFrameClient {};
46 
47 }  // namespace
48 
49 using ppapi::proxy::URLRequestInfoResource;
50 using ppapi::URLRequestInfoData;
51 
52 namespace content {
53 
54 class URLRequestInfoTest : public RenderViewTest {
55  public:
URLRequestInfoTest()56   URLRequestInfoTest() : pp_instance_(1234) {}
57 
SetUp()58   virtual void SetUp() OVERRIDE {
59     RenderViewTest::SetUp();
60     ppapi::ProxyLock::DisableLockingOnThreadForTest();
61 
62     test_globals_.GetResourceTracker()->DidCreateInstance(pp_instance_);
63 
64     // This resource doesn't do IPC, so a null connection is fine.
65     info_ = new URLRequestInfoResource(
66         ppapi::proxy::Connection(), pp_instance_, URLRequestInfoData());
67   }
68 
TearDown()69   virtual void TearDown() OVERRIDE {
70     test_globals_.GetResourceTracker()->DidDeleteInstance(pp_instance_);
71     RenderViewTest::TearDown();
72   }
73 
GetDownloadToFile()74   bool GetDownloadToFile() {
75     WebURLRequest web_request;
76     URLRequestInfoData data = info_->GetData();
77     if (!CreateWebURLRequest(pp_instance_, &data, GetMainFrame(), &web_request))
78       return false;
79     return web_request.downloadToFile();
80   }
81 
GetURL()82   WebCString GetURL() {
83     WebURLRequest web_request;
84     URLRequestInfoData data = info_->GetData();
85     if (!CreateWebURLRequest(pp_instance_, &data, GetMainFrame(), &web_request))
86       return WebCString();
87     return web_request.url().spec();
88   }
89 
GetMethod()90   WebString GetMethod() {
91     WebURLRequest web_request;
92     URLRequestInfoData data = info_->GetData();
93     if (!CreateWebURLRequest(pp_instance_, &data, GetMainFrame(), &web_request))
94       return WebString();
95     return web_request.httpMethod();
96   }
97 
GetHeaderValue(const char * field)98   WebString GetHeaderValue(const char* field) {
99     WebURLRequest web_request;
100     URLRequestInfoData data = info_->GetData();
101     if (!CreateWebURLRequest(pp_instance_, &data, GetMainFrame(), &web_request))
102       return WebString();
103     return web_request.httpHeaderField(WebString::fromUTF8(field));
104   }
105 
SetBooleanProperty(PP_URLRequestProperty prop,bool b)106   bool SetBooleanProperty(PP_URLRequestProperty prop, bool b) {
107     return info_->SetBooleanProperty(prop, b);
108   }
SetStringProperty(PP_URLRequestProperty prop,const std::string & s)109   bool SetStringProperty(PP_URLRequestProperty prop, const std::string& s) {
110     return info_->SetStringProperty(prop, s);
111   }
112 
113   PP_Instance pp_instance_;
114 
115   // Needs to be alive for resource tracking to work.
116   ppapi::TestGlobals test_globals_;
117 
118   scoped_refptr<URLRequestInfoResource> info_;
119 };
120 
TEST_F(URLRequestInfoTest,GetInterface)121 TEST_F(URLRequestInfoTest, GetInterface) {
122   const PPB_URLRequestInfo* request_info =
123       ppapi::thunk::GetPPB_URLRequestInfo_1_0_Thunk();
124   EXPECT_TRUE(request_info);
125   EXPECT_TRUE(request_info->Create);
126   EXPECT_TRUE(request_info->IsURLRequestInfo);
127   EXPECT_TRUE(request_info->SetProperty);
128   EXPECT_TRUE(request_info->AppendDataToBody);
129   EXPECT_TRUE(request_info->AppendFileToBody);
130 }
131 
TEST_F(URLRequestInfoTest,AsURLRequestInfo)132 TEST_F(URLRequestInfoTest, AsURLRequestInfo) {
133   EXPECT_EQ(info_, info_->AsPPB_URLRequestInfo_API());
134 }
135 
TEST_F(URLRequestInfoTest,StreamToFile)136 TEST_F(URLRequestInfoTest, StreamToFile) {
137   SetStringProperty(PP_URLREQUESTPROPERTY_URL, "http://www.google.com");
138 
139   EXPECT_FALSE(GetDownloadToFile());
140 
141   EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_STREAMTOFILE, true));
142   EXPECT_TRUE(GetDownloadToFile());
143 
144   EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_STREAMTOFILE, false));
145   EXPECT_FALSE(GetDownloadToFile());
146 }
147 
TEST_F(URLRequestInfoTest,FollowRedirects)148 TEST_F(URLRequestInfoTest, FollowRedirects) {
149   EXPECT_TRUE(info_->GetData().follow_redirects);
150 
151   EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS, false));
152   EXPECT_FALSE(info_->GetData().follow_redirects);
153 
154   EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS, true));
155   EXPECT_TRUE(info_->GetData().follow_redirects);
156 }
157 
TEST_F(URLRequestInfoTest,RecordDownloadProgress)158 TEST_F(URLRequestInfoTest, RecordDownloadProgress) {
159   EXPECT_FALSE(info_->GetData().record_download_progress);
160 
161   EXPECT_TRUE(
162       SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS, true));
163   EXPECT_TRUE(info_->GetData().record_download_progress);
164 
165   EXPECT_TRUE(
166       SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS, false));
167   EXPECT_FALSE(info_->GetData().record_download_progress);
168 }
169 
TEST_F(URLRequestInfoTest,RecordUploadProgress)170 TEST_F(URLRequestInfoTest, RecordUploadProgress) {
171   EXPECT_FALSE(info_->GetData().record_upload_progress);
172 
173   EXPECT_TRUE(
174       SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS, true));
175   EXPECT_TRUE(info_->GetData().record_upload_progress);
176 
177   EXPECT_TRUE(
178       SetBooleanProperty(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS, false));
179   EXPECT_FALSE(info_->GetData().record_upload_progress);
180 }
181 
TEST_F(URLRequestInfoTest,AllowCrossOriginRequests)182 TEST_F(URLRequestInfoTest, AllowCrossOriginRequests) {
183   EXPECT_FALSE(info_->GetData().allow_cross_origin_requests);
184 
185   EXPECT_TRUE(
186       SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS, true));
187   EXPECT_TRUE(info_->GetData().allow_cross_origin_requests);
188 
189   EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS,
190                                  false));
191   EXPECT_FALSE(info_->GetData().allow_cross_origin_requests);
192 }
193 
TEST_F(URLRequestInfoTest,AllowCredentials)194 TEST_F(URLRequestInfoTest, AllowCredentials) {
195   EXPECT_FALSE(info_->GetData().allow_credentials);
196 
197   EXPECT_TRUE(SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS, true));
198   EXPECT_TRUE(info_->GetData().allow_credentials);
199 
200   EXPECT_TRUE(
201       SetBooleanProperty(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS, false));
202   EXPECT_FALSE(info_->GetData().allow_credentials);
203 }
204 
TEST_F(URLRequestInfoTest,SetURL)205 TEST_F(URLRequestInfoTest, SetURL) {
206   const char* url = "http://www.google.com/";
207   EXPECT_TRUE(SetStringProperty(PP_URLREQUESTPROPERTY_URL, url));
208   EXPECT_TRUE(IsExpected(GetURL(), url));
209 }
210 
TEST_F(URLRequestInfoTest,JavascriptURL)211 TEST_F(URLRequestInfoTest, JavascriptURL) {
212   const char* url = "javascript:foo = bar";
213   EXPECT_FALSE(URLRequestRequiresUniversalAccess(info_->GetData()));
214   SetStringProperty(PP_URLREQUESTPROPERTY_URL, url);
215   EXPECT_TRUE(URLRequestRequiresUniversalAccess(info_->GetData()));
216 }
217 
TEST_F(URLRequestInfoTest,SetMethod)218 TEST_F(URLRequestInfoTest, SetMethod) {
219   // Test default method is "GET".
220   EXPECT_TRUE(IsExpected(GetMethod(), "GET"));
221   EXPECT_TRUE(SetStringProperty(PP_URLREQUESTPROPERTY_METHOD, "POST"));
222   EXPECT_TRUE(IsExpected(GetMethod(), "POST"));
223 }
224 
TEST_F(URLRequestInfoTest,SetHeaders)225 TEST_F(URLRequestInfoTest, SetHeaders) {
226   // Test default header field.
227   EXPECT_TRUE(IsExpected(GetHeaderValue("foo"), ""));
228   // Test that we can set a header field.
229   EXPECT_TRUE(SetStringProperty(PP_URLREQUESTPROPERTY_HEADERS, "foo: bar"));
230   EXPECT_TRUE(IsExpected(GetHeaderValue("foo"), "bar"));
231   // Test that we can set multiple header fields using \n delimiter.
232   EXPECT_TRUE(
233       SetStringProperty(PP_URLREQUESTPROPERTY_HEADERS, "foo: bar\nbar: baz"));
234   EXPECT_TRUE(IsExpected(GetHeaderValue("foo"), "bar"));
235   EXPECT_TRUE(IsExpected(GetHeaderValue("bar"), "baz"));
236 }
237 
238 // TODO(bbudge) Unit tests for AppendDataToBody, AppendFileToBody.
239 
240 }  // namespace content
241