OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/url_matcher/url_matcher.h" | 5 #include "components/url_matcher/url_matcher.h" |
6 | 6 |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "url/gurl.h" | 9 #include "url/gurl.h" |
10 | 10 |
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
496 regex_conditions.clear(); | 496 regex_conditions.clear(); |
497 URLMatcherCondition r2 = factory.CreateOriginAndPathMatchesCondition("b[a]r"); | 497 URLMatcherCondition r2 = factory.CreateOriginAndPathMatchesCondition("b[a]r"); |
498 regex_conditions.insert(r2); | 498 regex_conditions.insert(r2); |
499 scoped_refptr<URLMatcherConditionSet> condition_set7( | 499 scoped_refptr<URLMatcherConditionSet> condition_set7( |
500 new URLMatcherConditionSet(1, regex_conditions)); | 500 new URLMatcherConditionSet(1, regex_conditions)); |
501 EXPECT_FALSE(condition_set7->IsMatch(matching_patterns, url1)); | 501 EXPECT_FALSE(condition_set7->IsMatch(matching_patterns, url1)); |
502 matching_patterns.insert(r2.string_pattern()->id()); | 502 matching_patterns.insert(r2.string_pattern()->id()); |
503 EXPECT_TRUE(condition_set7->IsMatch(matching_patterns, url1)); | 503 EXPECT_TRUE(condition_set7->IsMatch(matching_patterns, url1)); |
504 } | 504 } |
505 | 505 |
506 namespace { | |
507 | |
508 bool IsQueryMatch( | |
509 std::string url_query, | |
510 std::string key, | |
511 URLQueryElementMatcherCondition::QueryElementType query_element_type, | |
512 std::string value, | |
513 URLQueryElementMatcherCondition::QueryValueMatchType query_value_match_type, | |
514 URLQueryElementMatcherCondition::Type match_type) { | |
515 URLMatcherConditionFactory factory; | |
516 | |
517 URLMatcherCondition m1 = factory.CreateHostSuffixCondition("example.com"); | |
518 URLMatcherCondition m2 = factory.CreatePathContainsCondition("foo"); | |
519 URLMatcherConditionSet::Conditions conditions; | |
520 conditions.insert(m1); | |
521 conditions.insert(m2); | |
522 | |
523 URLQueryElementMatcherCondition q1(key, | |
524 value, | |
525 query_value_match_type, | |
526 query_element_type, | |
527 match_type, | |
528 &factory); | |
529 URLMatcherConditionSet::QueryConditions query_conditions; | |
530 query_conditions.insert(q1); | |
531 | |
532 scoped_ptr<URLMatcherSchemeFilter> scheme_filter; | |
533 scoped_ptr<URLMatcherPortFilter> port_filter; | |
534 | |
535 scoped_refptr<URLMatcherConditionSet> condition_set( | |
536 new URLMatcherConditionSet(1, | |
537 conditions, | |
538 query_conditions, | |
539 scheme_filter.Pass(), | |
540 port_filter.Pass())); | |
541 | |
542 GURL url("http://www.example.com/foo?" + url_query); | |
543 | |
544 URLMatcher matcher; | |
545 URLMatcherConditionSet::Vector vector; | |
546 vector.push_back(condition_set); | |
547 matcher.AddConditionSets(vector); | |
548 | |
549 return matcher.MatchURL(url).size() == 1; | |
550 } | |
551 | |
552 } | |
Joao da Silva
2014/04/07 15:12:39
// namespace
kaliamoorthi
2014/04/08 09:28:53
Done.
| |
553 | |
554 TEST(URLMatcherConditionSetTest, QueryMatching) { | |
555 EXPECT_TRUE( | |
556 IsQueryMatch("a=foo&b=foo&a=barr", | |
557 "a", | |
558 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
559 "bar", | |
560 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
561 URLQueryElementMatcherCondition::MATCH_ANY)); | |
562 EXPECT_FALSE( | |
563 IsQueryMatch("a=foo&b=foo&a=barr", | |
564 "a", | |
565 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
566 "bar", | |
567 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
568 URLQueryElementMatcherCondition::MATCH_ANY)); | |
569 EXPECT_TRUE( | |
570 IsQueryMatch("a=foo&b=foo&a=barr", | |
571 "a", | |
572 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
573 "bar", | |
574 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
575 URLQueryElementMatcherCondition::MATCH_ANY)); | |
576 EXPECT_FALSE( | |
577 IsQueryMatch("a=foo&b=foo&a=barr", | |
578 "a", | |
579 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
580 "bar", | |
581 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
582 URLQueryElementMatcherCondition::MATCH_ANY)); | |
583 EXPECT_TRUE( | |
584 IsQueryMatch("a&b=foo&a=barr", | |
585 "a", | |
586 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
587 "bar", | |
588 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
589 URLQueryElementMatcherCondition::MATCH_ANY)); | |
590 EXPECT_FALSE( | |
591 IsQueryMatch("a=foo&b=foo&a=barr", | |
592 "a", | |
593 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
594 "bar", | |
595 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
596 URLQueryElementMatcherCondition::MATCH_ANY)); | |
597 | |
598 EXPECT_FALSE( | |
599 IsQueryMatch("a=foo&b=foo&a=bar", | |
600 "a", | |
601 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
602 "bar", | |
603 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
604 URLQueryElementMatcherCondition::MATCH_ALL)); | |
605 EXPECT_TRUE( | |
606 IsQueryMatch("a=bar&b=foo&a=bar", | |
607 "a", | |
608 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
609 "bar", | |
610 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
611 URLQueryElementMatcherCondition::MATCH_ALL)); | |
612 EXPECT_TRUE( | |
613 IsQueryMatch("a=bar&b=foo&a=bar", | |
614 "b", | |
615 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
616 "foo", | |
617 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
618 URLQueryElementMatcherCondition::MATCH_ALL)); | |
619 EXPECT_FALSE( | |
620 IsQueryMatch("a=bar&b=foo&a=bar", | |
621 "b", | |
622 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
623 "goo", | |
624 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
625 URLQueryElementMatcherCondition::MATCH_ALL)); | |
626 EXPECT_FALSE( | |
627 IsQueryMatch("a=bar&b=foo&a=bar", | |
628 "c", | |
629 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
630 "goo", | |
631 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
632 URLQueryElementMatcherCondition::MATCH_ALL)); | |
633 EXPECT_TRUE( | |
634 IsQueryMatch("a=foo1&b=foo&a=foo2", | |
635 "a", | |
636 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
637 "foo", | |
638 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
639 URLQueryElementMatcherCondition::MATCH_ALL)); | |
640 EXPECT_FALSE( | |
641 IsQueryMatch("a=foo1&b=foo&a=fo02", | |
642 "a", | |
643 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
644 "foo", | |
645 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
646 URLQueryElementMatcherCondition::MATCH_ALL)); | |
647 EXPECT_TRUE( | |
648 IsQueryMatch("a&b=foo&a", | |
649 "a", | |
650 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
651 "foo", | |
652 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
653 URLQueryElementMatcherCondition::MATCH_ALL)); | |
654 EXPECT_TRUE( | |
655 IsQueryMatch("alt&b=foo", | |
656 "a", | |
657 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
658 "foo", | |
659 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
660 URLQueryElementMatcherCondition::MATCH_ALL)); | |
661 EXPECT_TRUE( | |
662 IsQueryMatch("b=foo&a", | |
663 "a", | |
664 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
665 "foo", | |
666 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
667 URLQueryElementMatcherCondition::MATCH_ALL)); | |
668 EXPECT_FALSE( | |
669 IsQueryMatch("b=foo", | |
670 "a", | |
671 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
672 "foo", | |
673 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
674 URLQueryElementMatcherCondition::MATCH_ALL)); | |
675 EXPECT_TRUE( | |
676 IsQueryMatch("b=foo&a", | |
677 "a", | |
678 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
679 "foo", | |
680 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
681 URLQueryElementMatcherCondition::MATCH_ALL)); | |
682 | |
683 EXPECT_TRUE( | |
684 IsQueryMatch("a=foo&b=foo&a=bar", | |
685 "a", | |
686 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
687 "foo", | |
688 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
689 URLQueryElementMatcherCondition::MATCH_FIRST)); | |
690 EXPECT_FALSE( | |
691 IsQueryMatch("a=foo&b=foo&a=bar", | |
692 "a", | |
693 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
694 "bar", | |
695 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
696 URLQueryElementMatcherCondition::MATCH_FIRST)); | |
697 EXPECT_TRUE( | |
698 IsQueryMatch("a=foo1&b=foo&a=bar", | |
699 "a", | |
700 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
701 "foo", | |
702 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
703 URLQueryElementMatcherCondition::MATCH_FIRST)); | |
704 EXPECT_FALSE( | |
705 IsQueryMatch("a=foo1&b=foo&a=bar", | |
706 "a", | |
707 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
708 "foo", | |
709 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
710 URLQueryElementMatcherCondition::MATCH_FIRST)); | |
711 EXPECT_TRUE( | |
712 IsQueryMatch("a&b=foo&a=bar", | |
713 "a", | |
714 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
715 "foo", | |
716 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
717 URLQueryElementMatcherCondition::MATCH_FIRST)); | |
718 EXPECT_TRUE( | |
719 IsQueryMatch("alt&b=foo&a=bar", | |
720 "a", | |
721 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
722 "foo", | |
723 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
724 URLQueryElementMatcherCondition::MATCH_FIRST)); | |
725 EXPECT_FALSE( | |
726 IsQueryMatch("alt&b=foo&a=bar", | |
727 "a", | |
728 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
729 "foo", | |
730 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
731 URLQueryElementMatcherCondition::MATCH_FIRST)); | |
732 | |
733 EXPECT_FALSE( | |
734 IsQueryMatch("a=foo&b=foo&a=bar", | |
735 "a", | |
736 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
737 "foo", | |
738 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
739 URLQueryElementMatcherCondition::MATCH_LAST)); | |
740 EXPECT_TRUE( | |
741 IsQueryMatch("a=foo&b=foo&a=bar", | |
742 "a", | |
743 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
744 "bar", | |
745 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
746 URLQueryElementMatcherCondition::MATCH_LAST)); | |
747 EXPECT_FALSE( | |
748 IsQueryMatch("a=foo1&b=foo&a=bar", | |
749 "a", | |
750 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
751 "foo", | |
752 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
753 URLQueryElementMatcherCondition::MATCH_LAST)); | |
754 EXPECT_TRUE( | |
755 IsQueryMatch("a=foo1&b=foo&a=bar1", | |
756 "a", | |
757 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY_VALUE, | |
758 "bar", | |
759 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
760 URLQueryElementMatcherCondition::MATCH_LAST)); | |
761 EXPECT_FALSE( | |
762 IsQueryMatch("a&b=foo&a=bar", | |
763 "a", | |
764 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
765 "foo", | |
766 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
767 URLQueryElementMatcherCondition::MATCH_LAST)); | |
768 EXPECT_TRUE( | |
769 IsQueryMatch("b=foo&alt", | |
770 "a", | |
771 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
772 "foo", | |
773 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_PREFIX, | |
774 URLQueryElementMatcherCondition::MATCH_LAST)); | |
775 EXPECT_FALSE( | |
776 IsQueryMatch("b=foo&alt", | |
777 "a", | |
778 URLQueryElementMatcherCondition::ELEMENT_TYPE_KEY, | |
779 "foo", | |
780 URLQueryElementMatcherCondition::QUERY_VALUE_MATCH_EXACT, | |
781 URLQueryElementMatcherCondition::MATCH_LAST)); | |
782 } | |
506 | 783 |
507 // | 784 // |
508 // URLMatcher | 785 // URLMatcher |
509 // | 786 // |
510 | 787 |
511 TEST(URLMatcherTest, FullTest) { | 788 TEST(URLMatcherTest, FullTest) { |
512 GURL url1("http://www.example.com/foo?bar=1"); | 789 GURL url1("http://www.example.com/foo?bar=1"); |
513 GURL url2("http://foo.example.com/index.html"); | 790 GURL url2("http://foo.example.com/index.html"); |
514 | 791 |
515 URLMatcher matcher; | 792 URLMatcher matcher; |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
673 conditions.insert(factory->CreateOriginAndPathMatchesCondition("val")); | 950 conditions.insert(factory->CreateOriginAndPathMatchesCondition("val")); |
674 const int kConditionSetId = 1; | 951 const int kConditionSetId = 1; |
675 URLMatcherConditionSet::Vector insert; | 952 URLMatcherConditionSet::Vector insert; |
676 insert.push_back(make_scoped_refptr( | 953 insert.push_back(make_scoped_refptr( |
677 new URLMatcherConditionSet(kConditionSetId, conditions))); | 954 new URLMatcherConditionSet(kConditionSetId, conditions))); |
678 matcher.AddConditionSets(insert); | 955 matcher.AddConditionSets(insert); |
679 EXPECT_EQ(0u, matcher.MatchURL(url).size()); | 956 EXPECT_EQ(0u, matcher.MatchURL(url).size()); |
680 } | 957 } |
681 | 958 |
682 } // namespace url_matcher | 959 } // namespace url_matcher |
OLD | NEW |