NetworkMock
public struct NetworkMock : Equatable, @unchecked Sendable
A Mock which can be used for mocking data requests with the Mocker by calling Mocker.register(...).
-
Declaration
Swift
public enum HTTPMethod : String, Sendable -
Undocumented
Declaration
Swift
public typealias OnRequest = @Sendable (_ request: URLRequest, _ httpBodyArguments: [String : Any]?) -> Void -
The type of the data which designates the Content-Type header. If set to
nil, no Content-Type header is added to the headers.Declaration
Swift
public let contentType: DataType? -
If set, the error that URLProtocol will report as a result rather than returning data from the mock
Declaration
Swift
public let requestError: Error? -
The headers to send back with the response.
Declaration
Swift
public let headers: [String : String] -
The HTTP status code to return with the response.
Declaration
Swift
public let statusCode: Int -
The URL value generated based on the Mock data. Force unwrapped on purpose. If you access this URL while it’s not set, this is a programming error.
Declaration
Swift
public var url: URL { get } -
The
URLRequestto use if you did not set a specific URL.Declaration
Swift
public let request: URLRequest -
If
true, checking the URL will ignore the query and match only for the scheme, host and path.Declaration
Swift
public let ignoreQuery: Bool -
The file extensions to match for.
Declaration
Swift
public let fileExtensions: [String]? -
Add a delay to a certain mock, which makes the response returned later.
Declaration
Swift
public var delay: DispatchTimeInterval? -
Allow response cache.
Declaration
Swift
public var cacheStoragePolicy: URLCache.StoragePolicy -
The callback which will be executed everytime this
Mockwas completed. Can be used within unit tests for validating that a request has been executed. The callback must be set before callingregister.Declaration
Swift
public var completion: (@Sendable () -> Void)? -
The on request handler which will be executed everytime this
Mockwas started. Can be used within unit tests for validating that a request has been started. The handler must be set before callingregister.Declaration
Swift
public var onRequestHandler: NetworkMockOnRequestHandler? -
Creates a
Mockfor the given content type. The mock will be automatically matched based on a URL created from the given parameters.Declaration
Swift
public init(contentType: DataType?, statusCode: Int, data: [HTTPMethod : Data], additionalHeaders: [String : String] = [:])Parameters
contentTypeThe type of the data which designates the Content-Type header. Defaults to
nil, which means that no Content-Type header is added to the headers.statusCodeThe HTTP status code to return with the response.
dataThe data which will be returned as the response based on the HTTP Method.
additionalHeadersAdditional headers to be added to the response.
-
init(url:ignoreQuery: cacheStoragePolicy: contentType: statusCode: data: additionalHeaders: requestError: ) Creates a
Mockfor the given URL.Declaration
Swift
public init(url: URL, ignoreQuery: Bool = false, cacheStoragePolicy: URLCache.StoragePolicy = .notAllowed, contentType: DataType? = nil, statusCode: Int, data: [HTTPMethod : Data], additionalHeaders: [String : String] = [:], requestError: Error? = nil)Parameters
urlThe URL to match for and to return the mocked data for.
ignoreQueryIf
true, checking the URL will ignore the query and match only for the scheme, host and path. Defaults tofalse.cacheStoragePolicyThe caching strategy. Defaults to
notAllowed.contentTypeThe type of the data which designates the Content-Type header. Defaults to
nil, which means that no Content-Type header is added to the headers.statusCodeThe HTTP status code to return with the response.
dataThe data which will be returned as the response based on the HTTP Method.
additionalHeadersAdditional headers to be added to the response.
requestErrorIf provided, the URLSession will report the passed error rather than returning data. Defaults to
nil. -
Creates a
Mockfor the given file extensions. The mock will only be used for urls matching the extension.Declaration
Swift
public init(fileExtensions: String..., contentType: DataType? = nil, statusCode: Int, data: [HTTPMethod : Data], additionalHeaders: [String : String] = [:])Parameters
fileExtensionsThe file extension to match for.
contentTypeThe type of the data which designates the Content-Type header. Defaults to
nil, which means that no Content-Type header is added to the headers.statusCodeThe HTTP status code to return with the response.
dataThe data which will be returned as the response based on the HTTP Method.
additionalHeadersAdditional headers to be added to the response.
-
init(request:ignoreQuery: cacheStoragePolicy: contentType: statusCode: data: additionalHeaders: requestError: ) Creates a
Mockfor the givenURLRequest.Declaration
Swift
public init(request: URLRequest, ignoreQuery: Bool = false, cacheStoragePolicy: URLCache.StoragePolicy = .notAllowed, contentType: DataType? = nil, statusCode: Int, data: Data = Data(), additionalHeaders: [String : String] = [:], requestError: Error? = nil)Parameters
requestThe URLRequest, from which the URL and request method is used to match for and to return the mocked data for.
ignoreQueryIf
true, checking the URL will ignore the query and match only for the scheme, host and path. Defaults tofalse.cacheStoragePolicyThe caching strategy. Defaults to
notAllowed.contentTypeThe type of the data which designates the Content-Type header. Defaults to
nil, which means that no Content-Type header is added to the headers.statusCodeThe HTTP status code to return with the response.
dataThe data which will be returned as the response. Defaults to an empty
Datainstance.additionalHeadersAdditional headers to be added to the response.
requestErrorIf provided, the URLSession will report the passed error rather than returning data. Defaults to
nil. -
Registers the mock with the shared
Mocker.Declaration
Swift
public func register() -
Declaration
Swift
public static func == (lhs: NetworkMock, rhs: NetworkMock) -> Bool -
The types of content of a request. Will be used as Content-Type header inside a
See moreMock.Declaration
Swift
public struct DataType : Sendable
View on GitHub