Skip to content

Commit

Permalink
Merge pull request #150 from WeTransfer/feature/sendable
Browse files Browse the repository at this point in the history
Simplify tests and add Sendable to subtypes
  • Loading branch information
AvdLee authored Jan 5, 2024
2 parents d56222c + d4014d0 commit edf95a4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Sources/Mocker/Mock+DataType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation

extension Mock {
/// The types of content of a request. Will be used as Content-Type header inside a `Mock`.
public struct DataType {
public struct DataType: Sendable {

/// Name of the data type.
public let name: String
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mocker/Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public struct Mock: Equatable {
/// HTTP method definitions.
///
/// See https://tools.ietf.org/html/rfc7231#section-4.3
public enum HTTPMethod: String {
public enum HTTPMethod: String, Sendable {
case options = "OPTIONS"
case get = "GET"
case head = "HEAD"
Expand Down
22 changes: 13 additions & 9 deletions Tests/MockerTests/MockerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,14 @@ final class MockerTests: XCTestCase {
let onRequestExpectation = expectation(description: "Data request should start")

let expectedParameters = ["test": "value"]
var request = URLRequest(url: URL(string: "https://www.fakeurl.com")!)
let requestURL = URL(string: "https://www.fakeurl.com")!
var request = URLRequest(url: requestURL)
request.httpMethod = Mock.HTTPMethod.post.rawValue
request.httpBody = try JSONSerialization.data(withJSONObject: expectedParameters, options: .prettyPrinted)

var mock = Mock(url: request.url!, contentType: .json, statusCode: 200, data: [.post: Data()])
var mock = Mock(url: requestURL, contentType: .json, statusCode: 200, data: [.post: Data()])
mock.onRequest = { request, postBodyArguments in
XCTAssertEqual(request.url, mock.request.url)
XCTAssertEqual(request.url, requestURL)
XCTAssertEqual(expectedParameters, postBodyArguments as? [String: String])
onRequestExpectation.fulfill()
}
Expand All @@ -364,13 +365,14 @@ final class MockerTests: XCTestCase {
let onRequestExpectation = expectation(description: "Data request should start")

let expectedParameters = RequestParameters(name: UUID().uuidString)
var request = URLRequest(url: URL(string: "https://www.fakeurl.com")!)
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, callback: { request, postBodyDecodable in
XCTAssertEqual(request.url, mock.request.url)
XCTAssertEqual(request.url, requestURL)
XCTAssertEqual(expectedParameters, postBodyDecodable)
onRequestExpectation.fulfill()
})
Expand All @@ -385,13 +387,14 @@ final class MockerTests: XCTestCase {
let onRequestExpectation = expectation(description: "Data request should start")

let expectedParameters = ["test": "value"]
var request = URLRequest(url: URL(string: "https://www.fakeurl.com")!)
let requestURL = URL(string: "https://www.fakeurl.com")!
var request = URLRequest(url: requestURL)
request.httpMethod = Mock.HTTPMethod.post.rawValue
request.httpBody = try JSONSerialization.data(withJSONObject: expectedParameters, options: .prettyPrinted)

var mock = Mock(url: request.url!, contentType: .json, statusCode: 200, data: [.post: Data()])
mock.onRequestHandler = .init(jsonDictionaryCallback: { request, postBodyArguments in
XCTAssertEqual(request.url, mock.request.url)
XCTAssertEqual(request.url, requestURL)
XCTAssertEqual(expectedParameters, postBodyArguments as? [String: String])
onRequestExpectation.fulfill()
})
Expand Down Expand Up @@ -424,13 +427,14 @@ final class MockerTests: XCTestCase {
let onRequestExpectation = expectation(description: "Data request should start")

let expectedParameters = [["test": "value"], ["test": "value"]]
var request = URLRequest(url: URL(string: "https://www.fakeurl.com")!)
let requestURL = URL(string: "https://www.fakeurl.com")!
var request = URLRequest(url: requestURL)
request.httpMethod = Mock.HTTPMethod.post.rawValue
request.httpBody = try JSONSerialization.data(withJSONObject: expectedParameters, options: .prettyPrinted)

var mock = Mock(url: request.url!, contentType: .json, statusCode: 200, data: [.post: Data()])
mock.onRequestHandler = OnRequestHandler(jsonArrayCallback: { request, postBodyArguments in
XCTAssertEqual(request.url, mock.request.url)
XCTAssertEqual(request.url, requestURL)
XCTAssertEqual(expectedParameters, postBodyArguments as? [[String: String]])
onRequestExpectation.fulfill()
})
Expand Down

0 comments on commit edf95a4

Please sign in to comment.