Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Sources/PerfectFileMakerDataAPI/FMPSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ actor FMPSession {
private static func login(host: String, port: Int, database: String,
username: String, password: String,
urlSession: URLSession) async throws -> String {
let scheme = port == 443 ? "https" : "http"
let db = database.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? database
guard let url = URL(string: "\(scheme)://\(host):\(port)/fmi/data/v1/databases/\(db)/sessions") else {
guard let url = URL(string: "https://\(host):\(port)/fmi/data/v1/databases/\(db)/sessions") else {
throw FMPError.invalidResponse
}
var request = URLRequest(url: url)
Expand Down
9 changes: 3 additions & 6 deletions Sources/PerfectFileMakerDataAPI/FileMakerServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public struct FileMakerServer: Sendable {

/// Returns the names of all databases available to these credentials.
public func databaseNames() async throws -> [String] {
let scheme = port == 443 ? "https" : "http"
guard let url = URL(string: "\(scheme)://\(host):\(port)/fmi/data/v1/databases") else {
guard let url = URL(string: "https://\(host):\(port)/fmi/data/v1/databases") else {
throw FMPError.invalidResponse
}
var req = URLRequest(url: url)
Expand Down Expand Up @@ -201,9 +200,8 @@ public struct FileMakerServer: Sendable {
/// Closes the current session on the server and clears the cached token.
public func logout() async throws {
guard let token = await session.currentToken() else { return }
let scheme = port == 443 ? "https" : "http"
let db = encoded(database)
guard let url = URL(string: "\(scheme)://\(host):\(port)/fmi/data/v1/databases/\(db)/sessions/\(token)") else {
guard let url = URL(string: "https://\(host):\(port)/fmi/data/v1/databases/\(db)/sessions/\(token)") else {
return
}
var req = URLRequest(url: url)
Expand All @@ -216,8 +214,7 @@ public struct FileMakerServer: Sendable {
// MARK: - Private helpers

var baseURL: String {
let scheme = port == 443 ? "https" : "http"
return "\(scheme)://\(host):\(port)/fmi/data/v1/databases/\(encoded(database))"
return "https://\(host):\(port)/fmi/data/v1/databases/\(encoded(database))"
}

func encoded(_ segment: String) -> String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Testing
import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
@testable import PerfectFileMakerDataAPI

// MARK: - FMPQuery
Expand Down Expand Up @@ -187,3 +190,51 @@ import Foundation
#expect(FMPFieldType("calculation") == .unknown)
}
}

// MARK: - Transport

/// Records each request's URL and answers with HTTP 500, so a call fails right after its first request.
final class RecordingURLProtocol: URLProtocol {
nonisolated(unsafe) private static var urls: [URL] = []
private static let lock = NSLock()

static func reset() { lock.withLock { urls = [] } }
static func recorded() -> [URL] { lock.withLock { urls } }

override class func canInit(with request: URLRequest) -> Bool { true }
override class func canonicalRequest(for request: URLRequest) -> URLRequest { request }

override func startLoading() {
if let url = request.url {
Self.lock.withLock { Self.urls.append(url) }
}
let response = HTTPURLResponse(url: request.url!, statusCode: 500, httpVersion: nil, headerFields: nil)!
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
client?.urlProtocol(self, didLoad: Data("{}".utf8))
client?.urlProtocolDidFinishLoading(self)
}

override func stopLoading() {}
}

@Suite("Transport", .serialized) struct TransportTests {

@Test(arguments: [443, 8443, 80])
func everyRequestUsesHTTPS(port: Int) async {
let config = URLSessionConfiguration.ephemeral
config.protocolClasses = [RecordingURLProtocol.self]
let server = FileMakerServer(host: "fm.example.com", port: port, database: "My DB",
username: "u", password: "p",
urlSession: URLSession(configuration: config))
RecordingURLProtocol.reset()

#expect(server.baseURL.hasPrefix("https://fm.example.com:\(port)/"))
_ = try? await server.databaseNames() // no session needed
_ = try? await server.getRecords(layout: "L") // logs in first

let urls = RecordingURLProtocol.recorded()
#expect(urls.count == 2)
#expect(urls.allSatisfy { $0.scheme == "https" && $0.port == port })
#expect(urls.last?.path == "/fmi/data/v1/databases/My DB/sessions")
}
}
Loading