Skip to content

Commit

Permalink
Fix important mismatch for getting the right mock
Browse files Browse the repository at this point in the history
  • Loading branch information
AvdLee committed Nov 12, 2019
1 parent ae5f00c commit 9037dcf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
## Changelog

### Next
- `MockingURLProtocol` Error is now localized
- `MockingURLProtocol` Error now conforms to `CustomDebugStringConvertible` for better debugging logs.
- Fixed a bug in which two Mocks would not be registered for the same URL if the HTTP method was different.

### 2.0.0
- A new completion callback can be set on `Mock` to use for expectation fulfilling once a `Mock` is completed.
Expand Down
12 changes: 12 additions & 0 deletions MockerTests/MockerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,16 @@ final class MockerTests: XCTestCase {
Mocker.removeAll()
XCTAssertTrue(Mocker.shared.mocks.isEmpty)
}

/// It should correctly add two mocks for the same URL if the HTTP method is different.
func testDifferentHTTPMethodSameURL() {
let url = URL(string: "https://www.fakeurl.com/\(UUID().uuidString)")!
Mock(url: url, dataType: .json, statusCode: 200, data: [.get: Data()]).register()
Mock(url: url, dataType: .json, statusCode: 200, data: [.put: Data()]).register()
var request = URLRequest(url: url)
request.httpMethod = Mock.HTTPMethod.get.rawValue
XCTAssertNotNil(Mocker.mock(for: request))
request.httpMethod = Mock.HTTPMethod.put.rawValue
XCTAssertNotNil(Mocker.mock(for: request))
}
}
2 changes: 1 addition & 1 deletion Sources/Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public struct Mock: Equatable {

public static func == (lhs: Mock, rhs: Mock) -> Bool {
let lhsHTTPMethods: [String] = lhs.data.keys.compactMap { $0.rawValue }
let rhsHTTPMethods: [String] = lhs.data.keys.compactMap { $0.rawValue }
let rhsHTTPMethods: [String] = rhs.data.keys.compactMap { $0.rawValue }
return lhs.request.url!.absoluteString == rhs.request.url!.absoluteString && lhsHTTPMethods == rhsHTTPMethods
}
}
Expand Down
6 changes: 5 additions & 1 deletion Sources/MockingURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ import Foundation
/// The protocol which can be used to send Mocked data back. Use the `Mocker` to register `Mock` data
public final class MockingURLProtocol: URLProtocol {

enum Error: Swift.Error, LocalizedError {
enum Error: Swift.Error, LocalizedError, CustomDebugStringConvertible {
case missingMockedData(url: String)

var errorDescription: String? {
return debugDescription
}

var debugDescription: String {
switch self {
case .missingMockedData(let url):
return "Missing mock for URL: \(url)"
Expand Down

0 comments on commit 9037dcf

Please sign in to comment.