summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/url_request_info_unittest.cc
blob: cc56abc36a39f7d3248ade3245a9c653336c2f29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrameClient.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"

#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
#include "webkit/plugins/ppapi/ppb_url_request_info_impl.h"
#include "webkit/plugins/ppapi/ppapi_unittest.h"

using WebKit::WebCString;
using WebKit::WebFrame;
using WebKit::WebFrameClient;
using WebKit::WebString;
using WebKit::WebView;
using WebKit::WebURL;
using WebKit::WebURLRequest;

namespace {

bool IsExpected(const WebCString& web_string, const char* expected) {
  const char* result = web_string.data();
  return strcmp(result, expected) == 0;
}

bool IsExpected(const WebString& web_string, const char* expected) {
  return IsExpected(web_string.utf8(), expected);
}

bool IsNullOrEmpty(const WebString& web_string) {
  return web_string.isNull() || web_string.isEmpty();
}

// The base class destructor is protected, so derive.
class TestWebFrameClient : public WebFrameClient {
};

}  // namespace

namespace webkit {
namespace ppapi {

class URLRequestInfoTest : public PpapiUnittest {
 public:
  URLRequestInfoTest() : info_(new PPB_URLRequestInfo_Impl(instance())) {
  }

  static void SetUpTestCase() {
    web_view_ = WebView::create(NULL);
    web_view_->initializeMainFrame(&web_frame_client_);
    WebURL web_url(GURL(""));
    WebURLRequest url_request;
    url_request.initialize();
    url_request.setURL(web_url);
    frame_ = web_view_->mainFrame();
    frame_->loadRequest(url_request);
  }

  static void TearDownTestCase() {
    web_view_->close();
  }

  bool GetDownloadToFile() {
    WebURLRequest web_request = info_->ToWebURLRequest(frame_);
    return web_request.downloadToFile();
  }

  WebCString GetURL() {
    WebURLRequest web_request = info_->ToWebURLRequest(frame_);
    return web_request.url().spec();
  }

  WebString GetMethod() {
    WebURLRequest web_request = info_->ToWebURLRequest(frame_);
    return web_request.httpMethod();
  }

  WebString GetHeaderValue(const char* field) {
    WebURLRequest web_request = info_->ToWebURLRequest(frame_);
    return web_request.httpHeaderField(WebString::fromUTF8(field));
  }

  scoped_refptr<PPB_URLRequestInfo_Impl> info_;

