aboutsummaryrefslogtreecommitdiff
path: root/Tests/swiftGopherClientTests/swiftGopherClientTests.swift
blob: 088124d373e1902d5a79159934702cc5c48e3bbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
//  swiftGopherClientTests.swift
//
//
//  Created by Navan Chauhan on 12/12/23.
//

import NIO
import XCTest

@testable import swiftGopherClient

final class GopherClientTests: XCTestCase {

    var client: GopherClient!

    override func setUp() {
        super.setUp()
        client = GopherClient()
    }

    override func tearDown() {
        client = nil
        super.tearDown()
    }

    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(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)
    }

    @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: 10.0)
    }

    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)
    }
}