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