DomainDigTests/InspectionInputTests.swift
81 lines · 3155 bytes
16 symbols in this file
DomainHostURLTeststestBuildsHTTPSURLForAPlainHosttestBuildsHTTPURLForAPlainHosttestTrimsSurroundingWhitespacetestRejectsAnInteriorSpacetestRejectsAnInteriorTabtestRejectsAControlCharactertestRejectsInputWithNoHostComponenttestEncodesAnInternationalizedHostRDAPRegistryOriginTeststestRegistryResponseCountsAsAnAnswertestBootstrapResponseDoesNotCountAsAnAnswertestBootstrapSubdomainDoesNotCountAsAnAnswertestHostMatchIsCaseInsensitivetestMissingResponseURLDoesNotCountAsAnAnswertestLookalikeHostCountsAsAnAnswer
1import XCTest
2@testable import DomainDig
3
4/// The inspected domain is free text, and the section services used to force it
5/// into a URL with `!`. These pin the two pure helpers that replaced that.
6final class DomainHostURLTests: XCTestCase {
7
8 func testBuildsHTTPSURLForAPlainHost() {
9 XCTAssertEqual(DomainHostURL.https("example.com")?.absoluteString, "https://example.com")
10 }
11
12 func testBuildsHTTPURLForAPlainHost() {
13 XCTAssertEqual(DomainHostURL.http("example.com")?.absoluteString, "http://example.com")
14 }
15
16 func testTrimsSurroundingWhitespace() {
17 XCTAssertEqual(DomainHostURL.https(" example.com\n")?.absoluteString, "https://example.com")
18 }
19
20 /// The crash this replaced: `URL(string: "https://exa mple.com")` is nil, so
21 /// the force-unwrap trapped. Reachable by typing `example .com`, or by
22 /// separating bulk input with spaces instead of commas or newlines.
23 func testRejectsAnInteriorSpace() {
24 XCTAssertNil(DomainHostURL.https("exa mple.com"))
25 }
26
27 func testRejectsAnInteriorTab() {
28 XCTAssertNil(DomainHostURL.https("exa\tmple.com"))
29 }
30
31 func testRejectsAControlCharacter() {
32 XCTAssertNil(DomainHostURL.https("exa\u{0}mple.com"))
33 }
34
35 func testRejectsInputWithNoHostComponent() {
36 XCTAssertNil(DomainHostURL.https(""))
37 XCTAssertNil(DomainHostURL.https(" "))
38 XCTAssertNil(DomainHostURL.https("/"))
39 XCTAssertNil(DomainHostURL.https(":443"))
40 }
41
42 func testEncodesAnInternationalizedHost() {
43 XCTAssertEqual(DomainHostURL.https("münchen.de")?.host, "xn--mnchen-3ya.de")
44 }
45}
46
47/// `rdap.org` answers 404 both for a domain its registry does not hold and for a
48/// TLD it knows no RDAP server for. Only the first means "available", so the
49/// availability check turns on which server answered.
50final class RDAPRegistryOriginTests: XCTestCase {
51
52 func testRegistryResponseCountsAsAnAnswer() {
53 let url = URL(string: "https://rdap.verisign.com/com/v1/domain/example.com")
54 XCTAssertTrue(RDAPService.answeredByRegistry(responseURL: url))
55 }
56
57 func testBootstrapResponseDoesNotCountAsAnAnswer() {
58 let url = URL(string: "https://rdap.org/domain/example.invalidtld")
59 XCTAssertFalse(RDAPService.answeredByRegistry(responseURL: url))
60 }
61
62 func testBootstrapSubdomainDoesNotCountAsAnAnswer() {
63 let url = URL(string: "https://www.rdap.org/domain/example.invalidtld")
64 XCTAssertFalse(RDAPService.answeredByRegistry(responseURL: url))
65 }
66
67 func testHostMatchIsCaseInsensitive() {
68 let url = URL(string: "https://RDAP.ORG/domain/example.com")
69 XCTAssertFalse(RDAPService.answeredByRegistry(responseURL: url))
70 }
71
72 func testMissingResponseURLDoesNotCountAsAnAnswer() {
73 XCTAssertFalse(RDAPService.answeredByRegistry(responseURL: nil))
74 }
75
76 /// A host that merely ends in the bootstrap name is a different domain.
77 func testLookalikeHostCountsAsAnAnswer() {
78 let url = URL(string: "https://notrdap.org/domain/example.com")
79 XCTAssertTrue(RDAPService.answeredByRegistry(responseURL: url))
80 }
81}