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