Swiftpack.co -  feliperuzg/Mimic as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
feliperuzg/Mimic
Simple mocking network solution written in Swift
.package(url: "https://github.com/feliperuzg/Mimic.git", from: "1.3.0")

Mimic

Mimic is a simple library for stubbing network request.

Integration

Cocoapods

Using Mimic with Cocoapods.

To integrate Mimic into your Xcode project, add the following to your Podfile.

    pod 'Mimic', '~> 1.3.0'

Swift Package Manager

Using Mimic with Swift Package Manager.

Add Mimic as a dependcy in your Package.swift file.

dependencies: [
    .package(url: "https://github.com/feliperuzg/Mimic", .upToNextMajor(from: "1.3.0"))
]

Carthage

Using Mimic with Carthage.

To integrate Mimic into your Xcode project, add the following to your Cartfile.

github "feliperuzg/Mimic" 

Usage

Adding new stub

To create a new stub use

@discardableResult public class func mimic(
    request: @escaping MimicRequest,
    delay: TimeInterval = 0,
    response: @escaping MimicResponse
) -> MimicObject

Where MimicRequest is constructed like:

request(with method: MimicHTTPMethod, url: String, wildCard: Bool = false)

And MimicResponse is constructed like:

response(with json: Any status: Int = 200, headers: String : String]? = nil)

Example:

let stub = Mimic.mimic(
    request: request(with: .get, url: "http://example.com"),
    delay: 2,
    response: response(with: ["response": true], status: 200, headers: ["SomeHeader": "SomeValue"]) 
)

You could also prefer not to store the MimicObject that's returned, like:

Mimic.mimic(
    request: request(with: .get, url: "http://example.com"),
    delay: 2,
    response: response(with: ["response": true], status: 200, headers: ["SomeHeader": "SomeValue"]) 
)

Randomize stubs

Mimic supports returning a random mimic for the same request, to archive this just add more than one mimic for the same request, for example:

Mimic.mimic(
    request: request(with: .get, url: "http://example.com"),
    delay: 2,
    response: response(with: ["response": true], status: 200]) 
)

Mimic.mimic(
    request: request(with: .get, url: "http://example.com"),
    delay: 2,
    response: response(with: ["response": false], status: 400]) 
)

And finally activate the randomize fuction by activating it's parameter in Mimic

Mimic.randomizeMimics = true

This will search all mimics for a request and return a random element.

Using wild card parameters

Mimic support adding multiple wild card parameters inside an url by replacing the string with @wild. This will allow to stub request to endpoints with dynamic paramters inside path o query, for example:

Let's take as example the followig url

http://example.com/path1/path2?item1=value1&&item2=value2

Adding wild parameter to path parater path2

http://example.com/path1/@wild?item1=value1&&item2=value2

Adding wild parameter to query parameter value1

http://example.com/path1/path2?item1=@wild&&item2=value2

Adding wild paramter to both path2 and value1

http://example.com/path1/@wild?item1=@wild&&item2=value2

Removing stubs

To remove a single stub you can use

public class func stopMimic(_ mimic: MimicObject)

For Example:

This will add a new stub

let stub = Mimic(
    request: request(with: .get, url: "http://example.com"),
    delay: 2,
    response: response(with: ["response": true))
)

For removing it you would do:

Mimic.stopMimic(stub)

For removing all stubs call:

Mimic.stopAllMimics()

Testing

To ensure the URLSessionConfiguration swizzle is performed before the request it's recommended to initialize Mimic first

class myTests: XCTests {
    ...
    // Test properties here
    override func setUp() {
        super.setUp()
        Mimic.start()
        ...
        // Other initializers
    }
    ...
    // Other tests functions
}

Limitations

Mimic only support simple request, it doesn't support downloading or uploading tasks yet.

Request methods are limited to:

  • GET
  • POST
  • DELETE
  • PUT

GitHub

link
Stars: 6
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

SPM Support
1 week ago

Mimic 1.3.0

What was added

  • Support for Swift Package Manager

What was removed

  • Carthage support, you still can add Mimic to your project using Carthage but we won't be giving support to it.

Other stuffs

  • Mimic is now using Tuist

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API