Skip to content

Commit

Permalink
feat: allow to pass a custom JSONDecoder to OnRequestHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
DominikPalo committed Mar 8, 2024
1 parent e38e120 commit 47cdf02
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Sources/Mocker/OnRequestHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTTPBody: Decodable>(httpBodyType: HTTPBody.Type?, callback: @escaping OnRequest<HTTPBody>) {
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<HTTPBody: Decodable>(httpBodyType: HTTPBody.Type?, jsonDecoder: JSONDecoder, callback: @escaping OnRequest<HTTPBody>) {
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
Expand Down
26 changes: 26 additions & 0 deletions Tests/MockerTests/MockerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 47cdf02

Please sign in to comment.