Skip to content

Commit

Permalink
Adjust Harbor tests to accept Swift 6 concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarJalil committed Oct 26, 2024
1 parent 9bd9347 commit 302f922
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
23 changes: 12 additions & 11 deletions Tests/HarborTests/HarborTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,65 @@ import XCTest

final class HarborTests: XCTestCase {

func testShouldAddSinglePathParameterCorrectlyToURL() throws {
func testShouldAddSinglePathParameterCorrectlyToURL() async throws {
// Given
let baseUrl = "https://api.github.com/users/{USER}/"
let expectedURL = "https://api.github.com/users/OmarJalil/"

// When
let url = HRequestManager.compositeURL(url: baseUrl, pathParameters: ["USER": "OmarJalil"], queryParameters: nil)
let url = await HRequestManager.compositeURL(url: baseUrl, pathParameters: ["USER": "OmarJalil"], queryParameters: nil)

// Then
XCTAssertEqual(expectedURL, url?.absoluteString)
}

func testShouldAddMultiplePathParametersCorrectlyToURL() throws {
func testShouldAddMultiplePathParametersCorrectlyToURL() async throws {
// Given
let baseUrl = "https://api.github.com/users/{USER}/following/{FOLLOWS}/"
let expectedURL = "https://api.github.com/users/OmarJalil/following/javiermanzo/"

// When
let url = HRequestManager.compositeURL(url: baseUrl, pathParameters: ["FOLLOWS": "javiermanzo", "USER": "OmarJalil"], queryParameters: nil)
let url = await HRequestManager.compositeURL(url: baseUrl, pathParameters: ["FOLLOWS": "javiermanzo", "USER": "OmarJalil"], queryParameters: nil)

// Then
XCTAssertEqual(expectedURL, url?.absoluteString)
}

func testBuildGetRequest() {
func testBuildGetRequest() async throws {
// Given
let service = MockGetRequestService(url: "https://example.com", queryParameters: ["id": "123", "sort": "desc"])

// When
let request = HRequestManager.buildUrlRequest(request: service)
let request = await HRequestManager.buildUrlRequest(request: service)

// Then
XCTAssertNotNil(request)
XCTAssertEqual(request?.url?.absoluteString, "https://example.com?id=123&sort=desc")
XCTAssertEqual(request?.httpMethod, "GET")
}

func testBuildPostRequest() {
func testBuildPostRequest() async throws {
// Given
let service = MockPostRequestService(url: "https://example.com", bodyParameters: ["name": "John"])

// When
let request = HRequestManager.buildUrlRequest(request: service)
let request = await HRequestManager.buildUrlRequest(request: service)

// Then
XCTAssertNotNil(request)
XCTAssertEqual(request?.url?.absoluteString, "https://example.com")
XCTAssertEqual(request?.httpMethod, "POST")
XCTAssertEqual(request?.allHTTPHeaderFields?["Content-Type"], "application/json")
XCTAssertEqual(request?.httpBody, HRequestManager.dataBody(params: ["name": "John"], type: .json))
let httpBody = await HRequestManager.dataBody(params: ["name": "John"], type: .json)
XCTAssertEqual(request?.httpBody, httpBody)
}

func testBuildInvalidRequest() {
func testBuildInvalidRequest() async throws {
// Given
let service = MockInvalidRequestService()

// When
let request = HRequestManager.buildUrlRequest(request: service)
let request = await HRequestManager.buildUrlRequest(request: service)

// Then
XCTAssertNil(request)
Expand Down
8 changes: 4 additions & 4 deletions Tests/HarborTests/Mocks/MocksRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Harbor

final class MockGetRequestService: HGetRequestProtocol {
final class MockGetRequestService: HGetRequestProtocol, @unchecked Sendable {

typealias Model = String
var headerParameters: [String: String]?
Expand All @@ -27,7 +27,7 @@ final class MockGetRequestService: HGetRequestProtocol {
}
}

final class MockPostRequestService: HPostRequestProtocol {
final class MockPostRequestService: HPostRequestProtocol, @unchecked Sendable {
var headerParameters: [String: String]?
var needsAuth: Bool
var retries: Int?
Expand All @@ -48,7 +48,7 @@ final class MockPostRequestService: HPostRequestProtocol {
}
}

final class MockPostBodyRequestService: HPostRequestProtocol {
final class MockPostBodyRequestService: HPostRequestProtocol, @unchecked Sendable {
var headerParameters: [String: String]?
var needsAuth: Bool
var retries: Int?
Expand All @@ -68,7 +68,7 @@ final class MockPostBodyRequestService: HPostRequestProtocol {
}
}

final class MockInvalidRequestService: HRequestBaseRequestProtocol {
final class MockInvalidRequestService: HRequestBaseRequestProtocol, @unchecked Sendable {
var headerParameters: [String: String]?
var url: String
var needsAuth: Bool = false
Expand Down

0 comments on commit 302f922

Please sign in to comment.