Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to pass a custom JSONDecoder to OnRequestHandler #155

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading