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

Fix important mismatch for getting the right mock #31

Merged
merged 1 commit into from
Nov 13, 2019
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
Fix important mismatch for getting the right mock
  • Loading branch information
AvdLee committed Nov 12, 2019
commit 9037dcfe7ab57f8f17d2de82bc3b3ffe4344b041
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