WeTransfer / Mocker

Mock Alamofire and URLSession requests without touching your code implementation
MIT License
1.1k stars 96 forks source link

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

Closed DominikPalo closed 6 months ago

DominikPalo commented 6 months ago

This PR allows providing a custom instance of JSONEncoder while initializing the OnRequestHandler.

Motivation

Some API endpoints require all JSON keys in payloads to adhere to the snake-case naming convention. However, to be consistent with the Swift naming convention, we use camelCase naming convention for declaring properties in underlying Codable structures (JSON models). When calling the API, we configure the Alamofire key encoding strategy to snake-case (encoder.keyEncodingStrategy = .convertToSnakeCase). Unfortunately, with the current implementation, it is not possible to mock such endpoints, as the request handler always uses the default JSONDecoder() for parsing/decoding the request body.

After applying this PR, it will be possible to configure the JSONDecoder as needed; for example, in our case, to set the key decoding strategy to _snakecase:

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase

mock.onRequestHandler = OnRequestHandler(httpBodyType: BodyType.self, jsonDecoder: decoder) { _, body in
    // Assertions
}
wetransferplatform commented 6 months ago
Warnings
:warning: 'onRequest' is deprecated: Use `onRequestHandler` instead.
:warning: 'onRequest' is deprecated: Use `onRequestHandler` instead.
Messages
:book: MockerTests: Executed 33 tests (0 failed, 0 retried, 0 skipped) in 1.841 seconds
:book: View more details on Bitrise

Code Coverage Report

Name Coverage
Mocker 82.35%

SwiftLint found issues

Severity File Reason
Warning OnRequestHandler.swift:30 Line should be 140 characters or less; currently it has 149 characters (line_length)
Warning OnRequestHandler.swift:56 Line should be 140 characters or less; currently it has 149 characters (line_length)
Error OnRequestHandler.swift:66 Line should be 160 characters or less; currently it has 168 characters (line_length)
Error OnRequestHandler.swift:82 Line should be 160 characters or less; currently it has 163 characters (line_length)
Warning MockerTests.swift:374 Prefer named constructors over .init and type inference (explicit_init)
Warning MockerTests.swift:400 Prefer named constructors over .init and type inference (explicit_init)
Warning MockerTests.swift:422 Prefer named constructors over .init and type inference (explicit_init)
Warning MockerTests.swift:441 Prefer named constructors over .init and type inference (explicit_init)
Warning MockerTests.swift:666 File should contain 660 lines or less: currently contains 666 (file_length)
Warning MockerTests.swift:187 Line should be 140 characters or less; currently it has 151 characters (line_length)
Warning MockerTests.swift:205 Line should be 140 characters or less; currently it has 149 characters (line_length)
Warning MockerTests.swift:263 Line should be 140 characters or less; currently it has 157 characters (line_length)
Warning MockerTests.swift:400 Line should be 140 characters or less; currently it has 145 characters (line_length)
Warning MockerTests.swift:52 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:71 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:94 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:116 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:148 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:173 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:189 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:207 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:230 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:283 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:574 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:608 Chained function calls should be either on the same line, or one per line (multiline_function_chains)
Warning MockerTests.swift:261 Variables should not have redundant type annotation (redundant_type_annotation)
Warning MockerTests.swift:132 Use shorthand syntax for optional binding (shorthand_optional_binding)
Warning MockerTests.swift:267 Use shorthand syntax for optional binding (shorthand_optional_binding)
Warning MockerTests.swift:562 Use shorthand syntax for optional binding (shorthand_optional_binding)
Warning MockerTests.swift:152 Lines should not have trailing whitespace (trailing_whitespace)
Warning MockerTests.swift:167 Lines should not have trailing whitespace (trailing_whitespace)
Warning MockerTests.swift:15 Type body should span 400 lines or less excluding comments and whitespace: currently spans 465 lines (type_body_length)
Warning MockerTests.swift:48 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:67 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:90 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:112 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:130 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:162 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:185 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:203 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:226 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:242 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:265 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:557 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:600 Parentheses are not needed when declaring closure arguments (unneeded_parentheses_in_closure_argument)
Warning MockerTests.swift:316 Prefer the specific matcher 'XCTAssertEqual' instead (xct_specific_matcher)

Generated by :no_entry_sign: Danger Swift against 47cdf02c060e27c5b499ba4bd4803c794660e534