Welcome to the documentation for MMBarricade. This powerful library provides network interception and stubbing capabilities for iOS developers.
Installation
MMBarricade can be easily added to your project using CocoaPods. Simply add the following line to your Podfile:
pod 'MMBarricade'
Then, run pod install
to download and include the library in your project workspace.
Usage
Enabling
To start using MMBarricade, you need to enable it in your app’s code. Import the MMBarricade framework and call the start()
method:
// Import MMBarricade
import MMBarricade
// Enable MMBarricade - usually in your AppDelegate or during app setup
MMBarricade.start()
Registering Stubs
To intercept and stub requests, you need to register stubs with MMBarricade. Each stub defines the request to match and the response to return. Here’s an example of registering a simple stub:
let stub = MMBarricadeStub.stub(for: "/api/v1/user")
.andReturn(200)
.withJSONResponse(["name": "John Doe"])
.stubResponseTime(0.5)
MMBarricade.add(stub)
In the above example, the stub is registered for requests made to ‘/api/v1/user’. It returns a 200 status code, along with a JSON response containing a user’s name. Additionally, the stub introduces a delay of 0.5 seconds before responding.
Stub Variants
MMBarricade allows you to define different variants for your stubs. A variant is a small modification to the original stub’s response. Here’s an example:
let stub = MMBarricadeStub.stub(for: "/api/v1/user")
.andReturn(200)
.withJSONResponse(["name": "John Doe"])
let variantStub = MMBarricadeVariantStub.variantStub(parent: stub)
.andReturn(401)
.withJSONResponse(["error": "Invalid token"])
MMBarricade.add(variantStub)
In this example, a variant stub is created from the original stub defined earlier. The variant stub returns a 401 status code and a JSON response indicating an invalid token. This allows you to easily test different scenarios.
Removing Stubs
If you need to remove a specific stub, you can do so using the remove()
method:
MMBarricade.remove(stub)
This will remove the specified stub from MMBarricade and it will no longer intercept matching requests.
Advanced Usage
Request Filters
MMBarricade provides powerful request filters that allow you to customize the behavior of stubs based on various conditions. Here’s an example:
// Import the necessary MMBarricadeFilter classes
import MMBarricadeFilterRequestPath
import MMBarricadeFilterRequestMethod
let stub = MMBarricadeStub.stub(for: "/api/v1/user")
.andReturn(200)
.withJSONResponse(["name": "John Doe"])
.whereRequestPath(is: MMBarricadeFilterRequestPath.matches(path: "/api/v1/user") && MMBarricadeFilterRequestMethod.isGETRequest())
MMBarricade.add(stub)
In this example, the stub will only be applied to GET requests made to ‘/api/v1/user’. Request filters give you fine-grained control over stub behavior.
Custom Response Providers
You can also define custom response providers to dynamically generate responses for your stubs. Here’s an example:
// Implement MMBarricadeResponseProvider for a custom response
struct CustomResponseProvider: MMBarricadeResponseProvider {
func response(for request: URLRequest) -> MMBarricadeResponse? {
// ... your custom response generation logic here ...
}
}
// Set the response provider for a specific stub
let stub = MMBarricadeStub.stub(for: "/api/v1/user")
.andExpectResponseProvider(CustomResponseProvider())
MMBarricade.add(stub)
In this example, the custom response provider is implemented conforming to the MMBarricadeResponseProvider
protocol. Whenever a request matches the stub, the response(for request: URLRequest)
method will be called, allowing you to generate a dynamic response based on the request.
Intercepting Real Requests
MMBarricade also allows you to intercept and modify real network requests. By default, requests matching a registered stub will be intercepted. You can access the intercepted request and modify it before it is sent to the server. Here’s an example:
// Implement MMBarricadeMutableRequestModifier for a custom request modifier
struct CustomRequestModifier: MMBarricadeMutableRequestModifier {
func modify(_ request: URLRequest) -> URLRequest {
// ... your request modification logic here ...
}
}
// Set the request modifier for a specific stub
let stub = MMBarricadeStub.stub(for: "/api/v1/user")
.andExpectMutableRequestModifier(CustomRequestModifier())
MMBarricade.add(stub)
In this example, the custom request modifier is implemented conforming to the MMBarricadeMutableRequestModifier
protocol. Whenever a request matching the stub is intercepted, the modify(_ request: URLRequest)
method will be called, allowing you to modify the intercepted request before sending it to the server.
Conclusion
Congratulations! You now have a solid understanding of MMBarricade and its capabilities. Start using MMBarricade to intercept and stub network requests for efficient iOS development!