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

Side by Side Diff: url/gurl_unittest.cc

Issue 2956643002: Add GURL::HostNoBracketsPiece() (Closed)
Patch Set: Created 3 years, 6 months 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
« no previous file with comments | « url/gurl.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "url/gurl.h" 10 #include "url/gurl.h"
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after
554 {"", false}, 554 {"", false},
555 {"some random input!", false}, 555 {"some random input!", false},
556 }; 556 };
557 557
558 for (size_t i = 0; i < arraysize(ip_tests); i++) { 558 for (size_t i = 0; i < arraysize(ip_tests); i++) {
559 GURL url(ip_tests[i].spec); 559 GURL url(ip_tests[i].spec);
560 EXPECT_EQ(ip_tests[i].expected_ip, url.HostIsIPAddress()); 560 EXPECT_EQ(ip_tests[i].expected_ip, url.HostIsIPAddress());
561 } 561 }
562 } 562 }
563 563
564 TEST(GURLTest, HostNoBrackets) { 564 // Type-parameterized test for variants of the host returning methods.
565 template <typename StringType>
566 class GURLHostTest : public ::testing::Test {
brettw 2017/06/30 19:33:14 This is a lot of hard-to-read complexity, when I t
Adam Rice 2017/07/03 04:51:51 Thank you! I was so fixated on the type difference
567 public:
568 // The GURL methods are wrapped to deal with having different method names for
569 // the types.
570 StringType host(const GURL& url) { return url.host(); }
571 StringType HostNoBrackets(const GURL& url) { return url.HostNoBrackets(); }
572 };
573
574 template <>
575 class GURLHostTest<base::StringPiece> : public ::testing::Test {
576 public:
577 base::StringPiece host(const GURL& url) { return url.host_piece(); }
578 base::StringPiece HostNoBrackets(const GURL& url) {
579 return url.HostNoBracketsPiece();
580 }
581 };
582
583 using GURLHostTestTypes = ::testing::Types<std::string, base::StringPiece>;
584 TYPED_TEST_CASE(GURLHostTest, GURLHostTestTypes);
585
586 TYPED_TEST(GURLHostTest, HostNoBrackets) {
565 struct TestCase { 587 struct TestCase {
566 const char* input; 588 const char* input;
567 const char* expected_host; 589 const char* expected_host;
568 const char* expected_plainhost; 590 const char* expected_plainhost;
569 } cases[] = { 591 } cases[] = {
570 {"http://www.google.com", "www.google.com", "www.google.com"}, 592 {"http://www.google.com", "www.google.com", "www.google.com"},
571 {"http://[2001:db8::1]/", "[2001:db8::1]", "2001:db8::1"}, 593 {"http://[2001:db8::1]/", "[2001:db8::1]", "2001:db8::1"},
572 {"http://[::]/", "[::]", "::"}, 594 {"http://[::]/", "[::]", "::"},
573 595
574 // Don't require a valid URL, but don't crash either. 596 // Don't require a valid URL, but don't crash either.
575 {"http://[]/", "[]", ""}, 597 {"http://[]/", "[]", ""},
576 {"http://[x]/", "[x]", "x"}, 598 {"http://[x]/", "[x]", "x"},
577 {"http://[x/", "[x", "[x"}, 599 {"http://[x/", "[x", "[x"},
578 {"http://x]/", "x]", "x]"}, 600 {"http://x]/", "x]", "x]"},
579 {"http://[/", "[", "["}, 601 {"http://[/", "[", "["},
580 {"http://]/", "]", "]"}, 602 {"http://]/", "]", "]"},
581 {"", "", ""}, 603 {"", "", ""},
582 }; 604 };
583 for (size_t i = 0; i < arraysize(cases); i++) { 605 for (size_t i = 0; i < arraysize(cases); i++) {
584 GURL url(cases[i].input); 606 GURL url(cases[i].input);
585 EXPECT_EQ(cases[i].expected_host, url.host()); 607 EXPECT_EQ(cases[i].expected_host, this->host(url));
586 EXPECT_EQ(cases[i].expected_plainhost, url.HostNoBrackets()); 608 EXPECT_EQ(cases[i].expected_plainhost, this->HostNoBrackets(url));
587 } 609 }
588 } 610 }
589 611
590 TEST(GURLTest, DomainIs) { 612 TEST(GURLTest, DomainIs) {
591 GURL url_1("http://google.com/foo"); 613 GURL url_1("http://google.com/foo");
592 EXPECT_TRUE(url_1.DomainIs("google.com")); 614 EXPECT_TRUE(url_1.DomainIs("google.com"));
593 615
594 // Subdomain and port are ignored. 616 // Subdomain and port are ignored.
595 GURL url_2("http://www.google.com:99/foo"); 617 GURL url_2("http://www.google.com:99/foo");
596 EXPECT_TRUE(url_2.DomainIs("google.com")); 618 EXPECT_TRUE(url_2.DomainIs("google.com"));
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
823 // A versus B. 845 // A versus B.
824 EXPECT_EQ(test_case.are_equals, 846 EXPECT_EQ(test_case.are_equals,
825 GURL(test_case.url_a).EqualsIgnoringRef(GURL(test_case.url_b))); 847 GURL(test_case.url_a).EqualsIgnoringRef(GURL(test_case.url_b)));
826 // B versus A. 848 // B versus A.
827 EXPECT_EQ(test_case.are_equals, 849 EXPECT_EQ(test_case.are_equals,
828 GURL(test_case.url_b).EqualsIgnoringRef(GURL(test_case.url_a))); 850 GURL(test_case.url_b).EqualsIgnoringRef(GURL(test_case.url_a)));
829 } 851 }
830 } 852 }
831 853
832 } // namespace url 854 } // namespace url
OLDNEW
« no previous file with comments | « url/gurl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698