From 47cdf02c060e27c5b499ba4bd4803c794660e534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Pa=C4=BEo?= Date: Fri, 8 Mar 2024 14:17:20 +0100 Subject: [PATCH] feat: allow to pass a custom `JSONDecoder` to `OnRequestHandler` --- Sources/Mocker/OnRequestHandler.swift | 11 ++++++++++- Tests/MockerTests/MockerTests.swift | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Sources/Mocker/OnRequestHandler.swift b/Sources/Mocker/OnRequestHandler.swift index a830211..e605d1d 100644 --- a/Sources/Mocker/OnRequestHandler.swift +++ b/Sources/Mocker/OnRequestHandler.swift @@ -24,10 +24,19 @@ public struct OnRequestHandler { /// - httpBodyType: The decodable type to use for parsing the request body. /// - callback: The callback which will be called just before the request executes. public init(httpBodyType: HTTPBody.Type?, callback: @escaping OnRequest) { + self.init(httpBodyType: httpBodyType, jsonDecoder: JSONDecoder(), callback: callback) + } + + /// Creates a new request handler using the given `HTTPBody` type, which can be any `Decodable` and decoding it using the provided `JSONDecoder`. + /// - Parameters: + /// - httpBodyType: The decodable type to use for parsing the request body. + /// - jsonDecoder: The decoder to use for decoding the request body. + /// - callback: The callback which will be called just before the request executes. + public init(httpBodyType: HTTPBody.Type?, jsonDecoder: JSONDecoder, callback: @escaping OnRequest) { self.internalCallback = { request in guard let httpBody = request.httpBodyStreamData() ?? request.httpBody, - let decodedObject = try? JSONDecoder().decode(HTTPBody.self, from: httpBody) + let decodedObject = try? jsonDecoder.decode(HTTPBody.self, from: httpBody) else { callback(request, nil) return diff --git a/Tests/MockerTests/MockerTests.swift b/Tests/MockerTests/MockerTests.swift index c7cfc92..88db6e3 100644 --- a/Tests/MockerTests/MockerTests.swift +++ b/Tests/MockerTests/MockerTests.swift @@ -383,6 +383,32 @@ final class MockerTests: XCTestCase { wait(for: [onRequestExpectation], timeout: 2.0) } + func testOnRequestDecodablePostBodyParametersWithCustomJSONDecoder() throws { + struct RequestParameters: Codable, Equatable { + let name: String + } + + let onRequestExpectation = expectation(description: "Data request should start") + + let expectedParameters = RequestParameters(name: UUID().uuidString) + let requestURL = URL(string: "https://www.fakeurl.com")! + var request = URLRequest(url: requestURL) + request.httpMethod = Mock.HTTPMethod.post.rawValue + request.httpBody = try JSONEncoder().encode(expectedParameters) + + var mock = Mock(url: request.url!, contentType: .json, statusCode: 200, data: [.post: Data()]) + mock.onRequestHandler = .init(httpBodyType: RequestParameters.self, jsonDecoder: JSONDecoder(), callback: { request, postBodyDecodable in + XCTAssertEqual(request.url, requestURL) + XCTAssertEqual(expectedParameters, postBodyDecodable) + onRequestExpectation.fulfill() + }) + mock.register() + + URLSession.shared.dataTask(with: request).resume() + + wait(for: [onRequestExpectation], timeout: 2.0) + } + func testOnRequestJSONDictionaryPostBodyParameters() throws { let onRequestExpectation = expectation(description: "Data request should start")