Commit 9ce1f4a1 authored by Alex Clarke's avatar Alex Clarke Committed by Commit Bot

Headless: GenericURLRequestJob::OnFetchComplete to store cookies

GenericURLRequestJob already reads cookies, but it didn't write them.

Bug: 
Change-Id: I2b3b93d652e8c3a79c6f8bb854b0ddec84fe1a93
Reviewed-on: https://chromium-review.googlesource.com/848993Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
Commit-Queue: Alex Clarke <alexclarke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#526676}
parent c7818d1c
......@@ -143,6 +143,34 @@ void GenericURLRequestJob::OnFetchComplete(
body_size_ = body_size;
load_timing_info_ = load_timing_info;
// Save any cookies from the response.
if (!(request_->load_flags() & net::LOAD_DO_NOT_SAVE_COOKIES) &&
request_->context()->cookie_store()) {
net::CookieOptions options;
options.set_include_httponly();
base::Time response_date;
if (!response_headers_->GetDateValue(&response_date))
response_date = base::Time();
options.set_server_time(response_date);
// Set all cookies, without waiting for them to be set. Any subsequent read
// will see the combined result of all cookie operation.
const base::StringPiece name("Set-Cookie");
std::string cookie_line;
size_t iter = 0;
while (response_headers_->EnumerateHeader(&iter, name, &cookie_line)) {
std::unique_ptr<net::CanonicalCookie> cookie =
net::CanonicalCookie::Create(final_url, cookie_line,
base::Time::Now(), options);
if (!cookie || !CanSetCookie(*cookie, &options))
continue;
request_->context()->cookie_store()->SetCanonicalCookieAsync(
std::move(cookie), final_url.SchemeIsCryptographic(),
!options.exclude_httponly(), net::CookieStore::SetCookiesCallback());
}
}
DispatchHeadersComplete();
delegate_->OnResourceLoadComplete(this, final_url, response_headers_, body_,
......
......@@ -27,7 +27,11 @@
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::AllOf;
using testing::ElementsAre;
using testing::Eq;
using testing::NotNull;
using testing::Property;
using testing::_;
std::ostream& operator<<(std::ostream& os, const base::DictionaryValue& value) {
......@@ -519,6 +523,27 @@ TEST_F(GenericURLRequestJobTest, RequestWithCookies) {
EXPECT_THAT(fetch_request_, MatchesJson(expected_request_json));
}
TEST_F(GenericURLRequestJobTest, ResponseWithCookies) {
std::string reply = R"(
{
"url": "https://example.com",
"data": "Reply",
"headers": {
"Set-Cookie": "A=foobar; path=/; "
}
})";
std::unique_ptr<net::URLRequest> request(
CreateAndCompleteGetJob(GURL("https://example.com"), reply));
EXPECT_THAT(*cookie_store_.cookies(),
ElementsAre(AllOf(
Property(&net::CanonicalCookie::Name, Eq("A")),
Property(&net::CanonicalCookie::Value, Eq("foobar")),
Property(&net::CanonicalCookie::Domain, Eq("example.com")),
Property(&net::CanonicalCookie::Path, Eq("/")))));
}
TEST_F(GenericURLRequestJobTest, OnResourceLoadFailed) {
EXPECT_CALL(job_delegate_,
OnResourceLoadFailed(_, net::ERR_ADDRESS_UNREACHABLE));
......
......@@ -50,7 +50,7 @@ void MockCookieStore::SetCanonicalCookieAsync(
bool secure_source,
bool can_modify_httponly,
SetCookiesCallback callback) {
CHECK(false);
cookies_.push_back(std::move(*cookie));
}
void MockCookieStore::GetCookiesWithOptionsAsync(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment