Commit 6c3d3ae095

6c3d3ae095ca1dd78f11432d8c8523b2f41dac7f

parent: 9cfb50f90b

Verified · cmc

cmc <hello@cleberg.net> · 2026-10-24 03:40 UTC

Make the availability path's transports injectable

RDAPService fetched through URLSession.shared and DomainAvailabilityService
called DNSLookupService directly, so neither could be exercised without
network. Add RDAPTransport and DNSLookup protocols, injected as defaulted
parameters on registrationStatus, ownership, check and suggestions.

Layout: unified · split

DomainDig/DNSLookupService.swift +12
@@ -56,6 +56,12 @@ enum DNSResolverOption: String, CaseIterable, Identifiable {
5656 }
5757}
5858
59/// A single record lookup, injectable so callers that branch on its result can
60/// be exercised without network.
61protocol DNSLookup {
62 func lookup(domain: String, recordType: DNSRecordType) async throws -> [DNSRecord]
63}
64
5965struct DNSLookupService {
6066 private static let internetClass = 1
6167
@@ -532,6 +538,12 @@ struct DNSLookupService {
532538 }
533539}
534540
541extension DNSLookupService: DNSLookup {
542 func lookup(domain: String, recordType: DNSRecordType) async throws -> [DNSRecord] {
543 try await Self.lookup(domain: domain, recordType: recordType)
544 }
545}
546
535547private struct DNSLookupResponse {
536548 let answers: [CloudflareDNSResponse.CloudflareDNSAnswer]
537549 let authenticatedData: Bool
DomainDig/DomainAvailabilityService.swift +19 −10
@@ -3,14 +3,18 @@ import Foundation
33struct DomainAvailabilityService {
44 private static let suggestionTLDs = ["net", "io", "dev", "app", "co", "org"]
55
6 static func check(domain: String) async -> DomainAvailabilityResult {
6 static func check(
7 domain: String,
8 rdapTransport: RDAPTransport = URLSessionRDAPTransport(),
9 dns: DNSLookup = DNSLookupService()
10 ) async -> DomainAvailabilityResult {
711 let normalizedDomain = normalize(domain)
812 guard !normalizedDomain.isEmpty else {
913 return DomainAvailabilityResult(domain: domain, status: .unknown)
1014 }
1115
1216 // RDAP is the registry's own record, so it settles both directions.
13 if let registryStatus = await checkViaRDAP(domain: normalizedDomain) {
17 if let registryStatus = await checkViaRDAP(domain: normalizedDomain, transport: rdapTransport) {
1418 return DomainAvailabilityResult(domain: normalizedDomain, status: registryStatus)
1519 }
1620
@@ -18,11 +22,16 @@ struct DomainAvailabilityService {
1822 // resolves is registered. Absence of records is not evidence of
1923 // availability, because the DoH layer drops the response code, so
2024 // NXDOMAIN and a resolver failure arrive here identically.
21 let fallbackStatus = await checkViaDNSFallback(domain: normalizedDomain)
25 let fallbackStatus = await checkViaDNSFallback(domain: normalizedDomain, dns: dns)
2226 return DomainAvailabilityResult(domain: normalizedDomain, status: fallbackStatus)
2327 }
2428
25 static func suggestions(for domain: String, limit: Int = 6) async -> [DomainSuggestionResult] {
29 static func suggestions(
30 for domain: String,
31 limit: Int = 6,
32 rdapTransport: RDAPTransport = URLSessionRDAPTransport(),
33 dns: DNSLookup = DNSLookupService()
34 ) async -> [DomainSuggestionResult] {
2635 let normalizedDomain = normalize(domain)
2736 let candidates = suggestionCandidates(for: normalizedDomain, limit: limit)
2837 guard !candidates.isEmpty else { return [] }
@@ -30,20 +39,20 @@ struct DomainAvailabilityService {
3039 var results: [DomainSuggestionResult] = []
3140 for candidate in candidates {
3241 if Task.isCancelled { break }
33 let result = await check(domain: candidate)
42 let result = await check(domain: candidate, rdapTransport: rdapTransport, dns: dns)
3443 results.append(DomainSuggestionResult(domain: result.domain, status: result.status))
3544 }
3645 return results
3746 }
3847
39 private static func checkViaRDAP(domain: String) async -> DomainAvailabilityStatus? {
40 let status = await RDAPService.registrationStatus(for: domain)
48 private static func checkViaRDAP(domain: String, transport: RDAPTransport) async -> DomainAvailabilityStatus? {
49 let status = await RDAPService.registrationStatus(for: domain, transport: transport)
4150 return status
4251 }
4352
44 private static func checkViaDNSFallback(domain: String) async -> DomainAvailabilityStatus {
53 private static func checkViaDNSFallback(domain: String, dns: DNSLookup) async -> DomainAvailabilityStatus {
4554 do {
46 let aRecords = try await DNSLookupService.lookup(domain: domain, recordType: .A)
55 let aRecords = try await dns.lookup(domain: domain, recordType: .A)
4756 if !aRecords.isEmpty {
4857 return .registered
4958 }
@@ -52,7 +61,7 @@ struct DomainAvailabilityService {
5261 }
5362
5463 do {
55 let nsRecords = try await DNSLookupService.lookup(domain: domain, recordType: .NS)
64 let nsRecords = try await dns.lookup(domain: domain, recordType: .NS)
5665 if !nsRecords.isEmpty {
5766 return .registered
5867 }
DomainDig/RDAPService.swift +24 −6
@@ -1,13 +1,28 @@
11import Foundation
22
3/// The HTTP fetch behind an RDAP query, injectable so the status mapping can be
4/// exercised without network.
5protocol RDAPTransport {
6 func fetch(_ request: URLRequest) async throws -> (Data, URLResponse)
7}
8
9struct URLSessionRDAPTransport: RDAPTransport {
10 func fetch(_ request: URLRequest) async throws -> (Data, URLResponse) {
11 try await URLSession.shared.data(for: request)
12 }
13}
14
315enum RDAPService {
416 /// The registry's answer about `domain`, or nil when the registry did not
517 /// give one.
6 static func registrationStatus(for domain: String) async -> DomainAvailabilityStatus? {
18 static func registrationStatus(
19 for domain: String,
20 transport: RDAPTransport = URLSessionRDAPTransport()
21 ) async -> DomainAvailabilityStatus? {
722 let normalizedDomain = normalize(domain)
823 guard !normalizedDomain.isEmpty else { return nil }
924
10 switch await fetchRDAPResponse(for: normalizedDomain) {
25 switch await fetchRDAPResponse(for: normalizedDomain, transport: transport) {
1126 case .found:
1227 return .registered
1328 case .notRegistered:
@@ -29,13 +44,16 @@ enum RDAPService {
2944 return host != bootstrapHost && !host.hasSuffix(".\(bootstrapHost)")
3045 }
3146
32 static func ownership(for domain: String) async -> ServiceResult<DomainOwnership> {
47 static func ownership(
48 for domain: String,
49 transport: RDAPTransport = URLSessionRDAPTransport()
50 ) async -> ServiceResult<DomainOwnership> {
3351 let normalizedDomain = normalize(domain)
3452 guard !normalizedDomain.isEmpty else {
3553 return .empty("Unavailable")
3654 }
3755
38 switch await fetchRDAPResponse(for: normalizedDomain) {
56 switch await fetchRDAPResponse(for: normalizedDomain, transport: transport) {
3957 case let .found(response):
4058 let ownership = DomainOwnership(
4159 registrar: response.registrarName,
@@ -77,7 +95,7 @@ private enum RDAPFetchOutcome {
7795 case failed(String)
7896}
7997
80private func fetchRDAPResponse(for domain: String) async -> RDAPFetchOutcome {
98private func fetchRDAPResponse(for domain: String, transport: RDAPTransport) async -> RDAPFetchOutcome {
8199 let startedAt = DomainDebugLog.signpostStart("RDAP.fetch", domain: domain)
82100 guard let url = URL(string: "https://\(bootstrapHost)/domain/\(domain)") else {
83101 return .failed("Unavailable")
@@ -88,7 +106,7 @@ private func fetchRDAPResponse(for domain: String) async -> RDAPFetchOutcome {
88106 request.setValue("application/rdap+json, application/json", forHTTPHeaderField: "Accept")
89107 DomainDebugLog.debug("RDAP.request url=\(url.absoluteString) timeout=8")
90108
91 let (data, response) = try await URLSession.shared.data(for: request)
109 let (data, response) = try await transport.fetch(request)
92110 guard let httpResponse = response as? HTTPURLResponse else {
93111 DomainDebugLog.error("RDAP.badResponse domain=\(domain) response=nil")
94112 return .failed(URLError(.badServerResponse).localizedDescription)