Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "content/public/test/browser_test.h" | 9 #include "content/public/test/browser_test.h" |
| 10 #include "headless/public/domains/page.h" | 10 #include "headless/public/domains/page.h" |
| 11 #include "headless/public/headless_browser.h" | 11 #include "headless/public/headless_browser.h" |
| 12 #include "headless/public/headless_devtools_client.h" | 12 #include "headless/public/headless_devtools_client.h" |
| 13 #include "headless/public/headless_devtools_target.h" | 13 #include "headless/public/headless_devtools_target.h" |
| 14 #include "headless/public/headless_web_contents.h" | 14 #include "headless/public/headless_web_contents.h" |
| 15 #include "headless/test/headless_browser_test.h" | 15 #include "headless/test/headless_browser_test.h" |
| 16 #include "headless/test/test_protocol_handler.h" | 16 #include "headless/test/test_protocol_handler.h" |
| 17 #include "headless/test/test_url_request_job.h" | |
| 18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
| 19 #include "net/cookies/cookie_store.h" | |
| 17 #include "net/test/spawned_test_server/spawned_test_server.h" | 20 #include "net/test/spawned_test_server/spawned_test_server.h" |
| 21 #include "net/url_request/url_request_context.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "ui/gfx/geometry/size.h" | 23 #include "ui/gfx/geometry/size.h" |
| 20 | 24 |
| 21 namespace headless { | 25 namespace headless { |
| 22 | 26 |
| 23 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) { | 27 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) { |
| 24 HeadlessWebContents* web_contents = | 28 HeadlessWebContents* web_contents = |
| 25 browser()->CreateWebContentsBuilder().Build(); | 29 browser()->CreateWebContentsBuilder().Build(); |
| 26 EXPECT_TRUE(web_contents); | 30 EXPECT_TRUE(web_contents); |
| 27 | 31 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 EXPECT_TRUE(EvaluateScript( | 171 EXPECT_TRUE(EvaluateScript( |
| 168 web_contents, | 172 web_contents, |
| 169 "(document.createElement('canvas').getContext('webgl')" | 173 "(document.createElement('canvas').getContext('webgl')" |
| 170 " instanceof WebGLRenderingContext)") | 174 " instanceof WebGLRenderingContext)") |
| 171 ->GetResult() | 175 ->GetResult() |
| 172 ->GetValue() | 176 ->GetValue() |
| 173 ->GetAsBoolean(&webgl_supported)); | 177 ->GetAsBoolean(&webgl_supported)); |
| 174 EXPECT_TRUE(webgl_supported); | 178 EXPECT_TRUE(webgl_supported); |
| 175 } | 179 } |
| 176 | 180 |
| 181 namespace { | |
| 182 | |
| 183 // True if the request method is "safe" (per section 4.2.1 of RFC 7231). | |
| 184 bool IsMethodSafe(const std::string& method) { | |
| 185 return method == "GET" || method == "HEAD" || method == "OPTIONS" || | |
| 186 method == "TRACE"; | |
| 187 } | |
| 188 | |
| 189 class ProtocolHandlerWithCookies | |
| 190 : public net::URLRequestJobFactory::ProtocolHandler { | |
| 191 public: | |
| 192 ProtocolHandlerWithCookies(net::CookieList* sent_cookies); | |
| 193 ~ProtocolHandlerWithCookies() override {} | |
| 194 | |
| 195 net::URLRequestJob* MaybeCreateJob( | |
| 196 net::URLRequest* request, | |
| 197 net::NetworkDelegate* network_delegate) const override; | |
| 198 | |
| 199 private: | |
| 200 net::CookieList* sent_cookies_; // Not owned. | |
| 201 | |
| 202 DISALLOW_COPY_AND_ASSIGN(ProtocolHandlerWithCookies); | |
| 203 }; | |
| 204 | |
| 205 class URLRequestJobWithCookies : public TestURLRequestJob { | |
| 206 public: | |
| 207 URLRequestJobWithCookies(net::URLRequest* request, | |
| 208 net::NetworkDelegate* network_delegate, | |
| 209 net::CookieList* sent_cookies); | |
| 210 ~URLRequestJobWithCookies() override {} | |
| 211 | |
| 212 // net::URLRequestJob implementation: | |
| 213 void Start() override; | |
| 214 | |
| 215 private: | |
| 216 void SaveCookiesAndStart(const net::CookieList& cookie_list); | |
| 217 | |
| 218 net::CookieList* sent_cookies_; // Not owned. | |
| 219 base::WeakPtrFactory<URLRequestJobWithCookies> weak_factory_; | |
| 220 DISALLOW_COPY_AND_ASSIGN(URLRequestJobWithCookies); | |
| 221 }; | |
| 222 | |
| 223 ProtocolHandlerWithCookies::ProtocolHandlerWithCookies( | |
| 224 net::CookieList* sent_cookies) | |
| 225 : sent_cookies_(sent_cookies) {} | |
| 226 | |
| 227 net::URLRequestJob* ProtocolHandlerWithCookies::MaybeCreateJob( | |
| 228 net::URLRequest* request, | |
| 229 net::NetworkDelegate* network_delegate) const { | |
| 230 return new URLRequestJobWithCookies(request, network_delegate, sent_cookies_); | |
| 231 } | |
| 232 | |
| 233 URLRequestJobWithCookies::URLRequestJobWithCookies( | |
| 234 net::URLRequest* request, | |
| 235 net::NetworkDelegate* network_delegate, | |
| 236 net::CookieList* sent_cookies) | |
| 237 // Return an empty response for every request. | |
| 238 : TestURLRequestJob(request, network_delegate, ""), | |
| 239 sent_cookies_(sent_cookies), | |
| 240 weak_factory_(this) {} | |
| 241 | |
| 242 void URLRequestJobWithCookies::Start() { | |
| 243 net::CookieStore* cookie_store = request_->context()->cookie_store(); | |
| 244 net::CookieOptions options; | |
| 245 options.set_include_httponly(); | |
| 246 | |
| 247 // See net::URLRequestHttpJob::AddCookieHeaderAndStart(). | |
| 248 url::Origin requested_origin(request_->url()); | |
| 249 url::Origin site_for_cookies(request_->first_party_for_cookies()); | |
| 250 | |
| 251 if (net::registry_controlled_domains::SameDomainOrHost( | |
| 252 requested_origin, site_for_cookies, | |
| 253 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { | |
| 254 if (net::registry_controlled_domains::SameDomainOrHost( | |
| 255 requested_origin, request_->initiator(), | |
| 256 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES)) { | |
| 257 options.set_same_site_cookie_mode( | |
| 258 net::CookieOptions::SameSiteCookieMode::INCLUDE_STRICT_AND_LAX); | |
| 259 } else if (IsMethodSafe(request_->method())) { | |
| 260 options.set_same_site_cookie_mode( | |
| 261 net::CookieOptions::SameSiteCookieMode::INCLUDE_LAX); | |
| 262 } | |
| 263 } | |
| 264 cookie_store->GetCookieListWithOptionsAsync( | |
| 265 request_->url(), options, | |
| 266 base::Bind(&URLRequestJobWithCookies::SaveCookiesAndStart, | |
| 267 weak_factory_.GetWeakPtr())); | |
| 268 } | |
| 269 | |
| 270 void URLRequestJobWithCookies::SaveCookiesAndStart( | |
| 271 const net::CookieList& cookie_list) { | |
| 272 *sent_cookies_ = cookie_list; | |
| 273 NotifyHeadersComplete(); | |
| 274 } | |
| 275 | |
| 276 } // namespace | |
| 277 | |
| 278 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, ReadCookiesInProtocolHandler) { | |
| 279 net::CookieList sent_cookies; | |
| 280 ProtocolHandlerMap protocol_handlers; | |
| 281 protocol_handlers[url::kHttpsScheme] = | |
| 282 base::WrapUnique(new ProtocolHandlerWithCookies(&sent_cookies)); | |
| 283 | |
| 284 HeadlessBrowser::Options::Builder builder; | |
| 285 builder.SetProtocolHandlers(std::move(protocol_handlers)); | |
| 286 SetBrowserOptions(builder.Build()); | |
| 287 | |
| 288 HeadlessWebContents* web_contents = | |
| 289 browser() | |
| 290 ->CreateWebContentsBuilder() | |
| 291 .SetInitialURL(GURL("https://example.com/cookie.html")) | |
|
Eric Seckler
2016/08/02 19:50:47
Nit: I'd change this to a fictive domain to indica
Sami
2016/08/03 09:55:58
Actually that's exactly what example.com is :) htt
| |
| 292 .Build(); | |
| 293 EXPECT_TRUE(WaitForLoad(web_contents)); | |
| 294 | |
| 295 // The first load has no cookies. | |
| 296 EXPECT_EQ(0u, sent_cookies.size()); | |
| 297 | |
| 298 // Set a cookie and reload the page. | |
| 299 EXPECT_FALSE(EvaluateScript( | |
| 300 web_contents, | |
| 301 "document.cookie = 'shape=oblong', window.location.reload()") | |
| 302 ->HasExceptionDetails()); | |
| 303 EXPECT_TRUE(WaitForLoad(web_contents)); | |
| 304 | |
| 305 // We should have sent the cookie this time. | |
| 306 EXPECT_EQ(1u, sent_cookies.size()); | |
| 307 EXPECT_EQ("shape", sent_cookies[0].Name()); | |
| 308 EXPECT_EQ("oblong", sent_cookies[0].Value()); | |
| 309 } | |
| 310 | |
| 177 } // namespace headless | 311 } // namespace headless |
| OLD | NEW |