NetworkMock

public struct NetworkMock : Equatable

A Mock which can be used for mocking data requests with the Mocker by calling Mocker.register(...).

  • Declaration

    Swift

    public enum HTTPMethod : String
  • The types of content of a request. Will be used as Content-Type header inside a Mock.

    See more

    Declaration

    Swift

    public enum DataType : String
  • Undocumented

    Declaration

    Swift

    public typealias OnRequest = (_ request: URLRequest, _ httpBodyArguments: [String : Any]?) -> Void
  • The type of the data which is returned.

    Declaration

    Swift

    public let dataType: 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: (() -> Void)?
  • The callback which will be executed everytime this Mock was started. Can be used within unit tests for validating that a request has been started. The callback must be set before calling register.

    Declaration

    Swift

    public var onRequest: OnRequest?
  • Creates a Mock for the given data type. The mock will be automatically matched based on a URL created from the given parameters.

    Declaration

    Swift

    public init(dataType: DataType, statusCode: Int, data: [HTTPMethod : Data], additionalHeaders: [String : String] = [:])

    Parameters

    dataType

    The type of the data which is returned.

    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, dataType: DataType, 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.

    reportFailure

    if true, the URLsession will report an error loading the URL rather than returning data. Defaults to false.

    dataType

    The type of the data which is returned.

    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 file extensions. The mock will only be used for urls matching the extension.

    Declaration

    Swift

    public init(fileExtensions: String..., dataType: DataType, statusCode: Int, data: [HTTPMethod : Data], additionalHeaders: [String : String] = [:])

    Parameters

    fileExtensions

    The file extension to match for.

    dataType

    The type of the data which is returned.

    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.

  • Registers the mock with the shared Mocker.

    Declaration

    Swift

    public func register()
  • Declaration

    Swift

    public static func == (lhs: NetworkMock, rhs: NetworkMock) -> Bool