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
  • url

    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 URLRequest to 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 Mock was completed. Can be used within unit tests for validating that a request has been executed. The callback must be set before calling register.

    Declaration

    Swift

    public var completion: (@Sendable () -> Void)?
  • The on request handler which will be executed everytime this Mock was started. Can be used within unit tests for validating that a request has been started. The handler must be set before calling register.

    Declaration

    Swift

    public var onRequestHandler: NetworkMockOnRequestHandler?
  • Creates a Mock for 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

    contentType

    The 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.

    statusCode

    The HTTP status code to return with the response.

    data

    The data which will be returned as the response based on the HTTP Method.

    additionalHeaders

    Additional headers to be added to the response.

  • Creates a Mock for 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

    url

    The URL to match for and to return the mocked data for.

    ignoreQuery

    If true, checking the URL will ignore the query and match only for the scheme, host and path. Defaults to false.

    cacheStoragePolicy

    The caching strategy. Defaults to notAllowed.

    contentType

    The 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.

    statusCode

    The HTTP status code to return with the response.

    data

    The data which will be returned as the response based on the HTTP Method.

    additionalHeaders

    Additional headers to be added to the response.

    requestError

    If provided, the URLSession will report the passed error rather than returning data. Defaults to nil.

  • Creates a Mock for 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

    fileExtensions

    The file extension to match for.

    contentType

    The 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.

    statusCode

    The HTTP status code to return with the response.

    data

    The data which will be returned as the response based on the HTTP Method.

    additionalHeaders

    Additional headers to be added to the response.

  • Creates a Mock for the given URLRequest.

    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

    request

    The URLRequest, from which the URL and request method is used to match for and to return the mocked data for.

    ignoreQuery

    If true, checking the URL will ignore the query and match only for the scheme, host and path. Defaults to false.

    cacheStoragePolicy

    The caching strategy. Defaults to notAllowed.

    contentType

    The 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.

    statusCode

    The HTTP status code to return with the response.

    data

    The data which will be returned as the response. Defaults to an empty Data instance.

    additionalHeaders

    Additional headers to be added to the response.

    requestError

    If 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 Mock.

    See more

    Declaration

    Swift

    public struct DataType : Sendable