From 296d3edad00612662d8e7739192701cd70310b61 Mon Sep 17 00:00:00 2001 From: Cedric Van Asch Date: Thu, 28 Mar 2024 22:35:53 +0100 Subject: [PATCH 1/2] Add encodable to Request body Remake of a PR by wvteijlingen, but his branch was not available anymore + missing the MockedEncodable inside the project.pbxproj, so it could not be found --- Example/Example.xcodeproj/project.pbxproj | 14 +++++++++++++- Example/Tests/Mocks/MockedEncodable.swift | 5 +++++ Example/Tests/Specs/Request/RequestSpec.swift | 8 ++++++++ .../Tests/{Mocks => Stubs}/StubbedRequest.swift | 0 Sources/Request/Request+URLRequest.swift | 9 +++++++-- Sources/Request/Request.swift | 3 ++- 6 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 Example/Tests/Mocks/MockedEncodable.swift rename Example/Tests/{Mocks => Stubs}/StubbedRequest.swift (100%) diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index 146895a..a0d6658 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -25,6 +25,7 @@ BEBF72B2A088D7289F7B2876 /* Pods_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 56EC7F2EF94A0A4D1B403A27 /* Pods_Tests.framework */; }; F31234B32BB593E2000C4F3A /* MockURLProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F31234B22BB593E2000C4F3A /* MockURLProtocol.swift */; }; F31234B52BB59C45000C4F3A /* StubbedRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = F31234B42BB59C45000C4F3A /* StubbedRequest.swift */; }; + F3253C382BB61739006B1BF4 /* MockedEncodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3253C372BB61739006B1BF4 /* MockedEncodable.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -50,6 +51,7 @@ 9DEA80C221D97275002A56A7 /* MockedLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockedLogger.swift; sourceTree = ""; }; F31234B22BB593E2000C4F3A /* MockURLProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockURLProtocol.swift; sourceTree = ""; }; F31234B42BB59C45000C4F3A /* StubbedRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StubbedRequest.swift; sourceTree = ""; }; + F3253C372BB61739006B1BF4 /* MockedEncodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockedEncodable.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -86,6 +88,7 @@ 607FACE81AFB9204008FA782 /* Tests */ = { isa = PBXGroup; children = ( + F3253C362BB6170D006B1BF4 /* Stubs */, 9D0E402A21B3FC2D0088D678 /* Helpers */, 9D60FF2621B402BE0078F791 /* Mocks */, 9D21FEF621D8F061006EF131 /* Resources */, @@ -139,7 +142,7 @@ 9D21FF0721D971C6006EF131 /* MockedPublicKeyPinningService.swift */, 9D60FF2721B402CA0078F791 /* MockedRequest.swift */, 9D60FF2C21B42A560078F791 /* MockedSerializer.swift */, - F31234B42BB59C45000C4F3A /* StubbedRequest.swift */, + F3253C372BB61739006B1BF4 /* MockedEncodable.swift */, ); path = Mocks; sourceTree = ""; @@ -189,6 +192,14 @@ name = Frameworks; sourceTree = ""; }; + F3253C362BB6170D006B1BF4 /* Stubs */ = { + isa = PBXGroup; + children = ( + F31234B42BB59C45000C4F3A /* StubbedRequest.swift */, + ); + path = Stubs; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -338,6 +349,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + F3253C382BB61739006B1BF4 /* MockedEncodable.swift in Sources */, 9D446A9C21B3EAC100FEA1F7 /* ServiceSpec.swift in Sources */, 9D21FF0621D97196006EF131 /* NetworkServiceSpec.swift in Sources */, 9DAD37C521B45A900042C19C /* RequestSpec.swift in Sources */, diff --git a/Example/Tests/Mocks/MockedEncodable.swift b/Example/Tests/Mocks/MockedEncodable.swift new file mode 100644 index 0000000..c0edb4e --- /dev/null +++ b/Example/Tests/Mocks/MockedEncodable.swift @@ -0,0 +1,5 @@ +import Foundation + +struct MockedEncodable: Encodable { + let hello: String +} diff --git a/Example/Tests/Specs/Request/RequestSpec.swift b/Example/Tests/Specs/Request/RequestSpec.swift index ecf0bd5..671991c 100644 --- a/Example/Tests/Specs/Request/RequestSpec.swift +++ b/Example/Tests/Specs/Request/RequestSpec.swift @@ -188,6 +188,14 @@ class RequestSpec: QuickSpec { let urlRequest = try? request.makeURLRequest(with: configuration) expect(urlRequest?.httpBody) == data } + + it("should have an encodable body") { + let body = MockedEncodable(hello: "Jake") + let request = MockedRequest(url: URL(string: "request"), body: body) + let urlRequest = try? request.makeURLRequest(with: configuration) + let data = try? JSONEncoder().encode(body) + expect(urlRequest?.httpBody) == data + } } it("should have the correct cache policy") { diff --git a/Example/Tests/Mocks/StubbedRequest.swift b/Example/Tests/Stubs/StubbedRequest.swift similarity index 100% rename from Example/Tests/Mocks/StubbedRequest.swift rename to Example/Tests/Stubs/StubbedRequest.swift diff --git a/Sources/Request/Request+URLRequest.swift b/Sources/Request/Request+URLRequest.swift index d3af35f..013ec3e 100644 --- a/Sources/Request/Request+URLRequest.swift +++ b/Sources/Request/Request+URLRequest.swift @@ -64,10 +64,15 @@ extension Request { } private func makeBody() throws -> Data? { - guard let body = body else { return nil } + guard let body else { return nil } // When the body is of the data type we just return this raw data. if let body = body as? Data { return body } - // In all other cases we try to parse the json. + // When the body implements Encodable we try to encode it to json. + if let body = body as? Encodable { + return try JSONEncoder().encode(body) + } + + // In all other cases we try to encode it to json. return try JSONSerialization.data(withJSONObject: body, options: []) } } diff --git a/Sources/Request/Request.swift b/Sources/Request/Request.swift index 4eb7a9f..e23324d 100644 --- a/Sources/Request/Request.swift +++ b/Sources/Request/Request.swift @@ -23,9 +23,10 @@ public protocol Request { var query: RequestQuery? { get } /// Set the headers for this request. var headers: RequestHeaders? { get } - /// Set the type of body with it's content. In Cara's case we support 2 major types of body: + /// Set the type of body with it's content. In Cara's case we support 3 major types of body: /// - A raw `Data` object /// - An `Any` object that can be parsed as a json. + /// - A type that implements `Encodable` var body: Any? { get } /// Set a cache policy for every request. var cachePolicy: URLRequest.CachePolicy { get } From 11663da15b74c70514eeddd6640ccfd94b11cdc5 Mon Sep 17 00:00:00 2001 From: Cedric Van Asch Date: Thu, 28 Mar 2024 22:43:29 +0100 Subject: [PATCH 2/2] fixed the last swiftlint warnings --- Example/Tests/Helpers/SecTrust+Apple.swift | 1 + Sources/Logger/ConsoleLogger.swift | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Example/Tests/Helpers/SecTrust+Apple.swift b/Example/Tests/Helpers/SecTrust+Apple.swift index 1a38d0e..b8f9b45 100644 --- a/Example/Tests/Helpers/SecTrust+Apple.swift +++ b/Example/Tests/Helpers/SecTrust+Apple.swift @@ -28,4 +28,5 @@ extension SecTrust { SecTrustCreateWithCertificates(certificates, policy, &optionalSecTrust) return optionalSecTrust! } + // swiftlint:enable force_try } diff --git a/Sources/Logger/ConsoleLogger.swift b/Sources/Logger/ConsoleLogger.swift index 2ec81a1..d5f2169 100644 --- a/Sources/Logger/ConsoleLogger.swift +++ b/Sources/Logger/ConsoleLogger.swift @@ -21,8 +21,7 @@ extension ConsoleLogger: Logger { let url = urlRequest.url else { return } os_log("☁️ %{public}@: %{public}@", log: OSLog.request, type: .info, method, url.absoluteString) } - - // swiftlint:disable function_body_length + public func end(urlRequest: URLRequest, urlResponse: URLResponse, metrics: URLSessionTaskMetrics, error: Error?) { guard let method = urlRequest.httpMethod, @@ -70,5 +69,4 @@ extension ConsoleLogger: Logger { } } } - // swiftlint:enable function_body_length }