From 9976ccf4aaceab5ea603fbeaf30f156337722a1b Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Sun, 28 Jul 2024 00:12:12 -0600 Subject: add more tests --- .../swiftGopherClientTests.swift | 71 ++++++++++++++++++---- 1 file changed, 58 insertions(+), 13 deletions(-) diff --git a/Tests/swiftGopherClientTests/swiftGopherClientTests.swift b/Tests/swiftGopherClientTests/swiftGopherClientTests.swift index 707ff32..088124d 100644 --- a/Tests/swiftGopherClientTests/swiftGopherClientTests.swift +++ b/Tests/swiftGopherClientTests/swiftGopherClientTests.swift @@ -12,33 +12,78 @@ import XCTest final class GopherClientTests: XCTestCase { + var client: GopherClient! + override func setUp() { super.setUp() + client = GopherClient() } override func tearDown() { + client = nil super.tearDown() } - func testGopherServerConnection() { - let expectation = XCTestExpectation( - description: "Connect and receive response from Gopher server") - let client = GopherClient() - client.sendRequest(to: "gopher.floodgap.com", message: "\r\n") { result in + func testInitialization() { + XCTAssertNotNil(client, "GopherClient should be initialized successfully") + } + + func testSendRequestCompletion() { + let expectation = XCTestExpectation(description: "Send request completion") + + client.sendRequest(to: "gopher.navan.dev", message: "\r\n") { result in switch result { - case .success(_): - expectation.fulfill() + case .success(let items): + XCTAssertFalse(items.isEmpty, "Response should contain gopher items") case .failure(let error): - print("Error \(error)") + XCTFail("Request failed with error: \(error)") + } + expectation.fulfill() + } + + wait(for: [expectation], timeout: 10.0) + } + + @available(iOS 13.0, macOS 10.15, *) + func testSendRequestAsync() async throws { + do { + let items = try await client.sendRequest(to: "gopher.navan.dev", message: "\r\n") + XCTAssertFalse(items.isEmpty, "Response should contain gopher items") + } catch { + XCTFail("Async request failed with error: \(error)") + } + } + + func testInvalidHost() { + let expectation = XCTestExpectation(description: "Invalid host request") + + client.sendRequest(to: "invalid.host.example", message: "") { result in + switch result { + case .success: + XCTFail("Request should fail for invalid host") + case .failure: + // Expected failure + break } + expectation.fulfill() } - wait(for: [expectation], timeout: 30) + wait(for: [expectation], timeout: 10.0) } - func testGopherClientAsync() async throws { - let client = GopherClient() - let reply = try await client.sendRequest(to: "gopher.navan.dev", message: "\r\n") - XCTAssertNotNil(reply) + func testCustomPort() { + let expectation = XCTestExpectation(description: "Custom port request") + + client.sendRequest(to: "gopher.navan.dev", port: 70, message: "\r\n") { result in + switch result { + case .success(let items): + XCTAssertFalse(items.isEmpty, "Response should contain gopher items") + case .failure(let error): + XCTFail("Request failed with error: \(error)") + } + expectation.fulfill() + } + + wait(for: [expectation], timeout: 10.0) } } -- cgit v1.2.3