This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 448
Fix #1042: Implement DomainAndRegistry for eTLD+1 #5239
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,29 @@ import UIKit | |
import XCTest | ||
@testable import Shared | ||
|
||
private extension String { | ||
var isIPv6: Bool { | ||
return self.contains(":") | ||
} | ||
|
||
var baseDomain: String? { | ||
guard !NSURL.isHostIPAddress(host: self) else { return nil } | ||
|
||
// If this is just a hostname and not a FQDN, use the entire hostname. | ||
if !self.contains(".") { | ||
return self | ||
} | ||
|
||
let registry = NSURL.domainAndRegistry(host: self) | ||
return registry.isEmpty ? nil : registry | ||
} | ||
|
||
var publicSuffix: String? { | ||
let registry = NSURL.registry(host: self) | ||
return registry.isEmpty ? nil : registry | ||
} | ||
} | ||
|
||
class NSURLExtensionsTests: XCTestCase { | ||
func testRemovesHTTPFromURL() { | ||
let url = URL(string: "http://google.com") | ||
|
@@ -76,66 +99,78 @@ class NSURLExtensionsTests: XCTestCase { | |
let url = "http://a.bbc.co.uk".asURL! | ||
let expected = url.publicSuffix! | ||
XCTAssertEqual("co.uk", expected) | ||
XCTAssertEqual("co.uk", url.host?.publicSuffix!) | ||
} | ||
|
||
func testBaseDomainWithTrailingDot() { | ||
var url = URL(string: "https://test.domain.com") | ||
XCTAssertEqual(url?.baseDomain, "domain.com") | ||
XCTAssertEqual(url?.host?.baseDomain, "domain.com") | ||
|
||
url = URL(string: "https://test.domain.com.") | ||
XCTAssertEqual(url?.baseDomain, "domain.com") | ||
XCTAssertEqual(url?.baseDomain, "domain.com.") | ||
XCTAssertEqual(url?.host?.baseDomain, "domain.com.") | ||
|
||
url = URL(string: "https://test.domain.com..") | ||
XCTAssertEqual(url?.baseDomain, nil) | ||
XCTAssertEqual(url?.host?.baseDomain, nil) | ||
|
||
url = URL(string: "https://foo") | ||
XCTAssertEqual(url?.baseDomain, "foo") | ||
XCTAssertEqual(url?.host?.baseDomain, "foo") | ||
|
||
url = URL(string: "https://foo.") | ||
XCTAssertEqual(url?.baseDomain, ".foo") | ||
XCTAssertEqual(url?.host?.baseDomain, nil) | ||
|
||
url = URL(string: "https://.") | ||
XCTAssertEqual(url?.baseDomain, "") | ||
XCTAssertEqual(url?.baseDomain, nil) | ||
XCTAssertEqual(url?.host?.baseDomain, nil) | ||
} | ||
|
||
func testCanadaComputers() { | ||
let url = "http://m.canadacomputers.com".asURL! | ||
let actual = url.baseDomain! | ||
let actual = url.baseDomain | ||
XCTAssertEqual("canadacomputers.com", actual) | ||
XCTAssertEqual("canadacomputers.com", url.host?.baseDomain) | ||
} | ||
|
||
func testMultipleSuffixesInsideURL() { | ||
let url = "http://com:org@m.canadacomputers.co.uk".asURL! | ||
let actual = url.baseDomain! | ||
let actual = url.baseDomain | ||
XCTAssertEqual("canadacomputers.co.uk", actual) | ||
XCTAssertEqual("canadacomputers.co.uk", url.host?.baseDomain) | ||
} | ||
|
||
func testNormalBaseDomainWithManySubdomains() { | ||
// TLD Entry: co.uk | ||
let url = "http://a.b.c.d.bbc.co.uk".asURL! | ||
let expected = url.publicSuffix! | ||
let expected = url.publicSuffix | ||
XCTAssertEqual("co.uk", expected) | ||
XCTAssertEqual("co.uk", url.host?.publicSuffix) | ||
} | ||
|
||
func testWildCardDomainWithSingleSubdomain() { | ||
// TLD Entry: *.kawasaki.jp | ||
let url = "http://a.kawasaki.jp".asURL! | ||
let expected = url.publicSuffix! | ||
XCTAssertEqual("a.kawasaki.jp", expected) | ||
let expected = url.publicSuffix | ||
XCTAssertEqual(expected, nil) | ||
XCTAssertEqual(url.host?.publicSuffix, nil) | ||
} | ||
|
||
func testWildCardDomainWithManySubdomains() { | ||
// TLD Entry: *.kawasaki.jp | ||
let url = "http://a.b.c.d.kawasaki.jp".asURL! | ||
let expected = url.publicSuffix! | ||
let expected = url.publicSuffix | ||
XCTAssertEqual("d.kawasaki.jp", expected) | ||
XCTAssertEqual("d.kawasaki.jp", url.host?.publicSuffix) | ||
} | ||
|
||
func testExceptionDomain() { | ||
// TLD Entry: !city.kawasaki.jp | ||
let url = "http://city.kawasaki.jp".asURL! | ||
let expected = url.publicSuffix! | ||
XCTAssertEqual("kawasaki.jp", expected) | ||
XCTAssertEqual("kawasaki.jp", url.host?.publicSuffix) | ||
} | ||
|
||
//MARK: Base Domain | ||
|
@@ -144,41 +179,47 @@ class NSURLExtensionsTests: XCTestCase { | |
let url = "http://bbc.co.uk".asURL! | ||
let expected = url.baseDomain! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above and all the rest below. |
||
XCTAssertEqual("bbc.co.uk", expected) | ||
XCTAssertEqual("bbc.co.uk", url.host?.baseDomain) | ||
} | ||
|
||
func testNormalBaseSubdomainWithAdditionalSubdomain() { | ||
// TLD Entry: co.uk | ||
let url = "http://a.bbc.co.uk".asURL! | ||
let expected = url.baseDomain! | ||
XCTAssertEqual("bbc.co.uk", expected) | ||
XCTAssertEqual("bbc.co.uk", url.host?.baseDomain) | ||
} | ||
|
||
func testBaseDomainForWildcardDomain() { | ||
// TLD Entry: *.kawasaki.jp | ||
let url = "http://a.b.kawasaki.jp".asURL! | ||
let expected = url.baseDomain! | ||
XCTAssertEqual("a.b.kawasaki.jp", expected) | ||
XCTAssertEqual("a.b.kawasaki.jp", url.host?.baseDomain) | ||
} | ||
|
||
func testBaseDomainForWildcardDomainWithAdditionalSubdomain() { | ||
// TLD Entry: *.kawasaki.jp | ||
let url = "http://a.b.c.kawasaki.jp".asURL! | ||
let expected = url.baseDomain! | ||
XCTAssertEqual("b.c.kawasaki.jp", expected) | ||
XCTAssertEqual("b.c.kawasaki.jp", url.host?.baseDomain) | ||
} | ||
|
||
func testBaseDomainForExceptionDomain() { | ||
// TLD Entry: !city.kawasaki.jp | ||
let url = "http://city.kawasaki.jp".asURL! | ||
let expected = url.baseDomain! | ||
XCTAssertEqual("city.kawasaki.jp", expected) | ||
XCTAssertEqual("city.kawasaki.jp", url.host?.baseDomain) | ||
} | ||
|
||
func testBaseDomainForExceptionDomainWithAdditionalSubdomain() { | ||
// TLD Entry: !city.kawasaki.jp | ||
let url = "http://a.city.kawasaki.jp".asURL! | ||
let expected = url.baseDomain! | ||
XCTAssertEqual("city.kawasaki.jp", expected) | ||
XCTAssertEqual("city.kawasaki.jp", url.host?.baseDomain) | ||
} | ||
|
||
func testBugzillaURLDomain() { | ||
|
@@ -195,6 +236,7 @@ class NSURLExtensionsTests: XCTestCase { | |
let url = "http://[::1]/foo/bar".asURL! | ||
XCTAssertTrue(url.isIPv6) | ||
XCTAssertNil(url.baseDomain) | ||
XCTAssertNil("[::1]".baseDomain) | ||
XCTAssertEqual(url.normalizedHost()!, "[::1]") | ||
} | ||
|
||
Brandon-T marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe remove that
!
You're testing it's not nil so I don't think you should put that ! there plus you don't really need it. Both would result in a failed test but one will give you some better test information.