  static TestWebFrameClient web_frame_client_;
  static WebView* web_view_;
  static WebFrame* frame_;
};

TestWebFrameClient URLRequestInfoTest::web_frame_client_;
WebView* URLRequestInfoTest::web_view_;
WebFrame* URLRequestInfoTest::frame_;

TEST_F(URLRequestInfoTest, GetInterface) {
  const PPB_URLRequestInfo* interface = info_->GetInterface();
  ASSERT_TRUE(interface);
  ASSERT_TRUE(interface->Create);
  ASSERT_TRUE(interface->IsURLRequestInfo);
  ASSERT_TRUE(interface->SetProperty);
  ASSERT_TRUE(interface->AppendDataToBody);
  ASSERT_TRUE(interface->AppendFileToBody);
  ASSERT_TRUE(interface->Create);
  ASSERT_TRUE(interface->Create);
}

TEST_F(URLRequestInfoTest, AsURLRequestInfo) {
  ASSERT_EQ(info_, info_->AsPPB_URLRequestInfo_Impl());
}

TEST_F(URLRequestInfoTest, StreamToFile) {
  info_->SetStringProperty(PP_URLREQUESTPROPERTY_URL, "http://www.google.com");

  ASSERT_FALSE(GetDownloadToFile());

  ASSERT_TRUE(info_->SetBooleanProperty(
      PP_URLREQUESTPROPERTY_STREAMTOFILE, true));
  ASSERT_TRUE(GetDownloadToFile());

  ASSERT_TRUE(info_->SetBooleanProperty(
      PP_URLREQUESTPROPERTY_STREAMTOFILE, false));
  ASSERT_FALSE(GetDownloadToFile());
}

TEST_F(URLRequestInfoTest, FollowRedirects) {
  ASSERT_TRUE(info_->follow_redirects());

  ASSERT_TRUE(info_->SetBooleanProperty(
      PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS, false));
  ASSERT_FALSE(info_->follow_redirects());

  ASSERT_TRUE(info_->SetBooleanProperty(
      PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS, true));
  ASSERT_TRUE(info_->follow_redirects());
}

TEST_F(URLRequestInfoTest, RecordDownloadProgress) {
  ASSERT_FALSE(info_->record_download_progress());

  ASSERT_TRUE(info_->SetBooleanProperty(
      PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS, true));
  ASSERT_TRUE(info_->record_download_progress());

  ASSERT_TRUE(info_->SetBooleanProperty(
      PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS, false));
  ASSERT_FALSE(info_->record_download_progress());
}

TEST_F(URLRequestInfoTest, RecordUploadProgress) {
  ASSERT_FALSE(info_->record_upload_progress());

  ASSERT_TRUE(info_->SetBooleanProperty(
      PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS, true));
  ASSERT_TRUE(info_->record_upload_progress());

  ASSERT_TRUE(info_->SetBooleanProperty(
      PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS, false));
  ASSERT_FALSE(info_->record_upload_progress());
}

TEST_F(URLRequestInfoTest, AllowCrossOriginRequests) {
  ASSERT_FALSE(info_->allow_cross_origin_requests());

  ASSERT_TRUE(info_->SetBooleanProperty(
      PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS, true));
  ASSERT_TRUE(info_->allow_cross_origin_requests());

  ASSERT_TRUE(info_->SetBooleanProperty(
      PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS, false));
  ASSERT_FALSE(info_->allow_cross_origin_requests());
}

TEST_F(URLRequestInfoTest, AllowCredentials) {
  ASSERT_FALSE(info_->allow_credentials());

  ASSERT_TRUE(info_->SetBooleanProperty(
      PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS, true));
  ASSERT_TRUE(info_->allow_credentials());

  ASSERT_TRUE(info_->SetBooleanProperty(
      PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS, false));
  ASSERT_FALSE(info_->allow_credentials());
}

TEST_F(URLRequestInfoTest, SetURL) {
  // Test default URL is "about:blank".
  ASSERT_TRUE(IsExpected(GetURL(), "about:blank"));

  const char* url = "http://www.google.com/";
  ASSERT_TRUE(info_->SetStringProperty(
      PP_URLREQUESTPROPERTY_URL, url));
  ASSERT_TRUE(IsExpected(GetURL(), url));
}

TEST_F(URLRequestInfoTest, SetMethod) {
  // Test default method is "GET".
  ASSERT_TRUE(IsExpected(GetMethod(), "GET"));
  ASSERT_TRUE(info_->SetStringProperty(
      PP_URLREQUESTPROPERTY_METHOD, "POST"));
  ASSERT_TRUE(IsExpected(GetMethod(), "POST"));
}

TEST_F(URLRequestInfoTest, SetValidHeaders) {
  // Test default header field.
  ASSERT_TRUE(IsExpected(
      GetHeaderValue("foo"), ""));
  // Test that we can set a header field.
  ASSERT_TRUE(info_->SetStringProperty(
      PP_URLREQUESTPROPERTY_HEADERS, "foo: bar"));
  ASSERT_TRUE(IsExpected(
      GetHeaderValue("foo"), "bar"));
  // Test that we can set multiple header fields using \n delimiter.
  ASSERT_TRUE(info_->SetStringProperty(
      PP_URLREQUESTPROPERTY_HEADERS, "foo: bar\nbar: baz"));
  ASSERT_TRUE(IsExpected(
      GetHeaderValue("foo"), "bar"));
  ASSERT_TRUE(IsExpected(
      GetHeaderValue("bar"), "baz"));
}

TEST_F(URLRequestInfoTest, SetInvalidHeaders) {
  const char* const kForbiddenHeaderFields[] = {
    "accept-charset",
    "accept-encoding",
    "connection",
    "content-length",
    "cookie",
    "cookie2",
    "content-transfer-encoding",
    "date",
    "expect",
    "host",
    "keep-alive",
    "origin",
    "referer",
    "te",
    "trailer",
    "transfer-encoding",
    "upgrade",
    "user-agent",
    "via",

    "proxy-foo",  // Test for any header starting with proxy- or sec-.
    "sec-foo",
  };

  // Test that no forbidden header fields can be set.
  for (size_t i = 0; i < arraysize(kForbiddenHeaderFields); ++i) {
    std::string headers(kForbiddenHeaderFields[i]);
    headers.append(": foo");
    ASSERT_FALSE(info_->SetStringProperty(
        PP_URLREQUESTPROPERTY_HEADERS, headers.c_str()));
    ASSERT_TRUE(IsNullOrEmpty(GetHeaderValue(kForbiddenHeaderFields[i])));
  }

  // Test that forbidden header can't be set in various ways.
  ASSERT_FALSE(info_->SetStringProperty(
      PP_URLREQUESTPROPERTY_HEADERS, "cookie : foo"));
  ASSERT_TRUE(IsNullOrEmpty(GetHeaderValue("cookie")));

  // Test that forbidden header can't be set with an allowed one.
  ASSERT_FALSE(info_->SetStringProperty(
      PP_URLREQUESTPROPERTY_HEADERS, "foo: bar\ncookie: foo"));
  ASSERT_TRUE(IsNullOrEmpty(GetHeaderValue("cookie")));
}

// TODO(bbudge) Unit tests for AppendDataToBody, AppendFileToBody.

}  // namespace ppapi
}  // namespace webkit