From dba6e3a70160a70cec4ac0068c41f2fd4ea0f600 Mon Sep 17 00:00:00 2001 From: Elvis Date: Fri, 19 Jul 2024 20:20:16 +0200 Subject: [PATCH] Fix/test faking for new methods --- Sources/Networking/Networking+New.swift | 7 +++++ Tests/NetworkingTests/FakeRequestTests.swift | 33 ++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/Sources/Networking/Networking+New.swift b/Sources/Networking/Networking+New.swift index 8303113..d03955d 100644 --- a/Sources/Networking/Networking+New.swift +++ b/Sources/Networking/Networking+New.swift @@ -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 diff --git a/Tests/NetworkingTests/FakeRequestTests.swift b/Tests/NetworkingTests/FakeRequestTests.swift index dc2e27f..ef2af4a 100644 --- a/Tests/NetworkingTests/FakeRequestTests.swift +++ b/Tests/NetworkingTests/FakeRequestTests.swift @@ -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 = 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) + } + } }