| 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 "headless/public/util/generic_url_request_job.h" | 5 #include "headless/public/util/generic_url_request_job.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 const std::string& json_reply) { | 167 const std::string& json_reply) { |
| 168 json_fetch_reply_map_[url.spec()] = json_reply; | 168 json_fetch_reply_map_[url.spec()] = json_reply; |
| 169 | 169 |
| 170 std::unique_ptr<net::URLRequest> request(url_request_context_.CreateRequest( | 170 std::unique_ptr<net::URLRequest> request(url_request_context_.CreateRequest( |
| 171 url, net::DEFAULT_PRIORITY, &request_delegate_)); | 171 url, net::DEFAULT_PRIORITY, &request_delegate_)); |
| 172 request->Start(); | 172 request->Start(); |
| 173 base::RunLoop().RunUntilIdle(); | 173 base::RunLoop().RunUntilIdle(); |
| 174 return request; | 174 return request; |
| 175 } | 175 } |
| 176 | 176 |
| 177 std::unique_ptr<net::URLRequest> CreateAndCompletePostJob( |
| 178 const GURL& url, |
| 179 const std::string& post_data, |
| 180 const std::string& json_reply) { |
| 181 json_fetch_reply_map_[url.spec()] = json_reply; |
| 182 |
| 183 std::unique_ptr<net::URLRequest> request(url_request_context_.CreateRequest( |
| 184 url, net::DEFAULT_PRIORITY, &request_delegate_)); |
| 185 request->set_method("POST"); |
| 186 request->set_upload(net::ElementsUploadDataStream::CreateWithReader( |
| 187 base::MakeUnique<net::UploadBytesElementReader>(post_data.data(), |
| 188 post_data.size()), |
| 189 0)); |
| 190 request->Start(); |
| 191 base::RunLoop().RunUntilIdle(); |
| 192 return request; |
| 193 } |
| 194 |
| 177 protected: | 195 protected: |
| 178 base::MessageLoop message_loop_; | 196 base::MessageLoop message_loop_; |
| 179 ExpeditedDispatcher dispatcher_; | 197 ExpeditedDispatcher dispatcher_; |
| 180 | 198 |
| 181 net::URLRequestJobFactoryImpl url_request_job_factory_; | 199 net::URLRequestJobFactoryImpl url_request_job_factory_; |
| 182 net::URLRequestContext url_request_context_; | 200 net::URLRequestContext url_request_context_; |
| 183 MockCookieStore cookie_store_; | 201 MockCookieStore cookie_store_; |
| 184 | 202 |
| 185 MockURLRequestDelegate request_delegate_; | 203 MockURLRequestDelegate request_delegate_; |
| 186 base::DictionaryValue fetch_request_; // The request sent to MockFetcher. | 204 base::DictionaryValue fetch_request_; // The request sent to MockFetcher. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 208 request->SetExtraRequestHeaderByName("Accept", "text/plain", true); | 226 request->SetExtraRequestHeaderByName("Accept", "text/plain", true); |
| 209 request->Start(); | 227 request->Start(); |
| 210 base::RunLoop().RunUntilIdle(); | 228 base::RunLoop().RunUntilIdle(); |
| 211 | 229 |
| 212 std::string expected_request_json = R"( | 230 std::string expected_request_json = R"( |
| 213 { | 231 { |
| 214 "url": "https://example.com/", | 232 "url": "https://example.com/", |
| 215 "method": "GET", | 233 "method": "GET", |
| 216 "headers": { | 234 "headers": { |
| 217 "Accept": "text/plain", | 235 "Accept": "text/plain", |
| 218 "Cookie": "", | |
| 219 "Extra-Header": "Value", | 236 "Extra-Header": "Value", |
| 220 "Referer": "https://referrer.example.com/", | 237 "Referer": "https://referrer.example.com/", |
| 221 "User-Agent": "TestBrowser" | 238 "User-Agent": "TestBrowser" |
| 222 } | 239 } |
| 223 })"; | 240 })"; |
| 224 | 241 |
| 225 EXPECT_THAT(fetch_request_, MatchesJson(expected_request_json)); | 242 EXPECT_THAT(fetch_request_, MatchesJson(expected_request_json)); |
| 226 } | 243 } |
| 227 | 244 |
| 228 TEST_F(GenericURLRequestJobTest, BasicPostRequestParams) { | 245 TEST_F(GenericURLRequestJobTest, BasicPostRequestParams) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 252 request->Start(); | 269 request->Start(); |
| 253 base::RunLoop().RunUntilIdle(); | 270 base::RunLoop().RunUntilIdle(); |
| 254 | 271 |
| 255 std::string expected_request_json = R"( | 272 std::string expected_request_json = R"( |
| 256 { | 273 { |
| 257 "url": "https://example.com/", | 274 "url": "https://example.com/", |
| 258 "method": "POST", | 275 "method": "POST", |
| 259 "post_data": "lorem ipsom", | 276 "post_data": "lorem ipsom", |
| 260 "headers": { | 277 "headers": { |
| 261 "Accept": "text/plain", | 278 "Accept": "text/plain", |
| 262 "Cookie": "", | |
| 263 "Extra-Header": "Value", | 279 "Extra-Header": "Value", |
| 264 "Referer": "https://referrer.example.com/", | 280 "Referer": "https://referrer.example.com/", |
| 265 "User-Agent": "TestBrowser" | 281 "User-Agent": "TestBrowser" |
| 266 } | 282 } |
| 267 })"; | 283 })"; |
| 268 | 284 |
| 269 EXPECT_THAT(fetch_request_, MatchesJson(expected_request_json)); | 285 EXPECT_THAT(fetch_request_, MatchesJson(expected_request_json)); |
| 270 } | 286 } |
| 271 | 287 |
| 272 TEST_F(GenericURLRequestJobTest, BasicRequestProperties) { | 288 TEST_F(GenericURLRequestJobTest, BasicRequestProperties) { |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 })"; | 440 })"; |
| 425 | 441 |
| 426 std::unique_ptr<net::URLRequest> request( | 442 std::unique_ptr<net::URLRequest> request( |
| 427 CreateAndCompleteGetJob(GURL("https://example.com"), reply)); | 443 CreateAndCompleteGetJob(GURL("https://example.com"), reply)); |
| 428 | 444 |
| 429 std::string expected_request_json = R"( | 445 std::string expected_request_json = R"( |
| 430 { | 446 { |
| 431 "url": "https://example.com/", | 447 "url": "https://example.com/", |
| 432 "method": "GET", | 448 "method": "GET", |
| 433 "headers": { | 449 "headers": { |
| 434 "Cookie": "basic_cookie=1; secure_cookie=2; http_only_cookie=3", | 450 "Cookie": "basic_cookie=1; secure_cookie=2; http_only_cookie=3" |
| 435 "Referer": "" | |
| 436 } | 451 } |
| 437 })"; | 452 })"; |
| 438 | 453 |
| 439 EXPECT_THAT(fetch_request_, MatchesJson(expected_request_json)); | 454 EXPECT_THAT(fetch_request_, MatchesJson(expected_request_json)); |
| 440 } | 455 } |
| 441 | 456 |
| 442 TEST_F(GenericURLRequestJobTest, DelegateBlocksLoading) { | 457 TEST_F(GenericURLRequestJobTest, DelegateBlocksLoading) { |
| 443 std::string reply = R"( | 458 std::string reply = R"( |
| 444 { | 459 { |
| 445 "url": "https://example.com", | 460 "url": "https://example.com", |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 EXPECT_CALL(job_delegate_, | 617 EXPECT_CALL(job_delegate_, |
| 603 OnResourceLoadFailed(_, net::ERR_ADDRESS_UNREACHABLE)); | 618 OnResourceLoadFailed(_, net::ERR_ADDRESS_UNREACHABLE)); |
| 604 | 619 |
| 605 std::unique_ptr<net::URLRequest> request(url_request_context_.CreateRequest( | 620 std::unique_ptr<net::URLRequest> request(url_request_context_.CreateRequest( |
| 606 GURL("https://i-dont-exist.com"), net::DEFAULT_PRIORITY, | 621 GURL("https://i-dont-exist.com"), net::DEFAULT_PRIORITY, |
| 607 &request_delegate_)); | 622 &request_delegate_)); |
| 608 request->Start(); | 623 request->Start(); |
| 609 base::RunLoop().RunUntilIdle(); | 624 base::RunLoop().RunUntilIdle(); |
| 610 } | 625 } |
| 611 | 626 |
| 627 TEST_F(GenericURLRequestJobTest, RequestsHaveDistinctIds) { |
| 628 std::string reply = R"( |
| 629 { |
| 630 "url": "https://example.com", |
| 631 "http_response_code": 200, |
| 632 "data": "Reply", |
| 633 "headers": { |
| 634 "Content-Type": "text/html; charset=UTF-8" |
| 635 } |
| 636 })"; |
| 637 |
| 638 std::set<uint64_t> ids; |
| 639 job_delegate_.SetPolicy(base::Bind( |
| 640 [](std::set<uint64_t>* ids, PendingRequest* pending_request) { |
| 641 ids->insert(pending_request->GetRequest()->GetRequestId()); |
| 642 pending_request->AllowRequest(); |
| 643 }, |
| 644 &ids)); |
| 645 |
| 646 CreateAndCompleteGetJob(GURL("https://example.com"), reply); |
| 647 CreateAndCompleteGetJob(GURL("https://example.com"), reply); |
| 648 CreateAndCompleteGetJob(GURL("https://example.com"), reply); |
| 649 |
| 650 // We expect three distinct ids. |
| 651 EXPECT_EQ(3u, ids.size()); |
| 652 } |
| 653 |
| 654 TEST_F(GenericURLRequestJobTest, GetPostData) { |
| 655 std::string reply = R"( |
| 656 { |
| 657 "url": "https://example.com", |
| 658 "http_response_code": 200, |
| 659 "data": "Reply", |
| 660 "headers": { |
| 661 "Content-Type": "text/html; charset=UTF-8" |
| 662 } |
| 663 })"; |
| 664 |
| 665 std::string post_data; |
| 666 job_delegate_.SetPolicy(base::Bind( |
| 667 [](std::string* post_data, PendingRequest* pending_request) { |
| 668 *post_data = pending_request->GetRequest()->GetPostData(); |
| 669 pending_request->AllowRequest(); |
| 670 }, |
| 671 &post_data)); |
| 672 |
| 673 CreateAndCompletePostJob(GURL("https://example.com"), "payload", reply); |
| 674 |
| 675 EXPECT_EQ("payload", post_data); |
| 676 } |
| 677 |
| 612 } // namespace headless | 678 } // namespace headless |
| OLD | NEW |