OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 "third_party/WebKit/WebKit/chromium/public/WebFrame.h" | |
6 #include "third_party/WebKit/WebKit/chromium/public/WebFrameClient.h" | |
7 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" | |
8 #include "third_party/WebKit/WebKit/chromium/public/WebView.h" | |
9 | |
10 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | |
11 #include "webkit/plugins/ppapi/ppb_url_request_info_impl.h" | |
12 #include "webkit/plugins/ppapi/ppapi_unittest.h" | |
13 | |
14 using WebKit::WebCString; | |
15 using WebKit::WebFrame; | |
16 using WebKit::WebFrameClient; | |
17 using WebKit::WebString; | |
18 using WebKit::WebView; | |
19 using WebKit::WebURL; | |
20 using WebKit::WebURLRequest; | |
21 | |
22 namespace { | |
23 | |
24 bool IsExpected(const WebCString& web_string, const char* expected) { | |
25 const char* result = web_string.data(); | |
26 return strcmp(result, expected) == 0; | |
27 } | |
28 | |
29 bool IsExpected(const WebString& web_string, const char* expected) { | |
30 return IsExpected(web_string.utf8(), expected); | |
31 } | |
32 | |
33 bool IsNullOrEmpty(const WebString& web_string) { | |
34 return web_string.isNull() || web_string.isEmpty(); | |
35 } | |
36 | |
37 // The base class destructor is protected, so derive. | |
38 class TestWebFrameClient : public WebFrameClient { | |
39 }; | |
40 | |
41 } // namespace | |
42 | |
43 namespace webkit { | |
44 namespace ppapi { | |
45 | |
46 class URLRequestInfoTest : public PpapiUnittest { | |
47 public: | |
48 URLRequestInfoTest() : info_(new PPB_URLRequestInfo_Impl(module())) { | |
49 } | |
50 | |
51 static void SetUpTestCase() { | |
52 web_view_ = WebView::create(0, 0); | |
53 web_view_->initializeMainFrame(&web_frame_client_); | |
54 WebURL web_url(GURL("")); | |
55 WebURLRequest url_request; | |
56 url_request.initialize(); | |
57 url_request.setURL(web_url); | |
58 frame_ = web_view_->mainFrame(); | |
59 frame_->loadRequest(url_request); | |
60 } | |
61 | |
62 static void TearDownTestCase() { | |
63 web_view_->close(); | |
64 } | |
65 | |
66 bool GetDownloadToFile() { | |
67 WebURLRequest web_request = info_->ToWebURLRequest(frame_); | |
68 return web_request.downloadToFile(); | |
69 } | |
70 | |
71 WebCString GetURL() { | |
72 WebURLRequest web_request = info_->ToWebURLRequest(frame_); | |
73 return web_request.url().spec(); | |
74 } | |
75 | |
76 WebString GetMethod() { | |
77 WebURLRequest web_request = info_->ToWebURLRequest(frame_); | |
78 return web_request.httpMethod(); | |
79 } | |
80 | |
81 WebString GetHeaderValue(const char* field) { | |
82 WebURLRequest web_request = info_->ToWebURLRequest(frame_); | |
83 return web_request.httpHeaderField(WebString::fromUTF8(field)); | |
84 } | |
85 | |
86 scoped_refptr<PPB_URLRequestInfo_Impl> info_; | |
87 | |
88 static TestWebFrameClient web_frame_client_; | |
89 static WebView* web_view_; | |
90 static WebFrame* frame_; | |
91 }; | |
92 | |
93 TestWebFrameClient URLRequestInfoTest::web_frame_client_; | |
94 WebView* URLRequestInfoTest::web_view_; | |
95 WebFrame* URLRequestInfoTest::frame_; | |
96 | |
97 TEST_F(URLRequestInfoTest, GetInterface) { | |
98 const PPB_URLRequestInfo* interface = info_->GetInterface(); | |
99 ASSERT_TRUE(interface); | |
100 ASSERT_TRUE(interface->Create); | |
101 ASSERT_TRUE(interface->IsURLRequestInfo); | |
102 ASSERT_TRUE(interface->SetProperty); | |
103 ASSERT_TRUE(interface->AppendDataToBody); | |
104 ASSERT_TRUE(interface->AppendFileToBody); | |
105 ASSERT_TRUE(interface->Create); | |
106 ASSERT_TRUE(interface->Create); | |
107 } | |
108 | |
109 TEST_F(URLRequestInfoTest, AsURLRequestInfo) { | |
110 ASSERT_EQ(info_, info_->AsPPB_URLRequestInfo_Impl()); | |
111 } | |
112 | |
113 TEST_F(URLRequestInfoTest, StreamToFile) { | |
114 info_->SetStringProperty(PP_URLREQUESTPROPERTY_URL, "http://www.google.com"); | |
115 | |
116 ASSERT_FALSE(GetDownloadToFile()); | |
117 | |
118 ASSERT_TRUE(info_->SetBooleanProperty( | |
119 PP_URLREQUESTPROPERTY_STREAMTOFILE, true)); | |
120 ASSERT_TRUE(GetDownloadToFile()); | |
121 | |
122 ASSERT_TRUE(info_->SetBooleanProperty( | |
123 PP_URLREQUESTPROPERTY_STREAMTOFILE, false)); | |
124 ASSERT_FALSE(GetDownloadToFile()); | |
125 } | |
126 | |
127 TEST_F(URLRequestInfoTest, FollowRedirects) { | |
128 ASSERT_TRUE(info_->follow_redirects()); | |
129 | |
130 ASSERT_TRUE(info_->SetBooleanProperty( | |
131 PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS, false)); | |
132 ASSERT_FALSE(info_->follow_redirects()); | |
133 | |
134 ASSERT_TRUE(info_->SetBooleanProperty( | |
135 PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS, true)); | |
136 ASSERT_TRUE(info_->follow_redirects()); | |
137 } | |
138 | |
139 TEST_F(URLRequestInfoTest, RecordDownloadProgress) { | |
140 ASSERT_FALSE(info_->record_download_progress()); | |
141 | |
142 ASSERT_TRUE(info_->SetBooleanProperty( | |
143 PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS, true)); | |
144 ASSERT_TRUE(info_->record_download_progress()); | |
145 | |
146 ASSERT_TRUE(info_->SetBooleanProperty( | |
147 PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS, false)); | |
148 ASSERT_FALSE(info_->record_download_progress()); | |
149 } | |
150 | |
151 TEST_F(URLRequestInfoTest, RecordUploadProgress) { | |
152 ASSERT_FALSE(info_->record_upload_progress()); | |
153 | |
154 ASSERT_TRUE(info_->SetBooleanProperty( | |
155 PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS, true)); | |
156 ASSERT_TRUE(info_->record_upload_progress()); | |
157 | |
158 ASSERT_TRUE(info_->SetBooleanProperty( | |
159 PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS, false)); | |
160 ASSERT_FALSE(info_->record_upload_progress()); | |
161 } | |
162 | |
163 TEST_F(URLRequestInfoTest, SetURL) { | |
164 // Test default URL is "about:blank". | |
165 ASSERT_TRUE(IsExpected(GetURL(), "about:blank")); | |
166 | |
167 const char* url = "http://www.google.com/"; | |
168 ASSERT_TRUE(info_->SetStringProperty( | |
169 PP_URLREQUESTPROPERTY_URL, url)); | |
170 ASSERT_TRUE(IsExpected(GetURL(), url)); | |
171 } | |
172 | |
173 TEST_F(URLRequestInfoTest, SetMethod) { | |
174 // Test default method is "GET". | |
175 ASSERT_TRUE(IsExpected(GetMethod(), "GET")); | |
176 ASSERT_TRUE(info_->SetStringProperty( | |
177 PP_URLREQUESTPROPERTY_METHOD, "POST")); | |
178 ASSERT_TRUE(IsExpected(GetMethod(), "POST")); | |
179 } | |
180 | |
181 TEST_F(URLRequestInfoTest, SetValidHeaders) { | |
182 // Test default header field. | |
183 ASSERT_TRUE(IsExpected( | |
184 GetHeaderValue("foo"), "")); | |
185 // Test that we can set a header field. | |
186 ASSERT_TRUE(info_->SetStringProperty( | |
187 PP_URLREQUESTPROPERTY_HEADERS, "foo: bar")); | |
188 ASSERT_TRUE(IsExpected( | |
189 GetHeaderValue("foo"), "bar")); | |
190 // Test that we can set multiple header fields using \n delimiter. | |
191 ASSERT_TRUE(info_->SetStringProperty( | |
192 PP_URLREQUESTPROPERTY_HEADERS, "foo: bar\nbar: baz")); | |
193 ASSERT_TRUE(IsExpected( | |
194 GetHeaderValue("foo"), "bar")); | |
195 ASSERT_TRUE(IsExpected( | |
196 GetHeaderValue("bar"), "baz")); | |
197 } | |
198 | |
199 TEST_F(URLRequestInfoTest, SetInvalidHeaders) { | |
200 const char* const kForbiddenHeaderFields[] = { | |
201 "accept-charset", | |
202 "accept-encoding", | |
203 "connection", | |
204 "content-length", | |
205 "cookie", | |
206 "cookie2", | |
207 "content-transfer-encoding", | |
208 "date", | |
209 "expect", | |
210 "host", | |
211 "keep-alive", | |
212 "origin", | |
213 "referer", | |
214 "te", | |
215 "trailer", | |
216 "transfer-encoding", | |
217 "upgrade", | |
218 "user-agent", | |
219 "via", | |
220 | |
221 "proxy-foo", // Test for any header starting with proxy- or sec-. | |
222 "sec-foo", | |
223 }; | |
224 | |
225 // Test that no forbidden header fields can be set. | |
226 for (size_t i = 0; i < arraysize(kForbiddenHeaderFields); ++i) { | |
227 std::string headers(kForbiddenHeaderFields[i]); | |
228 headers.append(": foo"); | |
229 ASSERT_FALSE(info_->SetStringProperty( | |
230 PP_URLREQUESTPROPERTY_HEADERS, headers.c_str())); | |
231 ASSERT_TRUE(IsNullOrEmpty(GetHeaderValue(kForbiddenHeaderFields[i]))); | |
232 } | |
233 | |
234 // Test that forbidden header can't be set in various ways. | |
235 ASSERT_FALSE(info_->SetStringProperty( | |
236 PP_URLREQUESTPROPERTY_HEADERS, "cookie : foo")); | |
237 ASSERT_TRUE(IsNullOrEmpty(GetHeaderValue("cookie"))); | |
238 | |
239 // Test that forbidden header can't be set with an allowed one. | |
240 ASSERT_FALSE(info_->SetStringProperty( | |
241 PP_URLREQUESTPROPERTY_HEADERS, "foo: bar\ncookie: foo")); | |
242 ASSERT_TRUE(IsNullOrEmpty(GetHeaderValue("cookie"))); | |
243 } | |
244 | |
245 // TODO(bbudge) Unit tests for AppendDataToBody, AppendFileToBody. | |
246 | |
247 } // namespace ppapi | |
248 } // namespace webkit | |
249 | |
OLD | NEW |