Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: components/ntp_snippets/remote/ntp_snippets_fetcher_unittest.cc

Issue 2570673004: Remove host restrictions from snippets (Closed)
Patch Set: rebase Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/ntp_snippets/remote/ntp_snippets_fetcher.h" 5 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h"
6 6
7 #include <deque> 7 #include <deque>
8 #include <map> 8 #include <map>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 : NTPSnippetsFetcherTestBase(GURL(kTestChromeContentSuggestionsUrl)) { 435 : NTPSnippetsFetcherTestBase(GURL(kTestChromeContentSuggestionsUrl)) {
436 SetDefaultVariationParam("content_suggestions_backend", 436 SetDefaultVariationParam("content_suggestions_backend",
437 kContentSuggestionsServer); 437 kContentSuggestionsServer);
438 ResetSnippetsFetcher(); 438 ResetSnippetsFetcher();
439 } 439 }
440 }; 440 };
441 441
442 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestAuthenticated) { 442 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestAuthenticated) {
443 NTPSnippetsFetcher::RequestBuilder builder; 443 NTPSnippetsFetcher::RequestBuilder builder;
444 NTPSnippetsFetcher::Params params; 444 NTPSnippetsFetcher::Params params;
445 params.hosts = {"chromium.org"};
446 params.excluded_ids = {"1234567890"}; 445 params.excluded_ids = {"1234567890"};
447 params.count_to_fetch = 25; 446 params.count_to_fetch = 25;
448 params.interactive_request = false; 447 params.interactive_request = false;
449 builder.SetParams(params) 448 builder.SetParams(params)
450 .SetAuthentication("0BFUSGAIA", "headerstuff") 449 .SetAuthentication("0BFUSGAIA", "headerstuff")
451 .SetPersonalization(NTPSnippetsFetcher::Personalization::kPersonal) 450 .SetPersonalization(NTPSnippetsFetcher::Personalization::kPersonal)
452 .SetUserClassForTesting("ACTIVE_NTP_USER") 451 .SetUserClassForTesting("ACTIVE_NTP_USER")
453 .SetFetchAPI(NTPSnippetsFetcher::CHROME_READER_API); 452 .SetFetchAPI(NTPSnippetsFetcher::CHROME_READER_API);
454 453
455 EXPECT_THAT(builder.PreviewRequestHeadersForTesting(), 454 EXPECT_THAT(builder.PreviewRequestHeadersForTesting(),
(...skipping 15 matching lines...) Expand all
471 " \"value\": \"TITLE\"" 470 " \"value\": \"TITLE\""
472 " }," 471 " },"
473 " {" 472 " {"
474 " \"type\": \"METADATA\"," 473 " \"type\": \"METADATA\","
475 " \"value\": \"SNIPPET\"" 474 " \"value\": \"SNIPPET\""
476 " }," 475 " },"
477 " {" 476 " {"
478 " \"type\": \"METADATA\"," 477 " \"type\": \"METADATA\","
479 " \"value\": \"THUMBNAIL\"" 478 " \"value\": \"THUMBNAIL\""
480 " }" 479 " }"
481 " ],"
482 " \"content_selectors\": ["
483 " {"
484 " \"type\": \"HOST_RESTRICT\","
485 " \"value\": \"chromium.org\""
486 " }"
487 " ]" 480 " ]"
488 " }," 481 " },"
489 " \"global_scoring_params\": {" 482 " \"global_scoring_params\": {"
490 " \"num_to_return\": 25," 483 " \"num_to_return\": 25,"
491 " \"sort_type\": 1" 484 " \"sort_type\": 1"
492 " }" 485 " }"
493 " }" 486 " }"
494 "}")); 487 "}"));
495 488
496 builder.SetFetchAPI( 489 builder.SetFetchAPI(
497 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); 490 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API);
498 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), 491 EXPECT_THAT(builder.PreviewRequestBodyForTesting(),
499 EqualsJSON("{" 492 EqualsJSON("{"
500 " \"priority\": \"BACKGROUND_PREFETCH\"," 493 " \"priority\": \"BACKGROUND_PREFETCH\","
501 " \"regularlyVisitedHostNames\": ["
502 " \"chromium.org\""
503 " ],"
504 " \"excludedSuggestionIds\": [" 494 " \"excludedSuggestionIds\": ["
505 " \"1234567890\"" 495 " \"1234567890\""
506 " ]," 496 " ],"
507 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" 497 " \"userActivenessClass\": \"ACTIVE_NTP_USER\""
508 "}")); 498 "}"));
509 } 499 }
510 500
511 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestUnauthenticated) { 501 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestUnauthenticated) {
512 NTPSnippetsFetcher::RequestBuilder builder; 502 NTPSnippetsFetcher::RequestBuilder builder;
513 NTPSnippetsFetcher::Params params = test_params(); 503 NTPSnippetsFetcher::Params params = test_params();
(...skipping 20 matching lines...) Expand all
534 " \"value\": \"TITLE\"" 524 " \"value\": \"TITLE\""
535 " }," 525 " },"
536 " {" 526 " {"
537 " \"type\": \"METADATA\"," 527 " \"type\": \"METADATA\","
538 " \"value\": \"SNIPPET\"" 528 " \"value\": \"SNIPPET\""
539 " }," 529 " },"
540 " {" 530 " {"
541 " \"type\": \"METADATA\"," 531 " \"type\": \"METADATA\","
542 " \"value\": \"THUMBNAIL\"" 532 " \"value\": \"THUMBNAIL\""
543 " }" 533 " }"
544 " ]," 534 " ]"
545 " \"content_selectors\": []"
546 " }," 535 " },"
547 " \"global_scoring_params\": {" 536 " \"global_scoring_params\": {"
548 " \"num_to_return\": 10," 537 " \"num_to_return\": 10,"
549 " \"sort_type\": 1" 538 " \"sort_type\": 1"
550 " }" 539 " }"
551 " }" 540 " }"
552 "}")); 541 "}"));
553 542
554 builder.SetFetchAPI( 543 builder.SetFetchAPI(
555 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); 544 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API);
556 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), 545 EXPECT_THAT(builder.PreviewRequestBodyForTesting(),
557 EqualsJSON("{" 546 EqualsJSON("{"
558 " \"regularlyVisitedHostNames\": [],"
559 " \"priority\": \"USER_ACTION\"," 547 " \"priority\": \"USER_ACTION\","
560 " \"excludedSuggestionIds\": []," 548 " \"excludedSuggestionIds\": [],"
561 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" 549 " \"userActivenessClass\": \"ACTIVE_NTP_USER\""
562 "}")); 550 "}"));
563 } 551 }
564 552
565 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestExcludedIds) { 553 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestExcludedIds) {
566 NTPSnippetsFetcher::RequestBuilder builder; 554 NTPSnippetsFetcher::RequestBuilder builder;
567 NTPSnippetsFetcher::Params params = test_params(); 555 NTPSnippetsFetcher::Params params = test_params();
568 params.interactive_request = false; 556 params.interactive_request = false;
569 for (int i = 0; i < 200; ++i) { 557 for (int i = 0; i < 200; ++i) {
570 params.excluded_ids.insert(base::StringPrintf("%03d", i)); 558 params.excluded_ids.insert(base::StringPrintf("%03d", i));
571 } 559 }
572 builder.SetParams(params) 560 builder.SetParams(params)
573 .SetUserClassForTesting("ACTIVE_NTP_USER") 561 .SetUserClassForTesting("ACTIVE_NTP_USER")
574 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) 562 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal)
575 .SetFetchAPI( 563 .SetFetchAPI(
576 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); 564 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API);
577 565
578 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), 566 EXPECT_THAT(builder.PreviewRequestBodyForTesting(),
579 EqualsJSON("{" 567 EqualsJSON("{"
580 " \"regularlyVisitedHostNames\": [],"
581 " \"priority\": \"BACKGROUND_PREFETCH\"," 568 " \"priority\": \"BACKGROUND_PREFETCH\","
582 " \"excludedSuggestionIds\": [" 569 " \"excludedSuggestionIds\": ["
583 " \"000\", \"001\", \"002\", \"003\", \"004\"," 570 " \"000\", \"001\", \"002\", \"003\", \"004\","
584 " \"005\", \"006\", \"007\", \"008\", \"009\"," 571 " \"005\", \"006\", \"007\", \"008\", \"009\","
585 " \"010\", \"011\", \"012\", \"013\", \"014\"," 572 " \"010\", \"011\", \"012\", \"013\", \"014\","
586 " \"015\", \"016\", \"017\", \"018\", \"019\"," 573 " \"015\", \"016\", \"017\", \"018\", \"019\","
587 " \"020\", \"021\", \"022\", \"023\", \"024\"," 574 " \"020\", \"021\", \"022\", \"023\", \"024\","
588 " \"025\", \"026\", \"027\", \"028\", \"029\"," 575 " \"025\", \"026\", \"027\", \"028\", \"029\","
589 " \"030\", \"031\", \"032\", \"033\", \"034\"," 576 " \"030\", \"031\", \"032\", \"033\", \"034\","
590 " \"035\", \"036\", \"037\", \"038\", \"039\"," 577 " \"035\", \"036\", \"037\", \"038\", \"039\","
(...skipping 20 matching lines...) Expand all
611 NTPSnippetsFetcher::RequestBuilder builder; 598 NTPSnippetsFetcher::RequestBuilder builder;
612 NTPSnippetsFetcher::Params params = test_params(); 599 NTPSnippetsFetcher::Params params = test_params();
613 params.interactive_request = false; 600 params.interactive_request = false;
614 builder.SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) 601 builder.SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal)
615 .SetParams(params) 602 .SetParams(params)
616 .SetFetchAPI( 603 .SetFetchAPI(
617 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); 604 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API);
618 605
619 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), 606 EXPECT_THAT(builder.PreviewRequestBodyForTesting(),
620 EqualsJSON("{" 607 EqualsJSON("{"
621 " \"regularlyVisitedHostNames\": [],"
622 " \"priority\": \"BACKGROUND_PREFETCH\"," 608 " \"priority\": \"BACKGROUND_PREFETCH\","
623 " \"excludedSuggestionIds\": []" 609 " \"excludedSuggestionIds\": []"
624 "}")); 610 "}"));
625 } 611 }
626 612
627 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestWithTwoLanguages) { 613 TEST_F(ChromeReaderSnippetsFetcherTest, BuildRequestWithTwoLanguages) {
628 NTPSnippetsFetcher::RequestBuilder builder; 614 NTPSnippetsFetcher::RequestBuilder builder;
629 std::unique_ptr<translate::LanguageModel> language_model = 615 std::unique_ptr<translate::LanguageModel> language_model =
630 MakeLanguageModel({"de", "en"}); 616 MakeLanguageModel({"de", "en"});
631 NTPSnippetsFetcher::Params params = test_params(); 617 NTPSnippetsFetcher::Params params = test_params();
632 params.language_code = "en"; 618 params.language_code = "en";
633 builder.SetParams(params) 619 builder.SetParams(params)
634 .SetLanguageModel(language_model.get()) 620 .SetLanguageModel(language_model.get())
635 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) 621 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal)
636 .SetFetchAPI( 622 .SetFetchAPI(
637 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); 623 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API);
638 624
639 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), 625 EXPECT_THAT(builder.PreviewRequestBodyForTesting(),
640 EqualsJSON("{" 626 EqualsJSON("{"
641 " \"regularlyVisitedHostNames\": [],"
642 " \"priority\": \"USER_ACTION\"," 627 " \"priority\": \"USER_ACTION\","
643 " \"uiLanguage\": \"en\"," 628 " \"uiLanguage\": \"en\","
644 " \"excludedSuggestionIds\": []," 629 " \"excludedSuggestionIds\": [],"
645 " \"topLanguages\": [" 630 " \"topLanguages\": ["
646 " {" 631 " {"
647 " \"language\" : \"en\"," 632 " \"language\" : \"en\","
648 " \"frequency\" : 0.5" 633 " \"frequency\" : 0.5"
649 " }," 634 " },"
650 " {" 635 " {"
651 " \"language\" : \"de\"," 636 " \"language\" : \"de\","
(...skipping 10 matching lines...) Expand all
662 NTPSnippetsFetcher::Params params = test_params(); 647 NTPSnippetsFetcher::Params params = test_params();
663 params.language_code = "en"; 648 params.language_code = "en";
664 builder.SetParams(params) 649 builder.SetParams(params)
665 .SetLanguageModel(language_model.get()) 650 .SetLanguageModel(language_model.get())
666 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal) 651 .SetPersonalization(NTPSnippetsFetcher::Personalization::kNonPersonal)
667 .SetFetchAPI( 652 .SetFetchAPI(
668 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API); 653 NTPSnippetsFetcher::FetchAPI::CHROME_CONTENT_SUGGESTIONS_API);
669 654
670 EXPECT_THAT(builder.PreviewRequestBodyForTesting(), 655 EXPECT_THAT(builder.PreviewRequestBodyForTesting(),
671 EqualsJSON("{" 656 EqualsJSON("{"
672 " \"regularlyVisitedHostNames\": [],"
673 " \"priority\": \"USER_ACTION\"," 657 " \"priority\": \"USER_ACTION\","
674 " \"uiLanguage\": \"en\"," 658 " \"uiLanguage\": \"en\","
675 " \"excludedSuggestionIds\": []," 659 " \"excludedSuggestionIds\": [],"
676 " \"topLanguages\": [{" 660 " \"topLanguages\": [{"
677 " \"language\" : \"en\"," 661 " \"language\" : \"en\","
678 " \"frequency\" : 1.0" 662 " \"frequency\" : 1.0"
679 " }]" 663 " }]"
680 "}")); 664 "}"));
681 } 665 }
682 666
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
997 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 981 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
998 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 982 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
999 EXPECT_THAT( 983 EXPECT_THAT(
1000 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 984 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
1001 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 985 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
1002 EXPECT_THAT(histogram_tester().GetAllSamples( 986 EXPECT_THAT(histogram_tester().GetAllSamples(
1003 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 987 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
1004 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 988 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
1005 } 989 }
1006 990
1007 TEST_F(ChromeReaderSnippetsFetcherTest, ShouldRestrictToHosts) {
1008 DelegateCallingTestURLFetcherFactory fetcher_factory;
1009 NTPSnippetsFetcher::Params params = test_params();
1010 params.hosts = {"www.somehost1.com", "www.somehost2.com"};
1011 params.count_to_fetch = 17;
1012
1013 snippets_fetcher().FetchSnippets(
1014 params, ToSnippetsAvailableCallback(&mock_callback()));
1015
1016 net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher();
1017 ASSERT_THAT(fetcher, NotNull());
1018 std::unique_ptr<base::Value> value =
1019 base::JSONReader::Read(fetcher->upload_data());
1020 ASSERT_TRUE(value) << " failed to parse JSON: "
1021 << PrintToString(fetcher->upload_data());
1022 const base::DictionaryValue* dict = nullptr;
1023 ASSERT_TRUE(value->GetAsDictionary(&dict));
1024 const base::DictionaryValue* local_scoring_params = nullptr;
1025 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params",
1026 &local_scoring_params));
1027 const base::ListValue* content_selectors = nullptr;
1028 ASSERT_TRUE(
1029 local_scoring_params->GetList("content_selectors", &content_selectors));
1030 ASSERT_THAT(content_selectors->GetSize(), Eq(static_cast<size_t>(2)));
1031 const base::DictionaryValue* content_selector = nullptr;
1032 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector));
1033 std::string content_selector_value;
1034 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value));
1035 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com"));
1036 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector));
1037 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value));
1038 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com"));
1039 }
1040
1041 TEST_F(ChromeReaderSnippetsFetcherTest, RetryOnInteractiveRequests) { 991 TEST_F(ChromeReaderSnippetsFetcherTest, RetryOnInteractiveRequests) {
1042 DelegateCallingTestURLFetcherFactory fetcher_factory; 992 DelegateCallingTestURLFetcherFactory fetcher_factory;
1043 NTPSnippetsFetcher::Params params = test_params(); 993 NTPSnippetsFetcher::Params params = test_params();
1044 params.interactive_request = true; 994 params.interactive_request = true;
1045 995
1046 snippets_fetcher().FetchSnippets( 996 snippets_fetcher().FetchSnippets(
1047 params, ToSnippetsAvailableCallback(&mock_callback())); 997 params, ToSnippetsAvailableCallback(&mock_callback()));
1048 998
1049 net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher(); 999 net::TestURLFetcher* fetcher = fetcher_factory.GetLastCreatedFetcher();
1050 ASSERT_THAT(fetcher, NotNull()); 1000 ASSERT_THAT(fetcher, NotNull());
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
1244 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { 1194 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) {
1245 if (fetched_categories) { 1195 if (fetched_categories) {
1246 // Matchers above aren't any more precise than this, so this is sufficient 1196 // Matchers above aren't any more precise than this, so this is sufficient
1247 // for test-failure diagnostics. 1197 // for test-failure diagnostics.
1248 return os << "list with " << fetched_categories->size() << " elements"; 1198 return os << "list with " << fetched_categories->size() << " elements";
1249 } 1199 }
1250 return os << "null"; 1200 return os << "null";
1251 } 1201 }
1252 1202
1253 } // namespace ntp_snippets 1203 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/remote/ntp_snippets_fetcher.cc ('k') | components/ntp_snippets/remote/remote_suggestions_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698