Skip to content

Commit

Permalink
Fix/test faking for new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis committed Jul 19, 2024
1 parent d7dad91 commit dba6e3a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/Networking/Networking+New.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ extension Networking {
if T.self == Data.self {
logger.info("Successfully processed fake request to \(path, privacy: .public)")
return .success(Data() as! T)
} else if T.self == NetworkingResponse.self {
let headers = Dictionary(uniqueKeysWithValues: response.headers.compactMap { key, value in
(key as? String).map { ($0, AnyCodable(value)) }
})
let body = try JSONDecoder().decode([String: AnyCodable].self, from: response.data)
let networkingJSON = NetworkingResponse(headers: headers, body: body)
return .success(networkingJSON as! T)
} else {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
Expand Down
33 changes: 33 additions & 0 deletions Tests/NetworkingTests/FakeRequestTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -683,4 +683,37 @@ extension FakeRequestTests {
XCTFail(response.error.localizedDescription)
}
}

func testNewPostWithFakeHeaders() async {
let networking = Networking(baseURL: baseURL)

networking.fakePOST("/auth/verify_confirmation_code", response: [
"phone_number": "phoneNumber"
], headerFields: [
"client": "aClient",
"access-token": "anAccessToken",
"uid": "aUID",
"Authorization": "authorization",
])

let parameters: [String: Any] = [
"phone_number": "phoneNumber",
"confirmation_code": "confirmationCode"
]

let result: Result<NetworkingResponse, NetworkingError> = await networking.newPost("/auth/verify_confirmation_code", parameters: parameters)
switch result {
case .success(let response):
let headers = response.headers
XCTAssertEqual(headers.string(for: "access-token"), "anAccessToken")
XCTAssertEqual(headers.string(for: "client"), "aClient")
XCTAssertEqual(headers.string(for: "uid"), "aUID")
XCTAssertEqual(headers.string(for: "Authorization"), "authorization")

let body = response.body
XCTAssertEqual(body.string(for: "phone_number"), "phoneNumber")
case .failure (let response):
XCTFail(response.localizedDescription)
}
}
}

0 comments on commit dba6e3a

Please sign in to comment.